|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | +import { Loader } from '@googlemaps/js-api-loader'; |
| 3 | + |
| 4 | +class default_1 extends Controller { |
| 5 | + constructor() { |
| 6 | + super(...arguments); |
| 7 | + this.markers = new Map(); |
| 8 | + this.infoWindows = []; |
| 9 | + } |
| 10 | + initialize() { |
| 11 | + var _a; |
| 12 | + const providerConfig = (_a = window.__symfony_ux_maps.providers) === null || _a === void 0 ? void 0 : _a.google_maps; |
| 13 | + if (!providerConfig) { |
| 14 | + throw new Error('Google Maps provider configuration is missing, did you forget to call `{{ ux_map_script_tags() }}`?'); |
| 15 | + } |
| 16 | + const loaderOptions = { |
| 17 | + apiKey: providerConfig.key, |
| 18 | + }; |
| 19 | + this.dispatchEvent('init', { |
| 20 | + loaderOptions, |
| 21 | + }); |
| 22 | + this.loader = new Loader(loaderOptions); |
| 23 | + } |
| 24 | + 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, |
| 50 | + }); |
| 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); |
| 61 | + }); |
| 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 | + } |
| 72 | + } |
| 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, |
| 81 | + }); |
| 82 | + this.infoWindows.push(infoWindow); |
| 83 | + if (infoWindowConfiguration.opened) { |
| 84 | + infoWindow.open({ |
| 85 | + map: this.map, |
| 86 | + shouldFocus: false, |
| 87 | + anchor: marker, |
| 88 | + }); |
| 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 | + }); |
| 107 | + } |
| 108 | + createTextOrElement(content) { |
| 109 | + if (!content) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + if (content.includes('<')) { |
| 113 | + const div = document.createElement('div'); |
| 114 | + div.innerHTML = content; |
| 115 | + return div; |
| 116 | + } |
| 117 | + return content; |
| 118 | + } |
| 119 | + closeInfoWindowsExcept(infoWindow) { |
| 120 | + this.infoWindows.forEach((otherInfoWindow) => { |
| 121 | + if (otherInfoWindow !== infoWindow) { |
| 122 | + otherInfoWindow.close(); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + dispatchEvent(name, payload) { |
| 127 | + this.dispatch(name, { detail: payload, prefix: 'google-maps' }); |
| 128 | + } |
| 129 | +} |
| 130 | +default_1.values = { |
| 131 | + view: Object, |
| 132 | +}; |
| 133 | + |
| 134 | +export { default_1 as default }; |
0 commit comments