blob: 80d46a376f38aad0e319f9c2ba3166fb3ec1ed03 [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', [])
51 .controller('OvDriverMatrixCtrl',
52 ['$log', '$scope', 'TableBuilderService',
53 'FnService', 'WebSocketService',
54
55 function (_$log_, _$scope_, tbs, _fs_, _wss_) {
56 $log = _$log_;
57 $scope = _$scope_;
58 fs = _fs_;
59 wss = _wss_;
60
61 var handlers = {};
Simon Hunta2b11a52016-02-22 22:05:47 -080062 $scope.behaviours = [];
63 $scope.drivers = [];
64 $scope.matrix = {};
Thomas Vachuskabbf10502015-12-09 13:41:58 -080065
66 // details response handler
67 handlers[detailsResp] = respDetailsCb;
68 wss.bindHandlers(handlers);
69
Thomas Vachuska72667382016-02-22 14:27:48 -080070 wss.sendEvent(detailsReq);
Thomas Vachuskabbf10502015-12-09 13:41:58 -080071
Thomas Vachuska72667382016-02-22 14:27:48 -080072 //// custom selection callback
73 //function selCb($event, row) {
74 // if ($scope.selId) {
75 // wss.sendEvent(detailsReq, { id: row.id });
76 // } else {
77 // $scope.hidePanel();
78 // }
79 // $log.debug('Got a click on:', row);
80 //}
81
Simon Hunta2b11a52016-02-22 22:05:47 -080082 function cellHit(d, b) {
83 var drec = $scope.matrix[d],
84 brec = drec && drec[b];
85 return !!brec;
86 }
87
88 $scope.cellMarked = cellHit;
89
90 $scope.cellValue = function(d, b) {
91 return cellHit(d, b) ? 'x' : '';
92 };
Thomas Vachuskabbf10502015-12-09 13:41:58 -080093
94 // cleanup
95 $scope.$on('$destroy', function () {
96 wss.unbindHandlers(handlers);
97 $log.log('OvDriverMatrixCtrl has been destroyed');
98 });
99
100 $log.log('OvDriverMatrixCtrl has been created');
101 }])
102
Simon Hunta2b11a52016-02-22 22:05:47 -0800103 // TODO: implement row selection to show details panel
Thomas Vachuskabbf10502015-12-09 13:41:58 -0800104 .directive('ovDriverMatrixItemDetailsPanel', ['PanelService', 'KeyService',
105 function (ps, ks) {
106 return {
107 restrict: 'E',
108 link: function (scope, element, attrs) {
109 // insert details panel with PanelService
110 // create the panel
111 var panel = ps.createPanel(pName, {
112 width: 200,
113 margin: 20,
114 hideMargin: 0
115 });
116 panel.hide();
117 scope.hidePanel = function () { panel.hide(); };
118
119 function closePanel() {
120 if (panel.isVisible()) {
121 $scope.selId = null;
122 panel.hide();
123 return true;
124 }
125 return false;
126 }
127
128 // create key bindings to handle panel
129 ks.keyBindings({
130 esc: [closePanel, 'Close the details panel'],
131 _helpFormat: ['esc']
132 });
133 ks.gestureNotes([
134 ['click', 'Select a row to show item details']
135 ]);
136
137 // update the panel's contents when the data is changed
138 scope.$watch('panelDetails', function () {
139 if (!fs.isEmptyObject(scope.panelDetails)) {
140 panel.empty();
141 populatePanel(panel);
142 panel.show();
143 }
144 });
145
146 // cleanup on destroyed scope
147 scope.$on('$destroy', function () {
148 ks.unbindKeys();
149 ps.destroyPanel(pName);
150 });
151 }
152 };
153 }]);
154}());