gancio/pages/add/DateInput.vue

256 lines
9.1 KiB
Vue
Raw Normal View History

2020-10-25 00:31:38 +02:00
<template lang="pug">
2021-04-27 11:10:18 +02:00
v-col(cols=12)
2021-02-09 12:12:38 +01:00
.text-center
v-btn-toggle.v-col-6.flex-column.flex-sm-row(v-model='type' color='primary' @change='type => change("type", type)')
v-btn(value='normal' label="normal") {{$t('event.normal')}}
v-btn(value='multidate' label='multidate') {{$t('event.multidate')}}
v-btn(v-if='settings.allow_recurrent_event' value='recurrent' label="recurrent") {{$t('event.recurrent')}}
p {{$t(`event.${type}_description`)}}
2021-07-15 23:24:50 +02:00
2021-02-09 12:12:38 +01:00
v-btn-toggle.v-col-6.flex-column.flex-sm-row(v-if='type === "recurrent"' color='primary' :value='value.recurrent.frequency' @change='fq => change("frequency", fq)')
v-btn(v-for='f in frequencies' :key='f.value' :value='f.value') {{f.text}}
2021-07-15 23:24:50 +02:00
2021-02-09 12:12:38 +01:00
client-only
.datePicker.mt-3
2021-02-27 00:53:12 +01:00
v-input(:value='fromDate'
:rules="[$validators.required('common.when')]")
2021-03-16 19:57:00 +01:00
vc-date-picker(
2021-02-27 00:53:12 +01:00
:value='fromDate'
@input="date => change('date', date)"
:is-range='type === "multidate"'
:attributes='attributes'
:locale='$i18n.locale'
:from-page.sync='page'
:is-dark="settings['theme.is_dark']"
is-inline
is-expanded
:min-date='type !== "recurrent" && new Date()')
2021-02-09 12:12:38 +01:00
div.text-center.mb-2(v-if='type === "recurrent"')
span(v-if='value.recurrent.frequency !== "1m" && value.recurrent.frequency !== "2m"') {{whenPatterns}}
2021-05-11 15:12:49 +02:00
v-btn-toggle.mt-1.flex-column.flex-sm-row(v-else :value='value.recurrent.type' color='primary' @change='fq => change("recurrentType", fq)')
2021-02-09 12:12:38 +01:00
v-btn(v-for='whenPattern in whenPatterns' :value='whenPattern.key' :key='whenPatterns.key' small) {{whenPattern.label}}
v-row.mt-3.col-md-6.mx-auto
v-col.col-12.col-sm-6
2021-02-27 00:53:12 +01:00
v-select(dense :label="$t('event.from')" :value='fromHour' clearable
2021-04-13 18:03:57 +02:00
:disabled='!value.from'
2021-02-27 00:53:12 +01:00
:rules="[$validators.required('event.from')]"
2021-02-09 12:12:38 +01:00
:items='hourList' @change='hr => change("fromHour", hr)')
v-col.col-12.col-sm-6
2021-04-13 18:03:57 +02:00
v-select(dense :label="$t('event.due')"
:disabled='!fromHour'
:value='dueHour' clearable
2021-02-09 12:12:38 +01:00
:items='hourList' @change='hr => change("dueHour", hr)')
2021-03-10 16:09:35 +01:00
List(v-if='type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
2020-10-25 00:31:38 +02:00
</template>
<script>
import dayjs from 'dayjs'
import { mapState } from 'vuex'
import List from '@/components/List'
2021-03-10 16:09:35 +01:00
import { attributesFromEvents } from '../../assets/helper'
2020-10-25 00:31:38 +02:00
export default {
2021-03-10 16:09:35 +01:00
name: 'DateInput',
2020-10-25 00:31:38 +02:00
components: { List },
props: {
value: { type: Object, default: () => ({ from: null, due: null, recurrent: null }) },
event: { type: Object, default: () => null }
2020-10-25 00:31:38 +02:00
},
data () {
return {
2021-02-09 12:12:38 +01:00
type: 'normal',
page: null,
2021-03-10 16:09:35 +01:00
events: [],
2020-10-25 00:31:38 +02:00
frequencies: [
{ value: '1w', text: this.$t('event.each_week') },
{ value: '2w', text: this.$t('event.each_2w') },
{ value: '1m', text: this.$t('event.each_month') }
]
}
},
computed: {
2021-03-10 16:09:35 +01:00
...mapState(['settings', 'tags']),
todayEvents () {
const start = dayjs(this.value.from).startOf('day').unix()
const end = dayjs(this.value.from).endOf('day').unix()
const events = this.events.filter(e => (this.event.id && e.id !== this.event.id) && e.start_datetime >= start && e.start_datetime <= end)
2021-03-10 16:09:35 +01:00
return events
},
attributes () {
return attributesFromEvents(this.events, this.tags)
},
2021-02-09 12:12:38 +01:00
fromDate () {
if (this.value.multidate) {
2021-02-27 00:53:12 +01:00
return ({ start: dayjs(this.value.from).toDate(), end: dayjs(this.value.due).toDate() })
2021-02-09 12:12:38 +01:00
}
2021-02-27 00:53:12 +01:00
return this.value.from ? dayjs(this.value.from).toDate() : null
2021-02-09 12:12:38 +01:00
},
fromHour () {
2021-02-27 00:53:12 +01:00
return this.value.from && this.value.fromHour ? dayjs(this.value.from).format('HH:mm') : null
2021-02-09 12:12:38 +01:00
},
dueHour () {
2021-02-27 00:53:12 +01:00
return this.value.due && this.value.dueHour ? dayjs(this.value.due).format('HH:mm') : null
2021-02-09 12:12:38 +01:00
},
hourList () {
const hourList = []
2021-07-15 23:35:16 +02:00
const leftPad = h => ('00' + h).slice(-2)
2021-02-09 12:12:38 +01:00
for (let h = 0; h < 24; h++) {
2021-07-15 23:35:16 +02:00
const textHour = leftPad(h < 13 ? h : h - 12)
hourList.push({ text: textHour + ':00 ' + (h <= 12 ? 'AM' : 'PM'), value: leftPad(h) + ':00' })
hourList.push({ text: textHour + ':30 ' + (h <= 12 ? 'AM' : 'PM'), value: leftPad(h) + ':30' })
2021-02-09 12:12:38 +01:00
}
2021-07-15 23:24:50 +02:00
2021-02-09 12:12:38 +01:00
return hourList
},
2020-10-25 00:31:38 +02:00
whenPatterns () {
2021-01-11 00:17:56 +01:00
if (!this.value.from) { return }
const date = dayjs(this.value.from)
2020-10-25 00:31:38 +02:00
2020-11-13 00:13:59 +01:00
const freq = this.value.recurrent.frequency
2020-10-25 00:31:38 +02:00
const weekDay = date.format('dddd')
if (freq === '1w' || freq === '2w') {
2021-01-11 00:17:56 +01:00
return this.$t(`event.recurrent_${freq}_days`, { days: weekDay }).toUpperCase()
2020-10-25 00:31:38 +02:00
} else if (freq === '1m' || freq === '2m') {
const monthDay = date.format('D')
const n = Math.floor((monthDay - 1) / 7) + 1
const patterns = [
{ label: this.$t(`event.recurrent_${freq}_days`, { days: monthDay }), key: 'ordinal' }
// { label: this.$tc(`event.recurrent_${freq}_ordinal`, { n, days: weekDay }), key: 'weekday' }
]
2021-05-11 15:12:49 +02:00
if (n < 5) {
2020-10-25 00:31:38 +02:00
patterns.push(
{
2021-05-11 15:12:49 +02:00
label: this.$t(`event.recurrent_${freq}_ordinal`, { n: this.$t(`ordinal.${n}`), days: weekDay }),
key: n
2020-10-25 00:31:38 +02:00
}
)
}
2021-05-11 15:12:49 +02:00
// if selected day is in last week, propose also this type of selection
const lastWeek = date.daysInMonth() - monthDay < 7
if (lastWeek) {
2020-10-25 00:31:38 +02:00
patterns.push(
{
2021-05-11 15:12:49 +02:00
label: this.$t(`event.recurrent_${freq}_ordinal`, { n: this.$t('ordinal.-1'), days: weekDay }),
key: -1
2020-10-25 00:31:38 +02:00
}
)
}
2021-05-11 15:12:49 +02:00
2020-10-25 00:31:38 +02:00
return patterns
} else if (freq === '1d') {
return this.$t('event.recurrent_each_day')
}
return ''
}
},
2021-03-10 16:09:35 +01:00
async mounted () {
2021-02-09 12:12:38 +01:00
if (this.value.multidate) {
this.type = 'multidate'
} else if (this.value.recurrent) {
this.type = 'recurrent'
} else {
this.type = 'normal'
}
2021-03-10 16:09:35 +01:00
this.events = await this.$api.getEvents({
start: dayjs().unix()
})
2021-02-09 12:12:38 +01:00
},
2020-10-25 00:31:38 +02:00
methods: {
2021-01-11 00:17:56 +01:00
updateRecurrent (value) {
2021-02-09 12:12:38 +01:00
this.$emit('input', { ...this.value, recurrent: value || null })
2021-01-11 00:17:56 +01:00
},
2021-02-09 12:12:38 +01:00
change (what, value) {
// change event's type
if (what === 'type') {
if (typeof value === 'undefined') { this.type = 'normal' }
2021-02-09 12:12:38 +01:00
if (value === 'recurrent') {
2021-07-15 23:24:50 +02:00
this.$emit('input', { ...this.value, recurrent: { frequency: '1w' }, multidate: false })
2021-02-09 12:12:38 +01:00
} else if (value === 'multidate') {
this.$emit('input', { ...this.value, recurrent: null, multidate: true })
} else {
2021-03-15 22:27:59 +01:00
let from = this.value.from
if (from && from.start) {
from = from.start
2021-02-09 12:12:38 +01:00
}
2021-03-15 22:27:59 +01:00
let due = this.value.due
if (due && due.start) {
due = due.start
}
this.$emit('input', { ...this.value, from, due, recurrent: null, multidate: false })
2021-02-09 12:12:38 +01:00
}
} else if (what === 'frequency') {
this.$emit('input', { ...this.value, recurrent: { ...this.value.recurrent, frequency: value } })
2021-05-11 15:12:49 +02:00
} else if (what === 'recurrentType') {
this.$emit('input', { ...this.value, recurrent: { ...this.value.recurrent, type: value } })
2021-02-09 12:12:38 +01:00
} else if (what === 'fromHour') {
2021-02-27 00:53:12 +01:00
if (value) {
const [hour, minute] = value.split(':')
const from = dayjs(this.value.from).hour(hour).minute(minute).second(0)
2021-02-27 00:53:12 +01:00
this.$emit('input', { ...this.value, from, fromHour: true })
} else {
this.$emit('input', { ...this.value, fromHour: false })
}
2021-02-09 12:12:38 +01:00
} else if (what === 'dueHour') {
2021-02-27 00:53:12 +01:00
if (value) {
const [hour, minute] = value.split(':')
const fromHour = dayjs(this.value.from).hour()
// add a day
let due = dayjs(this.value.from)
2021-02-27 00:53:12 +01:00
if (fromHour > Number(hour) && !this.value.multidate) {
due = due.add(1, 'day')
}
due = due.hour(hour).minute(minute).second(0)
2021-02-27 00:53:12 +01:00
this.$emit('input', { ...this.value, due, dueHour: true })
} else {
this.$emit('input', { ...this.value, due: null, dueHour: false })
2021-02-09 12:12:38 +01:00
}
2021-05-11 15:12:49 +02:00
// change date in calendar (could be a range or a recurrent event...)
2021-02-09 12:12:38 +01:00
} else if (what === 'date') {
2021-04-21 11:42:03 +02:00
if (value === null) {
this.$emit('input', { ...this.value, from: null, fromHour: false })
return
}
2021-02-09 12:12:38 +01:00
if (this.value.multidate) {
2021-02-27 00:53:12 +01:00
let from = value.start
let due = value.end
if (this.value.fromHour) {
from = dayjs(value.start).hour(dayjs(this.value.from).hour())
}
if (this.value.dueHour) {
due = dayjs(value.end).hour(dayjs(this.value.due).hour())
}
this.$emit('input', { ...this.value, from, due })
2021-02-09 12:12:38 +01:00
} else {
2021-02-27 00:53:12 +01:00
let from = value
let due = this.value.due
2021-02-27 00:53:12 +01:00
if (this.value.fromHour) {
from = dayjs(value).hour(dayjs(this.value.from).hour())
}
if (this.value.dueHour && this.value.due) {
2021-02-27 00:53:12 +01:00
due = dayjs(value).hour(dayjs(this.value.due).hour())
}
this.$emit('input', { ...this.value, from, due })
2021-02-09 12:12:38 +01:00
}
}
},
2020-10-25 00:31:38 +02:00
}
}
</script>
2021-02-09 12:12:38 +01:00
<style>
2021-02-09 12:12:38 +01:00
.datePicker {
max-width: 500px !important;
margin: 0 auto;
}
</style>