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

238 lines
7.2 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-02-26 06:39:20 +01:00
<button id="dropdownMenuButton" :title="$t('firefly.select_period')" aria-expanded="false" aria-haspopup="true" class="btn btn-secondary dropdown-toggle"
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-02-13 20:04:18 +01: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;
},
generatePeriods: function () {
2021-02-14 10:43:58 +01:00
this.periods = [];
// create periods.
let today;
let end;
today = new Date(this.range.start);
// previous month
firstDayOfMonth = new Date(today.getFullYear(), today.getMonth()-1, 1);
lastDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 0);
this.periods.push(
{
start: firstDayOfMonth.toDateString(),
end: lastDayOfMonth.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(firstDayOfMonth)
}
);
// this month
firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
this.periods.push(
{
start: firstDayOfMonth.toDateString(),
end: lastDayOfMonth.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(firstDayOfMonth)
}
);
// next month
let firstDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 1);
let lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+2, 0);
this.periods.push(
{
start: firstDayOfMonth.toDateString(),
end: lastDayOfMonth.toDateString(),
title: new Intl.DateTimeFormat(this.locale, {year: 'numeric', month: 'long'}).format(firstDayOfMonth)
}
);
2021-02-14 10:43:58 +01:00
// last 7 days
today = new Date;
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>