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}">
|
2022-02-05 20:29:57 +01:00
|
|
|
<slot name="trigger" :toggle="toggle">
|
|
|
|
<x-button @click.prevent.stop="toggle()" type="secondary" :shadow="false" class="mb-2">
|
|
|
|
{{ $t('task.show.select') }}
|
|
|
|
</x-button>
|
|
|
|
</slot>
|
2021-12-29 20:54:01 +01:00
|
|
|
</template>
|
|
|
|
<template #content="{isOpen}">
|
|
|
|
<div class="datepicker-with-range" :class="{'is-open': isOpen}">
|
|
|
|
<div class="selections">
|
2022-01-09 16:46:12 +01:00
|
|
|
<button @click="setDateRange(null)" :class="{'is-active': customRangeActive}">
|
|
|
|
{{ $t('misc.custom') }}
|
|
|
|
</button>
|
2022-01-09 12:03:35 +01:00
|
|
|
<button
|
|
|
|
v-for="(value, text) in dateRanges"
|
|
|
|
:key="text"
|
2022-01-09 12:50:50 +01:00
|
|
|
@click="setDateRange(value)"
|
2022-01-09 14:05:23 +01:00
|
|
|
:class="{'is-active': from === value[0] && to === value[1]}">
|
2022-01-09 16:46:12 +01:00
|
|
|
{{ $t(`input.datepickerRange.ranges.${text}`) }}
|
2021-12-29 20:54:01 +01:00
|
|
|
</button>
|
|
|
|
</div>
|
2022-01-09 13:33:12 +01:00
|
|
|
<div class="flatpickr-container input-group">
|
|
|
|
<label class="label">
|
|
|
|
{{ $t('input.datepickerRange.from') }}
|
|
|
|
<div class="field has-addons">
|
|
|
|
<div class="control is-fullwidth">
|
2022-01-09 14:11:28 +01:00
|
|
|
<input class="input" type="text" v-model="from" @change="inputChanged"/>
|
2022-01-09 13:33:12 +01:00
|
|
|
</div>
|
|
|
|
<div class="control">
|
|
|
|
<x-button icon="calendar" variant="secondary" data-toggle/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
<label class="label">
|
|
|
|
{{ $t('input.datepickerRange.to') }}
|
|
|
|
<div class="field has-addons">
|
|
|
|
<div class="control is-fullwidth">
|
2022-01-09 14:11:28 +01:00
|
|
|
<input class="input" type="text" v-model="to" @change="inputChanged"/>
|
2022-01-09 13:33:12 +01:00
|
|
|
</div>
|
|
|
|
<div class="control">
|
|
|
|
<x-button icon="calendar" variant="secondary" data-toggle/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</label>
|
2021-12-29 20:54:01 +01:00
|
|
|
<flat-pickr
|
|
|
|
:config="flatPickerConfig"
|
2022-01-09 14:05:23 +01:00
|
|
|
v-model="flatpickrRange"
|
2021-12-29 20:54:01 +01:00
|
|
|
/>
|
2022-01-09 16:31:17 +01:00
|
|
|
|
|
|
|
<p>
|
|
|
|
{{ $t('input.datepickerRange.math.canuse') }}
|
|
|
|
<a @click="showHowItWorks = true">{{ $t('input.datepickerRange.math.learnhow') }}</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<modal
|
|
|
|
@close="() => showHowItWorks = false"
|
|
|
|
:enabled="showHowItWorks"
|
|
|
|
transition-name="fade"
|
|
|
|
:overflow="true"
|
|
|
|
variant="hint-modal"
|
|
|
|
>
|
|
|
|
<card class="has-no-shadow how-it-works-modal" :title="$t('input.datepickerRange.math.title')">
|
|
|
|
<p>
|
|
|
|
{{ $t('input.datepickerRange.math.intro') }}
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<i18n-t keypath="input.datepickerRange.math.expression">
|
|
|
|
<code>now</code>
|
|
|
|
<code>||</code>
|
|
|
|
</i18n-t>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<i18n-t keypath="input.datepickerRange.math.similar">
|
|
|
|
<a href="https://grafana.com/docs/grafana/latest/dashboards/time-range-controls/" rel="noreferrer noopener nofollow" target="_blank">
|
|
|
|
Grafana
|
|
|
|
</a>
|
|
|
|
<a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.3/common-options.html#date-math" rel="noreferrer noopener nofollow" target="_blank">
|
|
|
|
Elasticsearch
|
|
|
|
</a>
|
|
|
|
</i18n-t>
|
|
|
|
</p>
|
|
|
|
<p>{{ $t('misc.forExample') }}</p>
|
|
|
|
<ul>
|
|
|
|
<li><code>+1d</code>{{ $t('input.datepickerRange.math.add1Day') }}</li>
|
|
|
|
<li><code>-1d</code>{{ $t('input.datepickerRange.math.minus1Day') }}</li>
|
|
|
|
<li><code>/d</code>{{ $t('input.datepickerRange.math.roundDay') }}</li>
|
|
|
|
</ul>
|
|
|
|
<p>{{ $t('input.datepickerRange.math.supportedUnits') }}</p>
|
|
|
|
<table class="table">
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td><code>s</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.seconds') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>m</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.minutes') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>h</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.hours') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>H</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.hours') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>d</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.days') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>w</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.weeks') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>M</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.months') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>y</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.units.years') }}</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<p>{{ $t('input.datepickerRange.math.someExamples') }}</p>
|
|
|
|
<table class="table">
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td><code>now</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.now') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>now+24h</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.in24h') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>now/d</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.today') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>now/w</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.beginningOfThisWeek') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>now/w+1w</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.endOfThisWeek') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>now+30d</code></td>
|
|
|
|
<td>{{ $t('input.datepickerRange.math.examples.in30Days') }}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><code>{{ exampleDate }}||+1M/d</code></td>
|
|
|
|
<td>
|
|
|
|
<i18n-t keypath="input.datepickerRange.math.examples.datePlusMonth">
|
|
|
|
<code>{{ exampleDate }}</code>
|
|
|
|
</i18n-t>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</card>
|
|
|
|
</modal>
|
2021-12-29 20:54:01 +01:00
|
|
|
</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'
|
2022-01-09 14:21:33 +01:00
|
|
|
import {computed, ref, watch} from 'vue'
|
2021-12-29 15:44:13 +01:00
|
|
|
import {useI18n} from 'vue-i18n'
|
|
|
|
import {store} from '@/store'
|
2022-01-09 16:31:17 +01:00
|
|
|
import {format} from 'date-fns'
|
2022-01-09 14:21:33 +01:00
|
|
|
import Popup from '@/components/misc/popup.vue'
|
|
|
|
|
|
|
|
import {dateRanges} from '@/components/date/dateRanges'
|
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,
|
2022-01-09 13:33:12 +01:00
|
|
|
wrap: true,
|
2021-12-29 15:44:13 +01:00
|
|
|
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
|
|
|
}))
|
|
|
|
|
2022-01-09 16:31:17 +01:00
|
|
|
const showHowItWorks = ref(false)
|
|
|
|
const exampleDate = format(new Date(), 'yyyy-MM-dd')
|
|
|
|
|
2022-01-09 14:05:23 +01:00
|
|
|
const flatpickrRange = ref('')
|
|
|
|
|
|
|
|
const from = ref('')
|
|
|
|
const to = ref('')
|
|
|
|
|
|
|
|
function emitChanged() {
|
|
|
|
emit('dateChanged', {
|
2022-01-09 14:06:51 +01:00
|
|
|
dateFrom: from.value === '' ? null : from.value,
|
|
|
|
dateTo: to.value === '' ? null : to.value,
|
2022-01-09 14:05:23 +01:00
|
|
|
})
|
|
|
|
}
|
2021-12-29 15:44:13 +01:00
|
|
|
|
2022-01-09 14:11:28 +01:00
|
|
|
function inputChanged() {
|
|
|
|
flatpickrRange.value = ''
|
|
|
|
emitChanged()
|
|
|
|
}
|
|
|
|
|
2021-12-29 15:44:13 +01:00
|
|
|
watch(
|
2022-01-09 14:05:23 +01:00
|
|
|
() => flatpickrRange.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
|
|
|
|
2022-01-09 14:05:23 +01:00
|
|
|
from.value = fromDate
|
|
|
|
to.value = toDate
|
|
|
|
|
|
|
|
emitChanged()
|
2021-12-29 21:33:55 +01:00
|
|
|
},
|
2021-12-29 15:44:13 +01:00
|
|
|
)
|
|
|
|
|
2022-01-09 14:05:23 +01:00
|
|
|
function setDateRange(range: string[] | null) {
|
|
|
|
if (range === null) {
|
|
|
|
from.value = ''
|
|
|
|
to.value = ''
|
2022-01-09 14:11:28 +01:00
|
|
|
inputChanged()
|
2022-01-09 14:05:23 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
from.value = range[0]
|
|
|
|
to.value = range[1]
|
|
|
|
|
2022-01-09 14:11:28 +01:00
|
|
|
inputChanged()
|
2021-12-29 15:44:13 +01:00
|
|
|
}
|
|
|
|
|
2021-12-29 16:00:06 +01:00
|
|
|
const customRangeActive = computed<Boolean>(() => {
|
2022-01-09 14:05:23 +01:00
|
|
|
return !Object.values(dateRanges).some(el => from.value === el[0] && to.value === el[1])
|
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;
|
2021-12-29 20:54:01 +01:00
|
|
|
|
|
|
|
:deep(.popup) {
|
2021-12-29 21:12:43 +01:00
|
|
|
z-index: 10;
|
2021-12-29 20:54:01 +01:00
|
|
|
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);
|
2022-01-09 13:33:12 +01:00
|
|
|
padding: 1rem;
|
|
|
|
font-size: .9rem;
|
2021-12-29 15:44:13 +01:00
|
|
|
|
2022-01-09 13:33:12 +01:00
|
|
|
// Flatpickr has no option to use it without an input field so we're hiding it instead
|
|
|
|
:deep(input.form-control.input) {
|
|
|
|
height: 0;
|
|
|
|
padding: 0;
|
|
|
|
border: 0;
|
|
|
|
}
|
2022-01-09 14:05:23 +01:00
|
|
|
|
2022-01-09 13:33:12 +01:00
|
|
|
.field .control :deep(.button) {
|
|
|
|
border: 1px solid var(--input-border-color);
|
|
|
|
height: 2.25rem;
|
2022-01-09 14:05:23 +01:00
|
|
|
|
2022-01-09 13:33:12 +01:00
|
|
|
&:hover {
|
|
|
|
border: 1px solid var(--input-hover-border-color);
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 14:05:23 +01:00
|
|
|
|
2022-01-09 13:33:12 +01:00
|
|
|
.label, .input, :deep(.button) {
|
|
|
|
font-size: .9rem;
|
2021-12-29 15:44:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.selections {
|
|
|
|
width: 30%;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-12-29 20:36:53 +01:00
|
|
|
padding-top: .5rem;
|
2022-01-09 16:46:12 +01:00
|
|
|
overflow-y: scroll;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-09 16:31:17 +01:00
|
|
|
|
|
|
|
.how-it-works-modal{
|
|
|
|
font-size: 1rem;
|
|
|
|
|
|
|
|
p {
|
|
|
|
display: inline-block !important;
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 15:44:13 +01:00
|
|
|
</style>
|