blob: a742daecc24ae9ddd98ab2d1bc3ee4555a222b01 [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',
12 propOrder = ['id', 'alarmDeviceId', 'alarmDesc', 'alarmSource', 'alarmTimeRaised', 'alarmTimeUpdated', 'alarmTimeCleared', 'alarmSeverity'],
13 friendlyProps = ['Alarm Id', 'Device Id', 'Description', 'Source', 'Time Raised', 'Time Updated', 'Time Cleared', 'Severity'];
14
15
16 function addProp(tbody, index, value) {
17 var tr = tbody.append('tr');
18
19 function addCell(cls, txt) {
20 tr.append('td').attr('class', cls).html(txt);
21 }
22 addCell('label', friendlyProps[index] + ' :');
23 addCell('value', value);
24 }
25
26 function populatePanel(panel) {
27 var title = panel.append('h3'),
28 tbody = panel.append('table').append('tbody');
29
30 title.text('Alarm Details');
31
32 propOrder.forEach(function (prop, i) {
33 addProp(tbody, i, $scope.panelDetails[prop]);
34 });
35
36 panel.append('hr');
37 panel.append('h4').text('Comments');
38 panel.append('p').text($scope.panelDetails.comment);
39 }
40
41 function respDetailsCb(data) {
42 $scope.panelDetails = data.details;
43 $scope.$apply();
44 }
45
46 angular.module('ovAlarmTable', [])
Simon Hunt0b41d292016-02-02 15:07:57 -080047 .run(['IconService', function (is) {
48 // we want to be able to re-use the clock glyph in our nav icon...
49 is.registerIconMapping('nav_alarms', 'alarmsTopo-overlay-clock');
50 }])
kmcpeakeb172d5f2015-12-10 11:30:43 +000051
Simon Hunt0b41d292016-02-02 15:07:57 -080052 .controller('OvAlarmTableCtrl',
53 ['$log', '$scope', '$location', 'TableBuilderService',
54 'FnService', 'WebSocketService',
kmcpeakeb172d5f2015-12-10 11:30:43 +000055
Simon Hunt0b41d292016-02-02 15:07:57 -080056 function (_$log_, _$scope_, _$location_, tbs, _fs_, _wss_) {
57 var params;
kmcpeakeb172d5f2015-12-10 11:30:43 +000058
Simon Hunt0b41d292016-02-02 15:07:57 -080059 $log = _$log_;
60 $scope = _$scope_;
61 $loc = _$location_;
kmcpeakeb172d5f2015-12-10 11:30:43 +000062
Simon Hunt0b41d292016-02-02 15:07:57 -080063 fs = _fs_;
64 wss = _wss_;
kmcpeakeb172d5f2015-12-10 11:30:43 +000065
Simon Hunt0b41d292016-02-02 15:07:57 -080066 params = $loc.search();
67 if (params.hasOwnProperty('devId')) {
68 $scope.devId = params['devId'];
69 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000070
Simon Hunt0b41d292016-02-02 15:07:57 -080071 var handlers = {};
72 $scope.panelDetails = {};
kmcpeakeb172d5f2015-12-10 11:30:43 +000073
Simon Hunt0b41d292016-02-02 15:07:57 -080074 // details response handler
75 handlers[detailsResp] = respDetailsCb;
76 wss.bindHandlers(handlers);
kmcpeakeb172d5f2015-12-10 11:30:43 +000077
Simon Hunt0b41d292016-02-02 15:07:57 -080078 // custom selection callback
79 function selCb($event, row) {
80 $log.debug("selCb row=" + JSON.stringify(row, null, 4) +
81 ", $event=" + JSON.stringify($event, null, 4));
82 $log.debug('$scope.selId=', $scope.selId);
83 if ($scope.selId) {
84 $log.debug('send');
85 wss.sendEvent(detailsReq, {id: row.id});
86 } else {
87 $log.debug('hidePanel');
88 $scope.hidePanel();
89 }
90 $log.debug('Got a click on:', row);
91 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000092
Simon Hunt0b41d292016-02-02 15:07:57 -080093 // TableBuilderService creating a table for us
94 tbs.buildTable({
95 scope: $scope,
96 tag: 'alarmTable',
97 selCb: selCb,
98 query: params
99 });
kmcpeakeb172d5f2015-12-10 11:30:43 +0000100
Simon Hunt0b41d292016-02-02 15:07:57 -0800101 // cleanup
102 $scope.$on('$destroy', function () {
103 wss.unbindHandlers(handlers);
104 $log.log('OvAlarmTableCtrl has been destroyed');
105 });
kmcpeakeb172d5f2015-12-10 11:30:43 +0000106
Simon Hunt0b41d292016-02-02 15:07:57 -0800107 $log.log('OvAlarmTableCtrl has been created');
108 }])
109
110 .directive('ovAlarmTableItemDetailsPanel',
111 ['PanelService', 'KeyService',
112
113 function (ps, ks) {
114 return {
115 restrict: 'E',
116 link: function (scope, element, attrs) {
117 // insert details panel with PanelService
118 // create the panel
119 var panel = ps.createPanel(pName, {
120 width: 400,
121 margin: 20,
122 hideMargin: 0
123 });
124 panel.hide();
125 scope.hidePanel = function () {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000126 panel.hide();
Simon Hunt0b41d292016-02-02 15:07:57 -0800127 };
128
129 function closePanel() {
130 if (panel.isVisible()) {
131 $scope.selId = null;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000132 panel.hide();
Simon Hunt0b41d292016-02-02 15:07:57 -0800133 return true;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000134 }
Simon Hunt0b41d292016-02-02 15:07:57 -0800135 return false;
kmcpeakeb172d5f2015-12-10 11:30:43 +0000136 }
Simon Hunt0b41d292016-02-02 15:07:57 -0800137
138 // create key bindings to handle panel
139 ks.keyBindings({
140 esc: [closePanel, 'Close the details panel'],
141 _helpFormat: ['esc']
142 });
143 ks.gestureNotes([
144 ['click', 'Select a row to show item details']
145 ]);
146
147 // update the panel's contents when the data is changed
148 scope.$watch('panelDetails', function () {
149 if (!fs.isEmptyObject(scope.panelDetails)) {
150 panel.empty();
151 populatePanel(panel);
152 panel.show();
153 }
154 });
155
156 // cleanup on destroyed scope
157 scope.$on('$destroy', function () {
158 ks.unbindKeys();
159 ps.destroyPanel(pName);
160 });
161 }
162 };
163 }]);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000164}());