UNPKG

2.22 kBPlain TextView Raw
1import {Injectable, NgZone} from '@angular/core';
2import {Observable} from 'rxjs/Observable';
3import {Observer} from 'rxjs/Observer';
4
5import {SebmGoogleMapPolygon} from '../../directives/google-map-polygon';
6import {GoogleMapsAPIWrapper} from '../google-maps-api-wrapper';
7import {Polygon} from '../google-maps-types';
8
9@Injectable()
10export class PolygonManager {
11 private _polygons: Map<SebmGoogleMapPolygon, Promise<Polygon>> =
12 new Map<SebmGoogleMapPolygon, Promise<Polygon>>();
13
14 constructor(private _mapsWrapper: GoogleMapsAPIWrapper, private _zone: NgZone) {}
15
16 addPolygon(path: SebmGoogleMapPolygon) {
17 const polygonPromise = this._mapsWrapper.createPolygon({
18 clickable: path.clickable,
19 draggable: path.draggable,
20 editable: path.editable,
21 fillColor: path.fillColor,
22 fillOpacity: path.fillOpacity,
23 geodesic: path.geodesic,
24 paths: path.paths,
25 strokeColor: path.strokeColor,
26 strokeOpacity: path.strokeOpacity,
27 strokeWeight: path.strokeWeight,
28 visible: path.visible,
29 zIndex: path.zIndex,
30 });
31 this._polygons.set(path, polygonPromise);
32 }
33
34 updatePolygon(polygon: SebmGoogleMapPolygon): Promise<void> {
35 const m = this._polygons.get(polygon);
36 if (m == null) {
37 return Promise.resolve();
38 }
39 return m.then((l: Polygon) => this._zone.run(() => { l.setPaths(polygon.paths); }));
40 }
41
42 setPolygonOptions(path: SebmGoogleMapPolygon, options: {[propName: string]: any}): Promise<void> {
43 return this._polygons.get(path).then((l: Polygon) => { l.setOptions(options); });
44 }
45
46 deletePolygon(paths: SebmGoogleMapPolygon): Promise<void> {
47 const m = this._polygons.get(paths);
48 if (m == null) {
49 return Promise.resolve();
50 }
51 return m.then((l: Polygon) => {
52 return this._zone.run(() => {
53 l.setMap(null);
54 this._polygons.delete(paths);
55 });
56 });
57 }
58
59 createEventObservable<T>(eventName: string, path: SebmGoogleMapPolygon): Observable<T> {
60 return Observable.create((observer: Observer<T>) => {
61 this._polygons.get(path).then((l: Polygon) => {
62 l.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
63 });
64 });
65 }
66}