|
| 1 | +import "src/decidim/geocoding" |
| 2 | +import formatAddress from "src/decidim/geocoding/format_address" |
| 3 | + |
| 4 | +/** |
| 5 | + * For the available address format keys, refer to: |
| 6 | + * https://developer.here.com/documentation/geocoder-autocomplete/dev_guide/topics/resource-type-response-suggest.html |
| 7 | + */ |
| 8 | +$(() => { |
| 9 | + const generateAddressLabel = formatAddress; |
| 10 | + |
| 11 | + $("[data-decidim-geocoding]").each((_i, el) => { |
| 12 | + const $input = $(el); |
| 13 | + const config = $input.data("decidim-geocoding"); |
| 14 | + const queryMinLength = config.queryMinLength || 2; |
| 15 | + const addressFormat = config.addressFormat || [ |
| 16 | + ["street", "houseNumber"], |
| 17 | + "district", |
| 18 | + "city", |
| 19 | + "county", |
| 20 | + "state", |
| 21 | + "country" |
| 22 | + ]; |
| 23 | + const language = $("html").attr("lang"); |
| 24 | + let currentSuggestionQuery = null; |
| 25 | + |
| 26 | + if (!config.apiKey || config.apiKey.length < 1) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + $input.on("geocoder-suggest.decidim", (_ev, query, callback) => { |
| 31 | + clearTimeout(currentSuggestionQuery); |
| 32 | + |
| 33 | + // Do not trigger API calls on short queries |
| 34 | + if (`${query}`.trim().length < queryMinLength) { |
| 35 | + return; |
| 36 | + } |
| 37 | + // Changes to the autocomplete api call based on: |
| 38 | + // https://developer.here.com/documentation/geocoding-search-api/migration_guide/migration-geocoder/topics-api/autocomplete.html |
| 39 | + currentSuggestionQuery = setTimeout(() => { |
| 40 | + $.ajax({ |
| 41 | + method: "GET", |
| 42 | + url: "https://autocomplete.search.hereapi.com/v1/autocomplete", |
| 43 | + data: { |
| 44 | + apiKey: config.apiKey, |
| 45 | + // eslint-disable-next-line |
| 46 | + q: query, |
| 47 | + lang: language |
| 48 | + }, |
| 49 | + dataType: "json" |
| 50 | + }).done((resp) => { |
| 51 | + if (resp.items) { |
| 52 | + return callback(resp.items.map((item) => { |
| 53 | + const label = generateAddressLabel(item.address, addressFormat); |
| 54 | + |
| 55 | + return { |
| 56 | + key: label, |
| 57 | + value: label, |
| 58 | + locationId: item.id |
| 59 | + } |
| 60 | + })); |
| 61 | + } |
| 62 | + return null; |
| 63 | + }); |
| 64 | + }, 200); |
| 65 | + }); |
| 66 | + |
| 67 | + $input.on("geocoder-suggest-select.decidim", (_ev, selectedItem) => { |
| 68 | + $.ajax({ |
| 69 | + method: "GET", |
| 70 | + url: "https://lookup.search.hereapi.com/v1/lookup", |
| 71 | + data: { |
| 72 | + apiKey: config.apiKey, |
| 73 | + id: selectedItem.locationId |
| 74 | + }, |
| 75 | + dataType: "json" |
| 76 | + }).done((resp) => { |
| 77 | + if (!resp || Object.keys(resp).length < 1 |
| 78 | + ) { |
| 79 | + return; |
| 80 | + } |
| 81 | + const position = resp.position; |
| 82 | + if (!position || !position.lat || !position.lng) { |
| 83 | + return |
| 84 | + } |
| 85 | + const coordinates = [ |
| 86 | + position.lat, |
| 87 | + position.lng |
| 88 | + ]; |
| 89 | + |
| 90 | + $input.trigger( |
| 91 | + "geocoder-suggest-coordinates.decidim", |
| 92 | + [coordinates] |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + }) |
| 97 | +}) |
0 commit comments