@@ -2,11 +2,12 @@ import { Controller } from '@hotwired/stimulus';
22
33export type Point = { lat : number ; lng : number } ;
44
5- export type MapView < Options , MarkerOptions , InfoWindowOptions > = {
5+ export type MapView < Options , MarkerOptions , InfoWindowOptions , PolygonOptions > = {
66 center : Point | null ;
77 zoom : number | null ;
88 fitBoundsToMarkers : boolean ;
99 markers : Array < MarkerDefinition < MarkerOptions , InfoWindowOptions > > ;
10+ polygons : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
1011 options : Options ;
1112} ;
1213
@@ -27,6 +28,14 @@ export type MarkerDefinition<MarkerOptions, InfoWindowOptions> = {
2728 extra : Record < string , unknown > ;
2829} ;
2930
31+ export type PolygonDefinition < PolygonOptions , InfoWindowOptions > = {
32+ infoWindow ?: Omit < InfoWindowDefinition < InfoWindowOptions > , 'position' > ;
33+ points : Array < Point > ;
34+ title : string | null ;
35+ rawOptions ?: PolygonOptions ;
36+ extra : Record < string , unknown > ;
37+ } ;
38+
3039export type InfoWindowDefinition < InfoWindowOptions > = {
3140 headerContent : string | null ;
3241 content : string | null ;
@@ -54,34 +63,40 @@ export default abstract class<
5463 Marker ,
5564 InfoWindowOptions ,
5665 InfoWindow ,
66+ PolygonOptions ,
67+ Polygon ,
5768> extends Controller < HTMLElement > {
5869 static values = {
5970 providerOptions : Object ,
6071 view : Object ,
6172 } ;
6273
63- declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions > ;
74+ declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions , PolygonOptions > ;
6475
6576 protected map : Map ;
6677 protected markers : Array < Marker > = [ ] ;
6778 protected infoWindows: Array < InfoWindow > = [ ] ;
79+ protected polygons : Array < Polygon > = [ ] ;
6880
6981 connect ( ) {
70- const { center, zoom, options, markers, fitBoundsToMarkers } = this . viewValue ;
82+ const { center, zoom, options, markers, polygons , fitBoundsToMarkers } = this . viewValue ;
7183
7284 this . dispatchEvent ( 'pre-connect' , { options } ) ;
7385
7486 this . map = this . doCreateMap ( { center, zoom, options } ) ;
7587
7688 markers . forEach ( ( marker ) => this . createMarker ( marker ) ) ;
7789
90+ polygons . forEach ( ( polygon ) => this . createPolygon ( polygon ) ) ;
91+
7892 if ( fitBoundsToMarkers ) {
7993 this . doFitBoundsToMarkers ( ) ;
8094 }
8195
8296 this . dispatchEvent ( 'connect' , {
8397 map : this . map ,
8498 markers : this . markers ,
99+ polygons : this . polygons ,
85100 infoWindows : this . infoWindows ,
86101 } ) ;
87102 }
@@ -106,18 +121,29 @@ export default abstract class<
106121 return marker ;
107122 }
108123
124+ createPolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon {
125+ this . dispatchEvent ( 'polygon:before-create' , { definition } ) ;
126+ const polygon = this . doCreatePolygon ( definition ) ;
127+ this . dispatchEvent ( 'polygon:after-create' , { polygon } ) ;
128+ this . polygons . push ( polygon ) ;
129+ return polygon ;
130+ }
131+
109132 protected abstract doCreateMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker ;
133+ protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
110134
111135 protected createInfoWindow ( {
112136 definition,
113- marker ,
137+ element ,
114138 } : {
115- definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
116- marker: Marker ;
139+ definition :
140+ | MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ]
141+ | PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
142+ element : Marker | Polygon ;
117143 } ) : InfoWindow {
118- this . dispatchEvent ( 'info-window:before-create' , { definition, marker } ) ;
119- const infoWindow = this . doCreateInfoWindow ( { definition, marker } ) ;
120- this . dispatchEvent ( 'info-window:after-create' , { infoWindow, marker } ) ;
144+ this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
145+ const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
146+ this . dispatchEvent ( 'info-window:after-create' , { infoWindow, element } ) ;
121147
122148 this . infoWindows . push ( infoWindow ) ;
123149
@@ -126,11 +152,16 @@ export default abstract class<
126152
127153 protected abstract doCreateInfoWindow ( {
128154 definition,
129- marker,
130- } : {
131- definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
132- marker: Marker ;
133- } ) : InfoWindow ;
155+ element,
156+ } :
157+ | {
158+ definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
159+ element: Marker ;
160+ }
161+ | {
162+ definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
163+ element: Polygon ;
164+ } ) : InfoWindow ;
134165
135166 protected abstract doFitBoundsToMarkers ( ) : void ;
136167
0 commit comments