From d4f7315a6e7ff9232f68ec27f85bfa0e7c5cdf1d Mon Sep 17 00:00:00 2001 From: Florian Cellier Date: Wed, 4 Dec 2024 14:50:35 +0100 Subject: [PATCH] WIP --- .../Google/assets/dist/map_controller.js | 106 +++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/src/Map/src/Bridge/Google/assets/dist/map_controller.js b/src/Map/src/Bridge/Google/assets/dist/map_controller.js index 543d9373a5..412db2c121 100644 --- a/src/Map/src/Bridge/Google/assets/dist/map_controller.js +++ b/src/Map/src/Bridge/Google/assets/dist/map_controller.js @@ -1,8 +1,110 @@ import { Loader } from '@googlemaps/js-api-loader'; -import AbstractMapController from '@symfony/ux-map'; +import { Controller } from '@hotwired/stimulus'; + +class default_1 extends Controller { + constructor() { + super(...arguments); + this.markers = new Map(); + this.polygons = new Map(); + this.polylines = new Map(); + this.infoWindows = []; + this.isConnected = false; + } + connect() { + const options = this.optionsValue; + this.dispatchEvent('pre-connect', { options }); + this.createMarker = this.createDrawingFactory('marker', this.markers, this.doCreateMarker.bind(this)); + this.createPolygon = this.createDrawingFactory('polygon', this.polygons, this.doCreatePolygon.bind(this)); + this.createPolyline = this.createDrawingFactory('polyline', this.polylines, this.doCreatePolyline.bind(this)); + this.map = this.doCreateMap({ + center: this.hasCenterValue ? this.centerValue : null, + zoom: this.hasZoomValue ? this.zoomValue : null, + options, + }); + this.markersValue.forEach((definition) => this.createMarker({ definition })); + this.polygonsValue.forEach((definition) => this.createPolygon({ definition })); + this.polylinesValue.forEach((definition) => this.createPolyline({ definition })); + if (this.fitBoundsToMarkersValue) { + this.doFitBoundsToMarkers(); + } + this.dispatchEvent('connect', { + map: this.map, + markers: [...this.markers.values()], + polygons: [...this.polygons.values()], + polylines: [...this.polylines.values()], + infoWindows: this.infoWindows, + }); + this.isConnected = true; + } + createInfoWindow({ definition, element, }) { + this.dispatchEvent('info-window:before-create', { definition, element }); + const infoWindow = this.doCreateInfoWindow({ definition, element }); + this.dispatchEvent('info-window:after-create', { infoWindow, element }); + this.infoWindows.push(infoWindow); + return infoWindow; + } + markersValueChanged() { + if (!this.isConnected) { + return; + } + this.onDrawChanged(this.markers, this.markersValue, this.createMarker, this.doRemoveMarker); + if (this.fitBoundsToMarkersValue) { + this.doFitBoundsToMarkers(); + } + } + polygonsValueChanged() { + if (!this.isConnected) { + return; + } + this.onDrawChanged(this.polygons, this.polygonsValue, this.createPolygon, this.doRemovePolygon); + } + polylinesValueChanged() { + if (!this.isConnected) { + return; + } + this.onDrawChanged(this.polylines, this.polylinesValue, this.createPolyline, this.doRemovePolyline); + } + createDrawingFactory(type, draws, factory) { + const eventBefore = `${type}:before-create`; + const eventAfter = `${type}:after-create`; + return ({ definition }) => { + this.dispatchEvent(eventBefore, { definition }); + const drawing = factory({ definition }); + this.dispatchEvent(eventAfter, { [type]: drawing }); + draws.set(definition['@id'], drawing); + return drawing; + }; + } + onDrawChanged(draws, newDrawDefinitions, factory, remover) { + const idsToRemove = new Set(draws.keys()); + newDrawDefinitions.forEach((definition) => { + idsToRemove.delete(definition['@id']); + }); + idsToRemove.forEach((id) => { + const draw = draws.get(id); + remover(draw); + draws.delete(id); + }); + newDrawDefinitions.forEach((definition) => { + if (!draws.has(definition['@id'])) { + factory({ definition }); + } + }); + } +} +default_1.values = { + providerOptions: Object, + center: Object, + zoom: Number, + fitBoundsToMarkers: Boolean, + markers: Array, + polygons: Array, + polylines: Array, + options: Object, +}; let _google; -class map_controller extends AbstractMapController { +class map_controller extends default_1 { async connect() { if (!_google) { _google = { maps: {} };