11import { Loader } from '@googlemaps/js-api-loader' ;
2- import { Controller } from '@hotwired/stimulus' ;
3-
4- class default_1 extends Controller {
5- constructor ( ) {
6- super ( ...arguments ) ;
7- this . markers = new Map ( ) ;
8- this . polygons = new Map ( ) ;
9- this . polylines = new Map ( ) ;
10- this . infoWindows = [ ] ;
11- this . isConnected = false ;
12- }
13- connect ( ) {
14- const options = this . optionsValue ;
15- this . dispatchEvent ( 'pre-connect' , { options } ) ;
16- this . createMarker = this . createDrawingFactory ( 'marker' , this . markers , this . doCreateMarker . bind ( this ) ) ;
17- this . createPolygon = this . createDrawingFactory ( 'polygon' , this . polygons , this . doCreatePolygon . bind ( this ) ) ;
18- this . createPolyline = this . createDrawingFactory ( 'polyline' , this . polylines , this . doCreatePolyline . bind ( this ) ) ;
19- this . map = this . doCreateMap ( {
20- center : this . hasCenterValue ? this . centerValue : null ,
21- zoom : this . hasZoomValue ? this . zoomValue : null ,
22- options,
23- } ) ;
24- this . markersValue . forEach ( ( definition ) => this . createMarker ( { definition } ) ) ;
25- this . polygonsValue . forEach ( ( definition ) => this . createPolygon ( { definition } ) ) ;
26- this . polylinesValue . forEach ( ( definition ) => this . createPolyline ( { definition } ) ) ;
27- if ( this . fitBoundsToMarkersValue ) {
28- this . doFitBoundsToMarkers ( ) ;
29- }
30- this . dispatchEvent ( 'connect' , {
31- map : this . map ,
32- markers : [ ...this . markers . values ( ) ] ,
33- polygons : [ ...this . polygons . values ( ) ] ,
34- polylines : [ ...this . polylines . values ( ) ] ,
35- infoWindows : this . infoWindows ,
36- } ) ;
37- this . isConnected = true ;
38- }
39- createInfoWindow ( { definition, element, } ) {
40- this . dispatchEvent ( 'info-window:before-create' , { definition, element } ) ;
41- const infoWindow = this . doCreateInfoWindow ( { definition, element } ) ;
42- this . dispatchEvent ( 'info-window:after-create' , { infoWindow, element } ) ;
43- this . infoWindows . push ( infoWindow ) ;
44- return infoWindow ;
45- }
46- markersValueChanged ( ) {
47- if ( ! this . isConnected ) {
48- return ;
49- }
50- this . onDrawChanged ( this . markers , this . markersValue , this . createMarker , this . doRemoveMarker ) ;
51- if ( this . fitBoundsToMarkersValue ) {
52- this . doFitBoundsToMarkers ( ) ;
53- }
54- }
55- polygonsValueChanged ( ) {
56- if ( ! this . isConnected ) {
57- return ;
58- }
59- this . onDrawChanged ( this . polygons , this . polygonsValue , this . createPolygon , this . doRemovePolygon ) ;
60- }
61- polylinesValueChanged ( ) {
62- if ( ! this . isConnected ) {
63- return ;
64- }
65- this . onDrawChanged ( this . polylines , this . polylinesValue , this . createPolyline , this . doRemovePolyline ) ;
66- }
67- createDrawingFactory ( type , draws , factory ) {
68- const eventBefore = `${ type } :before-create` ;
69- const eventAfter = `${ type } :after-create` ;
70- return ( { definition } ) => {
71- this . dispatchEvent ( eventBefore , { definition } ) ;
72- const drawing = factory ( { definition } ) ;
73- this . dispatchEvent ( eventAfter , { [ type ] : drawing } ) ;
74- draws . set ( definition [ '@id' ] , drawing ) ;
75- return drawing ;
76- } ;
77- }
78- onDrawChanged ( draws , newDrawDefinitions , factory , remover ) {
79- const idsToRemove = new Set ( draws . keys ( ) ) ;
80- newDrawDefinitions . forEach ( ( definition ) => {
81- idsToRemove . delete ( definition [ '@id' ] ) ;
82- } ) ;
83- idsToRemove . forEach ( ( id ) => {
84- const draw = draws . get ( id ) ;
85- remover ( draw ) ;
86- draws . delete ( id ) ;
87- } ) ;
88- newDrawDefinitions . forEach ( ( definition ) => {
89- if ( ! draws . has ( definition [ '@id' ] ) ) {
90- factory ( { definition } ) ;
91- }
92- } ) ;
93- }
94- }
95- default_1 . values = {
96- providerOptions : Object ,
97- center : Object ,
98- zoom : Number ,
99- fitBoundsToMarkers : Boolean ,
100- markers : Array ,
101- polygons : Array ,
102- polylines : Array ,
103- options : Object ,
104- } ;
2+ import AbstractMapController from '@symfony/ux-map' ;
1053
1064let _google ;
107- class map_controller extends default_1 {
5+ class map_controller extends AbstractMapController {
1086 async connect ( ) {
1097 if ( ! _google ) {
1108 _google = { maps : { } } ;
@@ -158,7 +56,7 @@ class map_controller extends default_1 {
15856 } ) ;
15957 }
16058 doCreateMarker ( { definition, } ) {
161- const { '@id' : _id , position, title, infoWindow, extra, rawOptions = { } , ...otherOptions } = definition ;
59+ const { '@id' : _id , position, title, infoWindow, icon , extra, rawOptions = { } , ...otherOptions } = definition ;
16260 const marker = new _google . maps . marker . AdvancedMarkerElement ( {
16361 position,
16462 title,
@@ -169,6 +67,9 @@ class map_controller extends default_1 {
16967 if ( infoWindow ) {
17068 this . createInfoWindow ( { definition : infoWindow , element : marker } ) ;
17169 }
70+ if ( icon ) {
71+ this . doCreateIcon ( { definition : icon , element : marker } ) ;
72+ }
17273 return marker ;
17374 }
17475 doRemoveMarker ( marker ) {
@@ -272,6 +173,21 @@ class map_controller extends default_1 {
272173 }
273174 return content ;
274175 }
176+ doCreateIcon ( { definition, element } ) {
177+ const { content, iconType, width, height } = definition ;
178+ if ( iconType === 'html' ) {
179+ const parser = new DOMParser ( ) ;
180+ const icon = parser . parseFromString ( content , "image/svg+xml" ) . documentElement ;
181+ element . content = icon ;
182+ }
183+ else {
184+ const icon = document . createElement ( 'img' ) ;
185+ icon . width = width . toString ( ) ;
186+ icon . height = height . toString ( ) ;
187+ icon . src = content ;
188+ element . content = icon ;
189+ }
190+ }
275191 closeInfoWindowsExcept ( infoWindow ) {
276192 this . infoWindows . forEach ( ( otherInfoWindow ) => {
277193 if ( otherInfoWindow !== infoWindow ) {
0 commit comments