modify localstorage close #48

This commit is contained in:
Adrian Zürcher
2026-02-13 12:27:30 +01:00
parent 3f02afde85
commit 0026f68320
5 changed files with 139 additions and 27 deletions

View File

@@ -14,6 +14,21 @@
:val="opt.value"
:label="opt.label"
/>
<q-tabs
v-model="activeTab"
dense
class="text-primary"
active-color="primary"
indicator-color="primary"
align="justify"
narrow-indicator
@update:model-value="onTabChange"
>
<q-tab no-caps name="today" :label="$t('today')" />
<q-tab no-caps name="week" :label="$t('week')" />
<q-tab no-caps name="month" :label="$t('month')" />
<q-tab no-caps name="year" :label="$t('year')" />
</q-tabs>
</div>
<div class="row">
<q-date v-model="dateRange" range flat />
@@ -22,7 +37,7 @@
</template>
<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { ref, onMounted, watch, type PropType } from 'vue';
import { date } from 'quasar';
import { i18n } from 'src/boot/lang';
@@ -32,11 +47,17 @@ const props = defineProps({
width: { type: Number, default: 300 },
});
const weekdays = defineModel('weekdays', {
type: Array as PropType<number[]>,
default: () => [0, 3],
});
const startDate = new Date();
const activeTab = ref('');
// Initial range (format: YYYY-MM-DD)
const dateRange = ref<string | { to: string; from: string }>('');
const selectedWeekdays = ref([0, 3]); // Default to weekdays
const selectedWeekdays = ref(weekdays); // Default to weekdays
const emit = defineEmits(['update:dates']);
onMounted(() => {
@@ -53,6 +74,14 @@ const weekdayOptions = [
{ label: i18n.global.t('SundayShort'), value: 0 },
];
const onTabChange = (val: 'today' | 'week' | 'month' | 'year') => {
if (val) setRange(val);
// Optional: Reset tab to empty so user can click the same tab again later
setTimeout(() => {
activeTab.value = '';
}, 500);
};
// The Logic: Calculate all specific dates within the range that match weekdays
watch(dateRange, () => {
if (!dateRange.value) {
@@ -81,4 +110,81 @@ watch(dateRange, () => {
emit('update:dates', result);
return result;
});
/**
* Logic for 1 Week / 1 Month / 1 Year
*/
const setRange = (type: 'today' | 'week' | 'month' | 'year') => {
const anchor =
typeof dateRange.value === 'string'
? new Date(dateRange.value)
: new Date(dateRange.value.from);
let from: Date;
let to: Date;
if (type === 'today') {
const now = new Date();
dateRange.value = date.formatDate(date.startOfDate(now, 'day'), 'YYYY-MM-DD');
return;
} else if (type === 'week') {
// getDay() returns 0 for Sunday.
// We calculate how many days to subtract to get to Monday (1).
const day = anchor.getDay();
const diffToMonday = day === 0 ? -6 : 1 - day;
from = date.addToDate(anchor, { days: diffToMonday });
from = date.startOfDate(from, 'day'); // Reset time to 00:00
to = date.addToDate(from, { days: 6 });
to = date.endOfDate(to, 'day'); // Set time to 23:59
} else if (type === 'month') {
// 'month' is a valid unit for startOfDate
from = date.startOfDate(anchor, 'month');
to = date.endOfDate(anchor, 'month');
} else {
// 'year' is a valid unit for startOfDate
from = date.startOfDate(anchor, 'year');
to = date.endOfDate(anchor, 'year');
}
dateRange.value = {
from: date.formatDate(from, 'YYYY/MM/DD'),
to: date.formatDate(to, 'YYYY/MM/DD'),
};
};
watch(
[dateRange, selectedWeekdays],
() => {
if (!dateRange.value) {
emit('update:dates', []);
return;
}
let start: Date, end: Date;
if (typeof dateRange.value === 'string') {
start = new Date(dateRange.value);
end = new Date(dateRange.value);
} else {
start = new Date(dateRange.value.from);
end = new Date(dateRange.value.to);
}
const result = [];
let current = start;
while (current <= end) {
if (selectedWeekdays.value.includes(current.getDay())) {
result.push(date.formatDate(current, 'YYYY-MM-DD'));
}
current = date.addToDate(current, { days: 1 });
}
emit('update:dates', result);
},
{ deep: true },
);
</script>