[]
- {{ item.properties.formatted }}
{ let result = []; for (let group in suggestions) { const newGroupName = store.groupNames[group] || group; result = […result, …suggestions[group].map((item) => ({ group: newGroupName, …item }))]; } return result; }; const fetchPlaces = lodash.debounce(async () => { store.places.loading = true; try { const response = await fetch(`https://api.geoapify.com/v1/geocode/autocomplete?text=${store.searchFields.place.address}&lang=en&filter=countrycode:us,ca&apiKey=d457da33b2404eac8594eefbf52d21c9`); const data = await response.json(); console.log(“data: “, data) if (data.features) { store.places.suggestions = data.features; } else { store.places.suggestions = []; } } catch (error) { console.error(error); } finally { store.places.loading = false; } }, 600); const updateSuggestions = lodash.debounce(async () => { if (store.searchFields.query.length >= 2) { store.find.loading = true; try { const response = await fetch(`/search/prefetch.php?prefix=${encodeURIComponent(store.searchFields.query)}${store.searchFields.place.address ? ‘&place=’ + encodeURIComponent(store.searchFields.place.address) : ”}`, { cors: ‘no-cors’ }); const data = await response.json(); store.find.suggestions = transformSuggestions(data, store.groupNames); } catch (error) { console.error(error); } finally { store.find.loading = false; } } else { store.find.suggestions = []; } }, 600); const fetchGeocodedData = async (place) => { const response = await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(place)}&limit=1`); const data = await response.json(); return data; }; function App() { return { $template: ‘#search-template’, store, setQuery(query) { this.store.searchFields.query = query; updateSuggestions(); }, setPlace(place) { this.store.searchFields.place.address = place; fetchPlaces(); }, search() { alert(`Search: ${this.store.searchFields.query} in ${this.store.searchFields.place}`); }, get groupedSuggestions() { return this.store.find.suggestions.reduce((acc, item) => { if (!acc[item.group]) acc[item.group] = []; acc[item.group].push(item); return acc; }, {}); }, selectSuggestion(query) { this.store.searchFields.query = query; this.store.find.showSuggestions = false; }, selectPlaceSuggestion(place) { this.store.searchFields.place.address = place; this.store.places.showSuggestions = false; }, handleMyLocationClick() { navigator.geolocation.getCurrentPosition( async function(position) { const { latitude, longitude } = position.coords; const response = await fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}`); const data = await response.json(); store.searchFields.place.address = data.display_name; store.searchFields.place.isExactAddress = true; }, function(error) { console.log(error); } ); }, async handleSearch() { const adressData = this.store.searchFields.place.address || window?.search?.presetPlace || ” const query = this.store.searchFields.query || ”; const geocodedData = await fetchGeocodedData(adressData); const isCity = geocodedData.length > 0 && (geocodedData[0].type === “administrative” || geocodedData[0].type === “residential”) ? false : true; window.location.href = `/search/?query=${query}&place=${adressData}${isCity ? ” : ‘&sort_by=rating’}`; }, closeSuggestions() { this.store.find.showSuggestions = false; this.store.places.showSuggestions = false; }, mounted(el, ctx) { console.log(‘mounted’); console.log(‘fieldGroup: ‘, el); // Добавляем обработчик клика на документ document.addEventListener(‘click’, (event) => { if (!el.contains(event.target)) { this.store[ctx].showSuggestions = false; } }); } } } createApp({ App }).mount() ]]>