1 | import {AfterContentInit, Directive, EventEmitter, OnChanges, OnDestroy, SimpleChanges} from '@angular/core';
|
2 | import {Subscription} from 'rxjs/Subscription';
|
3 |
|
4 | import {LatLng, LatLngLiteral, PolyMouseEvent, PolygonOptions} from '../services/google-maps-types';
|
5 | import {PolygonManager} from '../services/managers/polygon-manager';
|
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 | @Directive({
|
58 | selector: 'sebm-map-polygon',
|
59 | inputs: [
|
60 | 'clickable',
|
61 | 'draggable: polyDraggable',
|
62 | 'editable',
|
63 | 'fillColor',
|
64 | 'fillOpacity',
|
65 | 'geodesic',
|
66 | 'paths',
|
67 | 'strokeColor',
|
68 | 'strokeOpacity',
|
69 | 'strokeWeight',
|
70 | 'visible',
|
71 | 'zIndex',
|
72 | ],
|
73 | outputs: [
|
74 | 'polyClick', 'polyDblClick', 'polyDrag', 'polyDragEnd', 'polyMouseDown', 'polyMouseMove',
|
75 | 'polyMouseOut', 'polyMouseOver', 'polyMouseUp', 'polyRightClick'
|
76 | ]
|
77 | })
|
78 | export class SebmGoogleMapPolygon implements OnDestroy, OnChanges, AfterContentInit {
|
79 | |
80 |
|
81 |
|
82 | clickable: boolean = true;
|
83 | |
84 |
|
85 |
|
86 |
|
87 | draggable: boolean = false;
|
88 | |
89 |
|
90 |
|
91 |
|
92 | editable: boolean = false;
|
93 | |
94 |
|
95 |
|
96 |
|
97 | fillColor: string;
|
98 | |
99 |
|
100 |
|
101 | fillOpacity: number;
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | geodesic: boolean = false;
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | paths: Array<LatLng|LatLngLiteral>|Array<Array<LatLng|LatLngLiteral>> = [];
|
122 | |
123 |
|
124 |
|
125 |
|
126 | strokeColor: string;
|
127 | |
128 |
|
129 |
|
130 | strokeOpacity: number;
|
131 | |
132 |
|
133 |
|
134 | strokeWeight: number;
|
135 | |
136 |
|
137 |
|
138 | visible: boolean;
|
139 | |
140 |
|
141 |
|
142 | zIndex: number;
|
143 |
|
144 | |
145 |
|
146 |
|
147 | polyClick: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
148 |
|
149 | |
150 |
|
151 |
|
152 | polyDblClick: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
153 |
|
154 | |
155 |
|
156 |
|
157 | polyDrag: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
|
158 |
|
159 | |
160 |
|
161 |
|
162 | polyDragEnd: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
|
163 |
|
164 | |
165 |
|
166 |
|
167 | polyDragStart: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();
|
168 |
|
169 | |
170 |
|
171 |
|
172 | polyMouseDown: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
173 |
|
174 | |
175 |
|
176 |
|
177 | polyMouseMove: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
178 |
|
179 | |
180 |
|
181 |
|
182 | polyMouseOut: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
183 |
|
184 | |
185 |
|
186 |
|
187 | polyMouseOver: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
188 |
|
189 | |
190 |
|
191 |
|
192 | polyMouseUp: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
193 |
|
194 | |
195 |
|
196 |
|
197 | polyRightClick: EventEmitter<PolyMouseEvent> = new EventEmitter<PolyMouseEvent>();
|
198 |
|
199 | private static _polygonOptionsAttributes: Array<string> = [
|
200 | 'clickable', 'draggable', 'editable', 'fillColor', 'fillOpacity', 'geodesic', 'icon', 'map',
|
201 | 'paths', 'strokeColor', 'strokeOpacity', 'strokeWeight', 'visible', 'zIndex', 'draggable',
|
202 | 'editable', 'visible'
|
203 | ];
|
204 |
|
205 | private _id: string;
|
206 | private _polygonAddedToManager: boolean = false;
|
207 | private _subscriptions: Subscription[] = [];
|
208 |
|
209 | constructor(private _polygonManager: PolygonManager) {}
|
210 |
|
211 |
|
212 | ngAfterContentInit() {
|
213 | if (!this._polygonAddedToManager) {
|
214 | this._init();
|
215 | }
|
216 | }
|
217 |
|
218 | ngOnChanges(changes: SimpleChanges): any {
|
219 | if (!this._polygonAddedToManager) {
|
220 | this._init();
|
221 | return;
|
222 | }
|
223 |
|
224 | this._polygonManager.setPolygonOptions(this, this._updatePolygonOptions(changes));
|
225 | }
|
226 |
|
227 | private _init() {
|
228 | this._polygonManager.addPolygon(this);
|
229 | this._polygonAddedToManager = true;
|
230 | this._addEventListeners();
|
231 | }
|
232 |
|
233 | private _addEventListeners() {
|
234 | const handlers = [
|
235 | {name: 'click', handler: (ev: PolyMouseEvent) => this.polyClick.emit(ev)},
|
236 | {name: 'dbclick', handler: (ev: PolyMouseEvent) => this.polyDblClick.emit(ev)},
|
237 | {name: 'drag', handler: (ev: MouseEvent) => this.polyDrag.emit(ev)},
|
238 | {name: 'dragend', handler: (ev: MouseEvent) => this.polyDragEnd.emit(ev)},
|
239 | {name: 'dragstart', handler: (ev: MouseEvent) => this.polyDragStart.emit(ev)},
|
240 | {name: 'mousedown', handler: (ev: PolyMouseEvent) => this.polyMouseDown.emit(ev)},
|
241 | {name: 'mousemove', handler: (ev: PolyMouseEvent) => this.polyMouseMove.emit(ev)},
|
242 | {name: 'mouseout', handler: (ev: PolyMouseEvent) => this.polyMouseOut.emit(ev)},
|
243 | {name: 'mouseover', handler: (ev: PolyMouseEvent) => this.polyMouseOver.emit(ev)},
|
244 | {name: 'mouseup', handler: (ev: PolyMouseEvent) => this.polyMouseUp.emit(ev)},
|
245 | {name: 'rightclick', handler: (ev: PolyMouseEvent) => this.polyRightClick.emit(ev)},
|
246 | ];
|
247 | handlers.forEach((obj) => {
|
248 | const os = this._polygonManager.createEventObservable(obj.name, this).subscribe(obj.handler);
|
249 | this._subscriptions.push(os);
|
250 | });
|
251 | }
|
252 |
|
253 | private _updatePolygonOptions(changes: SimpleChanges): PolygonOptions {
|
254 | return Object.keys(changes)
|
255 | .filter(k => SebmGoogleMapPolygon._polygonOptionsAttributes.indexOf(k) !== -1)
|
256 | .reduce((obj: any, k: string) => {
|
257 | obj[k] = changes[k].currentValue;
|
258 | return obj;
|
259 | }, {});
|
260 | }
|
261 |
|
262 |
|
263 | id(): string { return this._id; }
|
264 |
|
265 |
|
266 | ngOnDestroy() {
|
267 | this._polygonManager.deletePolygon(this);
|
268 |
|
269 | this._subscriptions.forEach((s) => s.unsubscribe());
|
270 | }
|
271 | }
|