blob: 26951ca93edbce0a05e7e58705c7faabff58eef5 [file] [log] [blame]
Jimmy Yanda878fc2016-09-02 16:32:01 -07001// js for roadm port table view
2(function () {
3 'use strict';
4
5 var SET_TARGET_POWER_REQ = "roadmSetTargetPowerRequest";
6 var SET_TARGET_POWER_RESP = "roadmSetTargetPowerResponse";
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -07007 var SYNC_TARGET_POWER_REQ = "roadmSyncTargetPowerRequest";
8 var SYNC_TARGET_POWER_RESP = "roadmSyncTargetPowerResponse";
MaoLu937cf422017-03-03 23:31:46 -08009 var SHOW_ITEMS_REQ = "roadmShowPortItemsRequest";
10 var SHOW_ITEMS_RESP = "roadmShowPortItemsResponse";
11 var SET_OPS_MODE_REQ = "roadmSetOpsModeRequest";
12 var SET_OPS_MODE_RESP = "roadmSetOpsModeResponse";
Jimmy Yanda878fc2016-09-02 16:32:01 -070013
14 // injected references
15 var $log, $scope, $location, fs, tbs, wss, ns;
16
17 var portCbTable = {};
18
19 function setPortPower(port, targetVal, cb) {
20 var id = port.id;
21 portCbTable[id] = cb;
MaoLu937cf422017-03-03 23:31:46 -080022 wss.sendEvent(SET_TARGET_POWER_REQ,
Jimmy Yanda878fc2016-09-02 16:32:01 -070023 {
24 devId: $scope.devId,
25 id: port.id,
26 targetPower: targetVal
27 });
28 }
29
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -070030 function syncPortPower(port, cb) {
31 var id = port.id;
32 portCbTable[id] = cb;
33 wss.sendEvent(SYNC_TARGET_POWER_REQ,
34 {
35 devId: $scope.devId,
36 id: port.id
37 });
38 }
39
Jimmy Yanda878fc2016-09-02 16:32:01 -070040 function portPowerCb(data) {
41 portCbTable[data.id](data.valid, data.message);
42 }
43
MaoLu937cf422017-03-03 23:31:46 -080044 function queryShowItems() {
45 wss.sendEvent(SHOW_ITEMS_REQ,
46 {
47 devId: $scope.devId,
48 });
49 }
50
51 function showItemsCb(data) {
52 $scope.showTargetPower = data.showTargetPower;
53 $scope.showServiceState = data.showServiceState;
54 $scope.showFlowIcon = data.showFlowIcon;
MaoLu2846b112017-05-15 17:18:55 -070055 $scope.opsModeTypes = data.opsOperations;
56 if ($scope.opsModeType == null) {
57 $scope.opsModeType = $scope.opsModeTypes[0];
58 }
MaoLu937cf422017-03-03 23:31:46 -080059 $scope.$apply();
60 }
61
62 function changeOpsMode() {
63 wss.sendEvent(SET_OPS_MODE_REQ,
64 {
65 devId: $scope.devId,
MaoLu2846b112017-05-15 17:18:55 -070066 index: $scope.opsModeType.index,
67 operation: $scope.opsModeType.operation
MaoLu937cf422017-03-03 23:31:46 -080068 });
69 }
70
71 function changeOpsModeCb(data) {
72 $scope.changeModeFail = !data.valid;
73 if ($scope.changeModeFail) {
74 $scope.changeModeFailMsg = data.message;
75 }
76 $scope.$apply();
77 }
78
Jimmy Yanda878fc2016-09-02 16:32:01 -070079 // check if value is an integer
80 function isInteger(val) {
81 var INTEGER_REGEXP = /^\-?\d+$/;
82 if (INTEGER_REGEXP.test(val)) {
83 return true;
84 }
85 return false;
86 }
87
88 angular.module('ovRoadmPort', [])
89 .controller('OvRoadmPortCtrl',
90 ['$log', '$scope', '$location',
91 'FnService', 'TableBuilderService', 'WebSocketService', 'NavService',
92
93 function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _wss_, _ns_) {
94 var params;
95 $log = _$log_;
96 $scope = _$scope_;
97 $location = _$location_;
98 fs = _fs_;
99 tbs = _tbs_;
100 wss = _wss_;
101 ns = _ns_;
102
103 $scope.deviceTip = 'Show device table';
104 $scope.flowTip = 'Show flow view for this device';
MaoLu937cf422017-03-03 23:31:46 -0800105 $scope.portTip = 'Show port view for this device';
MaoLu2846b112017-05-15 17:18:55 -0700106 $scope.opsModeType = null;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700107
108 var handlers = {};
109 handlers[SET_TARGET_POWER_RESP] = portPowerCb;
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700110 handlers[SYNC_TARGET_POWER_RESP] = portPowerCb;
MaoLu937cf422017-03-03 23:31:46 -0800111 handlers[SHOW_ITEMS_RESP] = showItemsCb;
112 handlers[SET_OPS_MODE_RESP] = changeOpsModeCb;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700113 wss.bindHandlers(handlers);
114
115 params = $location.search();
116 if (params.hasOwnProperty('devId')) {
117 $scope.devId = params['devId'];
118 }
119
120 tbs.buildTable({
121 scope: $scope,
122 tag: 'roadmPort',
123 query: params
124 });
125
126 $scope.setPortPower = setPortPower;
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700127 $scope.syncPortPower = syncPortPower;
MaoLu937cf422017-03-03 23:31:46 -0800128 $scope.queryShowItems = queryShowItems;
129 $scope.changeOpsMode = changeOpsMode;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700130
131 $scope.setTargetPower = function (port, targetVal) {
MaoLu937cf422017-03-03 23:31:46 -0800132 wss.sendEvent(SET_TARGET_POWER_REQ,
Jimmy Yanda878fc2016-09-02 16:32:01 -0700133 {
134 devId: $scope.devId,
135 id: port.id,
136 targetPower: targetVal
137 });
138 $log.debug('Got a click on:', port);
139 }
140
141 $scope.nav = function (path) {
142 if ($scope.devId) {
143 ns.navTo(path, { devId: $scope.devId });
144 }
145 };
146
147 $scope.$on('$destroy', function () {
148 wss.unbindHandlers(handlers);
149 });
150
151 $log.log('OvRoadmPortCtrl has been created');
152 }])
153
154 .directive('roadmPower', ['WebSocketService', function() {
155
156 var retTemplate =
157 '<span class="target-power" ng-show="!editMode" ng-click="enableEdit()">{{currItem.targetPower}}</span>' +
158 '<form ng-show="editMode" name="form" novalidate>' +
159 '<input type="number" name="formVal" ng-model="formVal">' +
160 '<button type="submit" ng-click="send()">Set</button>' +
161 '<button type="button" ng-click="cancel()">Cancel</button>' +
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700162 '<button type="submit" ng-click="sync()">Sync</button>' +
Jimmy Yanda878fc2016-09-02 16:32:01 -0700163 '<span class="input-error" ng-show="showError">{{errorMessage}}</span>' +
164 '</form>';
165
166 return {
167 restrict: 'A',
168 scope: {
169 currItem: '=roadmPower',
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700170 roadmSetPower: '&',
171 roadmSyncPower: '&'
Jimmy Yanda878fc2016-09-02 16:32:01 -0700172 },
173 template: retTemplate,
174 link: function ($scope, $element) {
175 $scope.editMode = false;
176 $scope.showError = false;
177 $scope.errorMessage = "Invalid target power";
178 },
179 controller: function($scope, $timeout) {
180 $scope.enableEdit = function() {
181 if ($scope.currItem.hasTargetPower === "true" && $scope.editMode === false) {
182 // Ensure that the entry being edited remains the same even
183 // if the table entries are shifted around.
184 $scope.targetItem = $scope.currItem;
185 // Ensure the value seen in the field remains the same
186 $scope.formVal = parseInt($scope.currItem.targetPower);
187 $scope.editMode = true;
188 $timeout(function () {
189 $scope.$apply()
190 });
191 }
192 };
193 // Callback for server-side validation. Displays the error message
194 // if the input is invalid.
195 $scope.sendCb = function(valid, message) {
196 if (valid) {
197 // check if it's still pointing to the same item
198 // reordering the entries may change the binding
199 if ($scope.currItem.id === $scope.targetItem.id) {
200 // update the ui to display the new attenuation value
201 $scope.currItem.targetPower = $scope.formVal;
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700202 if (message.startsWith("Synced")) {
203 var str = message.split(' ');
204 $scope.currItem.targetPower = str[str.length-1].slice(0, -1);
205 }
Jimmy Yanda878fc2016-09-02 16:32:01 -0700206 }
207 $scope.cancel();
208 } else {
209 $scope.errorMessage = message;
210 $scope.showError = true;
211 }
212 $timeout(function () {
213 $scope.$apply()
214 });
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700215 };
Jimmy Yanda878fc2016-09-02 16:32:01 -0700216 $scope.send = function() {
217 // check input is an integer
218 if (!isInteger($scope.formVal)) {
219 $scope.sendCb(false, "Target power must be an integer");
220 return;
221 }
222 $scope.roadmSetPower({port: $scope.targetItem, targetVal: $scope.formVal, cb: $scope.sendCb});
223 };
224 $scope.cancel = function() {
225 $scope.editMode = false;
226 $scope.showError = false;
Boyuan Yanf3f6a8d2019-05-26 18:35:54 -0700227 };
228 $scope.sync = function() {
229 $scope.roadmSyncPower({port: $scope.targetItem, cb: $scope.sendCb});
Jimmy Yanda878fc2016-09-02 16:32:01 -0700230 }
231 }
232 };
233 }]);
234}());