Matteo Scandolo | cc8fa15 | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
Matteo Scandolo | 4b3d872 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 3 | angular.module('xos.diagnostic') |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 4 | .directive('selectSubscriberModal', function(){ |
Matteo Scandolo | cc8fa15 | 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 | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 12 | templateUrl: 'templates/select-subscriber-modal.tpl.html', |
Matteo Scandolo | cc8fa15 | 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 | 5bb1668 | 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 | 5fc82c3 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 37 | controller: function($log, $timeout, $scope, Subscribers){ |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 38 | |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 39 | const mb = 1000000; |
| 40 | |
Matteo Scandolo | 5fc82c3 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 41 | $scope.$watch(() => this.open, () => { |
| 42 | this.success = null; |
| 43 | this.formError = null; |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 44 | }); |
| 45 | |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 46 | $scope.$watch(() => this.subscriber, (newVal, oldVal) => { |
Matteo Scandolo | d1b3bf5 | 2016-03-07 16:42:03 -0800 | [diff] [blame] | 47 | if(!this.subscriber){ |
| 48 | return; |
| 49 | } |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 50 | console.log(newVal, oldVal); |
| 51 | console.log('subscriber change', newVal === oldVal); |
| 52 | this.subscriber.uplink_speed = parseInt(this.subscriber.uplink_speed, 10) / mb; |
| 53 | this.subscriber.downlink_speed = parseInt(this.subscriber.downlink_speed, 10) / mb; |
Matteo Scandolo | bd66374 | 2016-03-07 16:07:13 -0800 | [diff] [blame] | 54 | }); |
| 55 | |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 56 | this.close = () => { |
| 57 | this.open = false; |
| 58 | }; |
| 59 | |
Matteo Scandolo | 5fc82c3 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 60 | this.updateSubscriber = (subscriber) => { |
| 61 | |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 62 | // TODO Copy the subscriber, this will update the GUI also and we don't want |
| 63 | // TODO Change GBps to MBps |
Matteo Scandolo | d54a7fd | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 64 | |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 65 | let body = angular.copy(subscriber, body); |
Matteo Scandolo | d54a7fd | 2016-03-08 09:33:26 -0800 | [diff] [blame] | 66 | |
Matteo Scandolo | 97b82cf | 2016-03-08 17:01:17 -0800 | [diff] [blame] | 67 | body.uplink_speed = body.uplink_speed * mb; |
| 68 | body.downlink_speed = body.downlink_speed * mb; |
| 69 | |
| 70 | Subscribers.update(body).$promise |
| 71 | .then((res) => { |
Matteo Scandolo | 5fc82c3 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 72 | this.success = 'Subscriber successfully updated!'; |
| 73 | }) |
| 74 | .catch((e) => { |
| 75 | this.formError = e; |
| 76 | }) |
| 77 | .finally(() => { |
| 78 | $timeout(() => { |
| 79 | this.close(); |
| 80 | }, 1500); |
| 81 | }); |
Matteo Scandolo | 5bb1668 | 2016-03-01 17:08:45 -0800 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | }; |
Matteo Scandolo | cc8fa15 | 2016-02-22 09:57:55 -0800 | [diff] [blame] | 85 | }); |
| 86 | })(); |