blob: 447ac7d590a4b30795332f2d4f71972795946e70 [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";
7
8 // injected references
9 var $log, $scope, $location, fs, tbs, wss, ns;
10
11 var portCbTable = {};
12
13 function setPortPower(port, targetVal, cb) {
14 var id = port.id;
15 portCbTable[id] = cb;
16 wss.sendEvent("roadmSetTargetPowerRequest",
17 {
18 devId: $scope.devId,
19 id: port.id,
20 targetPower: targetVal
21 });
22 }
23
24 function portPowerCb(data) {
25 portCbTable[data.id](data.valid, data.message);
26 }
27
28 // check if value is an integer
29 function isInteger(val) {
30 var INTEGER_REGEXP = /^\-?\d+$/;
31 if (INTEGER_REGEXP.test(val)) {
32 return true;
33 }
34 return false;
35 }
36
37 angular.module('ovRoadmPort', [])
38 .controller('OvRoadmPortCtrl',
39 ['$log', '$scope', '$location',
40 'FnService', 'TableBuilderService', 'WebSocketService', 'NavService',
41
42 function (_$log_, _$scope_, _$location_, _fs_, _tbs_, _wss_, _ns_) {
43 var params;
44 $log = _$log_;
45 $scope = _$scope_;
46 $location = _$location_;
47 fs = _fs_;
48 tbs = _tbs_;
49 wss = _wss_;
50 ns = _ns_;
51
52 $scope.deviceTip = 'Show device table';
53 $scope.flowTip = 'Show flow view for this device';
54 $scope.groupTip = 'Show group view for this device';
55 $scope.meterTip = 'Show meter view for selected device';
56
57 var handlers = {};
58 handlers[SET_TARGET_POWER_RESP] = portPowerCb;
59 wss.bindHandlers(handlers);
60
61 params = $location.search();
62 if (params.hasOwnProperty('devId')) {
63 $scope.devId = params['devId'];
64 }
65
66 tbs.buildTable({
67 scope: $scope,
68 tag: 'roadmPort',
69 query: params
70 });
71
72 $scope.setPortPower = setPortPower;
73
74 $scope.setTargetPower = function (port, targetVal) {
75 wss.sendEvent("roadmSetTargetPowerRequest",
76 {
77 devId: $scope.devId,
78 id: port.id,
79 targetPower: targetVal
80 });
81 $log.debug('Got a click on:', port);
82 }
83
84 $scope.nav = function (path) {
85 if ($scope.devId) {
86 ns.navTo(path, { devId: $scope.devId });
87 }
88 };
89
90 $scope.$on('$destroy', function () {
91 wss.unbindHandlers(handlers);
92 });
93
94 $log.log('OvRoadmPortCtrl has been created');
95 }])
96
97 .directive('roadmPower', ['WebSocketService', function() {
98
99 var retTemplate =
100 '<span class="target-power" ng-show="!editMode" ng-click="enableEdit()">{{currItem.targetPower}}</span>' +
101 '<form ng-show="editMode" name="form" novalidate>' +
102 '<input type="number" name="formVal" ng-model="formVal">' +
103 '<button type="submit" ng-click="send()">Set</button>' +
104 '<button type="button" ng-click="cancel()">Cancel</button>' +
105 '<span class="input-error" ng-show="showError">{{errorMessage}}</span>' +
106 '</form>';
107
108 return {
109 restrict: 'A',
110 scope: {
111 currItem: '=roadmPower',
112 roadmSetPower: '&'
113 },
114 template: retTemplate,
115 link: function ($scope, $element) {
116 $scope.editMode = false;
117 $scope.showError = false;
118 $scope.errorMessage = "Invalid target power";
119 },
120 controller: function($scope, $timeout) {
121 $scope.enableEdit = function() {
122 if ($scope.currItem.hasTargetPower === "true" && $scope.editMode === false) {
123 // Ensure that the entry being edited remains the same even
124 // if the table entries are shifted around.
125 $scope.targetItem = $scope.currItem;
126 // Ensure the value seen in the field remains the same
127 $scope.formVal = parseInt($scope.currItem.targetPower);
128 $scope.editMode = true;
129 $timeout(function () {
130 $scope.$apply()
131 });
132 }
133 };
134 // Callback for server-side validation. Displays the error message
135 // if the input is invalid.
136 $scope.sendCb = function(valid, message) {
137 if (valid) {
138 // check if it's still pointing to the same item
139 // reordering the entries may change the binding
140 if ($scope.currItem.id === $scope.targetItem.id) {
141 // update the ui to display the new attenuation value
142 $scope.currItem.targetPower = $scope.formVal;
143 }
144 $scope.cancel();
145 } else {
146 $scope.errorMessage = message;
147 $scope.showError = true;
148 }
149 $timeout(function () {
150 $scope.$apply()
151 });
152 }
153 $scope.send = function() {
154 // check input is an integer
155 if (!isInteger($scope.formVal)) {
156 $scope.sendCb(false, "Target power must be an integer");
157 return;
158 }
159 $scope.roadmSetPower({port: $scope.targetItem, targetVal: $scope.formVal, cb: $scope.sendCb});
160 };
161 $scope.cancel = function() {
162 $scope.editMode = false;
163 $scope.showError = false;
164 }
165 }
166 };
167 }]);
168}());