Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 21 | angular.module('xos.diagnostic') |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 22 | .directive('selectSubscriberModal', function(){ |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 23 | return { |
| 24 | scope: { |
| 25 | subscribers: '=', |
| 26 | open: '=' |
| 27 | }, |
| 28 | bindToController: true, |
| 29 | restrict: 'E', |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 30 | templateUrl: 'templates/select-subscriber-modal.tpl.html', |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 31 | 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 Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 44 | }) |
| 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 Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 55 | controller: function($log, $timeout, $scope, Subscribers){ |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 56 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 57 | const mb = 1000000; |
| 58 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 59 | $scope.$watch(() => this.open, () => { |
| 60 | this.success = null; |
| 61 | this.formError = null; |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 62 | }); |
| 63 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 64 | $scope.$watch(() => this.subscriber, (newVal, oldVal) => { |
Matteo Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 65 | if(!this.subscriber){ |
| 66 | return; |
| 67 | } |
Matteo Scandolo | f79f0b3 | 2016-09-30 10:23:10 -0700 | [diff] [blame] | 68 | 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 Scandolo | 19acf7c | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 70 | }); |
| 71 | |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 72 | this.close = () => { |
| 73 | this.open = false; |
| 74 | }; |
| 75 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 76 | this.updateSubscriber = (subscriber) => { |
| 77 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 78 | // TODO Copy the subscriber, this will update the GUI also and we don't want |
| 79 | // TODO Change GBps to MBps |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 80 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 81 | let body = angular.copy(subscriber, body); |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 82 | |
Matteo Scandolo | f79f0b3 | 2016-09-30 10:23:10 -0700 | [diff] [blame] | 83 | 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 Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 88 | |
| 89 | Subscribers.update(body).$promise |
| 90 | .then((res) => { |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 91 | 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 Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 101 | }; |
| 102 | } |
| 103 | }; |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 104 | }); |
| 105 | })(); |