blob: e99f7e24966fe9058419f3daa8696489c24c5533 [file] [log] [blame]
Matteo Scandolo388795a2016-02-22 09:57:55 -08001(function () {
2 'use strict';
Matteo Scandolo04564952016-02-24 11:22:48 -08003 angular.module('xos.diagnostic')
Matteo Scandolo574c73f2016-03-01 17:08:45 -08004 .directive('selectSubscriberModal', function(){
Matteo Scandolo388795a2016-02-22 09:57:55 -08005 return {
6 scope: {
7 subscribers: '=',
8 open: '='
9 },
10 bindToController: true,
11 restrict: 'E',
Matteo Scandolo574c73f2016-03-01 17:08:45 -080012 templateUrl: 'templates/select-subscriber-modal.tpl.html',
Matteo Scandolo388795a2016-02-22 09:57:55 -080013 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 Scandolo574c73f2016-03-01 17:08:45 -080026 })
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 Scandolo6c6e5942016-03-02 10:59:46 -080037 controller: function($log, $timeout, $scope, Subscribers){
Matteo Scandolo574c73f2016-03-01 17:08:45 -080038
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080039 const mb = 1000000;
40
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080041 $scope.$watch(() => this.open, () => {
42 this.success = null;
43 this.formError = null;
Matteo Scandolo574c73f2016-03-01 17:08:45 -080044 });
45
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080046 $scope.$watch(() => this.subscriber, (newVal, oldVal) => {
Matteo Scandolo3a176a22016-03-07 16:42:03 -080047 if(!this.subscriber){
48 return;
49 }
Matteo Scandolof79f0b32016-09-30 10:23:10 -070050 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 Scandolo19acf7c2016-03-07 16:07:13 -080052 });
53
Matteo Scandolo574c73f2016-03-01 17:08:45 -080054 this.close = () => {
55 this.open = false;
56 };
57
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080058 this.updateSubscriber = (subscriber) => {
59
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080060 // TODO Copy the subscriber, this will update the GUI also and we don't want
61 // TODO Change GBps to MBps
Matteo Scandolo79108192016-03-08 09:33:26 -080062
Matteo Scandolo35d1c3a2016-03-08 17:01:17 -080063 let body = angular.copy(subscriber, body);
Matteo Scandolo79108192016-03-08 09:33:26 -080064
Matteo Scandolof79f0b32016-09-30 10:23:10 -070065 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 Scandolo35d1c3a2016-03-08 17:01:17 -080070
71 Subscribers.update(body).$promise
72 .then((res) => {
Matteo Scandolo6c6e5942016-03-02 10:59:46 -080073 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 Scandolo574c73f2016-03-01 17:08:45 -080083 };
84 }
85 };
Matteo Scandolo388795a2016-02-22 09:57:55 -080086 });
87})();