|
| 1 | +<template> |
| 2 | + <div v-if="addressState"> |
| 3 | + <!-- Street address --> |
| 4 | + <opensilex-InputForm |
| 5 | + v-model:value="addressState.streetAddress" |
| 6 | + :label="t('AddressForm.streetAddress.label')" |
| 7 | + type="text" |
| 8 | + :placeholder="t('AddressForm.streetAddress.placeholder')" |
| 9 | + @input="onAddressInput" |
| 10 | + @blur="onAddressBlur" |
| 11 | + /> |
| 12 | + |
| 13 | + <!-- Autocomplete --> |
| 14 | + <div ref="autocompleteMenuRef" class="autocompleteMenu btn-group-vertical w-100"> |
| 15 | + <button |
| 16 | + v-for="address in autocompleteAddressList" |
| 17 | + :key="address.readableAddress" |
| 18 | + type="button" |
| 19 | + class="btn btn-light text-start js-autocompleteButton" |
| 20 | + @click.prevent.stop="onAddressAutocompleteClick(address)" |
| 21 | + > |
| 22 | + {{ address.readableAddress }} |
| 23 | + </button> |
| 24 | + </div> |
| 25 | + |
| 26 | + <!-- Locality --> |
| 27 | + <opensilex-InputForm |
| 28 | + v-model:value="addressState.locality" |
| 29 | + :label="t('AddressForm.locality.label')" |
| 30 | + type="text" |
| 31 | + :placeholder="t('AddressForm.locality.placeholder')" |
| 32 | + /> |
| 33 | + |
| 34 | + <div class="row"> |
| 35 | + <div class="col-12 col-lg-8"> |
| 36 | + <!-- Region --> |
| 37 | + <opensilex-InputForm |
| 38 | + v-model:value="addressState.region" |
| 39 | + :label="t('AddressForm.region.label')" |
| 40 | + type="text" |
| 41 | + :placeholder="t('AddressForm.region.placeholder')" |
| 42 | + /> |
| 43 | + </div> |
| 44 | + |
| 45 | + <div class="col-12 col-lg-4"> |
| 46 | + <!-- Postal code --> |
| 47 | + <opensilex-InputForm |
| 48 | + v-model:value="addressState.postalCode" |
| 49 | + :label="t('AddressForm.postalCode.label')" |
| 50 | + type="text" |
| 51 | + :placeholder="t('AddressForm.postalCode.placeholder')" |
| 52 | + /> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + |
| 56 | + <!-- Country --> |
| 57 | + <opensilex-InputForm |
| 58 | + v-model:value="addressState.countryName" |
| 59 | + :label="t('AddressForm.countryName.label')" |
| 60 | + type="text" |
| 61 | + :placeholder="t('AddressForm.countryName.placeholder')" |
| 62 | + /> |
| 63 | + </div> |
| 64 | +</template> |
| 65 | + |
| 66 | +<script setup lang="ts"> |
| 67 | +import { inject, onBeforeUnmount, onMounted, ref, watch } from 'vue' |
| 68 | +import { useI18n } from 'vue-i18n' |
| 69 | +import type OpenSilexVuePlugin from '@/models/OpenSilexVuePlugin' |
| 70 | +import type IGeocodingService from "../../../services/geocoding/IGeocodingService"; |
| 71 | +import type { GeocodingAddressResult } from "../../../services/geocoding/IGeocodingService"; |
| 72 | +import type { FacilityAddressDTO } from 'opensilex-core/index' |
| 73 | +
|
| 74 | +const { t } = useI18n() |
| 75 | +const AUTOCOMPLETE_TIMEOUT_MS = 250 |
| 76 | +
|
| 77 | +const props = defineProps<{ |
| 78 | + address: FacilityAddressDTO |
| 79 | +}>() |
| 80 | +
|
| 81 | +const emit = defineEmits<{ |
| 82 | + (e: 'update:address', v: FacilityAddressDTO): void |
| 83 | +}>() |
| 84 | +
|
| 85 | +const $opensilex = inject<OpenSilexVuePlugin>('$opensilex')! |
| 86 | +const geocodingService = $opensilex.getService<IGeocodingService>('IGeocodingService') |
| 87 | +
|
| 88 | +const addressState = ref<FacilityAddressDTO>(props.address) |
| 89 | +
|
| 90 | +watch( |
| 91 | + () => props.address, |
| 92 | + (v) => { |
| 93 | + addressState.value = v |
| 94 | + }, |
| 95 | + { deep: true } |
| 96 | +) |
| 97 | +
|
| 98 | +watch( |
| 99 | + addressState, |
| 100 | + (v) => emit('update:address', v), |
| 101 | + { deep: true } |
| 102 | +) |
| 103 | +
|
| 104 | +const autocompleteMenuRef = ref<HTMLElement | null>(null) |
| 105 | +const autocompleteAddressList = ref<GeocodingAddressResult[]>([]) |
| 106 | +let addressInputTimeout: ReturnType<typeof setTimeout> | undefined |
| 107 | +
|
| 108 | +function clearAutocompleteMenu() { |
| 109 | + autocompleteAddressList.value = [] |
| 110 | +} |
| 111 | +
|
| 112 | +function onDocumentClick(e: MouseEvent) { |
| 113 | + // Si click dans le menu autocomplete -> ne pas fermer |
| 114 | + const menu = autocompleteMenuRef.value |
| 115 | + if (menu && e.target && menu.contains(e.target as Node)) return |
| 116 | + clearAutocompleteMenu() |
| 117 | +} |
| 118 | +
|
| 119 | +onMounted(() => { |
| 120 | + document.addEventListener('click', onDocumentClick) |
| 121 | +}) |
| 122 | +
|
| 123 | +onBeforeUnmount(() => { |
| 124 | + document.removeEventListener('click', onDocumentClick) |
| 125 | + if (addressInputTimeout) clearTimeout(addressInputTimeout) |
| 126 | +}) |
| 127 | +
|
| 128 | +function onAddressBlur() { |
| 129 | + if (addressInputTimeout) clearTimeout(addressInputTimeout) |
| 130 | +} |
| 131 | +
|
| 132 | +function onAddressInput() { |
| 133 | + if (addressInputTimeout) clearTimeout(addressInputTimeout) |
| 134 | + addressInputTimeout = setTimeout(() => { |
| 135 | + onAddressInputPause(addressState.value?.streetAddress) |
| 136 | + }, AUTOCOMPLETE_TIMEOUT_MS) |
| 137 | +} |
| 138 | +
|
| 139 | +async function onAddressInputPause(partialAddress?: string) { |
| 140 | + if (!partialAddress || !partialAddress.trim()) { |
| 141 | + autocompleteAddressList.value = [] |
| 142 | + return |
| 143 | + } |
| 144 | + const lang = $opensilex.getLang() |
| 145 | + try { |
| 146 | + const addresses = await geocodingService.search(partialAddress, { lang }) |
| 147 | + autocompleteAddressList.value = addresses ?? [] |
| 148 | + } catch (e) { |
| 149 | + console.warn('Error while fetching address suggestions', e) |
| 150 | + } |
| 151 | +} |
| 152 | +
|
| 153 | +function onAddressAutocompleteClick(address: GeocodingAddressResult) { |
| 154 | + if (!addressState.value) return |
| 155 | +
|
| 156 | + addressState.value.countryName = address.country |
| 157 | + addressState.value.postalCode = address.postcode |
| 158 | + addressState.value.region = address.state ? address.state : address.county |
| 159 | + addressState.value.locality = address.city |
| 160 | +
|
| 161 | + let streetAddress = '' |
| 162 | + if (address.street && address.street.length > 0) { |
| 163 | + if (address.houseNumber && address.houseNumber.length > 0) { |
| 164 | + streetAddress += `${address.houseNumber}, ` |
| 165 | + } |
| 166 | + streetAddress += address.street |
| 167 | + } else if ((address as any).name) { |
| 168 | + streetAddress = (address as any).name |
| 169 | + } |
| 170 | +
|
| 171 | + addressState.value.streetAddress = streetAddress |
| 172 | + autocompleteAddressList.value = [] |
| 173 | +} |
| 174 | +</script> |
| 175 | + |
| 176 | +<style scoped lang="scss"> |
| 177 | +.autocompleteMenu { |
| 178 | + position: absolute; |
| 179 | + z-index: 1000; |
| 180 | + max-height: 240px; |
| 181 | + overflow: auto; |
| 182 | +} |
| 183 | +</style> |
| 184 | + |
| 185 | +<i18n> |
| 186 | +en: |
| 187 | + AddressForm: |
| 188 | + streetAddress: |
| 189 | + label: "Address" |
| 190 | + placeholder: "Number and street name (start typing to see suggestions...)" |
| 191 | + locality: |
| 192 | + label: "City" |
| 193 | + placeholder: "City" |
| 194 | + region: |
| 195 | + label: "Region" |
| 196 | + placeholder: "State or region" |
| 197 | + postalCode: |
| 198 | + label: "Postal code" |
| 199 | + placeholder: "Zip code" |
| 200 | + countryName: |
| 201 | + label: "Country" |
| 202 | + placeholder: "Country name" |
| 203 | +fr: |
| 204 | + AddressForm: |
| 205 | + streetAddress: |
| 206 | + label: "Adresse" |
| 207 | + placeholder: "Numéro et nom de rue (commencer à taper pour voir des suggestions...)" |
| 208 | + locality: |
| 209 | + label: "Ville" |
| 210 | + placeholder: "Ville" |
| 211 | + region: |
| 212 | + label: "Région" |
| 213 | + placeholder: "Région ou état" |
| 214 | + postalCode: |
| 215 | + label: "Code postal" |
| 216 | + placeholder: "Code postal" |
| 217 | + countryName: |
| 218 | + label: "Pays" |
| 219 | + placeholder: "Nom du pays" |
| 220 | +</i18n> |
0 commit comments