gancio/components/DateInput.vue

289 lines
10 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(v-if='settings.allow_multidate_event' value='multidate' label='multidate') {{ $t('event.multidate') }}
v-btn(v-if='settings.allow_recurrent_event' value='recurrent' label="recurrent") {{ $t('event.recurrent') }}
2021-02-09 12:12:38 +01:00
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
v-input(:value='fromDate' :rules="[$validators.required('common.when')]")
2021-03-16 19:57:00 +01:00
vc-date-picker(
2022-11-09 10:18:42 +01:00
:value='fromDate'
2021-02-27 00:53:12 +01:00
:is-range='type === "multidate"'
2022-11-06 00:21:20 +01:00
@input="date => change('date', date)"
2021-02-27 00:53:12 +01:00
:attributes='attributes'
:locale='$i18n.locale'
:is-dark="settings['theme.is_dark']"
is-inline
is-expanded
:min-date='type !== "recurrent" && new Date()')
2023-01-09 16:56:01 +01:00
//- template(#placeholder)
.d-flex.calh.justify-center(slot='placeholder')
v-progress-circular(indeterminate)
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)')
v-btn(v-for='whenPattern in whenPatterns' :value='whenPattern.key' :key='whenPatterns.key' small) {{ whenPattern.label }}
2021-02-09 12:12:38 +01:00
v-row.mt-3.col-md-6.mx-auto
v-col.col-12.col-sm-6
2022-06-03 16:22:33 +02:00
v-menu(
v-model="menuFromHour"
:close-on-content-click="false"
offset-y
2022-11-09 10:18:42 +01:00
:value="value.fromHour"
2022-06-03 16:22:33 +02:00
transition="scale-transition")
template(v-slot:activator="{ on, attrs }")
v-text-field(
clearable
:clear-icon='mdiClose'
@click:clear='() => change("fromHour")'
2022-06-03 16:22:33 +02:00
:label="$t('event.from')"
2022-11-09 10:18:42 +01:00
:value="value.fromHour"
2022-06-03 16:22:33 +02:00
:disabled='!value.from'
2022-06-22 14:07:28 +02:00
readonly
2022-06-03 16:22:33 +02:00
:prepend-icon="mdiClockTimeFourOutline"
:rules="[$validators.required('event.from')]"
v-bind="attrs"
v-on="on")
v-time-picker(
v-if="menuFromHour"
2022-11-09 10:18:42 +01:00
:value="value.fromHour"
2022-06-03 16:22:33 +02:00
:allowedMinutes='allowedMinutes'
format='24hr'
@click:minute='menuFromHour = false'
@input='hr => change("fromHour", hr)')
2022-06-03 16:22:33 +02:00
2021-02-09 12:12:38 +01:00
v-col.col-12.col-sm-6
2022-06-03 16:22:33 +02:00
v-menu(
v-model="menuDueHour"
:close-on-content-click="false"
offset-y
2022-11-09 10:18:42 +01:00
:value="value.dueHour"
2022-06-03 16:22:33 +02:00
transition="scale-transition")
template(v-slot:activator="{ on, attrs }")
v-text-field(
clearable
:clear-icon='mdiClose'
@click:clear='() => change("dueHour")'
2022-06-03 16:22:33 +02:00
:label="$t('event.due')"
2022-11-09 10:18:42 +01:00
:value="value.dueHour"
:disabled='!value.fromHour'
2022-06-22 14:07:28 +02:00
readonly
2022-06-03 16:22:33 +02:00
:prepend-icon="mdiClockTimeEightOutline"
v-bind="attrs"
v-on="on")
v-time-picker(
v-if="menuDueHour"
2022-11-09 10:18:42 +01:00
:value="value.dueHour"
2022-06-03 16:22:33 +02:00
:allowedMinutes='allowedMinutes'
format='24hr'
@click:minute='menuDueHour = false'
@input='hr => change("dueHour", hr)')
2021-02-09 12:12:38 +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'
2023-01-09 16:56:01 +01:00
import { mapState, mapActions } from 'vuex'
2020-10-25 00:31:38 +02:00
import List from '@/components/List'
2022-05-26 11:10:21 +02:00
import { attributesFromEvents } from '../assets/helper'
import { mdiClockTimeFourOutline, mdiClockTimeEightOutline, mdiClose } from '@mdi/js'
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: {
2022-11-09 10:18:42 +01:00
value: { type: Object, default: () => ({ from: null, due: null, recurrent: null, fromHour: null, dueHour: null }) },
event: { type: Object, default: () => null }
2020-10-25 00:31:38 +02:00
},
data() {
2020-10-25 00:31:38 +02:00
return {
mdiClockTimeFourOutline, mdiClockTimeEightOutline, mdiClose,
2022-06-11 11:59:56 +02:00
allowedMinutes: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
2022-06-03 16:22:33 +02:00
menuFromHour: false,
menuDueHour: false,
type: this.value.type || 'normal',
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: {
2023-01-09 16:56:01 +01:00
...mapState(['settings', 'events']),
2022-11-09 10:18:42 +01:00
fromDate () {
if (this.value.from) {
if (this.value.multidate) {
return ({ start: dayjs(this.value.from).toDate(), end: dayjs(this.value.due).toDate() })
} else {
return new Date(this.value.from)
2022-11-06 00:21:20 +01:00
}
}
},
todayEvents() {
const start = dayjs.tz(this.value.from).startOf('day').unix()
const end = dayjs.tz(this.value.from).endOf('day').unix()
return this.events.filter(e => e.start_datetime >= start && e.start_datetime <= end)
2021-03-10 16:09:35 +01:00
},
attributes() {
2023-01-09 16:56:01 +01:00
return attributesFromEvents(this.events.filter(e => e.id !== this.event.id))
2021-03-10 16:09:35 +01: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 ''
}
},
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'
}
2023-01-09 16:56:01 +01:00
if (!this.events) {
this.getEvents()
}
2021-02-09 12:12:38 +01:00
},
2020-10-25 00:31:38 +02:00
methods: {
2023-01-09 16:56:01 +01:00
...mapActions(['getEvents']),
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
},
change(what, value) {
2021-02-09 12:12:38 +01:00
// 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) {
2022-11-09 10:18:42 +01:00
this.$emit('input', { ...this.value, fromHour: value })
2021-02-27 00:53:12 +01:00
} else {
2022-11-09 10:18:42 +01:00
this.$emit('input', { ...this.value, fromHour: null, dueHour: null })
2021-02-09 12:12:38 +01:00
}
2022-11-09 10:18:42 +01:00
} else if (what === 'dueHour') {
2021-02-27 00:53:12 +01:00
if (value) {
this.value.due = this.value.due ? this.value.due : this.value.from
2022-12-04 23:07:52 +01:00
const [hour, minute] = value.split(':')
const [fromHour, fromMinute] = this.value.fromHour.split(':')
if (!this.value.multidate) {
if (hour < fromHour) {
this.value.due = dayjs(this.value.from).add(1, 'day').toDate()
} else {
this.value.due = dayjs(this.value.from).toDate()
}
}
2021-02-27 00:53:12 +01:00
} else {
this.value.due = null
2021-02-09 12:12:38 +01:00
}
2022-11-09 10:18:42 +01:00
this.$emit('input', { ...this.value, dueHour: value })
// if (value) {
// // const [hour, minute] = value.split(':')
// // let due = dayjs.tz(this.value.due || this.value.from).hour(Number(hour)).minute(Number(minute)).second(0)
// // add a day
// // if (dayjs(this.value.from).hour() > Number(hour) && !this.value.multidate) {
// // due = due.add(1, 'day')
// // }
// // due = due.hour(hour).minute(minute).second(0)
// } else {
// this.$emit('input', { ...this.value, dueHour: null })
// }
// 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) {
2022-11-09 10:18:42 +01:00
this.$emit('input', { ...this.value, from: null, due: null, fromHour: null, dueHour: null })
2021-04-21 11:42:03 +02:00
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
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
2022-09-05 00:06:16 +02:00
let due = this.value.due
2021-02-27 00:53:12 +01:00
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>