ea379ce5e4
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import {ref, watch, type Ref} from 'vue'
|
|
import {isValidIso} from './dateFormat'
|
|
|
|
export function useCalendarView(viewMode: Ref<'days' | 'months' | 'years'>) {
|
|
const today = new Date()
|
|
const currentMonth = ref(today.getMonth())
|
|
const currentYear = ref(today.getFullYear())
|
|
const yearPageStart = ref(today.getFullYear() - 5)
|
|
|
|
watch(viewMode, (mode) => {
|
|
if (mode === 'years') yearPageStart.value = currentYear.value - 5
|
|
})
|
|
|
|
const goToPrev = () => {
|
|
if (viewMode.value === 'years') {
|
|
yearPageStart.value -= 12
|
|
return
|
|
}
|
|
if (viewMode.value === 'months') {
|
|
currentYear.value -= 1
|
|
return
|
|
}
|
|
if (currentMonth.value === 0) {
|
|
currentMonth.value = 11
|
|
currentYear.value -= 1
|
|
} else {
|
|
currentMonth.value -= 1
|
|
}
|
|
}
|
|
|
|
const goToNext = () => {
|
|
if (viewMode.value === 'years') {
|
|
yearPageStart.value += 12
|
|
return
|
|
}
|
|
if (viewMode.value === 'months') {
|
|
currentYear.value += 1
|
|
return
|
|
}
|
|
if (currentMonth.value === 11) {
|
|
currentMonth.value = 0
|
|
currentYear.value += 1
|
|
} else {
|
|
currentMonth.value += 1
|
|
}
|
|
}
|
|
|
|
const selectMonth = (m: number) => {
|
|
currentMonth.value = m
|
|
}
|
|
|
|
const selectYear = (y: number) => {
|
|
currentYear.value = y
|
|
}
|
|
|
|
const syncToIso = (iso: string | null) => {
|
|
if (iso && isValidIso(iso)) {
|
|
currentMonth.value = Number(iso.slice(5, 7)) - 1
|
|
currentYear.value = Number(iso.slice(0, 4))
|
|
} else {
|
|
const now = new Date()
|
|
currentMonth.value = now.getMonth()
|
|
currentYear.value = now.getFullYear()
|
|
}
|
|
}
|
|
|
|
return {currentMonth, currentYear, yearPageStart, goToPrev, goToNext, selectMonth, selectYear, syncToIso}
|
|
}
|