@@ -29,6 +29,15 @@ export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = {
29
29
extra : Record < string , unknown > ;
30
30
} ;
31
31
32
+ export type PolylineDefinition < PolylineOptions , InfoWindowOptions > = {
33
+ '@id' : string ;
34
+ infoWindow ?: Omit < InfoWindowDefinition < InfoWindowOptions > , 'position' > ;
35
+ points : Array < Point > ;
36
+ title : string | null ;
37
+ rawOptions ?: PolylineOptions ;
38
+ extra : Record < string , unknown > ;
39
+ } ;
40
+
32
41
export type InfoWindowDefinition < InfoWindowOptions > = {
33
42
headerContent : string | null ;
34
43
content : string | null ;
@@ -58,6 +67,8 @@ export default abstract class<
58
67
InfoWindow ,
59
68
PolygonOptions ,
60
69
Polygon ,
70
+ PolylineOptions ,
71
+ Polyline ,
61
72
> extends Controller < HTMLElement > {
62
73
static values = {
63
74
providerOptions : Object ,
@@ -66,6 +77,7 @@ export default abstract class<
66
77
fitBoundsToMarkers : Boolean ,
67
78
markers : Array ,
68
79
polygons : Array ,
80
+ polylines : Array ,
69
81
options : Object ,
70
82
} ;
71
83
@@ -74,12 +86,14 @@ export default abstract class<
74
86
declare fitBoundsToMarkersValue: boolean ;
75
87
declare markersValue: Array < MarkerDefinition < MarkerOptions , InfoWindowOptions > > ;
76
88
declare polygonsValue: Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
89
+ declare polylinesValue: Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
77
90
declare optionsValue: MapOptions ;
78
91
79
92
protected map : Map ;
80
93
protected markers = new Map < Marker > ( ) ;
81
94
protected infoWindows: Array < InfoWindow > = [ ] ;
82
95
protected polygons = new Map < Polygon > ( ) ;
96
+ protected polylines = new Map < Polyline > ( ) ;
83
97
84
98
connect ( ) {
85
99
const options = this . optionsValue ;
@@ -92,6 +106,8 @@ export default abstract class<
92
106
93
107
this . polygonsValue . forEach ( ( polygon ) => this . createPolygon ( polygon ) ) ;
94
108
109
+ this . polylinesValue . forEach ( ( polyline ) => this . createPolyline ( polyline ) ) ;
110
+
95
111
if ( this . fitBoundsToMarkersValue ) {
96
112
this . doFitBoundsToMarkers ( ) ;
97
113
}
@@ -100,6 +116,7 @@ export default abstract class<
100
116
map : this . map ,
101
117
markers : [ ...this . markers . values ( ) ] ,
102
118
polygons : [ ...this . polygons . values ( ) ] ,
119
+ polylines : [ ...this . polylines . values ( ) ] ,
103
120
infoWindows : this . infoWindows ,
104
121
} ) ;
105
122
}
@@ -142,17 +159,36 @@ export default abstract class<
142
159
return polygon ;
143
160
}
144
161
162
+ protected abstract removePolygon ( polygon : Polygon ) : void ;
163
+
145
164
protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
146
165
166
+ public createPolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline {
167
+ this . dispatchEvent ( 'polyline:before-create' , { definition } ) ;
168
+ const polyline = this . doCreatePolyline ( definition ) ;
169
+ this . dispatchEvent ( 'polyline:after-create' , { polyline } ) ;
170
+
171
+ polyline [ '@id' ] = definition [ '@id' ] ;
172
+
173
+ this . polylines . set ( definition [ '@id' ] , polyline ) ;
174
+
175
+ return polyline ;
176
+ }
177
+
178
+ protected abstract removePolyline ( polyline : Polyline ) : void ;
179
+
180
+ protected abstract doCreatePolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline ;
181
+
147
182
protected createInfoWindow ( {
148
183
definition,
149
184
element,
150
- } : {
151
- definition :
152
- | MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ]
153
- | PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
154
- element : Marker | Polygon ;
155
- } ) : InfoWindow {
185
+ } :
186
+ | { definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ; element: Marker }
187
+ | { definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ; element: Polygon }
188
+ | {
189
+ definition: PolylineDefinition < PolylineOptions , InfoWindowOptions > [ 'infoWindow' ] ;
190
+ element: Polyline ;
191
+ } ) : InfoWindow {
156
192
this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
157
193
const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
158
194
this . dispatchEvent ( 'info-window:after-create' , { infoWindow, element } ) ;
@@ -166,13 +202,11 @@ export default abstract class<
166
202
definition,
167
203
element,
168
204
} :
205
+ | { definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ; element: Marker }
206
+ | { definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ; element: Polygon }
169
207
| {
170
- definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
171
- element: Marker ;
172
- }
173
- | {
174
- definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
175
- element: Polygon ;
208
+ definition: PolylineDefinition < PolylineOptions , InfoWindowOptions > [ 'infoWindow' ] ;
209
+ element: Polyline ;
176
210
} ) : InfoWindow ;
177
211
178
212
protected abstract doFitBoundsToMarkers ( ) : void ;
@@ -213,7 +247,7 @@ export default abstract class<
213
247
214
248
this . polygons . forEach ( ( polygon ) => {
215
249
if ( ! this . polygonsValue . find ( ( p ) => p [ '@id' ] === polygon [ '@id' ] ) ) {
216
- polygon . remove ( ) ;
250
+ this . removePolygon ( polygon ) ;
217
251
this . polygons . delete ( polygon [ '@id' ] ) ;
218
252
}
219
253
} ) ;
@@ -224,4 +258,23 @@ export default abstract class<
224
258
}
225
259
} ) ;
226
260
}
261
+
262
+ public polylinesValueChanged ( ) : void {
263
+ if ( ! this . map ) {
264
+ return ;
265
+ }
266
+
267
+ this . polylines . forEach ( ( polyline ) => {
268
+ if ( ! this . polylinesValue . find ( ( p ) => p [ '@id' ] === polyline [ '@id' ] ) ) {
269
+ this . removePolyline ( polyline ) ;
270
+ this . polylines . delete ( polyline [ '@id' ] ) ;
271
+ }
272
+ } ) ;
273
+
274
+ this . polylinesValue . forEach ( ( polyline ) => {
275
+ if ( ! this . polylines . has ( polyline [ '@id' ] ) ) {
276
+ this . createPolyline ( polyline ) ;
277
+ }
278
+ } ) ;
279
+ }
227
280
}
0 commit comments