vikunja-frontend/src/components/date/datepickerWithRange.vue

159 lines
3.6 KiB
Vue
Raw Normal View History

<template>
<div class="datepicker-with-range">
<div class="selections">
2021-12-29 15:56:50 +01:00
<button @click="setDateRange(datesToday)" :class="{'is-active': dateRange === datesToday}">
{{ $t('task.show.today') }}
</button>
<button @click="setDateRange(datesNextWeek)" :class="{'is-active': dateRange === datesNextWeek}">
{{ $t('task.show.nextWeek') }}
</button>
<button @click="setDateRange(datesNextMonth)" :class="{'is-active': dateRange === datesNextMonth}">
{{ $t('task.show.nextMonth') }}
</button>
<button :class="{'is-active': customRangeActive}">
{{ $t('misc.custom') }}
</button>
</div>
<div class="flatpickr-container">
<flat-pickr
:config="flatPickerConfig"
v-model="dateRange"
/>
</div>
</div>
</template>
2021-12-29 16:00:06 +01:00
<script lang="ts" setup>
import flatPickr from 'vue-flatpickr-component'
import 'flatpickr/dist/flatpickr.css'
import {computed, ref, watch} from 'vue'
import {useI18n} from 'vue-i18n'
import {store} from '@/store'
import {format} from 'date-fns'
const {t} = useI18n()
const emit = defineEmits(['dateChanged'])
2021-12-29 16:00:06 +01:00
const weekStart = computed<number>(() => store.state.auth.settings.weekStart)
const flatPickerConfig = computed(() => ({
altFormat: t('date.altFormatLong'),
altInput: true,
dateFormat: 'Y-m-d H:i',
enableTime: true,
time_24hr: true,
inline: true,
mode: 'range',
locale: {
// FIXME: This seems to always contain the default value
firstDayOfWeek: weekStart,
},
}))
2021-12-29 16:00:06 +01:00
const dateRange = ref<string>('')
watch(
() => dateRange.value,
2021-12-29 16:00:06 +01:00
(newVal: string | null) => {
2021-12-29 15:56:50 +01:00
if (newVal === null) {
return
}
const [fromDate, toDate] = newVal.split(' to ')
2021-12-29 15:56:50 +01:00
if (typeof fromDate === 'undefined' || typeof toDate === 'undefined') {
return
}
2021-12-29 15:56:50 +01:00
emit('dateChanged', {
dateFrom: new Date(fromDate),
dateTo: new Date(toDate),
})
}
)
2021-12-29 16:00:06 +01:00
function formatDate(date: Date): string {
return format(date, 'yyyy-MM-dd HH:mm')
}
2021-12-29 16:00:06 +01:00
const datesToday = computed<string>(() => {
const startDate = new Date()
const endDate = new Date((new Date()).setDate((new Date()).getDate() + 1))
2021-12-29 15:56:50 +01:00
return `${formatDate(startDate)} to ${formatDate(endDate)}`
})
2021-12-29 16:00:06 +01:00
const datesNextWeek = computed<string>(() => {
const startDate = new Date()
const endDate = new Date((new Date()).getTime() + 7 * 24 * 60 * 60 * 1000)
2021-12-29 15:56:50 +01:00
return `${formatDate(startDate)} to ${formatDate(endDate)}`
})
2021-12-29 16:00:06 +01:00
const datesNextMonth = computed<string>(() => {
const startDate = new Date()
const endDate = new Date((new Date()).setMonth((new Date()).getMonth() + 1))
2021-12-29 15:56:50 +01:00
return `${formatDate(startDate)} to ${formatDate(endDate)}`
})
2021-12-29 16:00:06 +01:00
function setDateRange(range: string) {
2021-12-29 15:56:50 +01:00
dateRange.value = range
}
2021-12-29 15:56:50 +01:00
2021-12-29 16:00:06 +01:00
const customRangeActive = computed<Boolean>(() => {
2021-12-29 15:56:50 +01:00
return dateRange.value !== datesToday.value &&
dateRange.value !== datesNextWeek.value &&
dateRange.value !== datesNextMonth.value
})
</script>
<style lang="scss" scoped>
.datepicker-with-range {
border-radius: $radius;
border: 1px solid var(--grey-200);
background-color: var(--white);
box-shadow: $shadow;
display: flex;
width: 500px;
:deep(.flatpickr-calendar) {
margin: 0 auto 8px;
box-shadow: none;
}
}
.flatpickr-container {
width: 70%;
border-left: 1px solid var(--grey-200);
:deep(input.input) {
display: none;
}
}
.selections {
width: 30%;
display: flex;
flex-direction: column;
2021-12-29 15:56:50 +01:00
button {
display: block;
width: 100%;
text-align: left;
padding: .5rem 1rem;
transition: $transition;
font-size: .9rem;
color: var(--text);
2021-12-29 15:56:50 +01:00
background: transparent;
border: 0;
cursor: pointer;
2021-12-29 15:56:50 +01:00
&.is-active {
color: var(--primary);
}
2021-12-29 15:56:50 +01:00
&:hover, &.is-active {
background-color: var(--grey-100);
}
}
}
</style>