2021-12-29 15:44:13 +01:00
|
|
|
<template>
|
2021-12-29 20:36:53 +01:00
|
|
|
<div class="datepicker-with-range-container">
|
2021-12-29 20:54:01 +01:00
|
|
|
<popup>
|
|
|
|
<template #trigger="{toggle}">
|
|
|
|
<a @click.prevent.stop="toggle()">
|
|
|
|
{{ $t('task.show.select') }}
|
|
|
|
</a>
|
|
|
|
</template>
|
|
|
|
<template #content="{isOpen}">
|
|
|
|
<div class="datepicker-with-range" :class="{'is-open': isOpen}">
|
|
|
|
<div class="selections">
|
|
|
|
<button @click="setDateRange(datesToday)" :class="{'is-active': dateRange === datesToday}">
|
|
|
|
{{ $t('task.show.today') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesThisWeek)"
|
|
|
|
:class="{'is-active': dateRange === datesThisWeek}">
|
|
|
|
{{ $t('task.show.thisWeek') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesNextWeek)"
|
|
|
|
:class="{'is-active': dateRange === datesNextWeek}">
|
|
|
|
{{ $t('task.show.nextWeek') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesNext7Days)"
|
|
|
|
:class="{'is-active': dateRange === datesNext7Days}">
|
|
|
|
{{ $t('task.show.next7Days') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesThisMonth)"
|
|
|
|
:class="{'is-active': dateRange === datesThisMonth}">
|
|
|
|
{{ $t('task.show.thisMonth') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesNextMonth)"
|
|
|
|
:class="{'is-active': dateRange === datesNextMonth}">
|
|
|
|
{{ $t('task.show.nextMonth') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange(datesNext30Days)"
|
|
|
|
:class="{'is-active': dateRange === datesNext30Days}">
|
|
|
|
{{ $t('task.show.next30Days') }}
|
|
|
|
</button>
|
|
|
|
<button @click="setDateRange('')" :class="{'is-active': customRangeActive}">
|
|
|
|
{{ $t('misc.custom') }}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div class="flatpickr-container">
|
|
|
|
<flat-pickr
|
|
|
|
:config="flatPickerConfig"
|
|
|
|
v-model="dateRange"
|
|
|
|
/>
|
|
|
|
</div>
|
2021-12-29 20:36:53 +01:00
|
|
|
</div>
|
2021-12-29 20:54:01 +01:00
|
|
|
</template>
|
|
|
|
</popup>
|
2021-12-29 15:44:13 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2021-12-29 16:00:06 +01:00
|
|
|
<script lang="ts" setup>
|
2021-12-29 15:44:13 +01:00
|
|
|
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'
|
2021-12-29 20:54:01 +01:00
|
|
|
import Popup from '@/components/misc/popup'
|
2021-12-29 15:44:13 +01:00
|
|
|
|
|
|
|
const {t} = useI18n()
|
|
|
|
|
|
|
|
const emit = defineEmits(['dateChanged'])
|
|
|
|
|
2021-12-29 16:51:21 +01:00
|
|
|
// FIXME: This seems to always contain the default value - that breaks the picker
|
|
|
|
const weekStart = computed<number>(() => store.state.auth.settings.weekStart ?? 0)
|
2021-12-29 15:44:13 +01:00
|
|
|
const flatPickerConfig = computed(() => ({
|
|
|
|
altFormat: t('date.altFormatLong'),
|
|
|
|
altInput: true,
|
|
|
|
dateFormat: 'Y-m-d H:i',
|
2021-12-29 16:03:29 +01:00
|
|
|
enableTime: false,
|
2021-12-29 15:44:13 +01:00
|
|
|
inline: true,
|
|
|
|
mode: 'range',
|
2021-12-29 16:51:21 +01:00
|
|
|
locale: {
|
2021-12-29 17:08:33 +01:00
|
|
|
firstDayOf7Days: weekStart.value,
|
2021-12-29 16:51:21 +01:00
|
|
|
},
|
2021-12-29 15:44:13 +01:00
|
|
|
}))
|
|
|
|
|
2021-12-29 20:36:53 +01:00
|
|
|
const showPopup = ref<Boolean>(false)
|
2021-12-29 16:00:06 +01:00
|
|
|
const dateRange = ref<string>('')
|
2021-12-29 15:44:13 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:44:13 +01:00
|
|
|
const [fromDate, toDate] = newVal.split(' to ')
|
2021-12-29 15:56:50 +01:00
|
|
|
|
2021-12-29 15:44:13 +01:00
|
|
|
if (typeof fromDate === 'undefined' || typeof toDate === 'undefined') {
|
|
|
|
return
|
|
|
|
}
|
2021-12-29 15:56:50 +01:00
|
|
|
|
2021-12-29 15:44:13 +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 {
|
2021-12-29 15:44:13 +01:00
|
|
|
return format(date, 'yyyy-MM-dd HH:mm')
|
|
|
|
}
|
|
|
|
|
2021-12-29 16:48:34 +01:00
|
|
|
function startOfDay(date: Date): Date {
|
|
|
|
date.setHours(0)
|
|
|
|
date.setMinutes(0)
|
|
|
|
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
|
|
|
function endOfDay(date: Date): Date {
|
|
|
|
date.setHours(23)
|
|
|
|
date.setMinutes(59)
|
|
|
|
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
2021-12-29 16:00:06 +01:00
|
|
|
const datesToday = computed<string>(() => {
|
2021-12-29 16:48:34 +01:00
|
|
|
const startDate = startOfDay(new Date())
|
|
|
|
const endDate = endOfDay(new Date())
|
|
|
|
|
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
|
|
|
|
|
|
|
function thisWeek() {
|
|
|
|
const startDate = startOfDay(new Date())
|
2021-12-29 16:51:21 +01:00
|
|
|
const first = startDate.getDate() - startDate.getDay() + weekStart.value
|
2021-12-29 16:48:34 +01:00
|
|
|
startDate.setDate(first)
|
|
|
|
const endDate = endOfDay(new Date((new Date(startDate).setDate(first + 6))))
|
|
|
|
|
|
|
|
return {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const datesThisWeek = computed<string>(() => {
|
|
|
|
const {startDate, endDate} = thisWeek()
|
|
|
|
|
2021-12-29 15:56:50 +01:00
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
2021-12-29 15:44:13 +01:00
|
|
|
|
2021-12-29 16:00:06 +01:00
|
|
|
const datesNextWeek = computed<string>(() => {
|
2021-12-29 16:48:34 +01:00
|
|
|
const {startDate, endDate} = thisWeek()
|
|
|
|
startDate.setDate(startDate.getDate() + 7)
|
|
|
|
endDate.setDate(endDate.getDate() + 7)
|
|
|
|
|
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
|
|
|
|
|
|
|
const datesNext7Days = computed<string>(() => {
|
|
|
|
const startDate = startOfDay(new Date())
|
|
|
|
const endDate = endOfDay(new Date((new Date()).getTime() + 7 * 24 * 60 * 60 * 1000))
|
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
|
|
|
|
|
|
|
function thisMonth() {
|
|
|
|
const startDate = startOfDay(new Date())
|
|
|
|
startDate.setDate(1)
|
|
|
|
const endDate = endOfDay(new Date((new Date()).getFullYear(), (new Date()).getMonth() + 1, 0))
|
|
|
|
|
|
|
|
return {
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const datesThisMonth = computed<string>(() => {
|
|
|
|
const {startDate, endDate} = thisMonth()
|
|
|
|
|
2021-12-29 15:56:50 +01:00
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
2021-12-29 15:44:13 +01:00
|
|
|
|
2021-12-29 16:00:06 +01:00
|
|
|
const datesNextMonth = computed<string>(() => {
|
2021-12-29 16:48:34 +01:00
|
|
|
const {startDate, endDate} = thisMonth()
|
|
|
|
|
|
|
|
startDate.setMonth(startDate.getMonth() + 1)
|
|
|
|
endDate.setMonth(endDate.getMonth() + 1)
|
|
|
|
|
|
|
|
return `${formatDate(startDate)} to ${formatDate(endDate)}`
|
|
|
|
})
|
|
|
|
|
|
|
|
const datesNext30Days = computed<string>(() => {
|
|
|
|
const startDate = startOfDay(new Date())
|
|
|
|
const endDate = endOfDay(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:44:13 +01:00
|
|
|
}
|
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 &&
|
2021-12-29 16:48:34 +01:00
|
|
|
dateRange.value !== datesThisWeek.value &&
|
2021-12-29 15:56:50 +01:00
|
|
|
dateRange.value !== datesNextWeek.value &&
|
2021-12-29 16:48:34 +01:00
|
|
|
dateRange.value !== datesNext7Days.value &&
|
|
|
|
dateRange.value !== datesThisMonth.value &&
|
|
|
|
dateRange.value !== datesNextMonth.value &&
|
|
|
|
dateRange.value !== datesNext30Days.value
|
2021-12-29 15:56:50 +01:00
|
|
|
})
|
2021-12-29 15:44:13 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-12-29 20:36:53 +01:00
|
|
|
.datepicker-with-range-container {
|
|
|
|
position: relative;
|
|
|
|
z-index: 10;
|
2021-12-29 20:54:01 +01:00
|
|
|
|
|
|
|
:deep(.popup) {
|
|
|
|
margin-top: 1rem;
|
|
|
|
border-radius: $radius;
|
|
|
|
border: 1px solid var(--grey-200);
|
|
|
|
background-color: var(--white);
|
|
|
|
box-shadow: $shadow;
|
|
|
|
|
|
|
|
&.is-open {
|
|
|
|
width: 500px;
|
|
|
|
height: 320px;
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 20:36:53 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 15:44:13 +01:00
|
|
|
.datepicker-with-range {
|
|
|
|
display: flex;
|
2021-12-29 20:54:01 +01:00
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2021-12-29 20:36:53 +01:00
|
|
|
position: absolute;
|
2021-12-29 15:44:13 +01:00
|
|
|
|
|
|
|
: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 20:36:53 +01:00
|
|
|
padding-top: .5rem;
|
2021-12-29 15:44:13 +01:00
|
|
|
|
2021-12-29 15:56:50 +01:00
|
|
|
button {
|
2021-12-29 15:44:13 +01:00
|
|
|
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:44:13 +01:00
|
|
|
|
2021-12-29 15:56:50 +01:00
|
|
|
&.is-active {
|
2021-12-29 15:44:13 +01:00
|
|
|
color: var(--primary);
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:56:50 +01:00
|
|
|
&:hover, &.is-active {
|
2021-12-29 15:44:13 +01:00
|
|
|
background-color: var(--grey-100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|