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"; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 7 | 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 Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 11 | |
| 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; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 20 | wss.sendEvent(SET_TARGET_POWER_REQ, |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 21 | { |
| 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 | |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 32 | 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; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 43 | $scope.opsModeTypes = data.opsOperations; |
| 44 | if ($scope.opsModeType == null) { |
| 45 | $scope.opsModeType = $scope.opsModeTypes[0]; |
| 46 | } |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 47 | $scope.$apply(); |
| 48 | } |
| 49 | |
| 50 | function changeOpsMode() { |
| 51 | wss.sendEvent(SET_OPS_MODE_REQ, |
| 52 | { |
| 53 | devId: $scope.devId, |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 54 | index: $scope.opsModeType.index, |
| 55 | operation: $scope.opsModeType.operation |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 56 | }); |
| 57 | } |
| 58 | |
| 59 | function changeOpsModeCb(data) { |
| 60 | $scope.changeModeFail = !data.valid; |
| 61 | if ($scope.changeModeFail) { |
| 62 | $scope.changeModeFailMsg = data.message; |
| 63 | } |
| 64 | $scope.$apply(); |
| 65 | } |
| 66 | |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 67 | // check if value is an integer |
| 68 | function isInteger(val) { |
| 69 | var INTEGER_REGEXP = /^\-?\d+$/; |
| 70 | if (INTEGER_REGEXP.test(val)) { |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | angular.module('ovRoadmPort', []) |
| 77 | .controller('OvRoadmPortCtrl', |
| 78 | ['$log', '$scope', '$location', |
| 79 | 'FnService', 'TableBuilderService', 'WebSocketService', 'NavService', |
| 80 | |
| 81 | function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _wss_, _ns_) { |
| 82 | var params; |
| 83 | $log = _$log_; |
| 84 | $scope = _$scope_; |
| 85 | $location = _$location_; |
| 86 | fs = _fs_; |
| 87 | tbs = _tbs_; |
| 88 | wss = _wss_; |
| 89 | ns = _ns_; |
| 90 | |
| 91 | $scope.deviceTip = 'Show device table'; |
| 92 | $scope.flowTip = 'Show flow view for this device'; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 93 | $scope.portTip = 'Show port view for this device'; |
MaoLu | 2846b11 | 2017-05-15 17:18:55 -0700 | [diff] [blame] | 94 | $scope.opsModeType = null; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 95 | |
| 96 | var handlers = {}; |
| 97 | handlers[SET_TARGET_POWER_RESP] = portPowerCb; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 98 | handlers[SHOW_ITEMS_RESP] = showItemsCb; |
| 99 | handlers[SET_OPS_MODE_RESP] = changeOpsModeCb; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 100 | 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; |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 114 | $scope.queryShowItems = queryShowItems; |
| 115 | $scope.changeOpsMode = changeOpsMode; |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 116 | |
| 117 | $scope.setTargetPower = function (port, targetVal) { |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 118 | wss.sendEvent(SET_TARGET_POWER_REQ, |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 119 | { |
| 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 | }()); |