Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import * as _ from 'lodash'; |
| 18 | import './subscriber-dashboard.scss'; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 19 | import {IRCordSubscriber, IRCordSubscriberDevice} from '../interfaces/rcord-subscriber.interface'; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 20 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 21 | class RcordSubscriberDashboardCtrl { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 22 | |
| 23 | static $inject = [ |
| 24 | 'toastr', |
| 25 | 'XosModelStore', |
| 26 | 'XosModeldefsCache' |
| 27 | ]; |
| 28 | |
| 29 | public levelOptions = []; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 30 | public subscribers: IRCordSubscriber[] = []; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 31 | public statusFieldOptions = []; |
| 32 | public slider = { |
| 33 | floor: 0, |
| 34 | ceil: 1000000000, |
| 35 | translate: (value) => { |
| 36 | return Math.floor(value / 1000000); |
| 37 | } |
| 38 | }; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 39 | public selectedSubscriber: IRCordSubscriber; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 40 | |
| 41 | private subscriberDef; |
| 42 | |
| 43 | constructor ( |
| 44 | private toastr: any, |
| 45 | private XosModelStore: any, |
| 46 | private XosModeldefsCache: any |
| 47 | ) { |
| 48 | |
| 49 | } |
| 50 | |
| 51 | $onInit() { |
| 52 | this.XosModelStore.query('CordSubscriberRoot', '/rcord/cordsubscriberroots') |
| 53 | .subscribe( |
| 54 | res => { |
| 55 | this.subscribers = this.parseSubscribers(res); |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | this.levelOptions = [ |
| 60 | `G`, |
| 61 | `PG`, |
| 62 | `PG_13`, |
| 63 | `R`, |
| 64 | `X` |
| 65 | ]; |
| 66 | |
| 67 | this.subscriberDef = this.XosModeldefsCache.get('CordSubscriberRoot'); |
| 68 | this.statusFieldOptions = _.find(this.subscriberDef.fields, {name: 'status'}).options; |
| 69 | } |
| 70 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 71 | public addDevice(subscriber: IRCordSubscriber) { |
| 72 | if (angular.isUndefined(subscriber.service_specific_attribute.devices)) { |
| 73 | subscriber.service_specific_attribute.devices = []; |
| 74 | } |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 75 | subscriber.service_specific_attribute.devices.push({ |
| 76 | name: '', |
| 77 | mac: '', |
| 78 | level: 'PG_13' |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 79 | }); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 82 | public removeDevice(subscriber: IRCordSubscriber, device: IRCordSubscriberDevice) { |
| 83 | _.remove(subscriber.service_specific_attribute.devices, {name: device.name}); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 86 | public save(subscriber: IRCordSubscriber) { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 87 | const item: any = angular.copy(subscriber); |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 88 | |
| 89 | _.forEach(Object.keys(item), prop => { |
| 90 | if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) { |
| 91 | delete item[prop]; |
| 92 | } |
| 93 | }); |
| 94 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 95 | item.service_specific_attribute = JSON.stringify(item.service_specific_attribute); |
| 96 | item.$save() |
| 97 | .then(() => { |
| 98 | this.toastr.success(`Subscriber successfully saved`); |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 99 | }) |
| 100 | .catch(e => { |
| 101 | this.toastr.error(`Error while saving subscriber`); |
| 102 | console.error(e); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 103 | }); |
| 104 | } |
| 105 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 106 | private parseSubscribers(subscribers: IRCordSubscriber) { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 107 | return _.map(subscribers, (s) => { |
| 108 | if (angular.isString(s.service_specific_attribute)) { |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 109 | try { |
| 110 | s.service_specific_attribute = JSON.parse(s.service_specific_attribute); |
| 111 | } catch (e) { |
| 112 | s.service_specific_attribute = {}; |
| 113 | } |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 114 | } |
| 115 | return s; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 116 | }); |
| 117 | } |
| 118 | |
| 119 | $onDestroy() { |
| 120 | this.selectedSubscriber.service_specific_attribute = JSON.stringify(this.selectedSubscriber.service_specific_attribute); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | export const rcordSubscriberDashboard: angular.IComponentOptions = { |
| 125 | template: require('./subscriber-dashboard.html'), |
| 126 | controllerAs: 'vm', |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame^] | 127 | controller: RcordSubscriberDashboardCtrl |
| 128 | }; |