@@ -98,6 +98,23 @@ export type CircleDefinition<CircleOptions, InfoWindowOptions> = WithIdentifier<
98
98
extra : Record < string , unknown > ;
99
99
} > ;
100
100
101
+ export type RectangleDefinition < RectangleOptions , InfoWindowOptions > = WithIdentifier < {
102
+ infoWindow ?: InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
103
+ bounds : { northEast : Point ; southWest : Point } ;
104
+ title : string | null ;
105
+ /**
106
+ * Raw options passed to the rectangle constructor, specific to the map provider (e.g.: `L.rectangle()` for Leaflet).
107
+ */
108
+ rawOptions ?: RectangleOptions ;
109
+ /**
110
+ * Extra data defined by the developer.
111
+ * They are not directly used by the Stimulus controller, but they can be used by the developer with event listeners:
112
+ * - `ux:map:rectangle:before-create`
113
+ * - `ux:map:rectangle:after-create`
114
+ */
115
+ extra : Record < string , unknown > ;
116
+ } > ;
117
+
101
118
export type InfoWindowDefinition < InfoWindowOptions > = {
102
119
headerContent : string | null ;
103
120
content : string | null ;
@@ -136,6 +153,8 @@ export default abstract class<
136
153
Polyline ,
137
154
CircleOptions ,
138
155
Circle ,
156
+ RectangleOptions ,
157
+ Rectangle ,
139
158
> extends Controller < HTMLElement > {
140
159
static values = {
141
160
providerOptions : Object ,
@@ -146,6 +165,7 @@ export default abstract class<
146
165
polygons : Array ,
147
166
polylines : Array ,
148
167
circles : Array ,
168
+ rectangles : Array ,
149
169
options : Object ,
150
170
} ;
151
171
@@ -156,6 +176,7 @@ export default abstract class<
156
176
declare polygonsValue : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
157
177
declare polylinesValue : Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
158
178
declare circlesValue : Array < CircleDefinition < CircleOptions , InfoWindowOptions > > ;
179
+ declare rectanglesValue : Array < RectangleDefinition < RectangleOptions , InfoWindowOptions > > ;
159
180
declare optionsValue : MapOptions ;
160
181
161
182
declare hasCenterValue : boolean;
@@ -165,13 +186,15 @@ export default abstract class<
165
186
declare hasPolygonsValue : boolean;
166
187
declare hasPolylinesValue : boolean;
167
188
declare hasCirclesValue : boolean;
189
+ declare hasRectanglesValue : boolean;
168
190
declare hasOptionsValue : boolean;
169
191
170
192
protected map : Map ;
171
193
protected markers = new Map < Identifier , Marker > ( ) ;
172
194
protected polygons = new Map < Identifier , Polygon > ( ) ;
173
195
protected polylines = new Map < Identifier , Polyline > ( ) ;
174
196
protected circles = new Map < Identifier , Circle > ( ) ;
197
+ protected rectangles = new Map < Identifier , Rectangle > ( ) ;
175
198
protected infoWindows : Array < InfoWindow > = [ ] ;
176
199
177
200
private isConnected = false ;
@@ -187,6 +210,9 @@ export default abstract class<
187
210
private createCircle : ( {
188
211
definition,
189
212
} : { definition : CircleDefinition < CircleOptions , InfoWindowOptions > } ) = > Circle ;
213
+ private createRectangle : ( {
214
+ definition,
215
+ } : { definition : RectangleDefinition < RectangleOptions , InfoWindowOptions > } ) = > Rectangle ;
190
216
191
217
protected abstract dispatchEvent ( name : string , payload : Record < string , unknown > ) : void ;
192
218
@@ -199,6 +225,11 @@ export default abstract class<
199
225
this . createPolygon = this . createDrawingFactory ( 'polygon' , this . polygons , this . doCreatePolygon . bind ( this ) ) ;
200
226
this . createPolyline = this . createDrawingFactory ( 'polyline' , this . polylines , this . doCreatePolyline . bind ( this ) ) ;
201
227
this . createCircle = this . createDrawingFactory ( 'circle' , this . circles , this . doCreateCircle . bind ( this ) ) ;
228
+ this . createRectangle = this . createDrawingFactory (
229
+ 'rectangle' ,
230
+ this . rectangles ,
231
+ this . doCreateRectangle . bind ( this )
232
+ ) ;
202
233
203
234
this . map = this . doCreateMap ( {
204
235
center : this . hasCenterValue ? this . centerValue : null ,
@@ -209,6 +240,7 @@ export default abstract class<
209
240
this . polygonsValue . forEach ( ( definition ) => this . createPolygon ( { definition } ) ) ;
210
241
this . polylinesValue . forEach ( ( definition ) => this . createPolyline ( { definition } ) ) ;
211
242
this . circlesValue . forEach ( ( definition ) => this . createCircle ( { definition } ) ) ;
243
+ this . rectanglesValue . forEach ( ( definition ) => this . createRectangle ( { definition } ) ) ;
212
244
213
245
if ( this . fitBoundsToMarkersValue ) {
214
246
this . doFitBoundsToMarkers ( ) ;
@@ -220,6 +252,7 @@ export default abstract class<
220
252
polygons : [ ...this . polygons . values ( ) ] ,
221
253
polylines : [ ...this . polylines . values ( ) ] ,
222
254
circles : [ ...this . circles . values ( ) ] ,
255
+ rectangles : [ ...this . rectangles . values ( ) ] ,
223
256
infoWindows : this . infoWindows ,
224
257
} ) ;
225
258
@@ -232,7 +265,7 @@ export default abstract class<
232
265
element,
233
266
} : {
234
267
definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
235
- element: Marker | Polygon | Polyline | Circle ;
268
+ element: Marker | Polygon | Polyline | Circle | Rectangle ;
236
269
} ) : InfoWindow {
237
270
this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
238
271
const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
@@ -286,6 +319,14 @@ export default abstract class<
286
319
this . onDrawChanged ( this . circles , this . circlesValue , this . createCircle , this . doRemoveCircle ) ;
287
320
}
288
321
322
+ public rectanglesValueChanged ( ) : void {
323
+ if ( ! this . isConnected ) {
324
+ return ;
325
+ }
326
+
327
+ this . onDrawChanged ( this . rectangles , this . rectanglesValue , this . createRectangle , this . doRemoveRectangle ) ;
328
+ }
329
+
289
330
//endregion
290
331
291
332
//region Abstract factory methods to be implemented by the concrete classes, they are specific to the map provider
@@ -331,12 +372,20 @@ export default abstract class<
331
372
332
373
protected abstract doRemoveCircle ( circle : Circle ) : void ;
333
374
375
+ protected abstract doCreateRectangle ( {
376
+ definition,
377
+ } : {
378
+ definition : RectangleDefinition < RectangleOptions , InfoWindowOptions > ;
379
+ } ) : Rectangle ;
380
+
381
+ protected abstract doRemoveRectangle ( rectangle : Rectangle ) : void ;
382
+
334
383
protected abstract doCreateInfoWindow ( {
335
384
definition,
336
385
element,
337
386
} : {
338
387
definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
339
- element: Marker | Polygon | Polyline | Circle ;
388
+ element: Marker | Polygon | Polyline | Circle | Rectangle ;
340
389
} ) : InfoWindow ;
341
390
protected abstract doCreateIcon ( {
342
391
definition,
@@ -369,15 +418,21 @@ export default abstract class<
369
418
draws : typeof this . circles ,
370
419
factory : typeof this . doCreateCircle
371
420
) : typeof this . doCreateCircle ;
421
+ private createDrawingFactory (
422
+ type : 'rectangle' ,
423
+ draws : typeof this . rectangles ,
424
+ factory : typeof this . doCreateRectangle
425
+ ) : typeof this . doCreateRectangle ;
372
426
private createDrawingFactory <
373
427
Factory extends
374
428
| typeof this . doCreateMarker
375
429
| typeof this . doCreatePolygon
376
430
| typeof this . doCreatePolyline
377
- | typeof this . doCreateCircle ,
431
+ | typeof this . doCreateCircle
432
+ | typeof this . doCreateRectangle ,
378
433
Draw extends ReturnType < Factory > ,
379
434
> (
380
- type : 'marker' | 'polygon' | 'polyline' | 'circle' ,
435
+ type : 'marker' | 'polygon' | 'polyline' | 'circle' | 'rectangle' ,
381
436
draws : globalThis . Map < WithIdentifier < any > , Draw > ,
382
437
factory : Factory
383
438
) : Factory {
@@ -421,6 +476,12 @@ export default abstract class<
421
476
factory : typeof this . createCircle ,
422
477
remover : typeof this . doRemoveCircle
423
478
) : void ;
479
+ private onDrawChanged (
480
+ draws : typeof this . rectangles ,
481
+ newDrawDefinitions : typeof this . rectanglesValue ,
482
+ factory : typeof this . createRectangle ,
483
+ remover : typeof this . doRemoveRectangle
484
+ ) : void ;
424
485
private onDrawChanged < Draw , DrawDefinition extends WithIdentifier < Record < string , unknown > > > (
425
486
draws : globalThis . Map < WithIdentifier < any > , Draw > ,
426
487
newDrawDefinitions : Array < DrawDefinition > ,
0 commit comments