{"version":3,"file":"city_controller-CBbiMSGx.js","sources":["../../../app/javascript/controllers/property/address/city_controller.js"],"sourcesContent":["import { Loader } from \"@googlemaps/js-api-loader\"\n\nimport ApplicationController from '../../application_controller'\nimport {Current} from \"@/controllers/models/current\";\n\nexport default class extends ApplicationController {\n static values = {\n initial: String,\n apiKey: String,\n countryCode: String\n }\n\n static targets = [\n 'searchInput',\n 'hiddenInput',\n 'listContainer',\n 'list',\n 'notes',\n 'itemTemplate',\n 'manual',\n 'customItem',\n 'googleLogo'\n ]\n\n initialize() {\n super.initialize()\n\n this.loader = new Loader({\n apiKey: this.apiKeyValue,\n version: \"weekly\",\n libraries: [\"places\"]\n });\n\n this.countryCodeValue = Current.business.country.code\n this.buildAddressElement = this.buildAddressElement.bind(this)\n }\n\n connect() {\n super.connect()\n\n this.loader.load().then((google) => {\n this.google = google\n this.autocompleteAPI = new google.maps.places.AutocompleteService()\n })\n }\n\n search({ currentTarget }) {\n if(currentTarget.value) {\n this.debounce(() => this.searchCities(currentTarget))\n } else {\n this.removeSearchResults()\n this.show(this.notesTarget)\n this.hide(this.listContainerTarget)\n }\n }\n\n async searchCities(input) {\n this.element.setAttribute('data-arrow-navigation-enabled-value', false)\n\n this.autocompleteAPI.getPlacePredictions({\n input: input.value,\n language: Current.user.locale,\n componentRestrictions: {\n country: this.countryCodeValue\n },\n types: ['(cities)']\n }).then(response => {\n this.removeSearchResults()\n\n response.predictions.map(this.buildAddressElement).forEach(element => this.listTarget.appendChild(element))\n\n if(response.predictions.length > 0) {\n this.show(this.googleLogoTarget)\n } else {\n this.hide(this.googleLogoTarget)\n\n const clonedCustomItem = this.customItemTarget.cloneNode(true)\n\n clonedCustomItem.querySelector('p').innerText = this.translations.property.addresses.form.add.replace('%{address}', this.searchInputTarget.value)\n clonedCustomItem.setAttribute('data-description', this.searchInputTarget.value)\n clonedCustomItem.setAttribute('data-value', this.searchInputTarget.value)\n clonedCustomItem.setAttribute('data-arrow-navigatable', '')\n\n this.show(clonedCustomItem)\n this.listTarget.appendChild(clonedCustomItem)\n }\n }).catch(() => this.removeSearchResults()).finally(() => {\n this.hide(this.notesTarget)\n this.show(this.listContainerTarget)\n this.show(this.listTarget)\n\n this.element.setAttribute('data-arrow-navigation-enabled-value', true)\n })\n }\n\n selectCity({ currentTarget }) {\n this.searchInputTarget.value = currentTarget.dataset.description\n this.hiddenInputTarget.value = currentTarget.dataset.value\n\n this.reset()\n\n this.focus(this.searchInputTarget, { moveCursorToEnd: true })\n }\n\n submit() {\n this.dispatch('submit')\n }\n\n // private\n\n removeSearchResults() {\n this.listTarget.innerHTML = ''\n this.hide(this.googleLogoTarget)\n }\n\n reset() {\n this.removeSearchResults()\n\n this.hide(this.listContainerTarget)\n this.show(this.notesTarget)\n this.hide(this.googleLogoTarget)\n }\n\n buildAddressElement(prediction, index) {\n const item = this.itemTemplateTarget.cloneNode(true)\n this.removeClass(item, 'hidden')\n\n item.innerText = [prediction.structured_formatting.main_text, this.countryCodeValue].join(', ')\n\n item.setAttribute('data-id', prediction.place_id)\n item.setAttribute('data-description', item.innerText)\n item.setAttribute('data-value', prediction.structured_formatting.main_text)\n item.setAttribute('data-action', 'click->property--address--city#selectCity:stop')\n item.setAttribute('tabindex', index + 1)\n item.setAttribute('data-arrow-navigatable', '')\n\n return item\n }\n\n selectCityFromAddress({ detail }) {\n this.hiddenInputTarget.value = detail.city\n this.searchInputTarget.value = `${detail.city}, ${this.countryCodeValue}`\n }\n}\n"],"names":["city_controller","ApplicationController","Loader","Current","google","currentTarget","input","response","element","clonedCustomItem","prediction","index","item","detail","__publicField"],"mappings":"sQAKe,MAAKA,UAASC,CAAsB,CAmBjD,YAAa,CACX,MAAM,WAAU,EAEhB,KAAK,OAAS,IAAIC,EAAO,CACvB,OAAQ,KAAK,YACb,QAAS,SACT,UAAW,CAAC,QAAQ,CAC1B,CAAK,EAED,KAAK,iBAAmBC,EAAQ,SAAS,QAAQ,KACjD,KAAK,oBAAsB,KAAK,oBAAoB,KAAK,IAAI,C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