2020-10-25 00:31:38 +02:00
|
|
|
<template lang="pug">
|
2022-04-27 12:34:58 +02:00
|
|
|
v-row
|
|
|
|
v-col(cols=12 md=6)
|
|
|
|
v-combobox(ref='place'
|
|
|
|
:rules="[$validators.required('common.where')]"
|
|
|
|
:label="$t('common.where')"
|
|
|
|
:hint="$t('event.where_description')"
|
|
|
|
:prepend-icon='mdiMapMarker'
|
|
|
|
no-filter
|
2022-06-01 14:07:55 +02:00
|
|
|
:value='value.name'
|
|
|
|
hide-no-data
|
|
|
|
@input.native='search'
|
|
|
|
persistent-hint
|
|
|
|
:items="places"
|
2022-06-18 01:10:52 +02:00
|
|
|
@focus='search'
|
2022-04-27 12:34:58 +02:00
|
|
|
@change='selectPlace')
|
|
|
|
template(v-slot:item="{ item, attrs, on }")
|
|
|
|
v-list-item(v-bind='attrs' v-on='on')
|
2022-06-22 14:07:28 +02:00
|
|
|
v-list-item-content(two-line v-if='item.create')
|
|
|
|
v-list-item-title <v-icon color='primary' v-text='mdiPlus' :aria-label='$t("common.add")'></v-icon> {{$t('common.add')}} <strong>{{item.name}}</strong>
|
2022-04-27 12:34:58 +02:00
|
|
|
v-list-item-content(two-line v-else)
|
|
|
|
v-list-item-title(v-text='item.name')
|
|
|
|
v-list-item-subtitle(v-text='item.address')
|
2020-10-25 00:31:38 +02:00
|
|
|
|
2022-04-27 12:34:58 +02:00
|
|
|
v-col(cols=12 md=6)
|
|
|
|
v-text-field(ref='address'
|
|
|
|
:prepend-icon='mdiMap'
|
|
|
|
:disabled='disableAddress'
|
|
|
|
:rules="[ v => disableAddress ? true : $validators.required('common.address')(v)]"
|
|
|
|
:label="$t('common.address')"
|
|
|
|
@change="changeAddress"
|
|
|
|
:value="value.address")
|
2022-09-19 05:13:57 +02:00
|
|
|
v-col(cols=12 md=6 v-if='settings.allow_geolocation')
|
|
|
|
v-combobox.mr-4(ref='detailsView'
|
|
|
|
:prepend-icon='mdiMapSearch'
|
|
|
|
:disabled='disableDetails'
|
|
|
|
@input.native='searchCoordinates'
|
|
|
|
:label="$t('common.coordinates')"
|
|
|
|
:value='value.detailsView'
|
|
|
|
persistent-hint hide-no-data clearable no-filter
|
|
|
|
:loading='loading'
|
|
|
|
@change='selectDetails'
|
|
|
|
@focus='searchCoordinates'
|
|
|
|
:items="detailsList"
|
|
|
|
:hint="$t('event.coordinates_description')")
|
|
|
|
template(v-slot:item="{ item, attrs, on }")
|
|
|
|
v-list-item(v-bind='attrs' v-on='on')
|
|
|
|
v-list-item-content(two-line v-if='item')
|
|
|
|
v-list-item-title(v-text='item.display_name')
|
|
|
|
v-list-item-subtitle(v-text='`${item.lat}`+`,`+`${item.lon}`')
|
|
|
|
v-col(cols=12 md=3 v-if='settings.allow_geolocation')
|
|
|
|
v-text-field(ref='latitude' :value='value.latitude'
|
|
|
|
:prepend-icon='mdiLatitude'
|
|
|
|
:disabled='disableDetails'
|
|
|
|
:label="$t('common.latitude')" )
|
|
|
|
v-col(cols=12 md=3 v-if='settings.allow_geolocation')
|
|
|
|
v-text-field(ref='longitude' :value='value.longitude'
|
|
|
|
:prepend-icon='mdiLongitude'
|
|
|
|
:disabled='disableDetails'
|
|
|
|
:label="$t('common.longitude')")
|
2020-10-25 00:31:38 +02:00
|
|
|
|
|
|
|
</template>
|
|
|
|
<script>
|
2022-09-19 05:13:57 +02:00
|
|
|
import { mdiMap, mdiMapMarker, mdiPlus, mdiMapSearch, mdiLatitude, mdiLongitude } from '@mdi/js'
|
2022-06-01 14:07:55 +02:00
|
|
|
import debounce from 'lodash/debounce'
|
2022-09-02 08:32:13 +02:00
|
|
|
import { mapState } from 'vuex'
|
2020-10-25 00:31:38 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'WhereInput',
|
2020-10-27 11:56:47 +01:00
|
|
|
props: {
|
2022-06-01 14:07:55 +02:00
|
|
|
value: { type: Object, default: () => ({}) }
|
2020-10-27 11:56:47 +01:00
|
|
|
},
|
2020-10-25 00:31:38 +02:00
|
|
|
data () {
|
|
|
|
return {
|
2022-09-19 05:13:57 +02:00
|
|
|
mdiMap, mdiMapMarker, mdiPlus, mdiMapSearch, mdiLatitude, mdiLongitude,
|
2021-01-21 01:11:08 +01:00
|
|
|
place: { },
|
|
|
|
placeName: '',
|
2022-06-01 14:07:55 +02:00
|
|
|
places: [],
|
2022-09-02 08:32:13 +02:00
|
|
|
disableAddress: true,
|
|
|
|
details: { },
|
|
|
|
detailsView: '',
|
|
|
|
detailsList: [],
|
|
|
|
disableDetails: true,
|
|
|
|
loading: false
|
2020-10-25 00:31:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-09-02 08:32:13 +02:00
|
|
|
...mapState(['settings']),
|
2021-01-21 01:11:08 +01:00
|
|
|
filteredPlaces () {
|
|
|
|
if (!this.placeName) { return this.places }
|
2022-05-03 18:33:20 +02:00
|
|
|
const placeName = this.placeName.trim().toLowerCase()
|
2021-01-21 01:11:08 +01:00
|
|
|
let nameMatch = false
|
|
|
|
const matches = this.places.filter(p => {
|
|
|
|
const tmpName = p.name.toLowerCase()
|
|
|
|
const tmpAddress = p.address.toLowerCase()
|
|
|
|
if (tmpName.includes(placeName)) {
|
|
|
|
if (tmpName === placeName) { nameMatch = true }
|
|
|
|
return true
|
|
|
|
}
|
2022-04-27 12:34:58 +02:00
|
|
|
return tmpAddress.includes(placeName)
|
2021-01-21 01:11:08 +01:00
|
|
|
})
|
|
|
|
if (!nameMatch) {
|
|
|
|
matches.unshift({ create: true, name: this.placeName })
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
2020-10-25 00:31:38 +02:00
|
|
|
},
|
|
|
|
methods: {
|
2022-06-01 14:07:55 +02:00
|
|
|
search: debounce(async function(ev) {
|
|
|
|
const search = ev.target.value.trim().toLowerCase()
|
|
|
|
this.places = await this.$axios.$get(`place?search=${search}`)
|
2022-06-18 01:10:52 +02:00
|
|
|
if (!search && this.places.length) { return this.places }
|
2022-06-01 14:07:55 +02:00
|
|
|
const matches = this.places.find(p => search === p.name.toLocaleLowerCase())
|
2022-06-22 14:07:28 +02:00
|
|
|
if (!matches && search) {
|
2022-06-01 14:07:55 +02:00
|
|
|
this.places.unshift({ create: true, name: ev.target.value.trim() })
|
|
|
|
}
|
|
|
|
}, 100),
|
2020-10-25 00:31:38 +02:00
|
|
|
selectPlace (p) {
|
2021-01-21 01:11:08 +01:00
|
|
|
if (!p) { return }
|
|
|
|
if (typeof p === 'object' && !p.create) {
|
2022-05-03 18:33:20 +02:00
|
|
|
this.place.name = p.name.trim()
|
2020-11-04 10:55:30 +01:00
|
|
|
this.place.address = p.address
|
2022-09-08 20:37:20 +02:00
|
|
|
if (this.settings.allow_geolocation) {
|
2022-09-02 08:32:13 +02:00
|
|
|
this.place.details = p.details
|
2022-09-19 05:13:57 +02:00
|
|
|
this.place.latitude = p.latitude
|
|
|
|
this.place.longitude = p.longitude
|
2022-09-02 08:32:13 +02:00
|
|
|
}
|
2022-05-06 23:02:43 +02:00
|
|
|
this.place.id = p.id
|
2020-10-25 00:31:38 +02:00
|
|
|
this.disableAddress = true
|
2020-11-04 10:55:30 +01:00
|
|
|
} else { // this is a new place
|
2021-01-21 01:11:08 +01:00
|
|
|
this.place.name = p.name || p
|
2022-06-01 14:07:55 +02:00
|
|
|
const tmpPlace = this.place.name.trim().toLocaleLowerCase()
|
2021-06-07 00:00:31 +02:00
|
|
|
// search for a place with the same name
|
2022-06-01 14:07:55 +02:00
|
|
|
const place = this.places.find(p => !p.create && p.name.trim().toLocaleLowerCase() === tmpPlace)
|
2021-06-07 00:00:31 +02:00
|
|
|
if (place) {
|
2022-05-03 18:33:20 +02:00
|
|
|
this.place.name = place.name
|
2022-05-06 23:02:43 +02:00
|
|
|
this.place.id = place.id
|
2021-06-07 00:00:31 +02:00
|
|
|
this.place.address = place.address
|
|
|
|
this.disableAddress = true
|
|
|
|
} else {
|
2022-05-06 23:02:43 +02:00
|
|
|
delete this.place.id
|
2021-06-07 00:00:31 +02:00
|
|
|
this.place.address = ''
|
2022-09-08 20:37:20 +02:00
|
|
|
if (this.settings.allow_geolocation) {
|
2022-09-02 08:32:13 +02:00
|
|
|
this.place.details = p.details
|
2022-09-19 05:13:57 +02:00
|
|
|
this.place.latitude = p.latitude
|
|
|
|
this.place.longitude = p.longitude
|
2022-09-02 08:32:13 +02:00
|
|
|
}
|
2021-06-07 00:00:31 +02:00
|
|
|
this.disableAddress = false
|
|
|
|
this.$refs.place.blur()
|
|
|
|
this.$refs.address.focus()
|
|
|
|
}
|
2020-10-25 00:31:38 +02:00
|
|
|
}
|
2020-11-04 10:55:30 +01:00
|
|
|
this.$emit('input', { ...this.place })
|
2020-10-25 00:31:38 +02:00
|
|
|
},
|
|
|
|
changeAddress (v) {
|
|
|
|
this.place.address = v
|
2020-11-04 10:55:30 +01:00
|
|
|
this.$emit('input', { ...this.place })
|
2022-09-02 08:32:13 +02:00
|
|
|
this.disableDetails = false
|
|
|
|
},
|
|
|
|
selectDetails (v) {
|
|
|
|
if (!v) { return }
|
|
|
|
if (typeof v === 'object') {
|
|
|
|
this.place.detailsView = v.lat+', '+v.lon
|
|
|
|
let c = {}
|
|
|
|
c.lat = v.lat
|
|
|
|
c.lon = v.lon
|
|
|
|
if (typeof c === 'object') {
|
|
|
|
this.place.details = JSON.stringify(c)
|
2022-09-19 05:13:57 +02:00
|
|
|
this.place.latitude = v.lat
|
|
|
|
this.place.longitude = v.lon
|
2022-09-02 08:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.$emit('input', { ...this.place })
|
|
|
|
},
|
|
|
|
searchCoordinates: debounce(async function(ev) {
|
2022-09-08 20:37:20 +02:00
|
|
|
const pre_searchCoordinates = ev.target.value.trim().toLowerCase()
|
2022-10-28 08:27:47 +02:00
|
|
|
// allow pasting coordinates lat/lon and lat,lon
|
2022-09-08 20:37:20 +02:00
|
|
|
const searchCoordinates = pre_searchCoordinates.replace('/', ',')
|
2022-09-19 05:13:57 +02:00
|
|
|
var regex_coords_comma = "-?[1-9][0-9]*(\\.[0-9]+)?,\\s*-?[1-9][0-9]*(\\.[0-9]+)?";
|
|
|
|
var regex_coords_slash = "-?[1-9][0-9]*(\\.[0-9]+)?/\\s*-?[1-9][0-9]*(\\.[0-9]+)?";
|
|
|
|
|
|
|
|
const setCoords = (v) => {
|
2022-10-28 08:27:47 +02:00
|
|
|
const lat = v[0].trim()
|
|
|
|
const lon = v[1].trim()
|
|
|
|
// check coordinates are valid
|
|
|
|
if ((lat < 90 && lat > -90)
|
|
|
|
&& (lon < 180 && lon > -180)) {
|
|
|
|
this.place.latitude = lat
|
|
|
|
this.place.longitude = lon
|
|
|
|
} else {
|
|
|
|
this.$root.$message("Non existent coordinates", { color: 'error' })
|
|
|
|
return
|
2022-09-19 05:13:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pre_searchCoordinates.match(regex_coords_comma)) {
|
|
|
|
let v = pre_searchCoordinates.split(",")
|
|
|
|
setCoords(v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (pre_searchCoordinates.match(regex_coords_slash)) {
|
|
|
|
let v = pre_searchCoordinates.split("/")
|
|
|
|
setCoords(v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:37:20 +02:00
|
|
|
if (searchCoordinates.length) {
|
2022-10-28 08:27:47 +02:00
|
|
|
this.loading = true
|
2022-09-08 20:37:20 +02:00
|
|
|
this.detailsList = await this.$axios.$get(`placeNominatim/${searchCoordinates}`)
|
2022-10-28 08:27:47 +02:00
|
|
|
this.loading = false
|
2022-09-02 08:32:13 +02:00
|
|
|
}
|
|
|
|
}, 300),
|
2020-10-25 00:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|