1 | import {Directive, EventEmitter, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
|
2 | import {Subscription} from 'rxjs/Subscription';
|
3 |
|
4 | import {KmlMouseEvent} from './../services/google-maps-types';
|
5 | import {KmlLayerManager} from './../services/managers/kml-layer-manager';
|
6 |
|
7 | let layerId = 0;
|
8 |
|
9 | @Directive({
|
10 | selector: 'sebm-google-map-kml-layer',
|
11 | inputs:
|
12 | ['clickable', 'preserveViewport', 'screenOverlays', 'suppressInfoWindows', 'url', 'zIndex'],
|
13 | outputs: ['layerClick', 'defaultViewportChange', 'statusChange']
|
14 | })
|
15 | export class SebmGoogleMapKmlLayer implements OnInit, OnDestroy, OnChanges {
|
16 | private _addedToManager: boolean = false;
|
17 | private _id: string = (layerId++).toString();
|
18 | private _subscriptions: Subscription[] = [];
|
19 | private static _kmlLayerOptions: string[] =
|
20 | ['clickable', 'preserveViewport', 'screenOverlays', 'suppressInfoWindows', 'url', 'zIndex'];
|
21 |
|
22 | |
23 |
|
24 |
|
25 | clickable: boolean = true;
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | preserveViewport: boolean = false;
|
34 |
|
35 | |
36 |
|
37 |
|
38 | screenOverlays: boolean = true;
|
39 |
|
40 | |
41 |
|
42 |
|
43 | suppressInfoWindows: boolean = false;
|
44 |
|
45 | |
46 |
|
47 |
|
48 | url: string = null;
|
49 |
|
50 | |
51 |
|
52 |
|
53 | zIndex: number|null = null;
|
54 |
|
55 | |
56 |
|
57 |
|
58 | layerClick: EventEmitter<KmlMouseEvent> = new EventEmitter<KmlMouseEvent>();
|
59 |
|
60 | |
61 |
|
62 |
|
63 | defaultViewportChange: EventEmitter<void> = new EventEmitter<void>();
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 | statusChange: EventEmitter<void> = new EventEmitter<void>();
|
71 |
|
72 | constructor(private _manager: KmlLayerManager) {}
|
73 |
|
74 | ngOnInit() {
|
75 | if (this._addedToManager) {
|
76 | return;
|
77 | }
|
78 | this._manager.addKmlLayer(this);
|
79 | this._addedToManager = true;
|
80 | this._addEventListeners();
|
81 | }
|
82 |
|
83 | ngOnChanges(changes: SimpleChanges) {
|
84 | if (!this._addedToManager) {
|
85 | return;
|
86 | }
|
87 | this._updatePolygonOptions(changes);
|
88 | }
|
89 |
|
90 | private _updatePolygonOptions(changes: SimpleChanges) {
|
91 | const options = Object.keys(changes)
|
92 | .filter(k => SebmGoogleMapKmlLayer._kmlLayerOptions.indexOf(k) !== -1)
|
93 | .reduce((obj: any, k: string) => {
|
94 | obj[k] = changes[k].currentValue;
|
95 | return obj;
|
96 | }, {});
|
97 | if (Object.keys(options).length > 0) {
|
98 | this._manager.setOptions(this, options);
|
99 | }
|
100 | }
|
101 |
|
102 | private _addEventListeners() {
|
103 | const listeners = [
|
104 | {name: 'click', handler: (ev: KmlMouseEvent) => this.layerClick.emit(ev)},
|
105 | {name: 'defaultviewport_changed', handler: () => this.defaultViewportChange.emit()},
|
106 | {name: 'status_changed', handler: () => this.statusChange.emit()},
|
107 | ];
|
108 | listeners.forEach((obj) => {
|
109 | const os = this._manager.createEventObservable(obj.name, this).subscribe(obj.handler);
|
110 | this._subscriptions.push(os);
|
111 | });
|
112 | }
|
113 |
|
114 |
|
115 | id(): string { return this._id; }
|
116 |
|
117 |
|
118 | toString(): string { return `SebmGoogleMapKmlLayer-${this._id.toString()}`; }
|
119 |
|
120 |
|
121 | ngOnDestroy() {
|
122 | this._manager.deleteKmlLayer(this);
|
123 |
|
124 | this._subscriptions.forEach(s => s.unsubscribe());
|
125 | }
|
126 | }
|