Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 1 | // js for driver view |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 2 | (function () { |
| 3 | 'use strict'; |
| 4 | |
| 5 | // injected refs |
| 6 | var $log, $scope, fs, wss; |
| 7 | |
| 8 | // constants |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 9 | var detailsReq = 'driverDataRequest', |
| 10 | detailsResp = 'driverDataResponse', |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 11 | // TODO: deal with details panel |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 12 | pName = 'ov-driver-matrix-item-details-panel', |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 13 | propOrder = ['id', 'label', 'code'], |
| 14 | friendlyProps = ['Item ID', 'Item Label', 'Special Code']; |
| 15 | |
| 16 | |
| 17 | function addProp(tbody, index, value) { |
| 18 | var tr = tbody.append('tr'); |
| 19 | |
| 20 | function addCell(cls, txt) { |
| 21 | tr.append('td').attr('class', cls).html(txt); |
| 22 | } |
| 23 | addCell('label', friendlyProps[index] + ' :'); |
| 24 | addCell('value', value); |
| 25 | } |
| 26 | |
| 27 | function populatePanel(panel) { |
| 28 | var title = panel.append('h3'), |
| 29 | tbody = panel.append('table').append('tbody'); |
| 30 | |
| 31 | title.text('Item Details'); |
| 32 | |
| 33 | propOrder.forEach(function (prop, i) { |
| 34 | addProp(tbody, i, $scope.panelDetails[prop]); |
| 35 | }); |
| 36 | |
| 37 | panel.append('hr'); |
| 38 | panel.append('h4').text('Comments'); |
| 39 | panel.append('p').text($scope.panelDetails.comment); |
| 40 | } |
| 41 | |
| 42 | function respDetailsCb(data) { |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 43 | //$log.debug('Matrix Data', data); |
| 44 | $scope.behaviours = data.behaviours; |
| 45 | $scope.drivers = data.drivers; |
| 46 | $scope.matrix = data.matrix; |
| 47 | $scope.$apply(); |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | angular.module('ovDriverMatrix', []) |
Simon Hunt | 24d0c5c | 2016-03-03 00:05:08 -0800 | [diff] [blame] | 51 | .run(['IconService', function (is) { |
| 52 | // Create our icon-to-glyph binding here: |
| 53 | is.registerIconMapping('nav_drivers', 'cog'); |
| 54 | }]) |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 55 | .controller('OvDriverMatrixCtrl', |
| 56 | ['$log', '$scope', 'TableBuilderService', |
| 57 | 'FnService', 'WebSocketService', |
| 58 | |
| 59 | function (_$log_, _$scope_, tbs, _fs_, _wss_) { |
| 60 | $log = _$log_; |
| 61 | $scope = _$scope_; |
| 62 | fs = _fs_; |
| 63 | wss = _wss_; |
| 64 | |
| 65 | var handlers = {}; |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 66 | $scope.behaviours = []; |
| 67 | $scope.drivers = []; |
| 68 | $scope.matrix = {}; |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 69 | |
| 70 | // details response handler |
| 71 | handlers[detailsResp] = respDetailsCb; |
| 72 | wss.bindHandlers(handlers); |
| 73 | |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 74 | wss.sendEvent(detailsReq); |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 75 | |
Thomas Vachuska | 7266738 | 2016-02-22 14:27:48 -0800 | [diff] [blame] | 76 | //// custom selection callback |
| 77 | //function selCb($event, row) { |
| 78 | // if ($scope.selId) { |
| 79 | // wss.sendEvent(detailsReq, { id: row.id }); |
| 80 | // } else { |
| 81 | // $scope.hidePanel(); |
| 82 | // } |
| 83 | // $log.debug('Got a click on:', row); |
| 84 | //} |
| 85 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 86 | function cellHit(d, b) { |
| 87 | var drec = $scope.matrix[d], |
| 88 | brec = drec && drec[b]; |
| 89 | return !!brec; |
| 90 | } |
| 91 | |
| 92 | $scope.cellMarked = cellHit; |
| 93 | |
| 94 | $scope.cellValue = function(d, b) { |
| 95 | return cellHit(d, b) ? 'x' : ''; |
| 96 | }; |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 97 | |
| 98 | // cleanup |
| 99 | $scope.$on('$destroy', function () { |
| 100 | wss.unbindHandlers(handlers); |
| 101 | $log.log('OvDriverMatrixCtrl has been destroyed'); |
| 102 | }); |
| 103 | |
| 104 | $log.log('OvDriverMatrixCtrl has been created'); |
| 105 | }]) |
| 106 | |
Simon Hunt | a2b11a5 | 2016-02-22 22:05:47 -0800 | [diff] [blame] | 107 | // TODO: implement row selection to show details panel |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 108 | .directive('ovDriverMatrixItemDetailsPanel', ['PanelService', 'KeyService', |
| 109 | function (ps, ks) { |
| 110 | return { |
| 111 | restrict: 'E', |
| 112 | link: function (scope, element, attrs) { |
| 113 | // insert details panel with PanelService |
| 114 | // create the panel |
| 115 | var panel = ps.createPanel(pName, { |
| 116 | width: 200, |
| 117 | margin: 20, |
| 118 | hideMargin: 0 |
| 119 | }); |
| 120 | panel.hide(); |
| 121 | scope.hidePanel = function () { panel.hide(); }; |
| 122 | |
| 123 | function closePanel() { |
| 124 | if (panel.isVisible()) { |
| 125 | $scope.selId = null; |
| 126 | panel.hide(); |
| 127 | return true; |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // create key bindings to handle panel |
| 133 | ks.keyBindings({ |
| 134 | esc: [closePanel, 'Close the details panel'], |
| 135 | _helpFormat: ['esc'] |
| 136 | }); |
| 137 | ks.gestureNotes([ |
| 138 | ['click', 'Select a row to show item details'] |
| 139 | ]); |
| 140 | |
| 141 | // update the panel's contents when the data is changed |
| 142 | scope.$watch('panelDetails', function () { |
| 143 | if (!fs.isEmptyObject(scope.panelDetails)) { |
| 144 | panel.empty(); |
| 145 | populatePanel(panel); |
| 146 | panel.show(); |
| 147 | } |
| 148 | }); |
| 149 | |
| 150 | // cleanup on destroyed scope |
| 151 | scope.$on('$destroy', function () { |
| 152 | ks.unbindKeys(); |
| 153 | ps.destroyPanel(pName); |
| 154 | }); |
| 155 | } |
| 156 | }; |
| 157 | }]); |
| 158 | }()); |