blob: 128b72cbaf73e9e5aea7685a5ec1f39eda91e6a5 [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";
MaoLu937cf422017-03-03 23:31:46 -08007 var SHOW_ITEMS_REQ = "roadmShowPortItemsRequest";
8 var SHOW_ITEMS_RESP = "roadmShowPortItemsResponse";
9 var SET_OPS_MODE_REQ = "roadmSetOpsModeRequest";
10 var SET_OPS_MODE_RESP = "roadmSetOpsModeResponse";
Jimmy Yanda878fc2016-09-02 16:32:01 -070011
12 // injected references
13 var $log, $scope, $location, fs, tbs, wss, ns;
14
15 var portCbTable = {};
16
17 function setPortPower(port, targetVal, cb) {
18 var id = port.id;
19 portCbTable[id] = cb;
MaoLu937cf422017-03-03 23:31:46 -080020 wss.sendEvent(SET_TARGET_POWER_REQ,
Jimmy Yanda878fc2016-09-02 16:32:01 -070021 {
22 devId: $scope.devId,
23 id: port.id,
24 targetPower: targetVal
25 });
26 }
27
28 function portPowerCb(data) {
29 portCbTable[data.id](data.valid, data.message);
30 }
31
MaoLu937cf422017-03-03 23:31:46 -080032 function queryShowItems() {
33 wss.sendEvent(SHOW_ITEMS_REQ,
34 {
35 devId: $scope.devId,
36 });
37 }
38
39 function showItemsCb(data) {
40 $scope.showTargetPower = data.showTargetPower;
41 $scope.showServiceState = data.showServiceState;
42 $scope.showFlowIcon = data.showFlowIcon;
43 $scope.$apply();
44 }
45
46 function changeOpsMode() {
47 wss.sendEvent(SET_OPS_MODE_REQ,
48 {
49 devId: $scope.devId,
50 index: $scope.opsModeType.index
51 });
52 }
53
54 function changeOpsModeCb(data) {
55 $scope.changeModeFail = !data.valid;
56 if ($scope.changeModeFail) {
57 $scope.changeModeFailMsg = data.message;
58 }
59 $scope.$apply();
60 }
61
Jimmy Yanda878fc2016-09-02 16:32:01 -070062 // check if value is an integer
63 function isInteger(val) {
64 var INTEGER_REGEXP = /^\-?\d+$/;
65 if (INTEGER_REGEXP.test(val)) {
66 return true;
67 }
68 return false;
69 }
70
71 angular.module('ovRoadmPort', [])
72 .controller('OvRoadmPortCtrl',
73 ['$log', '$scope', '$location',
74 'FnService', 'TableBuilderService', 'WebSocketService', 'NavService',
75
76 function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _wss_, _ns_) {
77 var params;
78 $log = _$log_;
79 $scope = _$scope_;
80 $location = _$location_;
81 fs = _fs_;
82 tbs = _tbs_;
83 wss = _wss_;
84 ns = _ns_;
85
86 $scope.deviceTip = 'Show device table';
87 $scope.flowTip = 'Show flow view for this device';
MaoLu937cf422017-03-03 23:31:46 -080088 $scope.portTip = 'Show port view for this device';
89 $scope.opsModeTypes = [
90 {index: 0, type: "Auto"},
91 {index: 1, type: "Primary"},
92 {index: 2, type: "Secondary"}
93 ];
94 $scope.opsModeType = $scope.opsModeTypes[0];//auto mode
Jimmy Yanda878fc2016-09-02 16:32:01 -070095
96 var handlers = {};
97 handlers[SET_TARGET_POWER_RESP] = portPowerCb;
MaoLu937cf422017-03-03 23:31:46 -080098 handlers[SHOW_ITEMS_RESP] = showItemsCb;
99 handlers[SET_OPS_MODE_RESP] = changeOpsModeCb;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700100 wss.bindHandlers(handlers);
101
102 params = $location.search();
103 if (params.hasOwnProperty('devId')) {
104 $scope.devId = params['devId'];
105 }
106
107 tbs.buildTable({
108 scope: $scope,
109 tag: 'roadmPort',
110 query: params
111 });
112
113 $scope.setPortPower = setPortPower;
MaoLu937cf422017-03-03 23:31:46 -0800114 $scope.queryShowItems = queryShowItems;
115 $scope.changeOpsMode = changeOpsMode;
Jimmy Yanda878fc2016-09-02 16:32:01 -0700116
117 $scope.setTargetPower = function (port, targetVal) {
MaoLu937cf422017-03-03 23:31:46 -0800118 wss.sendEvent(SET_TARGET_POWER_REQ,
Jimmy Yanda878fc2016-09-02 16:32:01 -0700119 {
120 devId: $scope.devId,
121 id: port.id,
122 targetPower: targetVal
123 });
124 $log.debug('Got a click on:', port);
125 }
126
127 $scope.nav = function (path) {
128 if ($scope.devId) {
129 ns.navTo(path, { devId: $scope.devId });
130 }
131 };
132
133 $scope.$on('$destroy', function () {
134 wss.unbindHandlers(handlers);
135 });
136
137 $log.log('OvRoadmPortCtrl has been created');
138 }])
139
140 .directive('roadmPower', ['WebSocketService', function() {
141
142 var retTemplate =
143 '<span class="target-power" ng-show="!editMode" ng-click="enableEdit()">{{currItem.targetPower}}</span>' +
144 '<form ng-show="editMode" name="form" novalidate>' +
145 '<input type="number" name="formVal" ng-model="formVal">' +
146 '<button type="submit" ng-click="send()">Set</button>' +
147 '<button type="button" ng-click="cancel()">Cancel</button>' +
148 '<span class="input-error" ng-show="showError">{{errorMessage}}</span>' +
149 '</form>';
150
151 return {
152 restrict: 'A',
153 scope: {
154 currItem: '=roadmPower',
155 roadmSetPower: '&'
156 },
157 template: retTemplate,
158 link: function ($scope, $element) {
159 $scope.editMode = false;
160 $scope.showError = false;
161 $scope.errorMessage = "Invalid target power";
162 },
163 controller: function($scope, $timeout) {
164 $scope.enableEdit = function() {
165 if ($scope.currItem.hasTargetPower === "true" && $scope.editMode === false) {
166 // Ensure that the entry being edited remains the same even
167 // if the table entries are shifted around.
168 $scope.targetItem = $scope.currItem;
169 // Ensure the value seen in the field remains the same
170 $scope.formVal = parseInt($scope.currItem.targetPower);
171 $scope.editMode = true;
172 $timeout(function () {
173 $scope.$apply()
174 });
175 }
176 };
177 // Callback for server-side validation. Displays the error message
178 // if the input is invalid.
179 $scope.sendCb = function(valid, message) {
180 if (valid) {
181 // check if it's still pointing to the same item
182 // reordering the entries may change the binding
183 if ($scope.currItem.id === $scope.targetItem.id) {
184 // update the ui to display the new attenuation value
185 $scope.currItem.targetPower = $scope.formVal;
186 }
187 $scope.cancel();
188 } else {
189 $scope.errorMessage = message;
190 $scope.showError = true;
191 }
192 $timeout(function () {
193 $scope.$apply()
194 });
195 }
196 $scope.send = function() {
197 // check input is an integer
198 if (!isInteger($scope.formVal)) {
199 $scope.sendCb(false, "Target power must be an integer");
200 return;
201 }
202 $scope.roadmSetPower({port: $scope.targetItem, targetVal: $scope.formVal, cb: $scope.sendCb});
203 };
204 $scope.cancel = function() {
205 $scope.editMode = false;
206 $scope.showError = false;
207 }
208 }
209 };
210 }]);
211}());