UNPKG

3.94 kBPlain TextView Raw
1import {Directive, EventEmitter, OnChanges, OnDestroy, OnInit, SimpleChanges} from '@angular/core';
2import {Subscription} from 'rxjs/Subscription';
3
4import {KmlMouseEvent} from './../services/google-maps-types';
5import {KmlLayerManager} from './../services/managers/kml-layer-manager';
6
7let 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})
15export 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 * If true, the layer receives mouse events. Default value is true.
24 */
25 clickable: boolean = true;
26
27 /**
28 * By default, the input map is centered and zoomed to the bounding box of the contents of the
29 * layer.
30 * If this option is set to true, the viewport is left unchanged, unless the map's center and zoom
31 * were never set.
32 */
33 preserveViewport: boolean = false;
34
35 /**
36 * Whether to render the screen overlays. Default true.
37 */
38 screenOverlays: boolean = true;
39
40 /**
41 * Suppress the rendering of info windows when layer features are clicked.
42 */
43 suppressInfoWindows: boolean = false;
44
45 /**
46 * The URL of the KML document to display.
47 */
48 url: string = null;
49
50 /**
51 * The z-index of the layer.
52 */
53 zIndex: number|null = null;
54
55 /**
56 * This event is fired when a feature in the layer is clicked.
57 */
58 layerClick: EventEmitter<KmlMouseEvent> = new EventEmitter<KmlMouseEvent>();
59
60 /**
61 * This event is fired when the KML layers default viewport has changed.
62 */
63 defaultViewportChange: EventEmitter<void> = new EventEmitter<void>();
64
65 /**
66 * This event is fired when the KML layer has finished loading.
67 * At this point it is safe to read the status property to determine if the layer loaded
68 * successfully.
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 /** @internal */
115 id(): string { return this._id; }
116
117 /** @internal */
118 toString(): string { return `SebmGoogleMapKmlLayer-${this._id.toString()}`; }
119
120 /** @internal */
121 ngOnDestroy() {
122 this._manager.deleteKmlLayer(this);
123 // unsubscribe all registered observable subscriptions
124 this._subscriptions.forEach(s => s.unsubscribe());
125 }
126}