gancio-upstream/components/admin/Places.vue

184 lines
6.2 KiB
Vue
Raw Normal View History

2019-07-27 22:07:08 +02:00
<template lang='pug'>
2022-06-11 12:00:29 +02:00
v-container
v-card-title {{ $t('common.places') }}
2022-06-11 12:00:29 +02:00
v-spacer
v-text-field(v-model='search'
:append-icon='mdiMagnify' outlined rounded
2022-11-29 14:40:19 +01:00
:label="$t('common.search')"
2022-06-11 12:00:29 +02:00
single-line hide-details)
v-card-subtitle(v-html="$t('admin.place_description')")
2019-09-13 10:15:23 +02:00
2022-06-11 12:00:29 +02:00
v-dialog(v-model='dialog' width='600' :fullscreen='$vuetify.breakpoint.xsOnly')
v-card
v-card-title {{ $t('admin.edit_place') }}
2022-06-11 12:00:29 +02:00
v-card-text
v-form(v-model='valid' ref='form' lazy-validation)
v-text-field(
:rules="[$validators.required('common.name')]"
:label="$t('common.name')"
v-model='place.name'
:placeholder='$t("common.name")')
2019-09-11 19:12:24 +02:00
v-combobox(ref='address'
:prepend-icon='mdiMapSearch'
@input.native='searchAddress'
2022-06-11 12:00:29 +02:00
:label="$t('common.address')"
:rules="[ v => $validators.required('common.address')(v)]"
:value='place.address'
persistent-hint hide-no-data clearable no-filter
:loading='loading'
@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')
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}`')
2022-09-02 08:32:13 +02:00
2022-06-11 12:00:29 +02:00
v-card-actions
v-spacer
v-btn(@click='dialog = false' outlined color='warning') {{ $t('common.cancel') }}
v-btn(@click='savePlace' color='primary' outlined :loading='loading'
:disable='!valid || loading') {{ $t('common.save') }}
2020-07-25 21:41:22 +02:00
2022-06-11 12:00:29 +02:00
v-card-text
v-data-table(
:headers='headers'
:items='places'
:hide-default-footer='places.length < 5'
2022-11-29 14:40:19 +01:00
:header-props='{ sortIcon: mdiChevronDown }'
2022-06-11 12:00:29 +02:00
:footer-props='{ prevIcon: mdiChevronLeft, nextIcon: mdiChevronRight }'
:search='search')
template(v-slot:item.map='{ item }')
span {{item.latitude && item.longitude && 'YEP' }}
template(v-slot:item.actions='{ item }')
2022-06-11 12:00:29 +02:00
v-btn(@click='editPlace(item)' color='primary' icon)
v-icon(v-text='mdiPencil')
2022-06-18 01:09:25 +02:00
nuxt-link(:to='`/place/${item.name}`')
2022-06-11 12:00:29 +02:00
v-icon(v-text='mdiEye')
2022-06-01 14:08:13 +02:00
2019-07-27 22:07:08 +02:00
</template>
<script>
2022-11-29 14:40:19 +01:00
import { mdiPencil, mdiChevronLeft, mdiChevronRight, mdiMagnify, mdiEye, mdiMapSearch, mdiChevronDown } from '@mdi/js'
2022-09-02 08:32:13 +02:00
import { mapState } from 'vuex'
import debounce from 'lodash/debounce'
import get from 'lodash/get'
2019-07-27 22:07:08 +02:00
export default {
data() {
2019-09-11 19:12:24 +02:00
return {
2022-11-29 14:40:19 +01:00
mdiPencil, mdiChevronRight, mdiChevronLeft, mdiMagnify, mdiEye, mdiMapSearch, mdiChevronDown,
2020-10-07 11:29:40 +02:00
loading: false,
2020-10-07 10:06:49 +02:00
dialog: false,
2020-10-09 00:42:03 +02:00
valid: false,
2022-06-01 14:08:13 +02:00
places: [],
addressList: [],
address: '',
2022-03-07 16:39:02 +01:00
search: '',
2020-07-28 12:24:39 +02:00
place: { name: '', address: '', id: null },
headers: [
2022-11-29 14:40:19 +01:00
{ value: 'name', text: this.$t('common.name') },
{ value: 'address', text: this.$t('common.address') },
{ value: 'map', text: 'Map' },
2022-11-29 14:40:19 +01:00
{ value: 'actions', text: this.$t('common.actions'), align: 'right' }
],
geocoding_provider_type: $store.state.settings.geocoding_provider_type || 'Nominatim'
2019-07-27 22:07:08 +02:00
}
},
async fetch() {
2022-06-01 14:08:13 +02:00
this.places = await this.$axios.$get('/place/all')
},
2022-09-02 08:32:13 +02:00
computed: {
...mapState(['settings']),
},
2019-07-27 22:07:08 +02:00
methods: {
editPlace(item) {
2019-09-11 19:12:24 +02:00
this.place.name = item.name
this.place.address = item.address
if (this.settings.allow_geolocation) {
this.place.latitude = item.latitude
this.place.longitude = item.longitude
2022-09-02 08:32:13 +02:00
}
2019-09-11 19:12:24 +02:00
this.place.id = item.id
2020-10-07 10:06:49 +02:00
this.dialog = true
2019-09-11 19:12:24 +02:00
},
async savePlace() {
2020-10-09 00:42:03 +02:00
if (!this.$refs.form.validate()) return
2020-10-07 11:29:40 +02:00
this.loading = true
2020-07-25 21:41:22 +02:00
await this.$axios.$put('/place', this.place)
2022-06-07 21:10:17 +02:00
await this.$fetch()
2020-10-07 11:29:40 +02:00
this.loading = false
2020-10-07 10:06:49 +02:00
this.dialog = false
},
selectAddress (v) {
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
}
this.$emit('input', { ...this.place })
},
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(`placeOSM/${this.geocoding_provider_type}/${searchCoordinates}`)
if (ret && ret.length) {
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 {
lat: v.lat,
lon: v.lon,
name,
address
}
})
} else {
this.addressList = []
}
this.loading = false
}
}, 300)
2019-07-27 22:07:08 +02:00
}
}
</script>