Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 1 | // 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 Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 7 | var SYNC_TARGET_POWER_REQ = "roadmSyncTargetPowerRequest"; |
| 8 | var SYNC_TARGET_POWER_RESP = "roadmSyncTargetPowerResponse"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 9 | 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 Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 13 | |
| 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; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 22 | wss.sendEvent(SET_TARGET_POWER_REQ, |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 23 | { |
| 24 | devId: $scope.devId, |
| 25 | id: port.id, |
| 26 | targetPower: targetVal |
| 27 | }); |
| 28 | } |
| 29 | |
Boyuan Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 30 | 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 Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 40 | function portPowerCb(data) { |
| 41 | portCbTable[data.id](data.valid, data.message); |
| 42 | } |
| 43 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 44 | 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; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 55 | $scope.opsModeTypes = data.opsOperations; |
| 56 | if ($scope.opsModeType == null) { |
| 57 | $scope.opsModeType = $scope.opsModeTypes[0]; |
| 58 | } |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 59 | $scope.$apply(); |
| 60 | } |
| 61 | |
| 62 | function changeOpsMode() { |
| 63 | wss.sendEvent(SET_OPS_MODE_REQ, |
| 64 | { |
| 65 | devId: $scope.devId, |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 66 | index: $scope.opsModeType.index, |
| 67 | operation: $scope.opsModeType.operation |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 68 | }); |
| 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 Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 79 | // 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'; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 105 | $scope.portTip = 'Show port view for this device'; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 106 | $scope.opsModeType = null; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 107 | |
| 108 | var handlers = {}; |
| 109 | handlers[SET_TARGET_POWER_RESP] = portPowerCb; |
Boyuan Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 110 | handlers[SYNC_TARGET_POWER_RESP] = portPowerCb; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 111 | handlers[SHOW_ITEMS_RESP] = showItemsCb; |
| 112 | handlers[SET_OPS_MODE_RESP] = changeOpsModeCb; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 113 | 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 Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 127 | $scope.syncPortPower = syncPortPower; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 128 | $scope.queryShowItems = queryShowItems; |
| 129 | $scope.changeOpsMode = changeOpsMode; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 130 | |
| 131 | $scope.setTargetPower = function (port, targetVal) { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 132 | wss.sendEvent(SET_TARGET_POWER_REQ, |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 133 | { |
| 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 Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 162 | '<button type="submit" ng-click="sync()">Sync</button>' + |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 163 | '<span class="input-error" ng-show="showError">{{errorMessage}}</span>' + |
| 164 | '</form>'; |
| 165 | |
| 166 | return { |
| 167 | restrict: 'A', |
| 168 | scope: { |
| 169 | currItem: '=roadmPower', |
Boyuan Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 170 | roadmSetPower: '&', |
| 171 | roadmSyncPower: '&' |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 172 | }, |
| 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 Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 202 | if (message.startsWith("Synced")) { |
| 203 | var str = message.split(' '); |
| 204 | $scope.currItem.targetPower = str[str.length-1].slice(0, -1); |
| 205 | } |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 206 | } |
| 207 | $scope.cancel(); |
| 208 | } else { |
| 209 | $scope.errorMessage = message; |
| 210 | $scope.showError = true; |
| 211 | } |
| 212 | $timeout(function () { |
| 213 | $scope.$apply() |
| 214 | }); |
Boyuan Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 215 | }; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 216 | $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 Yan | f3f6a8d | 2019-05-26 18:35:54 -0700 | [diff] [blame] | 227 | }; |
| 228 | $scope.sync = function() { |
| 229 | $scope.roadmSyncPower({port: $scope.targetItem, cb: $scope.sendCb}); |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | }; |
| 233 | }]); |
| 234 | }()); |