1 | import {Component, ElementRef, EventEmitter, OnChanges, OnDestroy, OnInit, SimpleChange} from '@angular/core';
|
2 |
|
3 | import {InfoWindowManager} from '../services/managers/info-window-manager';
|
4 |
|
5 | import {SebmGoogleMapMarker} from './google-map-marker';
|
6 |
|
7 | let infoWindowId = 0;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | @Component({
|
39 | selector: 'sebm-google-map-info-window',
|
40 | inputs: ['latitude', 'longitude', 'disableAutoPan', 'isOpen', 'zIndex', 'maxWidth'],
|
41 | outputs: ['infoWindowClose'],
|
42 | template: `<div class='sebm-google-map-info-window-content'>
|
43 | <ng-content></ng-content>
|
44 | </div>
|
45 | `
|
46 | })
|
47 | export class SebmGoogleMapInfoWindow implements OnDestroy, OnChanges, OnInit {
|
48 | |
49 |
|
50 |
|
51 |
|
52 | latitude: number;
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | longitude: number;
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 | disableAutoPan: boolean;
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | zIndex: number;
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | maxWidth: number;
|
80 |
|
81 | |
82 |
|
83 |
|
84 | hostMarker: SebmGoogleMapMarker;
|
85 |
|
86 | |
87 |
|
88 |
|
89 | content: Node;
|
90 |
|
91 | |
92 |
|
93 |
|
94 | isOpen: boolean = false;
|
95 |
|
96 | |
97 |
|
98 |
|
99 | infoWindowClose: EventEmitter<void> = new EventEmitter<void>();
|
100 |
|
101 | private static _infoWindowOptionsInputs: string[] = ['disableAutoPan', 'maxWidth'];
|
102 | private _infoWindowAddedToManager: boolean = false;
|
103 | private _id: string = (infoWindowId++).toString();
|
104 |
|
105 | constructor(private _infoWindowManager: InfoWindowManager, private _el: ElementRef) {}
|
106 |
|
107 | ngOnInit() {
|
108 | this.content = this._el.nativeElement.querySelector('.sebm-google-map-info-window-content');
|
109 | this._infoWindowManager.addInfoWindow(this);
|
110 | this._infoWindowAddedToManager = true;
|
111 | this._updateOpenState();
|
112 | this._registerEventListeners();
|
113 | }
|
114 |
|
115 |
|
116 | ngOnChanges(changes: {[key: string]: SimpleChange}) {
|
117 | if (!this._infoWindowAddedToManager) {
|
118 | return;
|
119 | }
|
120 | if ((changes['latitude'] || changes['longitude']) && typeof this.latitude === 'number' &&
|
121 | typeof this.longitude === 'number') {
|
122 | this._infoWindowManager.setPosition(this);
|
123 | }
|
124 | if (changes['zIndex']) {
|
125 | this._infoWindowManager.setZIndex(this);
|
126 | }
|
127 | if (changes['isOpen']) {
|
128 | this._updateOpenState();
|
129 | }
|
130 | this._setInfoWindowOptions(changes);
|
131 | }
|
132 |
|
133 | private _registerEventListeners() {
|
134 | this._infoWindowManager.createEventObservable('closeclick', this).subscribe(() => {
|
135 | this.isOpen = false;
|
136 | this.infoWindowClose.emit();
|
137 | });
|
138 | }
|
139 |
|
140 | private _updateOpenState() {
|
141 | this.isOpen ? this.open() : this.close();
|
142 | }
|
143 |
|
144 | private _setInfoWindowOptions(changes: {[key: string]: SimpleChange}) {
|
145 | let options: {[propName: string]: any} = {};
|
146 | let optionKeys = Object.keys(changes).filter(
|
147 | k => SebmGoogleMapInfoWindow._infoWindowOptionsInputs.indexOf(k) !== -1);
|
148 | optionKeys.forEach((k) => { options[k] = changes[k].currentValue; });
|
149 | this._infoWindowManager.setOptions(this, options);
|
150 | }
|
151 |
|
152 | |
153 |
|
154 |
|
155 | open(): Promise<void> { return this._infoWindowManager.open(this); }
|
156 |
|
157 | |
158 |
|
159 |
|
160 | close(): Promise<void> {
|
161 | return this._infoWindowManager.close(this).then(() => { this.infoWindowClose.emit(); });
|
162 | }
|
163 |
|
164 |
|
165 | id(): string { return this._id; }
|
166 |
|
167 |
|
168 | toString(): string { return 'SebmGoogleMapInfoWindow-' + this._id.toString(); }
|
169 |
|
170 |
|
171 | ngOnDestroy() { this._infoWindowManager.deleteInfoWindow(this); }
|
172 | }
|