blob: e0a4689f7adb8234013cba7869fd9caffb3ac3a0 [file] [log] [blame]
Simon Hunta2b11a52016-02-22 22:05:47 -08001// js for driver view
Thomas Vachuskabbf10502015-12-09 13:41:58 -08002(function () {
3 'use strict';
4
5 // injected refs
6 var $log, $scope, fs, wss;
7
8 // constants
Thomas Vachuska72667382016-02-22 14:27:48 -08009 var detailsReq = 'driverDataRequest',
10 detailsResp = 'driverDataResponse',
Simon Hunta2b11a52016-02-22 22:05:47 -080011 // TODO: deal with details panel
Thomas Vachuskabbf10502015-12-09 13:41:58 -080012 pName = 'ov-driver-matrix-item-details-panel',
Thomas Vachuskabbf10502015-12-09 13:41:58 -080013 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 Hunta2b11a52016-02-22 22:05:47 -080043 //$log.debug('Matrix Data', data);
44 $scope.behaviours = data.behaviours;
45 $scope.drivers = data.drivers;
46 $scope.matrix = data.matrix;
47 $scope.$apply();
Thomas Vachuskabbf10502015-12-09 13:41:58 -080048 }
49
50 angular.module('ovDriverMatrix', [])
Simon Hunt24d0c5c2016-03-03 00:05:08 -080051 .run(['IconService', function (is) {
52 // Create our icon-to-glyph binding here:
53 is.registerIconMapping('nav_drivers', 'cog');
54 }])
Thomas Vachuskabbf10502015-12-09 13:41:58 -080055 .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 Hunta2b11a52016-02-22 22:05:47 -080066 $scope.behaviours = [];
67 $scope.drivers = [];
68 $scope.matrix = {};
Thomas Vachuskabbf10502015-12-09 13:41:58 -080069
70 // details response handler
71 handlers[detailsResp] = respDetailsCb;
72 wss.bindHandlers(handlers);
73
Thomas Vachuska72667382016-02-22 14:27:48 -080074 wss.sendEvent(detailsReq);
Thomas Vachuskabbf10502015-12-09 13:41:58 -080075
Thomas Vachuska72667382016-02-22 14:27:48 -080076 //// 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 Hunta2b11a52016-02-22 22:05:47 -080086 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 Vachuskabbf10502015-12-09 13:41:58 -080097
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 Hunta2b11a52016-02-22 22:05:47 -0800107 // TODO: implement row selection to show details panel
Thomas Vachuskabbf10502015-12-09 13:41:58 -0800108 .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}());