{"version":3,"file":"google_map_controller-jsvLR_uN.js","sources":["../../../app/javascript/controllers/google_map_controller.js"],"sourcesContent":["import {Loader} from \"@googlemaps/js-api-loader\";\n\nimport ApplicationController from \"./application_controller\";\n\nexport default class extends ApplicationController {\n static values = {\n apiKey: String,\n latitude: String,\n longitude: String,\n }\n\n static targets = [\"button\", \"map\", \"locateCurrentButton\", \"latitudeInput\", \"longitudeInput\", \"addressInput\", \"resetTarget\", \"offlineNotice\", \"closeButton\", \"placeIdInput\"]\n\n initialize() {\n this.onMapClick = this.onMapClick.bind(this)\n this.userLocated = this.userLocated.bind(this)\n this.userLocatedError = this.userLocatedError.bind(this)\n\n this.onNetworkConnection = this.onNetworkConnection.bind(this)\n this.onNetworkDisconnection = this.onNetworkDisconnection.bind(this)\n }\n\n connect() {\n this.loader = new Loader({\n apiKey: this.apiKeyValue,\n version: \"weekly\",\n libraries: [\"places\"]\n });\n\n window.addEventListener(\"online\", this.onNetworkConnection)\n window.addEventListener(\"offline\", this.onNetworkDisconnection)\n\n if(this.online) {\n this.loadMap()\n } else {\n this.onNetworkDisconnection()\n }\n }\n\n disconnect() {\n window.removeEventListener(\"online\", this.onNetworkConnection)\n window.removeEventListener(\"offline\", this.onNetworkDisconnection)\n }\n\n onNetworkConnection() {\n this.loadMap()\n\n if(this.visible) {\n this.inputs.forEach((input) => {\n input.disabled = false\n })\n }\n\n super.show(this.mapTarget)\n super.hide(this.offlineNoticeTarget)\n }\n\n onNetworkDisconnection() {\n if(this.visible) {\n this.inputs.forEach((input) => {\n input.disabled = true\n })\n }\n\n super.hide(this.mapTarget)\n super.show(this.offlineNoticeTarget)\n }\n\n loadMap() {\n if(this.offline) return\n\n this.loader.load().then((google) => {\n this.google = google\n this.map = new google.maps.Map(this.mapTarget, this.mapOptions);\n this.geocoder = new google.maps.Geocoder();\n this.marker = new this.google.maps.Marker({ map: this.map })\n\n this.centerToUserLocation()\n this.map.addListener(\"click\", this.onMapClick)\n });\n }\n\n onMapClick(e) {\n if(this.offline) return\n\n this.map.panTo(e.latLng);\n this.marker.setPosition(e.latLng);\n\n this.latitudeInputTarget.value = e.latLng.lat()\n this.longitudeInputTarget.value = e.latLng.lng()\n\n this.geocoder.geocode({ location: e.latLng }, (results, status) => {\n this.addressInputTarget.value = results[0].formatted_address\n this.placeIdInputTarget.value = results[0].place_id\n })\n\n this.nextTick(() => {\n this.dispatch('click', {\n detail: {\n latitude: e.latLng.lat(),\n longitude: e.latLng.lng()\n }\n })\n })\n }\n\n centerToUserLocation() {\n const currentLocation = new this.google.maps.LatLng(this.latitudeValue, this.longitudeValue);\n\n this.onMapClick({latLng: currentLocation})\n }\n\n shareCurrentLocation() {\n if(!navigator.geolocation || this.offline) return\n\n const options = {\n enableHighAccuracy: true\n }\n\n navigator.permissions.query({ name: 'geolocation' }).then(({ state }) => {\n if(state === 'granted' || state === 'prompt') {\n return navigator.geolocation.getCurrentPosition(this.userLocated, this.userLocatedError, options)\n }\n\n this.showToast({\n message: this.translations.compose.tools.location.permission_state.denied,\n hideAfter: 7000\n })\n })\n }\n\n userLocated(position) {\n const {latitude, longitude} = position.coords\n\n this.panTo(latitude, longitude)\n\n this.dispatch('click', {\n detail: {\n latitude,\n longitude\n }\n })\n }\n\n userLocatedError(error) {\n const message = error.code === error.PERMISSION_DENIED ? 'denied' : 'timeout'\n\n this.showToast({\n message: this.translations.compose.tools.location.permission_state[message],\n hideAfter: 7000\n })\n }\n\n show() {\n if(this.marker) {\n this.centerToUserLocation()\n this.inputs.forEach(super.enable)\n }\n\n this.dispatch('visible')\n\n super.show()\n }\n\n hide() {\n super.hide()\n this.reset()\n\n this.dispatch('hidden')\n }\n\n disable() {\n this.inputs.forEach(super.disable)\n this.mapTarget.classList.add(\"grayscale\", \"cursor-not-allowed\", \"pointer-events-none\")\n }\n\n enable() {\n if(this.isVisible()) {\n this.inputs.forEach(super.enable)\n }\n\n this.mapTarget.classList.remove(\"grayscale\", \"cursor-not-allowed\", \"pointer-events-none\")\n }\n\n clearAndHide() {\n this.reset()\n super.hide()\n }\n\n restore({ detail }) {\n this.latitudeValue = detail.latitude\n this.longitudeValue = detail.longitude\n\n if(!this.latitudeValue || !this.longitudeValue) {\n super.hide()\n return this.reset()\n }\n\n this.panTo(this.latitudeValue, this.longitudeValue)\n this.show()\n }\n\n panTo(latitude, longitude) {\n const currentLocation = new this.google.maps.LatLng(latitude, longitude);\n\n this.map.panTo(currentLocation);\n this.marker.setPosition(currentLocation);\n\n this.latitudeInputTarget.value = currentLocation.lat()\n this.longitudeInputTarget.value = currentLocation.lng()\n\n this.geocoder.geocode({location: currentLocation}, (results, status) => {\n this.addressInputTarget.value = results[0].formatted_address\n this.placeIdInputTarget.value = results[0].place_id\n })\n }\n\n reset() {\n if(this.offline) return\n\n const currentLocation = new this.google.maps.LatLng(this.latitudeValue, this.longitudeValue);\n\n this.map.panTo(currentLocation);\n this.marker.setPosition(currentLocation);\n\n this.inputs.forEach((input) => {\n input.value = \"\"\n input.disabled = true\n })\n }\n\n get inputs() {\n return [this.latitudeInputTarget, this.longitudeInputTarget, this.addressInputTarget, this.placeIdInputTarget]\n }\n\n get mapOptions() {\n return {\n center: {\n lat: 0,\n lng: 0\n },\n zoom: 15,\n disableDefaultUI: false,\n fullscreenControl: false,\n streetViewControl: false,\n mapTypeControl: false,\n language: this.userLocale\n }\n }\n}\n"],"names":["google_map_controller","ApplicationController","Loader","input","google","e","results","status","currentLocation","options","state","position","latitude","longitude","error","message","detail","__publicField"],"mappings":"+PAIe,MAAKA,UAASC,CAAsB,CASjD,YAAa,CACX,KAAK,WAAa,KAAK,WAAW,KAAK,IAAI,EAC3C,KAAK,YAAc,KAAK,YAAY,KAAK,IAAI,EAC7C,KAAK,iBAAmB,KAAK,iBAAiB,KAAK,IAAI,EAEvD,KAAK,oBAAsB,KAAK,oBAAoB,KAAK,IAAI,EAC7D,KAAK,uBAAyB,KAAK,uBAAuB,KAAK,IAAI,CACvE,CAEE,SAAU,CACR,KAAK,OAAS,IAAIC,EAAO,CACvB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