@@ -4,106 +4,94 @@ import { Loader } from '@googlemaps/js-api-loader';
4
4
class default_1 extends Controller {
5
5
constructor ( ) {
6
6
super ( ...arguments ) ;
7
- this . markers = new Map ( ) ;
7
+ this . markers = [ ] ;
8
8
this . infoWindows = [ ] ;
9
9
}
10
10
initialize ( ) {
11
- var _a ;
12
- const providerConfig = ( _a = window . __symfony_ux_maps . providers ) === null || _a === void 0 ? void 0 : _a . google_maps ;
11
+ var _a , _b ;
12
+ const providerConfig = ( _b = ( _a = window . __symfony_ux_maps ) === null || _a === void 0 ? void 0 : _a . providers ) === null || _b === void 0 ? void 0 : _b [ 'google-maps' ] ;
13
13
if ( ! providerConfig ) {
14
14
throw new Error ( 'Google Maps provider configuration is missing, did you forget to call `{{ ux_map_script_tags() }}`?' ) ;
15
15
}
16
- const loaderOptions = {
17
- apiKey : providerConfig . key ,
18
- } ;
16
+ const loaderOptions = providerConfig ;
19
17
this . dispatchEvent ( 'init' , {
20
18
loaderOptions,
21
19
} ) ;
22
20
this . loader = new Loader ( loaderOptions ) ;
23
21
}
24
22
async connect ( ) {
25
- const { Map : GoogleMap , InfoWindow } = await this . loader . importLibrary ( 'maps' ) ;
26
- const mapOptions = {
27
- gestureHandling : this . viewValue . gestureHandling ,
28
- backgroundColor : this . viewValue . backgroundColor ,
29
- disableDoubleClickZoom : this . viewValue . disableDoubleClickZoom ,
30
- zoomControl : this . viewValue . zoomControl ,
31
- zoomControlOptions : this . viewValue . zoomControlOptions ,
32
- mapTypeControl : this . viewValue . mapTypeControl ,
33
- mapTypeControlOptions : this . viewValue . mapTypeControlOptions ,
34
- streetViewControl : this . viewValue . streetViewControl ,
35
- streetViewControlOptions : this . viewValue . streetViewControlOptions ,
36
- fullscreenControl : this . viewValue . fullscreenControl ,
37
- fullscreenControlOptions : this . viewValue . fullscreenControlOptions ,
38
- } ;
39
- if ( this . viewValue . mapId ) {
40
- mapOptions . mapId = this . viewValue . mapId ;
41
- }
42
- if ( this . viewValue . center ) {
43
- mapOptions . center = this . viewValue . center ;
44
- }
45
- if ( this . viewValue . zoom ) {
46
- mapOptions . zoom = this . viewValue . zoom ;
47
- }
48
- this . dispatchEvent ( 'pre-connect' , {
49
- mapOptions,
23
+ const { Map : GoogleMap } = await this . loader . importLibrary ( 'maps' ) ;
24
+ const { center, zoom, fitBoundsToMarkers, options, markers, infoWindows } = this . viewValue ;
25
+ this . dispatchEvent ( 'pre-connect' , { options } ) ;
26
+ this . map = new GoogleMap ( this . element , Object . assign ( Object . assign ( { } , options ) , { center,
27
+ zoom } ) ) ;
28
+ this . createMarkers ( markers , fitBoundsToMarkers ) ;
29
+ this . createInfoWindows ( infoWindows ) ;
30
+ this . dispatchEvent ( 'connect' , {
31
+ map : this . map ,
32
+ markers : this . markers ,
33
+ infoWindows : this . infoWindows ,
50
34
} ) ;
51
- this . map = new GoogleMap ( this . element , mapOptions ) ;
52
- if ( this . viewValue . markers ) {
53
- const { AdvancedMarkerElement } = await this . loader . importLibrary ( 'marker' ) ;
54
- this . viewValue . markers . forEach ( ( markerConfiguration ) => {
55
- const marker = new AdvancedMarkerElement ( {
56
- position : markerConfiguration . position ,
57
- title : markerConfiguration . title ,
58
- map : this . map ,
59
- } ) ;
60
- this . markers . set ( markerConfiguration . _id , marker ) ;
35
+ }
36
+ createMarkers ( markers , fitBoundsToMarkers ) {
37
+ markers . forEach ( ( definition ) => this . createMarker ( definition ) ) ;
38
+ if ( this . markers . length > 0 && fitBoundsToMarkers ) {
39
+ const bounds = new google . maps . LatLngBounds ( ) ;
40
+ this . markers . forEach ( ( marker ) => {
41
+ if ( ! marker . position ) {
42
+ return ;
43
+ }
44
+ bounds . extend ( marker . position ) ;
61
45
} ) ;
62
- if ( this . viewValue . fitBoundsToMarkers ) {
63
- const bounds = new google . maps . LatLngBounds ( ) ;
64
- this . markers . forEach ( ( marker ) => {
65
- if ( ! marker . position ) {
66
- return ;
67
- }
68
- bounds . extend ( marker . position ) ;
69
- } ) ;
70
- this . map . fitBounds ( bounds ) ;
71
- }
46
+ this . map . fitBounds ( bounds ) ;
47
+ }
48
+ }
49
+ async createMarker ( definition ) {
50
+ const { AdvancedMarkerElement } = await this . loader . importLibrary ( 'marker' ) ;
51
+ const options = {
52
+ position : definition . position ,
53
+ title : definition . title ,
54
+ } ;
55
+ this . dispatchEvent ( 'marker:before-create' , { options } ) ;
56
+ const marker = new AdvancedMarkerElement ( Object . assign ( Object . assign ( { } , options ) , { map : this . map } ) ) ;
57
+ if ( definition . infoWindow ) {
58
+ this . createInfoWindow ( definition . infoWindow , marker ) ;
72
59
}
73
- this . viewValue . infoWindows . forEach ( ( infoWindowConfiguration ) => {
74
- const marker = infoWindowConfiguration . _markerId
75
- ? this . markers . get ( infoWindowConfiguration . _markerId )
76
- : undefined ;
77
- const infoWindow = new InfoWindow ( {
78
- headerContent : this . createTextOrElement ( infoWindowConfiguration . headerContent ) ,
79
- content : this . createTextOrElement ( infoWindowConfiguration . content ) ,
80
- position : infoWindowConfiguration . position ,
60
+ this . dispatchEvent ( 'marker:after-create' , { marker } ) ;
61
+ this . markers . push ( marker ) ;
62
+ }
63
+ createInfoWindows ( infoWindows ) {
64
+ infoWindows . forEach ( ( definition ) => this . createInfoWindow ( definition ) ) ;
65
+ }
66
+ async createInfoWindow ( definition , marker ) {
67
+ const { InfoWindow } = await this . loader . importLibrary ( 'maps' ) ;
68
+ const options = {
69
+ headerContent : this . createTextOrElement ( definition . headerContent ) ,
70
+ content : this . createTextOrElement ( definition . content ) ,
71
+ position : definition . position ,
72
+ } ;
73
+ this . dispatchEvent ( 'info-window:before-create' , { options } ) ;
74
+ const infoWindow = new InfoWindow ( options ) ;
75
+ this . infoWindows . push ( infoWindow ) ;
76
+ if ( definition . opened ) {
77
+ infoWindow . open ( {
78
+ map : this . map ,
79
+ shouldFocus : false ,
80
+ anchor : marker ,
81
81
} ) ;
82
- this . infoWindows . push ( infoWindow ) ;
83
- if ( infoWindowConfiguration . opened ) {
82
+ }
83
+ if ( marker ) {
84
+ marker . addListener ( 'click' , ( ) => {
85
+ if ( definition . autoClose ) {
86
+ this . closeInfoWindowsExcept ( infoWindow ) ;
87
+ }
84
88
infoWindow . open ( {
85
89
map : this . map ,
86
- shouldFocus : false ,
87
90
anchor : marker ,
88
91
} ) ;
89
- }
90
- if ( marker ) {
91
- marker . addListener ( 'click' , ( ) => {
92
- if ( infoWindowConfiguration . autoClose ) {
93
- this . closeInfoWindowsExcept ( infoWindow ) ;
94
- }
95
- infoWindow . open ( {
96
- map : this . map ,
97
- anchor : marker ,
98
- } ) ;
99
- } ) ;
100
- }
101
- } ) ;
102
- this . dispatchEvent ( 'connect' , {
103
- map : this . map ,
104
- markers : this . markers ,
105
- infoWindows : this . infoWindows ,
106
- } ) ;
92
+ } ) ;
93
+ }
94
+ this . dispatchEvent ( 'info-window:after-create' , { infoWindow } ) ;
107
95
}
108
96
createTextOrElement ( content ) {
109
97
if ( ! content ) {
0 commit comments