@@ -80,6 +80,24 @@ export type PolylineDefinition<PolylineOptions, InfoWindowOptions> = WithIdentif
80
80
extra : Record < string , unknown > ;
81
81
} > ;
82
82
83
+ export type CircleDefinition < CircleOptions , InfoWindowOptions > = WithIdentifier < {
84
+ infoWindow ?: InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
85
+ center : Point ;
86
+ radius : number ;
87
+ title : string | null ;
88
+ /**
89
+ * Raw options passed to the circle constructor, specific to the map provider (e.g.: `L.circle()` for Leaflet).
90
+ */
91
+ rawOptions ?: CircleOptions ;
92
+ /**
93
+ * Extra data defined by the developer.
94
+ * They are not directly used by the Stimulus controller, but they can be used by the developer with event listeners:
95
+ * - `ux:map:circle:before-create`
96
+ * - `ux:map:circle:after-create`
97
+ */
98
+ extra : Record < string , unknown > ;
99
+ } > ;
100
+
83
101
export type InfoWindowDefinition < InfoWindowOptions > = {
84
102
headerContent : string | null ;
85
103
content : string | null ;
@@ -116,6 +134,8 @@ export default abstract class<
116
134
Polygon ,
117
135
PolylineOptions ,
118
136
Polyline ,
137
+ CircleOptions ,
138
+ Circle ,
119
139
> extends Controller < HTMLElement > {
120
140
static values = {
121
141
providerOptions : Object ,
@@ -125,6 +145,7 @@ export default abstract class<
125
145
markers : Array ,
126
146
polygons : Array ,
127
147
polylines : Array ,
148
+ circles : Array ,
128
149
options : Object ,
129
150
} ;
130
151
@@ -134,6 +155,7 @@ export default abstract class<
134
155
declare markersValue : Array < MarkerDefinition < MarkerOptions , InfoWindowOptions > > ;
135
156
declare polygonsValue : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
136
157
declare polylinesValue : Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
158
+ declare circlesValue : Array < CircleDefinition < CircleOptions , InfoWindowOptions > > ;
137
159
declare optionsValue : MapOptions ;
138
160
139
161
declare hasCenterValue : boolean;
@@ -142,12 +164,14 @@ export default abstract class<
142
164
declare hasMarkersValue : boolean;
143
165
declare hasPolygonsValue : boolean;
144
166
declare hasPolylinesValue : boolean;
167
+ declare hasCirclesValue : boolean;
145
168
declare hasOptionsValue : boolean;
146
169
147
170
protected map : Map ;
148
171
protected markers = new Map < Identifier , Marker > ( ) ;
149
172
protected polygons = new Map < Identifier , Polygon > ( ) ;
150
173
protected polylines = new Map < Identifier , Polyline > ( ) ;
174
+ protected circles = new Map < Identifier , Circle > ( ) ;
151
175
protected infoWindows : Array < InfoWindow > = [ ] ;
152
176
153
177
private isConnected = false ;
@@ -160,6 +184,9 @@ export default abstract class<
160
184
private createPolyline : ( {
161
185
definition,
162
186
} : { definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > } ) = > Polyline ;
187
+ private createCircle : ( {
188
+ definition,
189
+ } : { definition : CircleDefinition < CircleOptions , InfoWindowOptions > } ) = > Circle ;
163
190
164
191
protected abstract dispatchEvent ( name : string , payload : Record < string , unknown > ) : void ;
165
192
@@ -171,6 +198,7 @@ export default abstract class<
171
198
this . createMarker = this . createDrawingFactory ( 'marker' , this . markers , this . doCreateMarker . bind ( this ) ) ;
172
199
this . createPolygon = this . createDrawingFactory ( 'polygon' , this . polygons , this . doCreatePolygon . bind ( this ) ) ;
173
200
this . createPolyline = this . createDrawingFactory ( 'polyline' , this . polylines , this . doCreatePolyline . bind ( this ) ) ;
201
+ this . createCircle = this . createDrawingFactory ( 'circle' , this . circles , this . doCreateCircle . bind ( this ) ) ;
174
202
175
203
this . map = this . doCreateMap ( {
176
204
center : this . hasCenterValue ? this . centerValue : null ,
@@ -180,6 +208,7 @@ export default abstract class<
180
208
this . markersValue . forEach ( ( definition ) => this . createMarker ( { definition } ) ) ;
181
209
this . polygonsValue . forEach ( ( definition ) => this . createPolygon ( { definition } ) ) ;
182
210
this . polylinesValue . forEach ( ( definition ) => this . createPolyline ( { definition } ) ) ;
211
+ this . circlesValue . forEach ( ( definition ) => this . createCircle ( { definition } ) ) ;
183
212
184
213
if ( this . fitBoundsToMarkersValue ) {
185
214
this . doFitBoundsToMarkers ( ) ;
@@ -190,6 +219,7 @@ export default abstract class<
190
219
markers : [ ...this . markers . values ( ) ] ,
191
220
polygons : [ ...this . polygons . values ( ) ] ,
192
221
polylines : [ ...this . polylines . values ( ) ] ,
222
+ circles : [ ...this . circles . values ( ) ] ,
193
223
infoWindows : this . infoWindows ,
194
224
} ) ;
195
225
@@ -202,7 +232,7 @@ export default abstract class<
202
232
element,
203
233
} : {
204
234
definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
205
- element: Marker | Polygon | Polyline ;
235
+ element: Marker | Polygon | Polyline | Circle ;
206
236
} ) : InfoWindow {
207
237
this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
208
238
const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
@@ -248,6 +278,14 @@ export default abstract class<
248
278
this . onDrawChanged ( this . polylines , this . polylinesValue , this . createPolyline , this . doRemovePolyline ) ;
249
279
}
250
280
281
+ public circlesValueChanged ( ) : void {
282
+ if ( ! this . isConnected ) {
283
+ return ;
284
+ }
285
+
286
+ this . onDrawChanged ( this . circles , this . circlesValue , this . createCircle , this . doRemoveCircle ) ;
287
+ }
288
+
251
289
//endregion
252
290
253
291
//region Abstract factory methods to be implemented by the concrete classes, they are specific to the map provider
@@ -285,12 +323,20 @@ export default abstract class<
285
323
286
324
protected abstract doRemovePolyline ( polyline : Polyline ) : void ;
287
325
326
+ protected abstract doCreateCircle ( {
327
+ definition,
328
+ } : {
329
+ definition : CircleDefinition < CircleOptions , InfoWindowOptions > ;
330
+ } ) : Circle ;
331
+
332
+ protected abstract doRemoveCircle ( circle : Circle ) : void ;
333
+
288
334
protected abstract doCreateInfoWindow ( {
289
335
definition,
290
336
element,
291
337
} : {
292
338
definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
293
- element: Marker | Polygon | Polyline ;
339
+ element: Marker | Polygon | Polyline | Circle ;
294
340
} ) : InfoWindow ;
295
341
protected abstract doCreateIcon ( {
296
342
definition,
@@ -318,11 +364,20 @@ export default abstract class<
318
364
draws : typeof this . polylines ,
319
365
factory : typeof this . doCreatePolyline
320
366
) : typeof this . doCreatePolyline ;
367
+ private createDrawingFactory (
368
+ type : 'circle' ,
369
+ draws : typeof this . circles ,
370
+ factory : typeof this . doCreateCircle
371
+ ) : typeof this . doCreateCircle ;
321
372
private createDrawingFactory <
322
- Factory extends typeof this . doCreateMarker | typeof this . doCreatePolygon | typeof this . doCreatePolyline ,
373
+ Factory extends
374
+ | typeof this . doCreateMarker
375
+ | typeof this . doCreatePolygon
376
+ | typeof this . doCreatePolyline
377
+ | typeof this . doCreateCircle ,
323
378
Draw extends ReturnType < Factory > ,
324
379
> (
325
- type : 'marker' | 'polygon' | 'polyline' ,
380
+ type : 'marker' | 'polygon' | 'polyline' | 'circle' ,
326
381
draws : globalThis . Map < WithIdentifier < any > , Draw > ,
327
382
factory : Factory
328
383
) : Factory {
@@ -360,6 +415,12 @@ export default abstract class<
360
415
factory : typeof this . createPolyline ,
361
416
remover : typeof this . doRemovePolyline
362
417
) : void ;
418
+ private onDrawChanged (
419
+ draws : typeof this . circles ,
420
+ newDrawDefinitions : typeof this . circlesValue ,
421
+ factory : typeof this . createCircle ,
422
+ remover : typeof this . doRemoveCircle
423
+ ) : void ;
363
424
private onDrawChanged < Draw , DrawDefinition extends WithIdentifier < Record < string , unknown > > > (
364
425
draws : globalThis . Map < WithIdentifier < any > , Draw > ,
365
426
newDrawDefinitions : Array < DrawDefinition > ,
0 commit comments