@@ -98,6 +98,23 @@ export type CircleDefinition<CircleOptions, InfoWindowOptions> = WithIdentifier<
9898 extra : Record < string , unknown > ;
9999} > ;
100100
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+
101118export type InfoWindowDefinition < InfoWindowOptions > = {
102119 headerContent : string | null ;
103120 content : string | null ;
@@ -136,6 +153,8 @@ export default abstract class<
136153 Polyline ,
137154 CircleOptions ,
138155 Circle ,
156+ RectangleOptions ,
157+ Rectangle ,
139158> extends Controller < HTMLElement > {
140159 static values = {
141160 providerOptions : Object ,
@@ -146,6 +165,7 @@ export default abstract class<
146165 polygons : Array ,
147166 polylines : Array ,
148167 circles : Array ,
168+ rectangles : Array ,
149169 options : Object ,
150170 } ;
151171
@@ -156,6 +176,7 @@ export default abstract class<
156176 declare polygonsValue : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
157177 declare polylinesValue : Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
158178 declare circlesValue : Array < CircleDefinition < CircleOptions , InfoWindowOptions > > ;
179+ declare rectanglesValue : Array < RectangleDefinition < RectangleOptions , InfoWindowOptions > > ;
159180 declare optionsValue : MapOptions ;
160181
161182 declare hasCenterValue : boolean;
@@ -165,13 +186,15 @@ export default abstract class<
165186 declare hasPolygonsValue : boolean;
166187 declare hasPolylinesValue : boolean;
167188 declare hasCirclesValue : boolean;
189+ declare hasRectanglesValue : boolean;
168190 declare hasOptionsValue : boolean;
169191
170192 protected map : Map ;
171193 protected markers = new Map < Identifier , Marker > ( ) ;
172194 protected polygons = new Map < Identifier , Polygon > ( ) ;
173195 protected polylines = new Map < Identifier , Polyline > ( ) ;
174196 protected circles = new Map < Identifier , Circle > ( ) ;
197+ protected rectangles = new Map < Identifier , Rectangle > ( ) ;
175198 protected infoWindows : Array < InfoWindow > = [ ] ;
176199
177200 private isConnected = false ;
@@ -187,6 +210,9 @@ export default abstract class<
187210 private createCircle : ( {
188211 definition,
189212 } : { definition : CircleDefinition < CircleOptions , InfoWindowOptions > } ) = > Circle ;
213+ private createRectangle : ( {
214+ definition,
215+ } : { definition : RectangleDefinition < RectangleOptions , InfoWindowOptions > } ) = > Rectangle ;
190216
191217 protected abstract dispatchEvent ( name : string , payload : Record < string , unknown > ) : void ;
192218
@@ -199,6 +225,11 @@ export default abstract class<
199225 this . createPolygon = this . createDrawingFactory ( 'polygon' , this . polygons , this . doCreatePolygon . bind ( this ) ) ;
200226 this . createPolyline = this . createDrawingFactory ( 'polyline' , this . polylines , this . doCreatePolyline . bind ( this ) ) ;
201227 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+ ) ;
202233
203234 this . map = this . doCreateMap ( {
204235 center : this . hasCenterValue ? this . centerValue : null ,
@@ -209,6 +240,7 @@ export default abstract class<
209240 this . polygonsValue . forEach ( ( definition ) => this . createPolygon ( { definition } ) ) ;
210241 this . polylinesValue . forEach ( ( definition ) => this . createPolyline ( { definition } ) ) ;
211242 this . circlesValue . forEach ( ( definition ) => this . createCircle ( { definition } ) ) ;
243+ this . rectanglesValue . forEach ( ( definition ) => this . createRectangle ( { definition } ) ) ;
212244
213245 if ( this . fitBoundsToMarkersValue ) {
214246 this . doFitBoundsToMarkers ( ) ;
@@ -220,6 +252,7 @@ export default abstract class<
220252 polygons : [ ...this . polygons . values ( ) ] ,
221253 polylines : [ ...this . polylines . values ( ) ] ,
222254 circles : [ ...this . circles . values ( ) ] ,
255+ rectangles : [ ...this . rectangles . values ( ) ] ,
223256 infoWindows : this . infoWindows ,
224257 } ) ;
225258
@@ -232,7 +265,7 @@ export default abstract class<
232265 element,
233266 } : {
234267 definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
235- element: Marker | Polygon | Polyline | Circle ;
268+ element: Marker | Polygon | Polyline | Circle | Rectangle ;
236269 } ) : InfoWindow {
237270 this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
238271 const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
@@ -286,6 +319,14 @@ export default abstract class<
286319 this . onDrawChanged ( this . circles , this . circlesValue , this . createCircle , this . doRemoveCircle ) ;
287320 }
288321
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+
289330 //endregion
290331
291332 //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<
331372
332373 protected abstract doRemoveCircle ( circle : Circle ) : void ;
333374
375+ protected abstract doCreateRectangle ( {
376+ definition,
377+ } : {
378+ definition : RectangleDefinition < RectangleOptions , InfoWindowOptions > ;
379+ } ) : Rectangle ;
380+
381+ protected abstract doRemoveRectangle ( rectangle : Rectangle ) : void ;
382+
334383 protected abstract doCreateInfoWindow ( {
335384 definition,
336385 element,
337386 } : {
338387 definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
339- element: Marker | Polygon | Polyline | Circle ;
388+ element: Marker | Polygon | Polyline | Circle | Rectangle ;
340389 } ) : InfoWindow ;
341390 protected abstract doCreateIcon ( {
342391 definition,
@@ -369,15 +418,21 @@ export default abstract class<
369418 draws : typeof this . circles ,
370419 factory : typeof this . doCreateCircle
371420 ) : typeof this . doCreateCircle ;
421+ private createDrawingFactory (
422+ type : 'rectangle' ,
423+ draws : typeof this . rectangles ,
424+ factory : typeof this . doCreateRectangle
425+ ) : typeof this . doCreateRectangle ;
372426 private createDrawingFactory <
373427 Factory extends
374428 | typeof this . doCreateMarker
375429 | typeof this . doCreatePolygon
376430 | typeof this . doCreatePolyline
377- | typeof this . doCreateCircle ,
431+ | typeof this . doCreateCircle
432+ | typeof this . doCreateRectangle ,
378433 Draw extends ReturnType < Factory > ,
379434 > (
380- type : 'marker' | 'polygon' | 'polyline' | 'circle' ,
435+ type : 'marker' | 'polygon' | 'polyline' | 'circle' | 'rectangle' ,
381436 draws : globalThis . Map < WithIdentifier < any > , Draw > ,
382437 factory : Factory
383438 ) : Factory {
@@ -421,6 +476,12 @@ export default abstract class<
421476 factory : typeof this . createCircle ,
422477 remover : typeof this . doRemoveCircle
423478 ) : 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 ;
424485 private onDrawChanged < Draw , DrawDefinition extends WithIdentifier < Record < string , unknown > > > (
425486 draws : globalThis . Map < WithIdentifier < any > , Draw > ,
426487 newDrawDefinitions : Array < DrawDefinition > ,
0 commit comments