blob: 2f33a5499799f6702a7d00b77c030dd81af71b1e [file] [log] [blame]
Thomas Vachuskabbf10502015-12-09 13:41:58 -08001// js for sample app table view
2(function () {
3 'use strict';
4
5 // injected refs
6 var $log, $scope, fs, wss;
7
8 // constants
9 var detailsReq = 'driverMatrixDetailsRequest',
10 detailsResp = 'driverMatrixDetailsResponse',
11 pName = 'ov-driver-matrix-item-details-panel',
12
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) {
43 $scope.panelDetails = data.details;
44 $scope.$apply();
45 }
46
47 angular.module('ovDriverMatrix', [])
48 .controller('OvDriverMatrixCtrl',
49 ['$log', '$scope', 'TableBuilderService',
50 'FnService', 'WebSocketService',
51
52 function (_$log_, _$scope_, tbs, _fs_, _wss_) {
53 $log = _$log_;
54 $scope = _$scope_;
55 fs = _fs_;
56 wss = _wss_;
57
58 var handlers = {};
59 $scope.panelDetails = {};
60
61 // details response handler
62 handlers[detailsResp] = respDetailsCb;
63 wss.bindHandlers(handlers);
64
65 // custom selection callback
66 function selCb($event, row) {
67 if ($scope.selId) {
68 wss.sendEvent(detailsReq, { id: row.id });
69 } else {
70 $scope.hidePanel();
71 }
72 $log.debug('Got a click on:', row);
73 }
74
75 // TableBuilderService creating a table for us
76 tbs.buildTable({
77 scope: $scope,
78 tag: 'driverMatrix',
79 selCb: selCb
80 });
81
82 // cleanup
83 $scope.$on('$destroy', function () {
84 wss.unbindHandlers(handlers);
85 $log.log('OvDriverMatrixCtrl has been destroyed');
86 });
87
88 $log.log('OvDriverMatrixCtrl has been created');
89 }])
90
91 .directive('ovDriverMatrixItemDetailsPanel', ['PanelService', 'KeyService',
92 function (ps, ks) {
93 return {
94 restrict: 'E',
95 link: function (scope, element, attrs) {
96 // insert details panel with PanelService
97 // create the panel
98 var panel = ps.createPanel(pName, {
99 width: 200,
100 margin: 20,
101 hideMargin: 0
102 });
103 panel.hide();
104 scope.hidePanel = function () { panel.hide(); };
105
106 function closePanel() {
107 if (panel.isVisible()) {
108 $scope.selId = null;
109 panel.hide();
110 return true;
111 }
112 return false;
113 }
114
115 // create key bindings to handle panel
116 ks.keyBindings({
117 esc: [closePanel, 'Close the details panel'],
118 _helpFormat: ['esc']
119 });
120 ks.gestureNotes([
121 ['click', 'Select a row to show item details']
122 ]);
123
124 // update the panel's contents when the data is changed
125 scope.$watch('panelDetails', function () {
126 if (!fs.isEmptyObject(scope.panelDetails)) {
127 panel.empty();
128 populatePanel(panel);
129 panel.show();
130 }
131 });
132
133 // cleanup on destroyed scope
134 scope.$on('$destroy', function () {
135 ks.unbindKeys();
136 ps.destroyPanel(pName);
137 });
138 }
139 };
140 }]);
141}());