gancio/components/WhereInput.vue

243 lines
8.4 KiB
Vue
Raw Normal View History

2020-10-25 00:31:38 +02:00
<template lang="pug">
v-row.mb-4
2022-04-27 12:34:58 +02:00
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
hide-no-data
@input.native='search'
persistent-hint
:value='value.name'
: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
//- 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-04-27 12:34:58 +02:00
v-col(cols=12 md=6)
2022-11-18 14:13:59 +01:00
v-combobox(ref='address'
:prepend-icon='mdiMapSearch'
2022-04-27 12:34:58 +02:00
:disabled='disableAddress'
2022-11-18 14:13:59 +01:00
@input.native='searchAddress'
:label="$t('common.address')"
:rules="[ v => disableAddress ? true : $validators.required('common.address')(v)]"
:value='value.address'
persistent-hint hide-no-data clearable no-filter
:loading='loading'
2022-11-18 14:13:59 +01:00
@change='selectAddress'
@focus='searchAddress'
:items="addressList"
:hint="$t('event.address_description')")
template(v-slot:item="{ item, attrs, on }")
v-list-item(v-bind='attrs' v-on='on')
2022-11-18 14:33:38 +01:00
v-icon.pr-4(v-text='loadCoordinatesResultIcon(item)')
v-list-item-content(two-line v-if='item')
v-list-item-title(v-text='item.name')
v-list-item-subtitle(v-text='`${item.address}`')
//- 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-11-18 14:33:38 +01:00
import { mdiMap, mdiMapMarker, mdiPlus, mdiMapSearch, mdiLatitude, mdiLongitude, mdiRoadVariant, mdiHome, mdiCityVariant } from '@mdi/js'
2022-09-02 08:32:13 +02:00
import { mapState } from 'vuex'
2022-11-18 14:13:59 +01:00
import debounce from 'lodash/debounce'
import get from 'lodash/get'
2020-10-25 00:31:38 +02:00
export default {
name: 'WhereInput',
2020-10-27 11:56:47 +01:00
props: {
value: { type: Object, default: () => ({}) }
2020-10-27 11:56:47 +01:00
},
2020-10-25 00:31:38 +02:00
data () {
return {
2022-11-18 14:33:38 +01:00
mdiMap, mdiMapMarker, mdiPlus, mdiMapSearch, mdiLatitude, mdiLongitude, mdiRoadVariant, mdiHome, mdiCityVariant,
place: { },
placeName: '',
places: [],
2022-09-02 08:32:13 +02:00
disableAddress: true,
2022-11-18 14:13:59 +01:00
addressList: [],
2022-11-18 14:33:38 +01:00
loading: false,
nominatim_osm_type: {
way: mdiRoadVariant,
house: mdiHome,
node: mdiMapMarker,
relation: mdiCityVariant,
},
nominatim_class: ['amenity', 'shop', 'tourism', 'leisure', 'building']
2020-10-25 00:31:38 +02:00
}
},
computed: {
2022-09-02 08:32:13 +02:00
...mapState(['settings']),
filteredPlaces () {
if (!this.placeName) { return this.places }
2022-05-03 18:33:20 +02:00
const placeName = this.placeName.trim().toLowerCase()
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)
})
if (!nameMatch) {
matches.unshift({ create: true, name: this.placeName })
}
return matches
}
2020-10-25 00:31:38 +02:00
},
methods: {
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 }
const matches = this.places.find(p => search === p.name.toLocaleLowerCase())
2022-06-22 14:07:28 +02:00
if (!matches && search) {
this.places.unshift({ create: true, name: ev.target.value.trim() })
}
}, 100),
2022-11-18 14:33:38 +01:00
loadCoordinatesResultIcon(item) {
if ( this.nominatim_class.includes(item.class)) {
return this.mdiHome
}
return this.nominatim_osm_type[item.type]
},
2020-10-25 00:31:38 +02:00
selectPlace (p) {
if (!p) { return }
if (typeof p === 'object' && !p.create) {
this.place.name = p.name
2020-11-04 10:55:30 +01:00
this.place.address = p.address
if (this.settings.allow_geolocation) {
2022-09-02 08:32:13 +02:00
this.place.details = p.details
this.place.latitude = p.latitude
this.place.longitude = p.longitude
2022-09-02 08:32:13 +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
this.place.name = (p.name || p).trim()
const tmpPlace = this.place.name.toLocaleLowerCase()
// search for a place with the same name
const place = this.places.find(p => !p.create && p.name.trim().toLocaleLowerCase() === tmpPlace)
if (place) {
2022-05-03 18:33:20 +02:00
this.place.name = place.name
this.place.id = place.id
this.place.address = place.address
this.disableAddress = true
} else {
delete this.place.id
this.place.address = ''
if (this.settings.allow_geolocation) {
2022-09-02 08:32:13 +02:00
this.place.details = p.details
this.place.latitude = p.latitude
this.place.longitude = p.longitude
2022-09-02 08:32:13 +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
},
2022-11-18 14:13:59 +01:00
// changeAddress (v) {
// this.place.address = v
// this.$emit('input', { ...this.place })
// this.disableDetails = false
// },
selectAddress (v) {
2022-09-02 08:32:13 +02:00
if (!v) { return }
if (typeof v === 'object') {
this.place.latitude = v.lat
this.place.longitude = v.lon
this.place.address = v.address
// }
} else {
this.place.address = v
this.place.latitude = this.place.longitude = null
2022-09-02 08:32:13 +02:00
}
this.$emit('input', { ...this.place })
},
2022-11-18 14:13:59 +01:00
searchAddress: debounce(async function(ev) {
const pre_searchCoordinates = ev.target.value.trim().toLowerCase()
// allow pasting coordinates lat/lon and lat,lon
const searchCoordinates = pre_searchCoordinates.replace('/', ',')
// const regex_coords_comma = "-?[1-9][0-9]*(\\.[0-9]+)?,\\s*-?[1-9][0-9]*(\\.[0-9]+)?";
// const regex_coords_slash = "-?[1-9][0-9]*(\\.[0-9]+)?/\\s*-?[1-9][0-9]*(\\.[0-9]+)?";
// const setCoords = (v) => {
// 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
// }
// }
// 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
// }
if (searchCoordinates.length) {
this.loading = true
const ret = await this.$axios.$get(`placeNominatim/${searchCoordinates}`)
if (ret && ret.length) {
2022-11-18 14:13:59 +01:00
this.addressList = ret.map(v => {
const name = get(v.namedetails, 'alt_name', get(v.namedetails, 'name'))
const address = v.display_name ? v.display_name.replace(name, '').replace(/^, ?/, '') : ''
return {
2022-11-18 14:33:38 +01:00
class: v.class,
type: v.osm_type,
lat: v.lat,
lon: v.lon,
name,
address
}
})
} else {
2022-11-18 14:13:59 +01:00
this.addressList = []
}
this.loading = false
2022-09-02 08:32:13 +02:00
}
}, 300)
2020-10-25 00:31:38 +02:00
}
}
</script>