Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 1 | // js for roadm flow table view |
| 2 | (function () { |
| 3 | 'use strict'; |
| 4 | |
| 5 | var SET_ATT_REQ = "roadmSetAttenuationRequest"; |
| 6 | var SET_ATT_RESP = "roadmSetAttenuationResponse"; |
| 7 | var DELETE_FLOW_REQ = "roadmDeleteFlowRequest"; |
| 8 | var CREATE_FLOW_REQ = "roadmCreateFlowRequest"; |
| 9 | var CREATE_FLOW_RESP = "roadmCreateFlowResponse"; |
| 10 | |
| 11 | // injected references |
| 12 | var $log, $scope, $location, fs, tbs, wss, ns; |
| 13 | |
| 14 | // used to map id to a request call function |
| 15 | var flowCbTable = {}; |
| 16 | |
| 17 | function setAttenuation(flow, targetVal, cb) { |
| 18 | flowCbTable[flow.id] = cb; |
| 19 | wss.sendEvent(SET_ATT_REQ, |
| 20 | { |
| 21 | devId: $scope.devId, |
| 22 | flowId: flow.id, |
| 23 | attenuation: targetVal |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | function attenuationCb(data) { |
| 28 | flowCbTable[data.flowId](data.valid, data.message); |
| 29 | } |
| 30 | |
| 31 | // check if value is an integer |
| 32 | function isInteger(val) { |
| 33 | var INTEGER_REGEXP = /^\-?\d+$/; |
| 34 | if (INTEGER_REGEXP.test(val)) { |
| 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | angular.module('ovRoadmFlow', []) |
| 41 | .controller('OvRoadmFlowCtrl', |
| 42 | ['$log', '$scope', '$location', |
| 43 | 'FnService', 'TableBuilderService', 'WebSocketService', 'NavService', |
| 44 | |
| 45 | function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _wss_, _ns_) { |
| 46 | var params; |
| 47 | $log = _$log_; |
| 48 | $scope = _$scope_; |
| 49 | $location = _$location_; |
| 50 | fs = _fs_; |
| 51 | tbs = _tbs_; |
| 52 | wss = _wss_; |
| 53 | ns = _ns_; |
| 54 | |
| 55 | $scope.addFlowTip = 'Create a flow'; |
| 56 | $scope.deviceTip = 'Show device table'; |
| 57 | $scope.flowTip = 'Show flow view for this device'; |
| 58 | $scope.groupTip = 'Show group view for this device'; |
| 59 | $scope.meterTip = 'Show meter view for selected device'; |
| 60 | |
| 61 | $scope.showFlowForm = false; |
| 62 | |
| 63 | var handlers = {}; |
| 64 | handlers[SET_ATT_RESP] = attenuationCb; |
| 65 | wss.bindHandlers(handlers); |
| 66 | |
| 67 | params = $location.search(); |
| 68 | if (params.hasOwnProperty('devId')) { |
| 69 | $scope.devId = params['devId']; |
| 70 | } |
| 71 | |
| 72 | tbs.buildTable({ |
| 73 | scope: $scope, |
| 74 | tag: 'roadmFlow', |
| 75 | query: params |
| 76 | }); |
| 77 | |
| 78 | $scope.displayFlowForm = function () { |
| 79 | $scope.showFlowForm = true; |
| 80 | } |
| 81 | |
| 82 | $scope.hideFlowForm = function () { |
| 83 | $scope.showFlowForm = false; |
| 84 | } |
| 85 | |
| 86 | $scope.setAttenuation = setAttenuation; |
| 87 | |
| 88 | $scope.deleteFlow = function ($event, row) { |
| 89 | wss.sendEvent(DELETE_FLOW_REQ, |
| 90 | { |
| 91 | devId: $scope.devId, |
| 92 | id: row.id |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | $scope.createFlow = function(flow) { |
| 97 | wss.sendEvent(CREATE_FLOW_REQ, |
| 98 | { |
| 99 | devId: $scope.devId, |
| 100 | flow: flow |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | $scope.fakeCurrentPower = function(flow) { |
| 105 | if (!isNaN(flow.currentPower)) { |
| 106 | var val = parseInt(flow.attenuation); |
| 107 | return val + (val % 5 - 2); |
| 108 | } else { |
| 109 | return flow.currentPower; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $scope.nav = function (path) { |
| 114 | if ($scope.devId) { |
| 115 | ns.navTo(path, { devId: $scope.devId }); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | $scope.$on('$destroy', function () { |
| 120 | wss.unbindHandlers(handlers); |
| 121 | }); |
| 122 | |
| 123 | $log.log('OvRoadmFlowCtrl has been created'); |
| 124 | }]) |
| 125 | |
| 126 | .directive('roadmAtt', ['WebSocketService', function() { |
| 127 | |
| 128 | var retTemplate = |
| 129 | '<span class="attenuation" ng-show="!editMode" ng-click="enableEdit()">{{currItem.attenuation}}</span>' + |
| 130 | '<form ng-show="editMode" name="form" novalidate>' + |
| 131 | '<input type="number" name="formVal" ng-model="formVal">' + |
| 132 | '<button type="submit" class="submit" ng-click="send()">Set</button>' + |
| 133 | '<button type="button" class="cancel" ng-click="cancel()">Cancel</button>' + |
| 134 | '<span class="input-error" ng-show="showError">{{errorMessage}}</span>' + |
| 135 | '</form>'; |
| 136 | |
| 137 | return { |
| 138 | restrict: 'A', |
| 139 | scope: { |
| 140 | currItem: '=roadmAtt', |
| 141 | roadmSetAtt: '&' |
| 142 | }, |
| 143 | template: retTemplate, |
| 144 | link: function ($scope, $element) { |
| 145 | $scope.editMode = false; |
| 146 | $scope.showError = false; |
| 147 | $scope.errorMessage = "Invalid attenuation" |
| 148 | }, |
| 149 | controller: function($scope, $timeout) { |
| 150 | $scope.enableEdit = function() { |
| 151 | // connection must support attenuation to be editable |
| 152 | if ($scope.editMode === false) { |
| 153 | // Ensure that the entry being edited remains the same even |
| 154 | // if the table entries are shifted around. |
| 155 | $scope.targetItem = $scope.currItem; |
| 156 | // Ensure the value seen in the field remains the same |
| 157 | $scope.formVal = parseInt($scope.currItem.attenuation); |
| 158 | $scope.editMode = true; |
| 159 | $timeout(function () { |
| 160 | $scope.$apply() |
| 161 | }); |
| 162 | } |
| 163 | }; |
| 164 | $scope.send = function() { |
| 165 | // check input is an integer |
| 166 | if (!isInteger($scope.formVal)) { |
| 167 | $scope.sendCb(false, "Attenuation must be an integer"); |
| 168 | return; |
| 169 | } |
| 170 | $scope.roadmSetAtt({flow: $scope.targetItem, targetVal: $scope.formVal, cb: $scope.sendCb}); |
| 171 | }; |
| 172 | // Callback for server-side validation. Displays the error message |
| 173 | // if the input is invalid. |
| 174 | $scope.sendCb = function(valid, message) { |
| 175 | if (valid) { |
| 176 | // check if it's still pointing to the same item |
| 177 | // reordering the entries may change the binding |
| 178 | if ($scope.currItem.id === $scope.targetItem.id) { |
| 179 | // update the ui to display the new attenuation value |
| 180 | $scope.currItem.attenuation = $scope.formVal; |
| 181 | } |
| 182 | $scope.cancel(); |
| 183 | } else { |
| 184 | $scope.errorMessage = message; |
| 185 | $scope.showError = true; |
| 186 | } |
| 187 | $timeout(function () { |
| 188 | $scope.$apply() |
| 189 | }); |
| 190 | } |
| 191 | $scope.cancel = function() { |
| 192 | $scope.editMode = false; |
| 193 | $scope.showError = false; |
| 194 | } |
| 195 | } |
| 196 | }; |
| 197 | }]) |
| 198 | |
| 199 | .controller('FlowFormController', function($timeout) { |
| 200 | var notIntegerError = "Must be an integer."; |
| 201 | |
| 202 | this.clearErrors = function() { |
| 203 | this.priorityError = false; |
| 204 | this.timeoutError = false; |
| 205 | this.isPermanentError = false; |
| 206 | this.inPortError = false; |
| 207 | this.outPortError = false; |
| 208 | this.spacingError = false; |
| 209 | this.multiplierError = false; |
| 210 | this.attenuationError = false; |
| 211 | this.connectionError = false; |
| 212 | this.channelError = false; |
| 213 | } |
| 214 | this.clearErrors(); |
| 215 | |
| 216 | this.spacings = [ |
| 217 | {index: 0, freq: "100 GHz"}, |
| 218 | {index: 1, freq: "50 GHz"}, |
| 219 | {index: 2, freq: "25 GHz"}, |
| 220 | {index: 3, freq: "12.5 GHz"} |
| 221 | ]; |
| 222 | |
| 223 | this.flow = {}; |
| 224 | //this.flow.priority = 88; |
| 225 | this.flow.permanent = true; |
| 226 | this.flow.timeout = 0; |
| 227 | //this.flow.inPort = 2; |
| 228 | //this.flow.outPort = 2; |
| 229 | this.flow.spacing = this.spacings[1]; |
| 230 | //this.flow.multiplier = 0; |
| 231 | this.flow.includeAttenuation = true; |
| 232 | this.flow.attenuation = 0; |
| 233 | |
| 234 | var parent = this; |
| 235 | |
| 236 | function createFlowCb(data) { |
| 237 | if (!data.inPort.valid) { |
| 238 | parent.inPortMessage = data.inPort.message; |
| 239 | parent.inPortError = true; |
| 240 | } |
| 241 | if (!data.outPort.valid) { |
| 242 | parent.outPortMessage = data.outPort.message; |
| 243 | parent.outPortError = true; |
| 244 | } |
| 245 | if (!data.connection.valid) { |
| 246 | parent.connectionMessage = data.connection.message; |
| 247 | parent.connectionError = true; |
| 248 | } |
| 249 | if (!data.spacing.valid) { |
| 250 | parent.spacingMessage = data.spacing.message; |
| 251 | parent.spacingError = true; |
| 252 | } |
| 253 | if (!data.multiplier.valid) { |
| 254 | parent.multiplierMessage = data.multiplier.message; |
| 255 | parent.multiplierError = true; |
| 256 | } |
| 257 | if (!data.channelAvailable.valid) { |
| 258 | parent.channelMessage = data.channelAvailable.message; |
| 259 | parent.channelError = true; |
| 260 | } |
| 261 | if (data.includeAttenuation && !data.attenuation.valid) { |
| 262 | parent.attenuationMessage = data.attenuation.message; |
| 263 | parent.attenuationError = true; |
| 264 | } |
| 265 | $timeout(function () { |
| 266 | $scope.$apply() |
| 267 | }); |
| 268 | } |
| 269 | |
| 270 | var handlers = {} |
| 271 | handlers[CREATE_FLOW_RESP] = createFlowCb; |
| 272 | wss.bindHandlers(handlers); |
| 273 | |
| 274 | this.createFlow = function(connection) { |
| 275 | this.clearErrors(); |
| 276 | |
| 277 | var error = false; |
| 278 | if (!isInteger(connection.priority)) { |
| 279 | this.priorityMessage = notIntegerError; |
| 280 | this.priorityError = true; |
| 281 | error = true; |
| 282 | } |
| 283 | if (!connection.permanent && !isInteger(connection.timeout)) { |
| 284 | this.timeoutMessage = notIntegerError; |
| 285 | this.timeoutError = true; |
| 286 | error = true; |
| 287 | } |
| 288 | if (!isInteger(connection.inPort)) { |
| 289 | this.inPortMessage = notIntegerError; |
| 290 | this.inPortError = true; |
| 291 | error = true; |
| 292 | } |
| 293 | if (!isInteger(connection.outPort)) { |
| 294 | this.outPortMessage = notIntegerError; |
| 295 | this.outPortError = true; |
| 296 | error = true; |
| 297 | } |
| 298 | if (!isInteger(connection.multiplier)) { |
| 299 | this.multiplierMessage = notIntegerError; |
| 300 | this.multiplierError = true; |
| 301 | error = true; |
| 302 | } |
| 303 | if (connection.includeAttenuation && !isInteger(connection.attenuation)) { |
| 304 | this.attenuationMessage = notIntegerError; |
| 305 | this.attenuationError = true; |
| 306 | error = true; |
| 307 | } |
| 308 | |
| 309 | if (!error) { |
| 310 | wss.sendEvent(CREATE_FLOW_REQ, |
| 311 | { |
| 312 | devId: $scope.devId, |
| 313 | formData: connection |
| 314 | }); |
| 315 | $log.log('Request to create connection has been sent'); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | $scope.$on('$destroy', function () { |
| 320 | wss.unbindHandlers(handlers); |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | }()); |