@@ -2,12 +2,13 @@ import { Controller } from '@hotwired/stimulus';
22
33export type Point = { lat : number ; lng : number } ;
44
5- export type MapView < Options , MarkerOptions , InfoWindowOptions , PolygonOptions > = {
5+ export type MapView < Options , MarkerOptions , InfoWindowOptions , PolygonOptions , PolylineOptions > = {
66 center : Point | null ;
77 zoom : number | null ;
88 fitBoundsToMarkers : boolean ;
99 markers : Array < MarkerDefinition < MarkerOptions , InfoWindowOptions > > ;
1010 polygons : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
11+ polylines : Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
1112 options : Options ;
1213} ;
1314
@@ -36,6 +37,14 @@ export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = {
3637 extra : Record < string , unknown > ;
3738} ;
3839
40+ export type PolylineDefinition < PolylineOptions , InfoWindowOptions > = {
41+ infoWindow ?: Omit < InfoWindowDefinition < InfoWindowOptions > , 'position' > ;
42+ points : Array < Point > ;
43+ title : string | null ;
44+ rawOptions ?: PolylineOptions ;
45+ extra : Record < string , unknown > ;
46+ } ;
47+
3948export type InfoWindowDefinition < InfoWindowOptions > = {
4049 headerContent : string | null ;
4150 content : string | null ;
@@ -56,7 +65,7 @@ export type InfoWindowDefinition<InfoWindowOptions> = {
5665 extra : Record < string , unknown > ;
5766} ;
5867
59- export default abstract class <
68+ export default abstract class <
6069 MapOptions ,
6170 Map ,
6271 MarkerOptions ,
@@ -65,18 +74,21 @@ export default abstract class<
6574 InfoWindow ,
6675 PolygonOptions ,
6776 Polygon ,
77+ PolylineOptions ,
78+ Polyline ,
6879> extends Controller < HTMLElement > {
6980 static values = {
7081 providerOptions : Object ,
7182 view : Object ,
7283 } ;
7384
74- declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions , PolygonOptions > ;
85+ declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions , PolygonOptions , PolylineOptions > ;
7586
7687 protected map : Map ;
7788 protected markers : Array < Marker > = [ ] ;
7889 protected infoWindows: Array < InfoWindow > = [ ] ;
7990 protected polygons : Array < Polygon > = [ ] ;
91+ protected polylines: Array < Polyline > = [ ] ;
8092
8193 connect ( ) {
8294 const { center, zoom, options, markers, polygons, fitBoundsToMarkers } = this . viewValue ;
@@ -89,6 +101,8 @@ export default abstract class<
89101
90102 polygons . forEach ( ( polygon ) => this . createPolygon ( polygon ) ) ;
91103
104+ polylines . forEach ( ( polyline ) => this . createPolyline ( polyline ) ) ;
105+
92106 if ( fitBoundsToMarkers ) {
93107 this . doFitBoundsToMarkers ( ) ;
94108 }
@@ -97,6 +111,7 @@ export default abstract class<
97111 map : this . map ,
98112 markers : this . markers ,
99113 polygons : this . polygons ,
114+ polylines : this . polylines ,
100115 infoWindows : this . infoWindows ,
101116 } ) ;
102117 }
@@ -129,17 +144,27 @@ export default abstract class<
129144 return polygon ;
130145 }
131146
147+ createPolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline {
148+ this . dispatchEvent ( 'polyline:before-create' , { definition } ) ;
149+ const polyline = this . doCreatePolyline ( definition ) ;
150+ this . dispatchEvent ( 'polyline:after-create' , { polyline } ) ;
151+ this . polylines . push ( polyline ) ;
152+ return polyline ;
153+ }
154+
132155 protected abstract doCreateMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker ;
133156 protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
157+ protected abstract doCreatePolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline ;
134158
135159 protected createInfoWindow ( {
136160 definition,
137161 element,
138162 } : {
139163 definition :
140- | MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ]
141- | PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
142- element : Marker | Polygon ;
164+ | MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ]
165+ | PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ]
166+ | PolylineDefinition < PolylineOptions , InfoWindowOptions > [ 'infoWindow' ] ;
167+ element : Marker | Polygon | Polyline ;
143168 } ) : InfoWindow {
144169 this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
145170 const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
@@ -155,13 +180,17 @@ export default abstract class<
155180 element,
156181 } :
157182 | {
158- definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
159- element: Marker ;
160- }
183+ definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > [ 'infoWindow' ] ;
184+ element: Marker ;
185+ }
186+ | {
187+ definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
188+ element: Polygon ;
189+ }
161190 | {
162- definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
163- element: Polygon ;
164- } ) : InfoWindow ;
191+ definition: PolylineDefinition < PolylineOptions , InfoWindowOptions > [ 'infoWindow' ] ;
192+ element: Polyline ;
193+ } ) : InfoWindow ;
165194
166195 protected abstract doFitBoundsToMarkers ( ) : void ;
167196
0 commit comments