blob: 773be70ede03a44f46e71094bffdb6f91cdc83ba [file] [log] [blame]
Matteo Scandolocc8fa152016-02-22 09:57:55 -08001(function () {
2 'use strict';
Matteo Scandolo4b3d8722016-02-24 11:22:48 -08003 angular.module('xos.diagnostic')
Matteo Scandolo5bb16682016-03-01 17:08:45 -08004 .directive('selectSubscriberModal', function(){
Matteo Scandolocc8fa152016-02-22 09:57:55 -08005 return {
6 scope: {
7 subscribers: '=',
8 open: '='
9 },
10 bindToController: true,
11 restrict: 'E',
Matteo Scandolo5bb16682016-03-01 17:08:45 -080012 templateUrl: 'templates/select-subscriber-modal.tpl.html',
Matteo Scandolocc8fa152016-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 Scandolo5bb16682016-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 Scandolo5fc82c32016-03-02 10:59:46 -080037 controller: function($log, $timeout, $scope, Subscribers){
Matteo Scandolo5bb16682016-03-01 17:08:45 -080038
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080039 const mb = 1000000;
40
Matteo Scandolo5fc82c32016-03-02 10:59:46 -080041 $scope.$watch(() => this.open, () => {
42 this.success = null;
43 this.formError = null;
Matteo Scandolo5bb16682016-03-01 17:08:45 -080044 });
45
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080046 $scope.$watch(() => this.subscriber, (newVal, oldVal) => {
Matteo Scandolod1b3bf52016-03-07 16:42:03 -080047 if(!this.subscriber){
48 return;
49 }
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080050 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 Scandolobd663742016-03-07 16:07:13 -080054 });
55
Matteo Scandolo5bb16682016-03-01 17:08:45 -080056 this.close = () => {
57 this.open = false;
58 };
59
Matteo Scandolo5fc82c32016-03-02 10:59:46 -080060 this.updateSubscriber = (subscriber) => {
61
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080062 // TODO Copy the subscriber, this will update the GUI also and we don't want
63 // TODO Change GBps to MBps
Matteo Scandolod54a7fd2016-03-08 09:33:26 -080064
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080065 let body = angular.copy(subscriber, body);
Matteo Scandolod54a7fd2016-03-08 09:33:26 -080066
Matteo Scandolo97b82cf2016-03-08 17:01:17 -080067 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 Scandolo5fc82c32016-03-02 10:59:46 -080072 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 Scandolo5bb16682016-03-01 17:08:45 -080082 };
83 }
84 };
Matteo Scandolocc8fa152016-02-22 09:57:55 -080085 });
86})();