blob: c681f93046add233331d0e0fc3174a2dfa3e61ac [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo388795a2016-02-22 09:57:55 -080019(function () {
20 'use strict';
Matteo Scandolo04564952016-02-24 11:22:48 -080021 angular.module('xos.diagnostic')
Matteo Scandolo574c73f2016-03-01 17:08:45 -080022 .directive('selectSubscriberModal', function(){
Matteo Scandolo388795a2016-02-22 09:57:55 -080023 return {
24 scope: {
25 subscribers: '=',
26 open: '='
27 },
28 bindToController: true,
29 restrict: 'E',
Matteo Scandolo574c73f2016-03-01 17:08:45 -080030 templateUrl: 'templates/select-subscriber-modal.tpl.html',
Matteo Scandolo388795a2016-02-22 09:57:55 -080031 controllerAs: 'vm',
32 controller: function($rootScope){
33
34 this.close = () => {
35 this.open = false;
36 };
37
38 this.select = (subscriber) => {
39 $rootScope.$emit('subscriber.selected', subscriber);
40 this.close();
41 };
42 }
43 };
Matteo Scandolo574c73f2016-03-01 17:08:45 -080044 })
45 .directive('subscriberStatusModal', function(){
46 return {
47 scope: {
48 open: '=',
49 subscriber: '='
50 },
51 bindToController: true,
52 restrict: 'E',
53 templateUrl: 'templates/subscriber-status-modal.tpl.html',
54 controllerAs: 'vm',
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080055 controller: function($log, $timeout, $scope, Subscribers){
Matteo Scandolo574c73f2016-03-01 17:08:45 -080056
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080057 const mb = 1000000;
58
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080059 $scope.$watch(() => this.open, () => {
60 this.success = null;
61 this.formError = null;
Matteo Scandolo574c73f2016-03-01 17:08:45 -080062 });
63
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080064 $scope.$watch(() => this.subscriber, (newVal, oldVal) => {
Matteo Scandolo3a176a22016-03-07 16:42:03 -080065 if(!this.subscriber){
66 return;
67 }
Matteo Scandolof79f0b32016-09-30 10:23:10 -070068 this.subscriber.features.uplink_speed = parseInt(this.subscriber.features.uplink_speed, 10) / mb;
69 this.subscriber.features.downlink_speed = parseInt(this.subscriber.features.downlink_speed, 10) / mb;
Matteo Scandolo19acf7c2016-03-07 16:07:13 -080070 });
71
Matteo Scandolo574c73f2016-03-01 17:08:45 -080072 this.close = () => {
73 this.open = false;
74 };
75
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080076 this.updateSubscriber = (subscriber) => {
77
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080078 // TODO Copy the subscriber, this will update the GUI also and we don't want
79 // TODO Change GBps to MBps
Matteo Scandolo79108192016-03-08 09:33:26 -080080
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080081 let body = angular.copy(subscriber, body);
Matteo Scandolo79108192016-03-08 09:33:26 -080082
Matteo Scandolof79f0b32016-09-30 10:23:10 -070083 body.features.uplink_speed = body.features.uplink_speed * mb;
84 body.features.downlink_speed = body.features.downlink_speed * mb;
85
86 // remove read only attributes
87 delete body.related;
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080088
89 Subscribers.update(body).$promise
90 .then((res) => {
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080091 this.success = 'Subscriber successfully updated!';
92 })
93 .catch((e) => {
94 this.formError = e;
95 })
96 .finally(() => {
97 $timeout(() => {
98 this.close();
99 }, 1500);
100 });
Matteo Scandolo574c73f2016-03-01 17:08:45 -0800101 };
102 }
103 };
Matteo Scandolo388795a2016-02-22 09:57:55 -0800104 });
105})();