1 | import { Observable } from 'rxjs/Observable';
|
2 | import { Observer } from 'rxjs/Observer';
|
3 | import {Injectable, NgZone} from '@angular/core';
|
4 |
|
5 | import {SebmGoogleMapInfoWindow} from '../../directives/google-map-info-window';
|
6 |
|
7 | import {GoogleMapsAPIWrapper} from '../google-maps-api-wrapper';
|
8 | import {InfoWindow, InfoWindowOptions} from '../google-maps-types';
|
9 | import {MarkerManager} from './marker-manager';
|
10 |
|
11 | @Injectable()
|
12 | export class InfoWindowManager {
|
13 | private _infoWindows: Map<SebmGoogleMapInfoWindow, Promise<InfoWindow>> =
|
14 | new Map<SebmGoogleMapInfoWindow, Promise<InfoWindow>>();
|
15 |
|
16 | constructor(
|
17 | private _mapsWrapper: GoogleMapsAPIWrapper, private _zone: NgZone,
|
18 | private _markerManager: MarkerManager) {}
|
19 |
|
20 | deleteInfoWindow(infoWindow: SebmGoogleMapInfoWindow): Promise<void> {
|
21 | const iWindow = this._infoWindows.get(infoWindow);
|
22 | if (iWindow == null) {
|
23 |
|
24 | return Promise.resolve();
|
25 | }
|
26 | return iWindow.then((i: InfoWindow) => {
|
27 | return this._zone.run(() => {
|
28 | i.close();
|
29 | this._infoWindows.delete(infoWindow);
|
30 | });
|
31 | });
|
32 | }
|
33 |
|
34 | setPosition(infoWindow: SebmGoogleMapInfoWindow): Promise<void> {
|
35 | return this._infoWindows.get(infoWindow).then((i: InfoWindow) => i.setPosition({
|
36 | lat: infoWindow.latitude,
|
37 | lng: infoWindow.longitude
|
38 | }));
|
39 | }
|
40 |
|
41 | setZIndex(infoWindow: SebmGoogleMapInfoWindow): Promise<void> {
|
42 | return this._infoWindows.get(infoWindow)
|
43 | .then((i: InfoWindow) => i.setZIndex(infoWindow.zIndex));
|
44 | }
|
45 |
|
46 | open(infoWindow: SebmGoogleMapInfoWindow): Promise<void> {
|
47 | return this._infoWindows.get(infoWindow).then((w) => {
|
48 | if (infoWindow.hostMarker != null) {
|
49 | return this._markerManager.getNativeMarker(infoWindow.hostMarker).then((marker) => {
|
50 | return this._mapsWrapper.getNativeMap().then((map) => w.open(map, marker));
|
51 | });
|
52 | }
|
53 | return this._mapsWrapper.getNativeMap().then((map) => w.open(map));
|
54 | });
|
55 | }
|
56 |
|
57 | close(infoWindow: SebmGoogleMapInfoWindow): Promise<void> {
|
58 | return this._infoWindows.get(infoWindow).then((w) => w.close());
|
59 | }
|
60 |
|
61 | setOptions(infoWindow: SebmGoogleMapInfoWindow, options: InfoWindowOptions) {
|
62 | return this._infoWindows.get(infoWindow).then((i: InfoWindow) => i.setOptions(options));
|
63 | }
|
64 |
|
65 | addInfoWindow(infoWindow: SebmGoogleMapInfoWindow) {
|
66 | const options: InfoWindowOptions = {
|
67 | content: infoWindow.content,
|
68 | maxWidth: infoWindow.maxWidth,
|
69 | zIndex: infoWindow.zIndex,
|
70 | };
|
71 | if (typeof infoWindow.latitude === 'number' && typeof infoWindow.longitude === 'number') {
|
72 | options.position = {lat: infoWindow.latitude, lng: infoWindow.longitude};
|
73 | }
|
74 | const infoWindowPromise = this._mapsWrapper.createInfoWindow(options);
|
75 | this._infoWindows.set(infoWindow, infoWindowPromise);
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 | createEventObservable<T>(eventName: string, infoWindow: SebmGoogleMapInfoWindow): Observable<T> {
|
82 | return Observable.create((observer: Observer<T>) => {
|
83 | this._infoWindows.get(infoWindow).then((i: InfoWindow) => {
|
84 | i.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
|
85 | });
|
86 | });
|
87 | }
|
88 | }
|