Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 3 | angular.module('xos.diagnostic') |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 4 | .directive('selectSubscriberModal', function(){ |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 5 | return { |
| 6 | scope: { |
| 7 | subscribers: '=', |
| 8 | open: '=' |
| 9 | }, |
| 10 | bindToController: true, |
| 11 | restrict: 'E', |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 12 | templateUrl: 'templates/select-subscriber-modal.tpl.html', |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 13 | controllerAs: 'vm', |
| 14 | controller: function($rootScope){ |
| 15 | |
| 16 | this.close = () => { |
| 17 | this.open = false; |
| 18 | }; |
| 19 | |
| 20 | this.select = (subscriber) => { |
| 21 | $rootScope.$emit('subscriber.selected', subscriber); |
| 22 | this.close(); |
| 23 | }; |
| 24 | } |
| 25 | }; |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 26 | }) |
| 27 | .directive('subscriberStatusModal', function(){ |
| 28 | return { |
| 29 | scope: { |
| 30 | open: '=', |
| 31 | subscriber: '=' |
| 32 | }, |
| 33 | bindToController: true, |
| 34 | restrict: 'E', |
| 35 | templateUrl: 'templates/subscriber-status-modal.tpl.html', |
| 36 | controllerAs: 'vm', |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 37 | controller: function($log, $timeout, $scope, Subscribers){ |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 38 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 39 | const mb = 1000000; |
| 40 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 41 | $scope.$watch(() => this.open, () => { |
| 42 | this.success = null; |
| 43 | this.formError = null; |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 44 | }); |
| 45 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 46 | $scope.$watch(() => this.subscriber, (newVal, oldVal) => { |
Matteo Scandolo | 3a176a2 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 47 | if(!this.subscriber){ |
| 48 | return; |
| 49 | } |
Matteo Scandolo | f79f0b3 | 2016-09-30 10:23:10 -0700 | [diff] [blame] | 50 | this.subscriber.features.uplink_speed = parseInt(this.subscriber.features.uplink_speed, 10) / mb; |
| 51 | 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] | 52 | }); |
| 53 | |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 54 | this.close = () => { |
| 55 | this.open = false; |
| 56 | }; |
| 57 | |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 58 | this.updateSubscriber = (subscriber) => { |
| 59 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 60 | // TODO Copy the subscriber, this will update the GUI also and we don't want |
| 61 | // TODO Change GBps to MBps |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 63 | let body = angular.copy(subscriber, body); |
Matteo Scandolo | 7910819 | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 64 | |
Matteo Scandolo | f79f0b3 | 2016-09-30 10:23:10 -0700 | [diff] [blame] | 65 | body.features.uplink_speed = body.features.uplink_speed * mb; |
| 66 | body.features.downlink_speed = body.features.downlink_speed * mb; |
| 67 | |
| 68 | // remove read only attributes |
| 69 | delete body.related; |
Matteo Scandolo | 35d1c3a | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 70 | |
| 71 | Subscribers.update(body).$promise |
| 72 | .then((res) => { |
Matteo Scandolo | 6c6e594 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 73 | this.success = 'Subscriber successfully updated!'; |
| 74 | }) |
| 75 | .catch((e) => { |
| 76 | this.formError = e; |
| 77 | }) |
| 78 | .finally(() => { |
| 79 | $timeout(() => { |
| 80 | this.close(); |
| 81 | }, 1500); |
| 82 | }); |
Matteo Scandolo | 574c73f | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 83 | }; |
| 84 | } |
| 85 | }; |
Matteo Scandolo | 388795a | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 86 | }); |
| 87 | })(); |