-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
map-info-window.ts
225 lines (196 loc) · 6.87 KB
/
map-info-window.ts
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
/// <reference types="googlemaps" />
import {
Directive,
ElementRef,
Input,
NgZone,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
import {BehaviorSubject, combineLatest, Observable, Subject} from 'rxjs';
import {map, take, takeUntil} from 'rxjs/operators';
import {GoogleMap} from '../google-map/google-map';
import {MapEventManager} from '../map-event-manager';
import {MapAnchorPoint} from '../map-anchor-point';
/**
* Angular component that renders a Google Maps info window via the Google Maps JavaScript API.
*
* See developers.google.com/maps/documentation/javascript/reference/info-window
*/
@Directive({
selector: 'map-info-window',
exportAs: 'mapInfoWindow',
host: {'style': 'display: none'},
})
export class MapInfoWindow implements OnInit, OnDestroy {
private _eventManager = new MapEventManager(this._ngZone);
private readonly _options = new BehaviorSubject<google.maps.InfoWindowOptions>({});
private readonly _position =
new BehaviorSubject<google.maps.LatLngLiteral|google.maps.LatLng|undefined>(undefined);
private readonly _destroy = new Subject<void>();
/**
* Underlying google.maps.InfoWindow
*
* See developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow
*/
infoWindow?: google.maps.InfoWindow;
@Input()
set options(options: google.maps.InfoWindowOptions) {
this._options.next(options || {});
}
@Input()
set position(position: google.maps.LatLngLiteral|google.maps.LatLng) {
this._position.next(position);
}
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.closeclick
*/
@Output() closeclick: Observable<void> = this._eventManager.getLazyEmitter<void>('closeclick');
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window
* #InfoWindow.content_changed
*/
@Output()
contentChanged: Observable<void> = this._eventManager.getLazyEmitter<void>('content_changed');
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.domready
*/
@Output() domready: Observable<void> = this._eventManager.getLazyEmitter<void>('domready');
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window
* #InfoWindow.position_changed
*/
@Output()
positionChanged: Observable<void> = this._eventManager.getLazyEmitter<void>('position_changed');
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window
* #InfoWindow.zindex_changed
*/
@Output()
zindexChanged: Observable<void> = this._eventManager.getLazyEmitter<void>('zindex_changed');
constructor(private readonly _googleMap: GoogleMap,
private _elementRef: ElementRef<HTMLElement>,
private _ngZone: NgZone) {}
ngOnInit() {
if (this._googleMap._isBrowser) {
const combinedOptionsChanges = this._combineOptions();
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
// Create the object outside the zone so its events don't trigger change detection.
// We'll bring it back in inside the `MapEventManager` only for the events that the
// user has subscribed to.
this._ngZone.runOutsideAngular(() => {
this.infoWindow = new google.maps.InfoWindow(options);
});
this._eventManager.setTarget(this.infoWindow);
});
this._watchForOptionsChanges();
this._watchForPositionChanges();
}
}
ngOnDestroy() {
this._eventManager.destroy();
this._destroy.next();
this._destroy.complete();
// If no info window has been created on the server, we do not try closing it.
// On the server, an info window cannot be created and this would cause errors.
if (this.infoWindow) {
this.close();
}
}
/**
* See developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.close
*/
close() {
this._assertInitialized();
this.infoWindow.close();
}
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getContent
*/
getContent(): string|Node {
this._assertInitialized();
return this.infoWindow.getContent();
}
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window
* #InfoWindow.getPosition
*/
getPosition(): google.maps.LatLng|null {
this._assertInitialized();
return this.infoWindow.getPosition();
}
/**
* See
* developers.google.com/maps/documentation/javascript/reference/info-window#InfoWindow.getZIndex
*/
getZIndex(): number {
this._assertInitialized();
return this.infoWindow.getZIndex();
}
/**
* Opens the MapInfoWindow using the provided anchor. If the anchor is not set,
* then the position property of the options input is used instead.
*/
open(anchor?: MapAnchorPoint) {
this._assertInitialized();
const anchorObject = anchor ? anchor.getAnchor() : undefined;
// Prevent the info window from initializing if trying to reopen on the same anchor.
if (this.infoWindow.get('anchor') !== anchorObject) {
this._elementRef.nativeElement.style.display = '';
this.infoWindow.open(this._googleMap.googleMap, anchorObject);
}
}
private _combineOptions(): Observable<google.maps.InfoWindowOptions> {
return combineLatest([this._options, this._position]).pipe(map(([options, position]) => {
const combinedOptions: google.maps.InfoWindowOptions = {
...options,
position: position || options.position,
content: this._elementRef.nativeElement,
};
return combinedOptions;
}));
}
private _watchForOptionsChanges() {
this._options.pipe(takeUntil(this._destroy)).subscribe(options => {
this._assertInitialized();
this.infoWindow.setOptions(options);
});
}
private _watchForPositionChanges() {
this._position.pipe(takeUntil(this._destroy)).subscribe(position => {
if (position) {
this._assertInitialized();
this.infoWindow.setPosition(position);
}
});
}
private _assertInitialized(): asserts this is {infoWindow: google.maps.InfoWindow} {
if (!this._googleMap.googleMap) {
throw Error(
'Cannot access Google Map information before the API has been initialized. ' +
'Please wait for the API to load before trying to interact with it.');
}
if (!this.infoWindow) {
throw Error(
'Cannot interact with a Google Map Info Window before it has been ' +
'initialized. Please wait for the Info Window to load before trying to interact with ' +
'it.');
}
}
}