blob: 20f8b842380d734769bc591312e2d03d0f1e9924 [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001// js for alarm app table view
2(function () {
3 'use strict';
4
5 // injected refs
6 var $log, $scope, $loc, devId, fs, wss;
7
8 // constants
9 var detailsReq = 'alarmTableDetailsRequest',
10 detailsResp = 'alarmTableDetailsResponse',
11 pName = 'ov-alarm-table-item-details-panel',
Simon Hunt239f09e2017-05-18 13:10:09 -070012 propOrder = [
13 'id', 'alarmDeviceId', 'alarmDesc', 'alarmSource',
14 'alarmTimeRaised', 'alarmTimeUpdated', 'alarmTimeCleared',
15 'alarmSeverity'
16 ],
17 friendlyProps = [
18 'Alarm Id', 'Device Id', 'Description', 'Source',
19 'Time Raised', 'Time Updated', 'Time Cleared', 'Severity'
20 ];
kmcpeakeb172d5f2015-12-10 11:30:43 +000021
22
23 function addProp(tbody, index, value) {
24 var tr = tbody.append('tr');
25
26 function addCell(cls, txt) {
Simon Hunt239f09e2017-05-18 13:10:09 -070027 tr.append('td').attr('class', cls).text(txt);
kmcpeakeb172d5f2015-12-10 11:30:43 +000028 }
29 addCell('label', friendlyProps[index] + ' :');
30 addCell('value', value);
31 }
32
33 function populatePanel(panel) {
34 var title = panel.append('h3'),
35 tbody = panel.append('table').append('tbody');
36
37 title.text('Alarm Details');
38
39 propOrder.forEach(function (prop, i) {
40 addProp(tbody, i, $scope.panelDetails[prop]);
41 });
42
43 panel.append('hr');
44 panel.append('h4').text('Comments');
45 panel.append('p').text($scope.panelDetails.comment);
46 }
47
48 function respDetailsCb(data) {
49 $scope.panelDetails = data.details;
50 $scope.$apply();
51 }
52
53 angular.module('ovAlarmTable', [])
Simon Hunt0b41d292016-02-02 15:07:57 -080054 .run(['IconService', function (is) {
55 // we want to be able to re-use the clock glyph in our nav icon...
56 is.registerIconMapping('nav_alarms', 'alarmsTopo-overlay-clock');
57 }])
kmcpeakeb172d5f2015-12-10 11:30:43 +000058
Simon Hunt0b41d292016-02-02 15:07:57 -080059 .controller('OvAlarmTableCtrl',
60 ['$log', '$scope', '$location', 'TableBuilderService',
61 'FnService', 'WebSocketService',
kmcpeakeb172d5f2015-12-10 11:30:43 +000062
Simon Hunt0b41d292016-02-02 15:07:57 -080063 function (_$log_, _$scope_, _$location_, tbs, _fs_, _wss_) {
64 var params;
kmcpeakeb172d5f2015-12-10 11:30:43 +000065
Simon Hunt0b41d292016-02-02 15:07:57 -080066 $log = _$log_;
67 $scope = _$scope_;
68 $loc = _$location_;
kmcpeakeb172d5f2015-12-10 11:30:43 +000069
Simon Hunt0b41d292016-02-02 15:07:57 -080070 fs = _fs_;
71 wss = _wss_;
kmcpeakeb172d5f2015-12-10 11:30:43 +000072
Simon Hunt0b41d292016-02-02 15:07:57 -080073 params = $loc.search();
74 if (params.hasOwnProperty('devId')) {
75 $scope.devId = params['devId'];
76 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000077
Simon Hunt0b41d292016-02-02 15:07:57 -080078 var handlers = {};
79 $scope.panelDetails = {};
kmcpeakeb172d5f2015-12-10 11:30:43 +000080
Simon Hunt0b41d292016-02-02 15:07:57 -080081 // details response handler
82 handlers[detailsResp] = respDetailsCb;
83 wss.bindHandlers(handlers);
kmcpeakeb172d5f2015-12-10 11:30:43 +000084
Simon Hunt0b41d292016-02-02 15:07:57 -080085 // custom selection callback
86 function selCb($event, row) {
87 $log.debug("selCb row=" + JSON.stringify(row, null, 4) +
88 ", $event=" + JSON.stringify($event, null, 4));
89 $log.debug('$scope.selId=', $scope.selId);
90 if ($scope.selId) {
91 $log.debug('send');
92 wss.sendEvent(detailsReq, {id: row.id});
93 } else {
94 $log.debug('hidePanel');
95 $scope.hidePanel();
96 }
97 $log.debug('Got a click on:', row);
98 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000099
Simon Hunt0b41d292016-02-02 15:07:57 -0800100 // TableBuilderService creating a table for us
101 tbs.buildTable({
102 scope: $scope,
103 tag: 'alarmTable',
104 selCb: selCb,
105 query: params
106 });
kmcpeakeb172d5f2015-12-10 11:30:43 +0000107
Simon Hunt0b41d292016-02-02 15:07:57 -0800108 // cleanup
109 $scope.$on('$destroy', function () {
110 wss.unbindHandlers(handlers);
111 $log.log('OvAlarmTableCtrl has been destroyed');
112 });
kmcpeakeb172d5f2015-12-10 11:30:43 +0000113
Simon Hunt0b41d292016-02-02 15:07:57 -0800114 $log.log('OvAlarmTableCtrl has been created');
115 }])
116
117 .directive('ovAlarmTableItemDetailsPanel',
118 ['PanelService', 'KeyService',
119
120 function (ps, ks) {
121 return {
122 restrict: 'E',
123 link: function (scope, element, attrs) {
124 // insert details panel with PanelService
125 // create the panel
126 var panel = ps.createPanel(pName, {
127 width: 400,
128 margin: 20,
129 hideMargin: 0
130 });
131 panel.hide();
132 scope.hidePanel = function () {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000133 panel.hide();
Simon Hunt0b41d292016-02-02 15:07:57 -0800134 };
135
136 function closePanel() {
137 if (panel.isVisible()) {
138 $scope.selId = null;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000139 panel.hide();
Simon Hunt0b41d292016-02-02 15:07:57 -0800140 return true;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000141 }
Simon Hunt0b41d292016-02-02 15:07:57 -0800142 return false;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000143 }
Simon Hunt0b41d292016-02-02 15:07:57 -0800144
145 // create key bindings to handle panel
146 ks.keyBindings({
147 esc: [closePanel, 'Close the details panel'],
148 _helpFormat: ['esc']
149 });
150 ks.gestureNotes([
151 ['click', 'Select a row to show item details']
152 ]);
153
154 // update the panel's contents when the data is changed
155 scope.$watch('panelDetails', function () {
156 if (!fs.isEmptyObject(scope.panelDetails)) {
157 panel.empty();
158 populatePanel(panel);
159 panel.show();
160 }
161 });
162
163 // cleanup on destroyed scope
164 scope.$on('$destroy', function () {
165 ks.unbindKeys();
166 ps.destroyPanel(pName);
167 });
168 }
169 };
170 }]);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000171}());