Max Chu | 3c8b65f | 2017-07-19 17:47:59 -0700 | [diff] [blame] | 1 | import {NgMap} from 'ngmap'; |
| 2 | import {Subscription} from 'rxjs/Subscription'; |
| 3 | import * as _ from 'lodash'; |
| 4 | |
| 5 | declare var google; |
| 6 | |
| 7 | let self; |
| 8 | |
| 9 | export class MngMap { |
| 10 | |
| 11 | static $inject = [ |
| 12 | 'NgMap', |
| 13 | 'XosModelStore', |
| 14 | 'AppConfig', |
| 15 | '$resource', |
| 16 | 'XosSidePanel', |
| 17 | 'XosModelDiscoverer', |
| 18 | 'ModelRest', |
| 19 | 'MapConfig', |
| 20 | ]; |
| 21 | |
| 22 | public unis = []; |
| 23 | public elines = []; |
| 24 | public cpilatlng = new Map(); |
| 25 | public paths = []; |
| 26 | public bwps = []; |
| 27 | public map; |
| 28 | public panelOpen = false; |
| 29 | public createMode = false; |
| 30 | public canCreateEline = true; |
| 31 | public eline; |
| 32 | public current_uni; |
| 33 | public mapStyles = [{'featureType': 'administrative', 'elementType': 'labels.text.fill', 'stylers': [{'color': '#444444'}]}, {'featureType': 'landscape', 'elementType': 'all', 'stylers': [{'color': '#f2f2f2'}]}, {'featureType': 'poi', 'elementType': 'all', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'road', 'elementType': 'all', 'stylers': [{'saturation': -100}, {'lightness': 45}]}, {'featureType': 'road.highway', 'elementType': 'all', 'stylers': [{'visibility': 'simplified'}]}, {'featureType': 'road.arterial', 'elementType': 'labels.icon', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'transit', 'elementType': 'all', 'stylers': [{'visibility': 'off'}]}, {'featureType': 'water', 'elementType': 'all', 'stylers': [{'color': '#9ce1fc'}, {'visibility': 'on'}]}]; |
| 34 | |
| 35 | private uniSubscription: Subscription; |
| 36 | private elineSubscription: Subscription; |
| 37 | private bwpSubscription: Subscription; |
| 38 | |
| 39 | constructor( |
| 40 | private NgMap: any, |
| 41 | private XosModelStore: any, |
| 42 | private AppConfig: any, |
| 43 | private $resource: any, |
| 44 | private XosSidePanel: any, |
| 45 | private XosModelDiscoverer: any, |
| 46 | private ModelRest: any, |
| 47 | private MapConfig: any, |
| 48 | ) { |
| 49 | self = this; |
| 50 | } |
| 51 | |
| 52 | $onInit() { |
| 53 | this.NgMap.getMap().then(map => { |
| 54 | this.map = map; |
| 55 | this.uniSubscription = this.XosModelStore.query('UserNetworkInterface', '/metronet/usernetworkinterfaces/').subscribe( |
| 56 | res => { |
| 57 | this.unis = res; |
| 58 | this.renderMap(map); |
| 59 | } |
| 60 | ); |
| 61 | this.elineSubscription = this.XosModelStore.query('ELine', '/metronet/elines/').subscribe( |
| 62 | res => { |
| 63 | this.elines = res; |
| 64 | this.createPaths(); |
| 65 | this.renderMap(map); |
| 66 | } |
| 67 | ); |
| 68 | this.bwpSubscription = this.XosModelStore.query('BandwidthProfile', '/metronet/bandwidthprofiles/').subscribe( |
| 69 | res => { |
| 70 | this.bwps = res; |
| 71 | } |
| 72 | ); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | $onDestroy() { |
| 77 | if (this.uniSubscription) { |
| 78 | this.uniSubscription.unsubscribe(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public renderMap(map: NgMap) { |
| 83 | |
| 84 | let bounds = new google.maps.LatLngBounds(); |
| 85 | |
| 86 | for (let i = 0; i < self.unis.length; i++) { |
| 87 | self.unis[i].eline_start = false; |
| 88 | let curr = JSON.parse(self.unis[i].latlng); |
| 89 | this.cpilatlng.set(self.unis[i].cpe_id, curr); |
| 90 | let latlng = new google.maps.LatLng(curr[0], curr[1]); |
| 91 | bounds.extend(latlng); |
| 92 | } |
| 93 | map.setCenter(bounds.getCenter()); |
| 94 | map.fitBounds(bounds); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | public createPaths() { |
| 99 | this.elines.forEach((eline: any) => { |
| 100 | let latlng_start = this.cpilatlng.get(eline.connect_point_1_id); |
| 101 | let latlng_end = this.cpilatlng.get(eline.connect_point_2_id); |
| 102 | eline.path = [latlng_start, latlng_end]; |
| 103 | }); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | public colorLine(eline_status : any) { |
| 108 | let status = parseInt(eline_status, 10); |
| 109 | switch (status) { |
| 110 | case 0: |
| 111 | return '#f39c12'; |
| 112 | case 1: |
| 113 | return '#2ecc71'; |
| 114 | default: |
| 115 | return '#e74c3c'; |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | public showUni(e: any, uni: any) { |
| 121 | self.current_uni = uni; |
| 122 | self.map.showInfoWindow('uni-info', this); |
| 123 | } |
| 124 | |
| 125 | // do not display backend status or ID in create mode |
| 126 | |
| 127 | public elinePanel(e: any, eline: any, exists: boolean) { |
| 128 | self.panelOpen = !self.panelOpen; |
| 129 | if (exists) { |
| 130 | self.eline = _.find(self.elines, {id: eline.id}); |
| 131 | } |
| 132 | self.XosSidePanel.toggleComponent('elineSide', {eline: self.eline, bwplist: self.bwps, mng: self}, false); |
| 133 | if (!self.panelOpen && self.createMode) { |
| 134 | self.createMode = false; |
| 135 | self.canCreateEline = true; |
| 136 | self.current_uni.eline_start = false; |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | public createConnection(uni: any) { |
| 142 | return () => { |
| 143 | self.canCreateEline = false; |
| 144 | self.createMode = true; |
| 145 | uni.eline_start = true; |
| 146 | self.current_uni = uni; |
| 147 | self.eline = { |
| 148 | name: uni.name, |
| 149 | uni1name: uni.name, |
| 150 | connect_point_1_id: uni.cpe_id, |
| 151 | }; |
| 152 | self.elinePanel({}, self.eline, false); |
| 153 | }; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | public finishConnection(uni: any) { |
| 158 | self.eline.connect_point_2_id = uni.cpe_id; |
| 159 | if (self.eline.name === self.eline.uni1name) { |
| 160 | self.eline.name = self.eline.name + uni.name; |
| 161 | } |
| 162 | delete self.eline.uni1name; |
| 163 | const resource = this.ModelRest.getResource('/metronet/elines/'); |
| 164 | let res = new resource({}); |
| 165 | for (let attr in self.eline) { |
| 166 | if (true) { |
| 167 | res[attr] = self.eline[attr]; |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | self.eline = res; |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | export const mngMap: angular.IComponentOptions = { |
| 177 | template: require('./mngMap.component.html'), |
| 178 | controllerAs: 'vm', |
| 179 | controller: MngMap, |
| 180 | }; |