2019-07-30 10:35:29 +02:00
|
|
|
<template>
|
2020-09-25 16:23:53 +02:00
|
|
|
<div class="address-autocomplete">
|
|
|
|
<b-field expanded>
|
|
|
|
<b-autocomplete
|
|
|
|
:data="addressData"
|
|
|
|
v-model="queryText"
|
|
|
|
:placeholder="$t('e.g. 10 Rue Jangot')"
|
|
|
|
field="fullName"
|
|
|
|
:loading="isFetching"
|
|
|
|
@typing="fetchAsyncData"
|
|
|
|
icon="map-marker"
|
|
|
|
expanded
|
|
|
|
@select="updateSelected"
|
|
|
|
>
|
2021-04-16 18:13:17 +02:00
|
|
|
<template #default="{ option }">
|
2020-09-25 16:23:53 +02:00
|
|
|
<b-icon :icon="option.poiInfos.poiIcon.icon" />
|
|
|
|
<b>{{ option.poiInfos.name }}</b
|
|
|
|
><br />
|
|
|
|
<small>{{ option.poiInfos.alternativeName }}</small>
|
|
|
|
</template>
|
|
|
|
</b-autocomplete>
|
|
|
|
</b-field>
|
2021-03-02 10:02:06 +01:00
|
|
|
<b-field v-if="canDoGeoLocation">
|
2020-11-30 10:24:11 +01:00
|
|
|
<b-button
|
|
|
|
type="is-text"
|
|
|
|
v-if="!gettingLocation"
|
|
|
|
icon-right="target"
|
|
|
|
@click="locateMe"
|
|
|
|
>{{ $t("Use my location") }}</b-button
|
|
|
|
>
|
2020-09-25 16:23:53 +02:00
|
|
|
<span v-else>{{ $t("Getting location") }}</span>
|
|
|
|
</b-field>
|
|
|
|
<!--
|
|
|
|
<div v-if="selected && selected.geom" class="control">
|
|
|
|
<b-checkbox @input="togglemap" />
|
|
|
|
<label class="label">{{ $t("Show map") }}</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="map" v-if="showmap && selected && selected.geom">
|
|
|
|
<map-leaflet
|
|
|
|
:coords="selected.geom"
|
|
|
|
:marker="{
|
|
|
|
text: [selected.poiInfos.name, selected.poiInfos.alternativeName],
|
|
|
|
icon: selected.poiInfos.poiIcon.icon,
|
|
|
|
}"
|
|
|
|
:updateDraggableMarkerCallback="reverseGeoCode"
|
|
|
|
:options="{ zoom: mapDefaultZoom }"
|
|
|
|
:readOnly="false"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
-->
|
|
|
|
</div>
|
2019-07-30 10:35:29 +02:00
|
|
|
</template>
|
|
|
|
<script lang="ts">
|
2020-02-18 08:57:00 +01:00
|
|
|
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
|
|
|
|
import { LatLng } from "leaflet";
|
2021-05-12 18:13:39 +02:00
|
|
|
import debounce from "lodash/debounce";
|
|
|
|
import { DebouncedFunc } from "lodash";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { Address, IAddress } from "../../types/address.model";
|
|
|
|
import { ADDRESS, REVERSE_GEOCODE } from "../../graphql/address";
|
|
|
|
import { CONFIG } from "../../graphql/config";
|
|
|
|
import { IConfig } from "../../types/config.model";
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2019-08-22 15:57:44 +02:00
|
|
|
@Component({
|
2020-09-25 16:23:53 +02:00
|
|
|
components: {
|
2020-11-30 10:24:11 +01:00
|
|
|
"map-leaflet": () =>
|
|
|
|
import(/* webpackChunkName: "map" */ "@/components/Map.vue"),
|
2020-09-25 16:23:53 +02:00
|
|
|
},
|
2019-11-20 13:49:57 +01:00
|
|
|
apollo: {
|
|
|
|
config: CONFIG,
|
|
|
|
},
|
2019-08-22 15:57:44 +02:00
|
|
|
})
|
2019-07-30 10:35:29 +02:00
|
|
|
export default class AddressAutoComplete extends Vue {
|
2019-11-08 19:37:14 +01:00
|
|
|
@Prop({ required: true }) value!: IAddress;
|
2021-02-12 18:19:49 +01:00
|
|
|
@Prop({ required: false, default: false }) type!: string | false;
|
2021-03-02 10:02:06 +01:00
|
|
|
@Prop({ required: false, default: true, type: Boolean })
|
|
|
|
doGeoLocation!: boolean;
|
2020-08-31 12:40:30 +02:00
|
|
|
|
2019-11-20 13:49:57 +01:00
|
|
|
addressData: IAddress[] = [];
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2019-11-20 13:49:57 +01:00
|
|
|
selected: IAddress = new Address();
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
isFetching = false;
|
|
|
|
|
2021-03-05 17:19:42 +01:00
|
|
|
initialQueryText = "";
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-09-25 16:23:53 +02:00
|
|
|
addressModalActive = false;
|
|
|
|
|
|
|
|
showmap = false;
|
|
|
|
|
|
|
|
private gettingLocation = false;
|
|
|
|
|
2020-12-14 10:21:04 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
2020-11-27 19:27:44 +01:00
|
|
|
private location!: GeolocationPosition;
|
2020-09-25 16:23:53 +02:00
|
|
|
|
|
|
|
private gettingLocationError: any;
|
|
|
|
|
|
|
|
private mapDefaultZoom = 15;
|
|
|
|
|
2019-11-20 13:49:57 +01:00
|
|
|
config!: IConfig;
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
fetchAsyncData!: DebouncedFunc<(query: string) => Promise<void>>;
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
// We put this in data because of issues like
|
|
|
|
// https://github.com/vuejs/vue-class-component/issues/263
|
2020-09-25 16:23:53 +02:00
|
|
|
data(): Record<string, unknown> {
|
2019-11-20 13:49:57 +01:00
|
|
|
return {
|
2019-12-20 11:39:30 +01:00
|
|
|
fetchAsyncData: debounce(this.asyncData, 200),
|
2019-11-20 13:49:57 +01:00
|
|
|
};
|
2019-11-08 19:37:14 +01:00
|
|
|
}
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2020-09-25 16:23:53 +02:00
|
|
|
async asyncData(query: string): Promise<void> {
|
2019-11-08 19:37:14 +01:00
|
|
|
if (!query.length) {
|
2019-11-20 13:49:57 +01:00
|
|
|
this.addressData = [];
|
2019-11-08 19:37:14 +01:00
|
|
|
this.selected = new Address();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.length < 3) {
|
2019-11-20 13:49:57 +01:00
|
|
|
this.addressData = [];
|
2019-07-30 10:35:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 13:49:57 +01:00
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
this.isFetching = true;
|
2021-02-12 18:19:49 +01:00
|
|
|
const variables: { query: string; locale: string; type?: string } = {
|
|
|
|
query,
|
|
|
|
locale: this.$i18n.locale,
|
|
|
|
};
|
|
|
|
if (this.type) {
|
|
|
|
variables.type = this.type;
|
|
|
|
}
|
2019-07-30 10:35:29 +02:00
|
|
|
const result = await this.$apollo.query({
|
|
|
|
query: ADDRESS,
|
2020-02-18 08:57:00 +01:00
|
|
|
fetchPolicy: "network-only",
|
2021-02-12 18:19:49 +01:00
|
|
|
variables,
|
2019-07-30 10:35:29 +02:00
|
|
|
});
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
this.addressData = result.data.searchAddress.map(
|
|
|
|
(address: IAddress) => new Address(address)
|
|
|
|
);
|
2019-08-22 15:57:44 +02:00
|
|
|
this.isFetching = false;
|
2019-07-30 10:35:29 +02:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Watch("config")
|
2020-09-25 16:23:53 +02:00
|
|
|
watchConfig(config: IConfig): void {
|
2019-11-20 13:49:57 +01:00
|
|
|
if (!config.geocoding.autocomplete) {
|
2020-02-18 08:57:00 +01:00
|
|
|
// If autocomplete is disabled, we put a larger debounce value
|
|
|
|
// so that we don't request with incomplete address
|
2019-11-20 13:49:57 +01:00
|
|
|
this.fetchAsyncData = debounce(this.asyncData, 2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 17:19:42 +01:00
|
|
|
get queryText(): string {
|
2021-04-16 17:18:41 +02:00
|
|
|
if (this.value !== undefined) {
|
2021-03-05 17:19:42 +01:00
|
|
|
return new Address(this.value).fullName;
|
|
|
|
}
|
|
|
|
return this.initialQueryText;
|
|
|
|
}
|
|
|
|
|
|
|
|
set queryText(queryText: string) {
|
|
|
|
this.initialQueryText = queryText;
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Watch("value")
|
2020-09-25 16:23:53 +02:00
|
|
|
updateEditing(): void {
|
2021-02-12 18:19:49 +01:00
|
|
|
if (!this.value?.id) return;
|
2019-11-20 13:49:57 +01:00
|
|
|
this.selected = this.value;
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:23:53 +02:00
|
|
|
updateSelected(option: IAddress): void {
|
2019-11-08 19:37:14 +01:00
|
|
|
if (option == null) return;
|
|
|
|
this.selected = option;
|
2020-02-18 08:57:00 +01:00
|
|
|
this.$emit("input", this.selected);
|
2019-07-30 10:35:29 +02:00
|
|
|
}
|
2020-09-25 16:23:53 +02:00
|
|
|
|
|
|
|
resetPopup(): void {
|
|
|
|
this.selected = new Address();
|
|
|
|
}
|
|
|
|
|
|
|
|
openNewAddressModal(): void {
|
|
|
|
this.resetPopup();
|
|
|
|
this.addressModalActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
togglemap(): void {
|
|
|
|
this.showmap = !this.showmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
async reverseGeoCode(e: LatLng, zoom: number): Promise<void> {
|
|
|
|
// If the position has been updated through autocomplete selection, no need to geocode it!
|
|
|
|
if (this.checkCurrentPosition(e)) return;
|
|
|
|
const result = await this.$apollo.query({
|
|
|
|
query: REVERSE_GEOCODE,
|
|
|
|
variables: {
|
|
|
|
latitude: e.lat,
|
|
|
|
longitude: e.lng,
|
|
|
|
zoom,
|
|
|
|
locale: this.$i18n.locale,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
this.addressData = result.data.reverseGeocode.map(
|
|
|
|
(address: IAddress) => new Address(address)
|
|
|
|
);
|
2020-10-15 16:48:11 +02:00
|
|
|
if (this.addressData.length > 0) {
|
|
|
|
const defaultAddress = new Address(this.addressData[0]);
|
|
|
|
this.selected = defaultAddress;
|
|
|
|
this.$emit("input", this.selected);
|
|
|
|
this.queryText = `${defaultAddress.poiInfos.name} ${defaultAddress.poiInfos.alternativeName}`;
|
|
|
|
}
|
2020-09-25 16:23:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
checkCurrentPosition(e: LatLng): boolean {
|
|
|
|
if (!this.selected || !this.selected.geom) return false;
|
|
|
|
const lat = parseFloat(this.selected.geom.split(";")[1]);
|
|
|
|
const lon = parseFloat(this.selected.geom.split(";")[0]);
|
|
|
|
|
|
|
|
return e.lat === lat && e.lng === lon;
|
|
|
|
}
|
|
|
|
|
|
|
|
async locateMe(): Promise<void> {
|
|
|
|
this.gettingLocation = true;
|
|
|
|
try {
|
|
|
|
this.location = await AddressAutoComplete.getLocation();
|
|
|
|
this.mapDefaultZoom = 12;
|
|
|
|
this.reverseGeoCode(
|
2020-11-30 10:24:11 +01:00
|
|
|
new LatLng(
|
|
|
|
this.location.coords.latitude,
|
|
|
|
this.location.coords.longitude
|
|
|
|
),
|
2020-09-25 16:23:53 +02:00
|
|
|
12
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.gettingLocationError = e.message;
|
|
|
|
}
|
|
|
|
this.gettingLocation = false;
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:21:04 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
2020-11-27 19:27:44 +01:00
|
|
|
static async getLocation(): Promise<GeolocationPosition> {
|
2020-09-25 16:23:53 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!("geolocation" in navigator)) {
|
|
|
|
reject(new Error("Geolocation is not available."));
|
|
|
|
}
|
|
|
|
|
|
|
|
navigator.geolocation.getCurrentPosition(
|
|
|
|
(pos) => {
|
|
|
|
resolve(pos);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2021-03-02 10:02:06 +01:00
|
|
|
get isSecureContext(): boolean {
|
2020-09-25 16:23:53 +02:00
|
|
|
return window.isSecureContext;
|
|
|
|
}
|
2021-03-02 10:02:06 +01:00
|
|
|
|
|
|
|
get canDoGeoLocation(): boolean {
|
|
|
|
return this.isSecureContext && this.doGeoLocation;
|
|
|
|
}
|
2019-07-30 10:35:29 +02:00
|
|
|
}
|
|
|
|
</script>
|
2019-08-22 15:57:44 +02:00
|
|
|
<style lang="scss">
|
2020-02-18 08:57:00 +01:00
|
|
|
.address-autocomplete {
|
|
|
|
margin-bottom: 0.75rem;
|
|
|
|
}
|
2019-12-20 13:04:34 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.autocomplete {
|
|
|
|
.dropdown-menu {
|
|
|
|
z-index: 2000;
|
|
|
|
}
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.dropdown-item.is-disabled {
|
|
|
|
opacity: 1 !important;
|
|
|
|
cursor: auto;
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.read-only {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.map {
|
|
|
|
height: 400px;
|
|
|
|
width: 100%;
|
|
|
|
}
|
2019-08-22 15:57:44 +02:00
|
|
|
</style>
|