Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 1 | // js for sample app custom view |
Thomas Vachuska | a2ae422 | 2015-04-29 18:42:09 -0700 | [diff] [blame] | 2 | (function () { |
| 3 | 'use strict'; |
| 4 | |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 5 | // injected refs |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 6 | var $log, $scope, wss, ks; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 7 | |
| 8 | // constants |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 9 | var dataReq = 'sampleCustomDataRequest', |
| 10 | dataResp = 'sampleCustomDataResponse'; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 11 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 12 | function addKeyBindings() { |
| 13 | var map = { |
| 14 | space: [getData, 'Fetch data from server'], |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 15 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 16 | _helpFormat: [ |
| 17 | ['space'] |
| 18 | ] |
| 19 | }; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 20 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 21 | ks.keyBindings(map); |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 24 | function getData() { |
| 25 | wss.sendEvent(dataReq); |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 28 | function respDataCb(data) { |
| 29 | $scope.data = data; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 30 | $scope.$apply(); |
| 31 | } |
| 32 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 33 | |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 34 | angular.module('ovSampleCustom', []) |
| 35 | .controller('OvSampleCustomCtrl', |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 36 | ['$log', '$scope', 'WebSocketService', 'KeyService', |
Thomas Vachuska | a2ae422 | 2015-04-29 18:42:09 -0700 | [diff] [blame] | 37 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 38 | function (_$log_, _$scope_, _wss_, _ks_) { |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 39 | $log = _$log_; |
| 40 | $scope = _$scope_; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 41 | wss = _wss_; |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 42 | ks = _ks_; |
Thomas Vachuska | a2ae422 | 2015-04-29 18:42:09 -0700 | [diff] [blame] | 43 | |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 44 | var handlers = {}; |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 45 | $scope.data = {}; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 46 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 47 | // data response handler |
| 48 | handlers[dataResp] = respDataCb; |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 49 | wss.bindHandlers(handlers); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 50 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 51 | addKeyBindings(); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 52 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 53 | // custom click handler |
| 54 | $scope.getData = getData; |
| 55 | |
| 56 | // get data the first time... |
| 57 | getData(); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 58 | |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 59 | // cleanup |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 60 | $scope.$on('$destroy', function () { |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 61 | wss.unbindHandlers(handlers); |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 62 | ks.unbindKeys(); |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 63 | $log.log('OvSampleCustomCtrl has been destroyed'); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 64 | }); |
Thomas Vachuska | a2ae422 | 2015-04-29 18:42:09 -0700 | [diff] [blame] | 65 | |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 66 | $log.log('OvSampleCustomCtrl has been created'); |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 67 | }]); |
Bri Prebilic Cole | 68844ba | 2015-07-22 15:41:37 -0700 | [diff] [blame] | 68 | |
Thomas Vachuska | a2ae422 | 2015-04-29 18:42:09 -0700 | [diff] [blame] | 69 | }()); |