Files
firefly-iii/frontend/src/components/dashboard/Calendar.vue

604 lines
17 KiB
Vue
Raw Normal View History

2020-11-15 14:05:04 +01:00
<!--
- Calendar.vue
- Copyright (c) 2020 james@firefly-iii.org
-
- This file is part of Firefly III (https://github.com/firefly-iii).
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<div>
<div class="row">
<div class="col">Start</div>
<div class="col-8">{{ new Intl.DateTimeFormat(locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(range.start) }}</div>
</div>
<div class="row">
<div class="col">End</div>
<div class="col-8">{{ new Intl.DateTimeFormat(locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(range.end) }}</div>
</div>
<date-picker
v-model="range"
2021-02-14 07:53:20 +01:00
:rows="2"
2020-11-15 14:05:04 +01:00
is-range
2021-02-26 06:39:20 +01:00
mode="date"
2020-11-15 14:05:04 +01:00
>
<template v-slot="{ inputValue, inputEvents, isDragging, togglePopover }">
2020-12-22 17:22:50 +01:00
<div class="row">
<div class="col">
<div class="btn-group btn-group-sm d-flex">
<button
2021-02-26 06:39:20 +01:00
:title="$t('firefly.custom_period')" class="btn btn-secondary btn-sm"
2021-02-14 07:53:20 +01:00
@click="togglePopover({ placement: 'auto-start', positionFixed: true })"
2020-12-22 17:22:50 +01:00
><i class="fas fa-calendar-alt"></i></button>
2021-02-26 06:39:20 +01:00
<button :title="$t('firefly.reset_to_current')"
2021-02-14 10:43:58 +01:00
class="btn btn-secondary"
2021-02-26 06:39:20 +01:00
@click="resetDate"
2020-12-22 17:22:50 +01:00
><i class="fas fa-history"></i></button>
2021-04-17 20:53:42 +02:00
<button id="dropdownMenuButton" :title="$t('firefly.select_period')" aria-expanded="false" aria-haspopup="true"
class="btn btn-secondary dropdown-toggle"
2021-02-26 06:39:20 +01:00
data-toggle="dropdown"
type="button">
2020-12-22 17:22:50 +01:00
<i class="fas fa-list"></i>
</button>
2021-02-26 06:39:20 +01:00
<div aria-labelledby="dropdownMenuButton" class="dropdown-menu">
2021-02-14 10:43:58 +01:00
<a v-for="period in periods" class="dropdown-item" href="#" @click="customDate(period.start, period.end)">{{ period.title }}</a>
2020-12-22 17:22:50 +01:00
</div>
2020-11-15 14:05:04 +01:00
2020-12-22 17:22:50 +01:00
</div>
2021-02-26 06:39:20 +01:00
<input v-on="inputEvents.start"
2020-12-22 17:22:50 +01:00
:class="isDragging ? 'text-gray-600' : 'text-gray-900'"
:value="inputValue.start"
2021-02-26 06:39:20 +01:00
type="hidden"
2020-12-22 17:22:50 +01:00
/>
2021-02-26 06:39:20 +01:00
<input v-on="inputEvents.end"
2020-12-22 17:22:50 +01:00
:class="isDragging ? 'text-gray-600' : 'text-gray-900'"
:value="inputValue.end"
2021-02-26 06:39:20 +01:00
type="hidden"
2020-12-22 17:22:50 +01:00
/>
</div>
2020-11-15 14:05:04 +01:00
</div>
</template>
</date-picker>
</div>
</template>
<script>
2021-02-14 07:53:20 +01:00
2021-02-13 20:04:18 +01:00
import {createNamespacedHelpers} from "vuex";
2021-03-27 10:19:22 +01:00
import Vue from "vue";
import DatePicker from "v-calendar/lib/components/date-picker.umd";
2021-04-18 12:29:42 +02:00
import subDays from 'date-fns/subDays';
import addDays from 'date-fns/addDays';
import addMonths from 'date-fns/addMonths';
import startOfDay from 'date-fns/startOfDay';
import endOfDay from 'date-fns/endOfDay';
import startOfWeek from 'date-fns/startOfWeek';
import endOfWeek from 'date-fns/endOfWeek';
import endOfMonth from 'date-fns/endOfMonth';
import format from 'date-fns/format';
2021-04-17 20:53:42 +02:00
import startOfQuarter from 'date-fns/startOfQuarter';
2021-04-18 12:29:42 +02:00
import subMonths from 'date-fns/subMonths';
2021-04-17 20:53:42 +02:00
import endOfQuarter from 'date-fns/endOfQuarter';
import subQuarters from 'date-fns/subQuarters';
import addQuarters from 'date-fns/addQuarters';
2021-04-18 12:29:42 +02:00
import startOfMonth from 'date-fns/startOfMonth';
2021-02-13 20:04:18 +01:00
2021-04-17 20:53:42 +02:00
const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('dashboard/index')
2021-03-27 10:19:22 +01:00
Vue.component('date-picker', DatePicker)
2020-11-15 14:05:04 +01:00
export default {
name: "Calendar",
2020-12-22 17:22:50 +01:00
created() {
2021-02-13 20:04:18 +01:00
this.ready = true;
2021-02-12 20:15:07 +01:00
this.locale = localStorage.locale ?? 'en-US';
2020-11-15 14:05:04 +01:00
},
2021-02-14 10:43:58 +01:00
data() {
return {
locale: 'en-US',
ready: false,
range: {
start: null,
end: null,
},
defaultRange: {
start: null,
end: null,
},
periods: []
};
},
2021-02-14 07:53:20 +01:00
methods: {
...mapMutations(
[
'setEnd',
'setStart',
],
),
2021-02-14 10:43:58 +01:00
resetDate: function () {
//console.log('Reset date to');
//console.log(this.defaultStart);
//console.log(this.defaultEnd);
this.range.start = this.defaultStart;
this.range.end = this.defaultEnd;
this.setStart(this.defaultStart);
this.setEnd(this.defaultEnd);
},
customDate: function (startStr, endStr) {
let start = new Date(startStr);
let end = new Date(endStr);
this.setStart(start);
this.setEnd(end);
this.range.start = start;
this.range.end = end;
this.generatePeriods()
2021-02-14 10:43:58 +01:00
return false;
},
2021-04-17 20:53:42 +02:00
generateDaily: function () {
let today = new Date(this.range.start);
// yesterday
this.periods.push(
{
start: startOfDay(subDays(today, 1)).toDateString(),
end: endOfDay(subDays(today, 1)).toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(subDays(today, 1))
}
);
2021-04-17 20:53:42 +02:00
// today
this.periods.push(
{
start: startOfDay(today).toDateString(),
end: endOfDay(today).toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(today)
}
);
2021-04-17 20:53:42 +02:00
// tomorrow:
this.periods.push(
{
start: startOfDay(addDays(today, 1)).toDateString(),
end: endOfDay(addDays(today, 1)).toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(addDays(today, 1))
}
);
// The Day After Tomorrow dun-dun-dun!
this.periods.push(
{
start: startOfDay(addDays(today, 2)).toDateString(),
end: endOfDay(addDays(today, 2)).toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long', day: 'numeric'}).format(addDays(today, 2))
}
);
},
generateWeekly: function () {
2021-04-19 06:49:58 +02:00
//console.log('weekly');
2021-04-17 20:53:42 +02:00
let today = new Date(this.range.start);
2021-04-19 06:49:58 +02:00
//console.log('Today is ' + today);
2021-04-17 20:53:42 +02:00
let start = startOfDay(startOfWeek(subDays(today, 7), {weekStartsOn: 1}));
let end = endOfDay(endOfWeek(subDays(today, 7), {weekStartsOn: 1}));
let dateFormat = this.$t('config.week_in_year_fns');
2021-04-19 06:49:58 +02:00
//console.log('Date format: "'+dateFormat+'"');
2021-04-17 20:53:42 +02:00
let title = format(start, dateFormat);
// last week
2021-04-19 06:49:58 +02:00
// console.log('Last week');
// console.log(start);
// console.log(end);
// console.log(title);
2021-04-17 20:53:42 +02:00
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// this week
start = startOfDay(startOfWeek(today, {weekStartsOn: 1}));
end = endOfDay(endOfWeek(today, {weekStartsOn: 1}));
title = format(start, dateFormat);
2021-04-19 06:49:58 +02:00
// console.log('This week');
// console.log(start);
// console.log(end);
// console.log(title);
2021-04-17 20:53:42 +02:00
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// next week
start = startOfDay(startOfWeek(addDays(today, 7), {weekStartsOn: 1}));
end = endOfDay(endOfWeek(addDays(today, 7), {weekStartsOn: 1}));
title = format(start, dateFormat);
2021-04-19 06:49:58 +02:00
// console.log('Next week');
// console.log(start);
// console.log(end);
// console.log(title);
2021-04-17 20:53:42 +02:00
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
},
generateMonthly: function () {
let today = new Date(this.range.start);
// previous month
2021-04-18 12:29:42 +02:00
let start = startOfDay(startOfMonth(subMonths(today, 1)));
let end = endOfDay(endOfMonth(subMonths(today, 1)));
this.periods.push(
{
2021-04-18 12:29:42 +02:00
start: start.toDateString(),
end: end.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(start)
}
);
// this month
2021-04-18 12:29:42 +02:00
start = startOfDay(startOfMonth(today));
end = endOfDay(endOfMonth(today));
this.periods.push(
{
2021-04-18 12:29:42 +02:00
start: start.toDateString(),
end: end.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(start)
}
);
// next month
2021-04-18 12:29:42 +02:00
start = startOfDay(startOfMonth(addMonths(today, 1)));
end = endOfDay(endOfMonth(addMonths(today, 1)));
this.periods.push(
{
2021-04-18 12:29:42 +02:00
start: start.toDateString(),
end: end.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(start)
}
);
2021-04-17 20:53:42 +02:00
},
generateQuarterly: function () {
let today = new Date(this.range.start);
// last quarter
let start = startOfDay(startOfQuarter(subQuarters(today, 1)));
let end = endOfDay(endOfQuarter(subQuarters(today, 1)));
let dateFormat = this.$t('config.quarter_fns');
let title = format(start, dateFormat);
// last week
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// this quarter
start = startOfDay(startOfQuarter(today));
end = endOfDay(endOfQuarter(today));
title = format(start, dateFormat);
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// next quarter
start = startOfDay(startOfQuarter(addQuarters(today, 1)));
end = endOfDay(endOfQuarter(addQuarters(today, 1)));
title = format(start, dateFormat);
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
},
generateHalfYearly: function () {
let today = new Date(this.range.start);
let start;
let end;
let title = 'todo';
let half = 1;
// its currently first half of year:
if (today.getMonth() <= 5) {
// previous year, last half:
start = today;
start.setFullYear(start.getFullYear() - 1);
start.setMonth(6);
start.setDate(1);
start = startOfDay(start);
end = start;
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
half = 2;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// this year, first half:
start = today;
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = today;
end.setMonth(5);
end.setDate(30);
end = endOfDay(start);
half = 1;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// this year, second half:
start = today;
start.setMonth(6);
start.setDate(1);
start = startOfDay(start);
end = start;
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
half = 2;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
return;
}
// this year, first half:
start = today;
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = start;
end.setMonth(5);
end.setDate(30);
end = endOfDay(end);
half = 1;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
2021-04-18 12:29:42 +02:00
// this year, current (second) half:
2021-04-17 20:53:42 +02:00
start = today;
start.setMonth(6);
start.setDate(1);
start = startOfDay(start);
end = today;
end.setMonth(11);
end.setDate(31);
end = endOfDay(start);
half = 2;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
// next year, first half:
start = today;
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = start;
end.setMonth(5);
end.setDate(30);
end = endOfDay(end);
half = 1;
title = format(start, this.$t('config.half_year_fns', {half: half}));
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: title
}
);
},
generateYearly: function () {
let today = new Date(this.range.start);
let start;
let end;
let title;
// last year
start = new Date(today);
start.setFullYear(start.getFullYear() - 1);
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = new Date(today);
end.setFullYear(end.getFullYear() - 1);
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: start.getFullYear()
}
);
// this year
start = new Date(today);
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = new Date(today);
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: start.getFullYear()
}
);
// next year
start = new Date(today);
start.setFullYear(start.getFullYear() + 1);
start.setMonth(0);
start.setDate(1);
start = startOfDay(start);
end = new Date(today);
end.setFullYear(end.getFullYear() + 1);
end.setMonth(11);
end.setDate(31);
end = endOfDay(end);
this.periods.push(
{
start: start.toDateString(),
end: end.toDateString(),
title: start.getFullYear()
}
);
},
generatePeriods: function () {
this.periods = [];
//console.log('The view range is "' + this.viewRange + '".');
switch (this.viewRange) {
case '1D':
this.generateDaily();
break;
case '1W':
this.generateWeekly();
break;
case '1M':
this.generateMonthly();
break;
case '3M':
this.generateQuarterly();
break;
case '6M':
this.generateHalfYearly();
break;
case '1Y':
this.generateYearly();
break;
}
2021-02-14 10:43:58 +01:00
// last 7 days
2021-04-17 20:53:42 +02:00
let today = new Date;
let end = new Date;
2021-02-14 10:43:58 +01:00
end.setDate(end.getDate() - 7);
this.periods.push(
{
start: end.toDateString(),
end: today.toDateString(),
title: this.$t('firefly.last_seven_days')
}
);
// last 30 days:
end.setDate(end.getDate() - 23);
this.periods.push(
{
start: end.toDateString(),
end: today.toDateString(),
title: this.$t('firefly.last_thirty_days')
}
);
// last 30 days
// everything
}
},
computed: {
...mapGetters([
'viewRange',
'start',
'end',
'defaultStart',
'defaultEnd'
]),
'datesReady': function () {
return null !== this.start && null !== this.end && this.ready;
},
},
watch: {
datesReady: function (value) {
if (false === value) {
return;
}
this.range.start = new Date(this.start);
this.range.end = new Date(this.end);
this.generatePeriods();
2021-02-13 20:04:18 +01:00
},
2021-02-14 10:43:58 +01:00
range: function (value) {
//console.log('User updated range');
2021-02-14 07:53:20 +01:00
this.setStart(value.start);
this.setEnd(value.end);
}
2021-02-14 10:43:58 +01:00
}
2020-11-15 14:05:04 +01:00
}
</script>
<style scoped>
2020-12-22 17:22:50 +01:00
.dropdown-item {
color: #212529;
}
.dropdown-item:hover {
color: #212529;
}
2020-11-15 14:05:04 +01:00
</style>