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"
|
|
|
|
>
|
|
|
|
<template slot-scope="{ option }">
|
|
|
|
<b-icon :icon="option.poiInfos.poiIcon.icon" />
|
|
|
|
<b>{{ option.poiInfos.name }}</b
|
|
|
|
><br />
|
|
|
|
<small>{{ option.poiInfos.alternativeName }}</small>
|
|
|
|
</template>
|
|
|
|
</b-autocomplete>
|
|
|
|
</b-field>
|
|
|
|
<b-field v-if="isSecureContext()">
|
|
|
|
<b-button type="is-text" v-if="!gettingLocation" icon-right="target" @click="locateMe">{{
|
|
|
|
$t("Use my location")
|
|
|
|
}}</b-button>
|
|
|
|
<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";
|
2020-11-27 19:27:44 +01:00
|
|
|
import { debounce, 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: {
|
|
|
|
"map-leaflet": () => import(/* webpackChunkName: "map" */ "@/components/Map.vue"),
|
|
|
|
},
|
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;
|
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;
|
|
|
|
|
|
|
|
queryText: string = (this.value && new Address(this.value).fullName) || "";
|
|
|
|
|
2020-09-25 16:23:53 +02:00
|
|
|
addressModalActive = false;
|
|
|
|
|
|
|
|
showmap = false;
|
|
|
|
|
|
|
|
private gettingLocation = false;
|
|
|
|
|
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;
|
|
|
|
const result = await this.$apollo.query({
|
|
|
|
query: ADDRESS,
|
2020-02-18 08:57:00 +01:00
|
|
|
fetchPolicy: "network-only",
|
2019-11-08 19:37:14 +01:00
|
|
|
variables: {
|
|
|
|
query,
|
|
|
|
locale: this.$i18n.locale,
|
|
|
|
},
|
2019-07-30 10:35:29 +02:00
|
|
|
});
|
|
|
|
|
2020-02-18 08:57:00 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Watch("value")
|
2020-09-25 16:23:53 +02:00
|
|
|
updateEditing(): void {
|
2019-12-03 11:29:51 +01:00
|
|
|
if (!(this.value && this.value.id)) return;
|
2019-11-20 13:49:57 +01:00
|
|
|
this.selected = this.value;
|
|
|
|
const address = new Address(this.selected);
|
|
|
|
this.queryText = `${address.poiInfos.name} ${address.poiInfos.alternativeName}`;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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(
|
|
|
|
new LatLng(this.location.coords.latitude, this.location.coords.longitude),
|
|
|
|
12
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.gettingLocationError = e.message;
|
|
|
|
}
|
|
|
|
this.gettingLocation = false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
isSecureContext(): boolean {
|
|
|
|
return window.isSecureContext;
|
|
|
|
}
|
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>
|