[ux] new event editor

This commit is contained in:
les
2020-01-15 23:56:11 +01:00
parent 337a0404d4
commit b91b54fc46

View File

@@ -1,170 +1,131 @@
<template lang="pug"> <template lang="pug">
el-main#add_event el-main
h4 <nuxt-link to='/'><img src='/favicon.ico'/></nuxt-link> {{edit?$t('common.edit_event'):$t('common.add_event')}} h5.text-center {{edit?$t('common.edit_event'):$t('common.add_event')}}
el-form(v-loading='loading') el-form(v-loading='loading')
//- NOT LOGGED EVENT
div(v-if='!$auth.loggedIn')
el-divider <v-icon name='user-secret'/> {{$t('event.anon')}}
p(v-html="$t('event.anon_description')")
//- title
span {{$t('event.what_description')}}
el-input.mb-3(v-model='event.title' ref='title' autofocus)
//- description
span {{$t('event.description_description')}}
Editor.mb-3(v-model='event.description' border no-save style='max-height: 400px;')
//- tags
div {{$t('event.tag_description')}}
el-select.mb-3(v-model='event.tags' multiple filterable
@input.native='queryTags=$event.target.value' @change='queryTags=""'
allow-create default-first-option placeholder='Tag')
el-option(v-for='tag in filteredTags' :key='tag' :label='tag' :value='tag')
//- WHERE
el-divider
i.el-icon-location-outline
span {{$t('common.where')}}
p(v-html="$t('event.where_description')")
el-autocomplete(v-model='event.place.name' @blur='selectPlace($event.target.value)'
highlight-first-item
:fetch-suggestions='filterPlaces' @select='selectPlace')
div {{$t("common.address")}}
el-input.mb-3(ref='address' v-model='event.place.address' :disabled='disableAddress')
//- WHEN
el-divider <v-icon name='clock'/> {{$t('common.when')}}
.text-center
el-radio-group(v-model="event.type")
el-radio-button(label="normal") <v-icon name='calendar-day'/> {{$t('event.normal')}}
el-radio-button(label="multidate") <v-icon name='calendar-week'/> {{$t('event.multidate')}}
el-radio-button(v-if='settings.allow_recurrent_event' label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}}
br
span {{$t(`event.${event.type}_description`)}}
el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.recurrent.frequency' placeholder='Frequenza')
el-option(:label="$t('event.each_week')" value='1w' key='1w')
el-option(:label="$t('event.each_2w')" value='2w' key='2w')
//- el-option(:label="$t('event.each_month')" value='1m' key='1m')
client-only client-only
el-tabs.mb-2(v-model='activeTab') #picker.mx-auto
v-date-picker.mb-2.mt-3(
:mode='event.type === "multidate" ? "range" : event.type === "recurrent" ? "multiple" : "single"'
:attributes='attributes'
v-model='date'
:locale='$i18n.locale'
:from-page.sync='page'
is-inline
is-expanded
:min-date='event.type !== "recurrent" && new Date()')
//- NOT LOGGED EVENT div.text-center.mb-2(v-if='event.type === "recurrent"')
el-tab-pane(v-if='!$auth.loggedIn') span(v-if='event.recurrent.frequency !== "1m" && event.recurrent.frequency !== "2m"') {{whenPatterns}}
span(slot='label') {{$t('event.anon')}} <v-icon name='user-secret'/> el-radio-group(v-else v-model='event.recurrent.type')
p(v-html="$t('event.anon_description')") el-radio-button(v-for='whenPattern in whenPatterns' :label='whenPattern.key' :key='whenPatterns.key')
el-button.float-right(@click='next' :disabled='!couldProceed') {{$t('common.next')}} span {{whenPattern.label}}
//- WHAT .text-center(inline)
el-tab-pane el-form-item(:label="$t('event.from')")
span(slot='label') {{$t('common.what')}} <v-icon name='file-alt'/> el-time-select.mr-2(ref='time_start'
span {{$t('event.what_description')}} v-model="time.start"
el-input.mb-3(v-model='event.title' ref='title') :picker-options="{ start: '00:00', step: '00:30', end: '24:00'}")
span {{$t('event.description_description')}} el-form-item(:label="$t('event.due')")
el-input.mb-3(v-model='event.description' type='textarea' :rows='9') el-time-select(v-model='time.end'
span {{$t('event.tag_description')}} :picker-options="{start: '00:00', step: '00:30', end: '24:00'}")
br
el-select(v-model='event.tags' multiple filterable allow-create
default-first-option placeholder='Tag')
el-option(v-for='tag in tags' :key='tag'
:label='tag' :value='tag')
el-button.float-right(@click.native='next' :disabled='!couldProceed') {{$t('common.next')}} List(v-if='event.type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
//- el-button.float-right(@click='next' type='succes' :disabled='!couldProceed') {{$t('common.next')}}
//- WHERE //- MEDIA / FLYER / POSTER
el-tab-pane el-divider <v-icon name='image'/> {{$t('common.media')}}
span(slot='label') <v-icon name='map-marker-alt'/> {{$t('common.where')}}
p(v-html="$t('event.where_description')")
span {{event.place.name}}
el-select.mb-3(v-model='event.place.name'
@keypress.tab='testBlur'
@change='placeChoosed'
filterable allow-create
default-first-option
@blur='testBlur'
)
el-option(v-for='place in places' :label='place.name' :value='place.name' :key='place.id')
div {{$t("common.address")}}
el-input.mb-3(ref='address' v-model='event.place.address'
:disabled='places_name.indexOf(event.place.name)>-1'
@keydown.native.enter='next')
el-button.float-right(@click='next' :disabled='!couldProceed') {{$t('common.next')}}
//- WHEN div.mb-2 {{$t('event.media_description')}}
el-tab-pane //- img(:src='mediaUrl' @load='imageLoaded')
span(slot='label') {{$t('common.when')}} <v-icon name='clock'/> el-upload.text-center(
.text-center action=''
el-radio-group(v-model="event.type") :limit="1"
el-radio-button(label="normal") <v-icon name='calendar-day'/> {{$t('event.normal')}} :auto-upload='false'
el-radio-button(label="multidate") <v-icon name='calendar-week'/> {{$t('event.multidate')}} drag
el-radio-button(v-if='settings.allow_recurrent_event' label="recurrent") <v-icon name='calendar-alt'/> {{$t('event.recurrent')}} accept='image/*'
br :on-remove='cleanFile'
span {{$t(`event.${event.type}_description`)}} :on-change='uploadedFile'
el-select.ml-2(v-if='event.type==="recurrent"' v-model='event.recurrent.frequency' placeholder='Frequenza') :multiple='false')
el-option(:label="$t('event.each_week')" value='1w' key='1w') i.el-icon-upload
el-option(:label="$t('event.each_2w')" value='2w' key='2w') //- el-input(v-model='mediaUrl')
//- el-option(:label="$t('event.each_month')" value='1m' key='1m') el-button.mt-2.float-right(@click='done' :disabled='!couldProceed') {{edit?$t('common.edit'):$t('common.send')}}
#picker.mx-auto
v-date-picker.mb-2.mt-3(
:mode='event.type === "multidate" ? "range" : event.type === "recurrent" ? "multiple" : "single"'
:attributes='attributes'
v-model='date'
:locale='$i18n.locale'
:from-page.sync='page'
is-inline
is-expanded
:min-date='event.type !== "recurrent" && new Date()'
)
div.text-center.mb-2(v-if='event.type === "recurrent"')
span(v-if='event.recurrent.frequency !== "1m" && event.recurrent.frequency !== "2m"') {{whenPatterns}}
el-radio-group(v-else v-model='event.recurrent.type')
el-radio-button(v-for='whenPattern in whenPatterns' :label='whenPattern.key' :key='whenPatterns.key')
span {{whenPattern.label}}
el-form.text-center(inline)
el-form-item(:label="$t('event.from')")
el-time-select.mr-2(ref='time_start'
v-model="time.start"
:picker-options="{ start: '00:00', step: '00:30', end: '24:00'}")
el-form-item(:label="$t('event.due')")
el-time-select(v-model='time.end'
:picker-options="{start: '00:00', step: '00:30', end: '24:00'}")
List(v-if='event.type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
el-button.float-right(@click='next' type='succes' :disabled='!couldProceed') {{$t('common.next')}}
//- MEDIA / FLYER / POSTER
el-tab-pane
span(slot='label') {{$t('common.media')}} <v-icon name='image'/>
div.mb-2 {{$t('event.media_description')}}
img(:src='mediaUrl' @load='imageLoaded')
el-upload.text-center(
action=''
:limit="1"
:auto-upload='false'
drag
accept='image/*'
:on-remove='cleanFile'
:on-change='uploadedFile'
:multiple='false')
i.el-icon-upload
el-input(v-model='mediaUrl' @blur='checkMediaUrl')
el-button.mt-2.float-right(@click='done' :disabled='!couldProceed') {{edit?$t('common.edit'):$t('common.send')}}
</template> </template>
<script> <script>
import { mapActions, mapState, mapGetters } from 'vuex' import { mapActions, mapState } from 'vuex'
import uniq from 'lodash/uniq' import _ from 'lodash'
import map from 'lodash/map' import moment from 'moment-timezone'
import moment from 'moment' import Editor from '@/components/Editor'
import List from '@/components/List' import List from '@/components/List'
import { Message } from 'element-ui' import { Message } from 'element-ui'
export default { export default {
name: 'newEvent', name: 'NewEvent',
components: { List }, components: { List, Editor },
validate ({ store }) { validate ({ store }) {
return (store.state.auth.loggedIn || store.state.settings.allow_anon_event) return (store.state.auth.loggedIn || store.state.settings.allow_anon_event)
}, },
head () { // fetch ({ store, $axios }) {
return { // try {
title: `${this.settings.title} - ${this.$t('common.add_event')}` // const now = new Date()
} // const events = await $axios.$get(`/event/${now.getMonth()}/${now.getFullYear()}`)
}, // store.commit('setEvents', events)
data () { // const { tags, places } = await $axios.$get('/event/meta')
const month = moment().month() + 1 // store.commit('update', { tags, places })
const year = moment().year() // } catch (e) {
return { // console.error('Error ', e)
event: { // }
type: 'normal', // moment.locale(store.state.locale)
place: { name: '', address: '' }, // },
title: '',
description: '',
tags: [],
image: false,
recurrent: { frequency: '1w', days: [], type: 'weekday' }
},
page: { month, year },
fileList: [],
id: null,
activeTab: '0',
date: null,
time: { start: '20:00', end: null },
edit: false,
loading: false,
mediaUrl: '',
}
},
watch: {
'time.start' (value) {
if (!value) { return }
const [h, m] = value.split(':')
this.time.end = (Number(h) + 1) + ':' + m
},
// month selected
page () {
this.updateEvents(this.page)
}
},
async asyncData ({ params, $axios, error, store }) { async asyncData ({ params, $axios, error, store }) {
if (params.edit) { if (params.edit) {
const data = { time: {}, event: { place: {} } } const data = { time: {}, event: { place: {} } }
@@ -194,7 +155,7 @@ export default {
data.time.start = moment.unix(event.start_datetime).format('HH:mm') data.time.start = moment.unix(event.start_datetime).format('HH:mm')
data.time.end = moment.unix(event.end_datetime).format('HH:mm') data.time.end = moment.unix(event.end_datetime).format('HH:mm')
data.event.title = event.title data.event.title = event.title
data.event.description = event.description.replace(/(<([^>]+)>)/ig, '') data.event.description = event.description
data.event.id = event.id data.event.id = event.id
data.event.recurrent = {} data.event.recurrent = {}
data.event.tags = event.tags data.event.tags = event.tags
@@ -202,37 +163,43 @@ export default {
} }
return {} return {}
}, },
async fetch ({ store, $axios }) { data () {
try { const month = moment().month() + 1
const now = new Date() const year = moment().year()
const events = await $axios.$get(`/event/${now.getMonth()}/${now.getFullYear()}`) return {
store.commit('setEvents', events) event: {
const { tags, places } = await $axios.$get('/event/meta') type: 'normal',
store.commit('update', { tags, places }) place: { name: '', address: '' },
} catch (e) { title: '',
console.error('Error ', e) description: '',
tags: [],
image: false,
recurrent: { frequency: '1w', days: [], type: 'weekday' }
},
page: { month, year },
fileList: [],
id: null,
date: null,
time: { start: '20:00', end: null },
edit: false,
loading: false,
mediaUrl: '',
queryTags: '',
disableAddress: true
} }
moment.locale(store.state.locale)
}, },
computed: { computed: {
...mapState({ ...mapState(['tags', 'places', 'events', 'settings']),
tags: state => state.tags.map(t => t.tag),
places_name: state => state.places.map(p => p.name).sort((a, b) => b.weigth - a.weigth),
places: state => state.places,
user: state => state.user,
events: state => state.events,
settings: state => state.settings
}),
whenPatterns () { whenPatterns () {
const dates = this.date const dates = this.date
if (!dates || !dates.length) { return } if (!dates || !dates.length) { return '' }
const freq = this.event.recurrent.frequency const freq = this.event.recurrent.frequency
const weekDays = uniq(map(dates, date => moment(date).format('dddd'))) const weekDays = _(dates).map(date => moment(date).format('dddd')).uniq()
if (freq === '1w' || freq === '2w') { if (freq === '1w' || freq === '2w') {
return this.$t(`event.recurrent_${freq}_days`, { days: weekDays.join(', ') }) return this.$t(`event.recurrent_${freq}_days`, { days: weekDays.join(', ') })
} else if (freq === '1m' || freq === '2m') { } else if (freq === '1m' || freq === '2m') {
const days = uniq(map(dates, date => moment(date).date())) const days = _(dates).map(date => moment(date).date()).uniq()
const n = Math.floor((days[0] - 1) / 7) + 1 const n = Math.floor((days[0] - 1) / 7) + 1
return [ return [
{ label: this.$tc(`event.recurrent_${freq}_days`, days.length, { days }), key: 'ordinal' }, { label: this.$tc(`event.recurrent_${freq}_days`, days.length, { days }), key: 'ordinal' },
@@ -241,7 +208,7 @@ export default {
} else if (freq === '1d') { } else if (freq === '1d') {
return this.$t('event.recurrent_each_day') return this.$t('event.recurrent_each_day')
} }
return return ''
}, },
todayEvents () { todayEvents () {
if (this.event.type === 'multidate') { if (this.event.type === 'multidate') {
@@ -262,109 +229,88 @@ export default {
!e.multidate !e.multidate
? !e.recurrent && date.isSame(moment.unix(e.start_datetime), 'day') ? !e.recurrent && date.isSame(moment.unix(e.start_datetime), 'day')
: moment.unix(e.start_datetime).isSame(date, 'day') || : moment.unix(e.start_datetime).isSame(date, 'day') ||
moment.unix(e.start_datetime).isBefore(date) && moment.unix(e.end_datetime).isAfter(date) (moment.unix(e.start_datetime).isBefore(date) && moment.unix(e.end_datetime).isAfter(date))
) )
} }
}, },
...mapGetters(['filteredEvents']), ...mapState(['events']),
// TOFIX
attributes () { attributes () {
let attributes = [] let attributes = []
attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'yellow' } }) attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'red' } })
attributes = attributes.concat(this.filteredEvents attributes = attributes.concat(this.events
.filter(e => !e.multidate && (!e.recurrent || this.event.type === 'recurrent')) .filter(e => !e.multidate && (!e.recurrent || this.event.type === 'recurrent'))
.map(e => ({ key: e.id, dot: { color: this.event.type === 'recurrent' ? 'orange' : 'green' }, dates: moment.unix(e.start_datetime).toDate() }))) .map(e => ({ key: e.id, dot: { color: this.event.type === 'recurrent' ? 'orange' : 'green' }, dates: moment.unix(e.start_datetime).toDate() })))
attributes = attributes.concat(this.filteredEvents console.error(this.event.type)
.filter(e => e.multidate && !e.recurrent) if (this.event.type === 'recurrent' && this.date && this.date.length) {
.map(e => ({ key: e.id, attributes.push({
highlight: {}, key: 'recurrent',
dot: { color: 'orange' },
dates: { dates: {
start: moment.unix(e.start_datetime).toDate(), end: moment.unix(e.end_datetime).toDate() } }))) weeklyInterval: this.event.recurrent.frequency === '1w' ? 1 : 2, // this.event.recurrent.freq,
weekdays: _.map(this.date, date => moment(date).day() + 1),
start: new Date(this.date[0])
}
})
}
attributes = attributes.concat(this.events
.filter(e => e.multidate && !e.recurrent)
.map(e => ({
key: e.id,
highlight: {},
dates: { start: moment.unix(e.start_datetime).toDate(), end: moment.unix(e.end_datetime).toDate() }
})))
return attributes return attributes
}, },
disableAddress () { filteredTags () {
return this.places_name.find(p => p.name === this.event.place.name) const queryTags = this.queryTags.toLowerCase()
return _(this.tags)
.filter(t => !this.event.tags.includes(t))
.filter(t => t.includes(queryTags))
.take(5)
.value()
}, },
couldProceed () { couldProceed () {
const t = this.$auth.loggedIn ? -1 : 0 return (this.event.place.name.length > 0 &&
switch (Number(this.activeTab)) { this.event.place.address.length > 0 &&
case 0 + t: (this.date && this.time.start) &&
return true this.event.title.length > 0)
break
case 1 + t:
return this.event.title.length > 0
break
case 2 + t:
return this.event.place.name.length > 0 &&
this.event.place.address.length > 0
break
case 3 + t:
if (this.date && this.time.start) { return true }
break
case 4 + t:
return this.event.place.name.length > 0 &&
this.event.place.address.length > 0 &&
(this.date && this.time.start) &&
this.event.title.length > 0
}
} }
}, },
mounted () {
this.$refs.title.focus()
},
methods: { methods: {
...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']), ...mapActions(['addEvent', 'updateEvent', 'updateMeta', 'updateEvents']),
testBlur (e) { filterPlaces (q, cb) {
console.error('nel blur!') const query = q.toLowerCase()
this.event.place.name = e.target.value const ret = _(this.places)
.filter(p => p.name.toLowerCase().includes(query))
.take(5)
.map(p => ({ value: p.name }))
.value()
ret.unshift({ value: q })
cb(ret)
}, },
imageLoaded () { selectPlace (p) {
console.error('image loaded!')
},
async checkMediaUrl () {
// if (!this.mediaUrl) return
// this.fileList.push({ name: this.mediaUrl, url: this.mediaUrl })
// const img = document.createElement('img')
// const c = document.createElement('canvas')
// const ctx = c.getContext('2d')
// img.crossOrigin = ''
// img.src = this.mediaUrl
// console.error('image loaded')
// c.width = this.naturalWidth
// c.height = this.naturalHeight
// ctx.drawImage(this, 0, 0)
// c.toBlob( raw => {
// this.event.image = { name: this.mediaUrl, raw}
// }, 'image/jpeg', 0.9)
// }
// fetch(this.mediaUrl, { mode: 'cors' })
// .then( ret => ret.blob() )
// .then( raw => { if (!raw) return; this.event.image = { name: this.mediaUrl, raw }})
},
recurrentDays () {
if (this.event.type !== 'recurrent' || !this.date || !this.date.length) { return }
const type = this.event.recurrent.type
if (type === 'ordinal') { return map(this.date, d => moment(d).date()) } else if (type === 'weekday') { return map(this.date, d => moment(d).day() + 1) }
},
next () {
this.activeTab = String(Number(this.activeTab) + 1)
if (this.activeTab === '2') {
this.$refs.title.focus()
}
},
prev () {
this.activeTab = String(Number(this.activeTab - 1))
},
placeChoosed () {
console.error('dentro place choosed')
const place = this.places.find(p => p.name === this.event.place.name) const place = this.places.find(p => p.name === this.event.place.name)
if (place && place.address) { if (place && place.address) {
this.event.place.address = place.address this.event.place.address = place.address
this.disableAddress = true
} else { } else {
this.disableAddress = false
this.event.place.address = '' this.event.place.address = ''
} }
this.$refs.address.focus() this.$nextTick(this.$refs.address.focus)
}, },
// recurrentDays () {
// if (this.event.type !== 'recurrent' || !this.date || !this.date.length) { return }
// const type = this.event.recurrent.type
// if (type === 'ordinal') { return map(this.date, d => moment(d).date()) } else if (type === 'weekday') { return map(this.date, d => moment(d).day() + 1) }
// },
// },
cleanFile () { cleanFile () {
this.event.image = null this.event.image = null
}, },
@@ -380,11 +326,11 @@ export default {
async done () { async done () {
this.loading = true this.loading = true
let start_datetime, end_datetime let start_datetime, end_datetime
const [ start_hour, start_minute ] = this.time.start.split(':') const [start_hour, start_minute] = this.time.start.split(':')
if (!this.time.end) { if (!this.time.end) {
this.time.end = (Number(start_hour) + 2) + ':' + start_minute this.time.end = (Number(start_hour) + 2) + ':' + start_minute
} }
const [ end_hour, end_minute ] = this.time.end.split(':') const [end_hour, end_minute] = this.time.end.split(':')
const formData = new FormData() const formData = new FormData()
@@ -404,7 +350,7 @@ export default {
end_datetime = moment().set('hour', end_hour).set('minute', end_minute) end_datetime = moment().set('hour', end_hour).set('minute', end_minute)
const recurrent = { const recurrent = {
frequency: this.event.recurrent.frequency, frequency: this.event.recurrent.frequency,
days: this.event.recurrent.type === 'ordinal' ? map(this.date, d => moment(d).date()) : map(this.date, d => moment(d).day() + 1), days: this.event.recurrent.type === 'ordinal' ? _.map(this.date, d => moment(d).date()) : _.map(this.date, d => moment(d).day() + 1),
type: this.event.recurrent.type type: this.event.recurrent.type
} }
if (end_hour < start_hour) { if (end_hour < start_hour) {
@@ -439,26 +385,44 @@ export default {
this.loading = false this.loading = false
Message({ type: 'success', showClose: true, message: this.$auth.loggedIn ? this.$t('event.added') : this.$t('event.added_anon') }) Message({ type: 'success', showClose: true, message: this.$auth.loggedIn ? this.$t('event.added') : this.$t('event.added_anon') })
} catch (e) { } catch (e) {
console.error(e.response)
switch (e.request.status) { switch (e.request.status) {
case 413: case 413:
Message({ type: 'error', showClose: true, message: this.$t('event.image_too_big') }) Message({ type: 'error', showClose: true, message: this.$t('event.image_too_big') })
break break
default: default:
Message({ type: 'error', showClose: true, message: e }) Message({ type: 'error', showClose: true, message: e.response.data })
} }
this.loading = false this.loading = false
console.error(e)
} }
} }
},
head () {
return {
title: `${this.settings.title} - ${this.$t('common.add_event')}`
}
} }
} }
</script> </script>
<style> <style style='less'>
i {
font-size: 1.3em;
}
#add_event { #add_event {
max-width: 800px; max-width: 800px;
} }
#picker { #picker {
max-width: 400px; max-width: 600px;
}
.el-upload,
.el-upload-dragger {
overflow: hidden;
text-align: center;
margin: 0 auto;
max-width: 80%;
} }
</style> </style>