gancio-upstream/components/WhereInput.vue

199 lines
7.2 KiB
Vue
Raw Normal View History

2020-10-25 00:31:38 +02:00
<template lang="pug">
v-row.mb-4
v-col(cols=12 md=4)
2023-04-05 16:14:55 +02:00
//- this is the name used by people
2022-04-27 12:34:58 +02:00
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'
2022-11-19 13:20:37 +01:00
item-text='name'
:items="places"
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>
2023-04-05 16:14:55 +02:00
v-list-item-content(two-line v-else-if='item.online')
v-list-item-title <v-icon color='primary' v-text='mdiLaptopAccount' :aria-label='$t("common.online")'></v-icon> {{$t('common.online')}}
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-col(cols=12 :md='settings.allow_online_event?5:8' v-if="!isOnLine")
v-text-field.mr-4(ref='address'
v-model='value.address'
:prepend-icon='mdiMap'
:disabled='disableAddress'
:rules="[ v => disableAddress ? true : $validators.required('common.address')(v)]"
:label="$t('common.address')"
:hint="$t('event.address_description')"
persistent-hint)
template(v-slot:append v-if="showAdvancedDialogButton")
TBtn(@click="whereInputAdvancedDialog = true" tooltip='admin.geolocation' small)
v-icon(v-text='mdiMapSearch')
v-col(cols=12 :md='isOnLine?8:3' v-if='settings.allow_online_event')
v-combobox.mr-4(v-model="onlineLocations"
:prepend-icon='mdiLink'
:hint="$t('event.online_locations_help')"
:label="$t('event.online_locations')"
:rules="isOnLine ? [$validators.required('event.online_locations')] : []"
clearable chips small-chips multiple deletable-chips hide-no-data hide-selected persistent-hint
:delimiters="[',', ';', '; ']"
:items="onlineLocations"
@change='selectLocations')
template(v-slot:selection="{ item, index, on, attrs, selected, parent }")
v-chip(v-bind="attrs" :outlined='index !== 0'
close :close-icon='mdiCloseCircle' @click:close='parent.selectItem(item)'
:input-value="selected" label small) {{ item }}
v-dialog(v-model='whereInputAdvancedDialog' destroy-on-close max-width='700px' :fullscreen='$vuetify.breakpoint.xsOnly' dense)
WhereInputAdvanced(ref='whereAdvanced' :place.sync='value' :event='event'
@close='whereInputAdvancedDialog = false && this.$refs.address.blur()')
2020-10-25 00:31:38 +02:00
2020-10-25 00:31:38 +02:00
</template>
<script>
import { mdiMap, mdiMapMarker, mdiPlus, mdiCog, mdiLink, mdiCloseCircle, mdiLaptopAccount, mdiMapSearch } 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 WhereInputAdvanced from './WhereInputAdvanced.vue'
import TBtn from '../components/TBtn.vue'
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: () => ({}) },
event: { type: Object, default: () => null },
2020-10-27 11:56:47 +01:00
},
components: { WhereInputAdvanced, TBtn },
2024-08-17 18:43:32 +02:00
data () {
2020-10-25 00:31:38 +02:00
return {
mdiMap, mdiMapMarker, mdiPlus, mdiCog, mdiLink, mdiCloseCircle, mdiLaptopAccount, mdiMapSearch,
places: [],
place: { isNew: false, name: '' },
placeName: '',
2022-09-02 08:32:13 +02:00
disableAddress: true,
whereInputAdvancedDialog: false,
onlineLocations: this.event.online_locations || [],
2020-10-25 00:31:38 +02:00
}
},
computed: {
2022-09-02 08:32:13 +02:00
...mapState(['settings']),
isOnLine () {
return this.settings.allow_online_event && this.value.name === 'online'
},
2023-04-05 16:14:55 +02:00
showAdvancedDialogButton () {
if (!this.place.name) return false
// do not show advanced dialog button in case geolocation is not allowed
if (!(this.settings.allow_geolocation)) {
2023-04-05 16:14:55 +02:00
return false
}
2023-04-05 16:14:55 +02:00
2023-04-09 21:35:58 +02:00
// known places already have coords
if (!this.place.isNew) return false
2023-04-09 21:35:58 +02:00
2023-04-05 16:14:55 +02:00
return true
}
2020-10-25 00:31:38 +02:00
},
mounted () {
this.$nextTick( () => {
this.search()
})
},
2020-10-25 00:31:38 +02:00
methods: {
search: debounce(async function(ev) {
const search = ev ? ev.target.value.trim().toLowerCase() : ''
this.places = await this.$axios.$get('place', { params: { search } }).catch()
// Filter out the place with name 'online' if not allowed
2023-04-05 16:14:55 +02:00
if (this.places.length) {
this.places = this.places.filter(p => p.name?.toLocaleLowerCase() !== 'online')
}
2023-04-05 16:14:55 +02:00
if (this.settings.allow_online_event) {
this.places.push({ online: true, name: 'online' })
}
if (!search && this.places.length) {
return this.places
}
2023-04-05 16:14:55 +02:00
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() })
}
2023-01-26 23:30:18 +01:00
}, 200),
2020-10-25 00:31:38 +02:00
selectPlace (p) {
// force online events under place: online address: online
2023-04-05 16:14:55 +02:00
// this.event_only_online = false
this.place.isNew = false
2023-04-05 16:14:55 +02:00
// this.whereAdvancedId++
if (!p) { return }
2023-04-05 16:14:55 +02:00
if (typeof p === 'object' && !p.create && !p.online) {
2022-11-19 13:20:37 +01:00
if (p.id === this.value.id) return
this.place.name = p.name
2020-11-04 10:55:30 +01:00
this.place.address = p.address
if (this.settings.allow_geolocation) {
this.place.latitude = p.latitude
this.place.longitude = p.longitude
2022-09-02 08:32:13 +02:00
}
this.place.id = p.id
2023-04-05 16:14:55 +02:00
// if (this.settings.allow_event_only_online && this.place.name === 'online') {
// this.event_only_online = true
// }
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.isNew = true
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) {
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.$nextTick(() => { this.$refs.address && 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
},
selectLocations () {
this.event.online_locations = []
if (this.onlineLocations) {
this.onlineLocations.forEach((item, i) => {
if (!item.startsWith('http')) { item = `https://${item}` }
this.onlineLocations[i] = item
})
// Remove duplicates
this.onlineLocations = [...new Set(this.onlineLocations)]
// Insert up to 3 online location: the main one and 2 fallback
this.onlineLocations = this.onlineLocations.slice(0, 3)
this.event.online_locations = this.onlineLocations
}
},
2020-10-25 00:31:38 +02:00
}
}
</script>