@@ -2,12 +2,13 @@ import { Controller } from '@hotwired/stimulus';
2
2
3
3
export type Point = { lat : number ; lng : number } ;
4
4
5
- export type MapView < Options , MarkerOptions , InfoWindowOptions , PolygonOptions > = {
5
+ export type MapView < Options , MarkerOptions , InfoWindowOptions , PolygonOptions , PolylineOptions > = {
6
6
center : Point | null ;
7
7
zoom : number | null ;
8
8
fitBoundsToMarkers : boolean ;
9
9
markers : Array < MarkerDefinition < MarkerOptions , InfoWindowOptions > > ;
10
10
polygons : Array < PolygonDefinition < PolygonOptions , InfoWindowOptions > > ;
11
+ polylines : Array < PolylineDefinition < PolylineOptions , InfoWindowOptions > > ;
11
12
options : Options ;
12
13
} ;
13
14
@@ -36,6 +37,14 @@ export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = {
36
37
extra : Record < string , unknown > ;
37
38
} ;
38
39
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
+
39
48
export type InfoWindowDefinition < InfoWindowOptions > = {
40
49
headerContent : string | null ;
41
50
content : string | null ;
@@ -56,7 +65,7 @@ export type InfoWindowDefinition<InfoWindowOptions> = {
56
65
extra : Record < string , unknown > ;
57
66
} ;
58
67
59
- export default abstract class <
68
+ export default abstract class <
60
69
MapOptions ,
61
70
Map ,
62
71
MarkerOptions ,
@@ -65,18 +74,21 @@ export default abstract class<
65
74
InfoWindow ,
66
75
PolygonOptions ,
67
76
Polygon ,
77
+ PolylineOptions ,
78
+ Polyline ,
68
79
> extends Controller < HTMLElement > {
69
80
static values = {
70
81
providerOptions : Object ,
71
82
view : Object ,
72
83
} ;
73
84
74
- declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions , PolygonOptions > ;
85
+ declare viewValue : MapView < MapOptions , MarkerOptions , InfoWindowOptions , PolygonOptions , PolylineOptions > ;
75
86
76
87
protected map : Map ;
77
88
protected markers : Array < Marker > = [ ] ;
78
89
protected infoWindows: Array < InfoWindow > = [ ] ;
79
90
protected polygons : Array < Polygon > = [ ] ;
91
+ protected polylines: Array < Polyline > = [ ] ;
80
92
81
93
connect ( ) {
82
94
const { center, zoom, options, markers, polygons, fitBoundsToMarkers } = this . viewValue ;
@@ -89,6 +101,8 @@ export default abstract class<
89
101
90
102
polygons . forEach ( ( polygon ) => this . createPolygon ( polygon ) ) ;
91
103
104
+ polylines . forEach ( ( polyline ) => this . createPolyline ( polyline ) ) ;
105
+
92
106
if ( fitBoundsToMarkers ) {
93
107
this . doFitBoundsToMarkers ( ) ;
94
108
}
@@ -97,6 +111,7 @@ export default abstract class<
97
111
map : this . map ,
98
112
markers : this . markers ,
99
113
polygons : this . polygons ,
114
+ polylines : this . polylines ,
100
115
infoWindows : this . infoWindows ,
101
116
} ) ;
102
117
}
@@ -129,17 +144,27 @@ export default abstract class<
129
144
return polygon ;
130
145
}
131
146
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
+
132
155
protected abstract doCreateMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker ;
133
156
protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
157
+ protected abstract doCreatePolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline ;
134
158
135
159
protected createInfoWindow ( {
136
160
definition,
137
161
element,
138
162
} : {
139
163
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 ;
143
168
} ) : InfoWindow {
144
169
this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
145
170
const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
@@ -155,13 +180,17 @@ export default abstract class<
155
180
element,
156
181
} :
157
182
| {
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
+ }
161
190
| {
162
- definition: PolygonDefinition < PolygonOptions , InfoWindowOptions > [ 'infoWindow' ] ;
163
- element: Polygon ;
164
- } ) : InfoWindow ;
191
+ definition: PolylineDefinition < PolylineOptions , InfoWindowOptions > [ 'infoWindow' ] ;
192
+ element: Polyline ;
193
+ } ) : InfoWindow ;
165
194
166
195
protected abstract doFitBoundsToMarkers ( ) : void ;
167
196
0 commit comments