#152 - optimization
This commit is contained in:
parent
7893282385
commit
4e16f363ac
@ -2,12 +2,11 @@ function isViewModeKey(key) {
|
|||||||
return this.key === key.value
|
return this.key === key.value
|
||||||
}
|
}
|
||||||
|
|
||||||
const modes = [
|
export const viewModes = [
|
||||||
{ key: 'week', name: 'Widok tygodnia', shortcut: 'Tydzień', is: isViewModeKey },
|
{ key: 'week', name: 'Widok tygodnia', shortcut: 'Tydzień', is: isViewModeKey },
|
||||||
{ key: 'month', name: 'Widok miesiąca', shortcut: 'Miesiąc', is: isViewModeKey },
|
{ key: 'month', name: 'Widok miesiąca', shortcut: 'Miesiąc', is: isViewModeKey },
|
||||||
]
|
]
|
||||||
|
|
||||||
export default {
|
export function find(modeKey) {
|
||||||
all: modes,
|
return viewModes.find(mode => mode.key === modeKey)
|
||||||
find: whereMode => modes.find(mode => mode.key === whereMode),
|
|
||||||
}
|
}
|
@ -2,7 +2,7 @@
|
|||||||
<section class="bg-white shadow-md">
|
<section class="bg-white shadow-md">
|
||||||
<div class="grid grid-cols-3 grid-rows-1 items-center justify-center p-4 sm:px-6">
|
<div class="grid grid-cols-3 grid-rows-1 items-center justify-center p-4 sm:px-6">
|
||||||
<h2 class="text-lg font-medium leading-6 text-gray-900">
|
<h2 class="text-lg font-medium leading-6 text-gray-900">
|
||||||
{{ calendarState.monthName }} {{ calendar.currents.year !== selectedYear ? calendar.currents.year : undefined }}
|
{{ calendarState.monthName }} {{ calendarState.isActualYear ? undefined : calendar.currents.year }}
|
||||||
</h2>
|
</h2>
|
||||||
<div class="flex justify-center">
|
<div class="flex justify-center">
|
||||||
<div class="flex items-center rounded-md shadow-sm md:items-stretch">
|
<div class="flex items-center rounded-md shadow-sm md:items-stretch">
|
||||||
@ -68,7 +68,7 @@
|
|||||||
<MenuItems class="absolute right-0 mt-3 w-36 origin-top-right overflow-hidden rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
|
<MenuItems class="absolute right-0 mt-3 w-36 origin-top-right overflow-hidden rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
|
||||||
<div class="py-1">
|
<div class="py-1">
|
||||||
<MenuItem
|
<MenuItem
|
||||||
v-for="option in viewModes.all"
|
v-for="option in viewModes"
|
||||||
:key="option.key"
|
:key="option.key"
|
||||||
v-slot="{ active }"
|
v-slot="{ active }"
|
||||||
>
|
>
|
||||||
@ -143,7 +143,7 @@ import { ref, watch, computed, reactive } from 'vue'
|
|||||||
import { DateTime } from 'luxon'
|
import { DateTime } from 'luxon'
|
||||||
import useCurrentYearPeriodInfo from '@/Composables/yearPeriodInfo'
|
import useCurrentYearPeriodInfo from '@/Composables/yearPeriodInfo'
|
||||||
import { useMonthInfo } from '@/Composables/monthInfo'
|
import { useMonthInfo } from '@/Composables/monthInfo'
|
||||||
import viewModes from '@/Shared/Widgets/VacationCalendar/ViewModeOptions'
|
import { viewModes, find as findViewMode } from '@/Shared/Widgets/Calendar/ViewModeOptions'
|
||||||
|
|
||||||
let days = ref([])
|
let days = ref([])
|
||||||
const months = useMonthInfo().getMonths()
|
const months = useMonthInfo().getMonths()
|
||||||
@ -159,16 +159,15 @@ const calendar = {
|
|||||||
currents: reactive({
|
currents: reactive({
|
||||||
year: selectedYear,
|
year: selectedYear,
|
||||||
month: selectedYear === currentDate.year ? currentDate.month : 1,
|
month: selectedYear === currentDate.year ? currentDate.month : 1,
|
||||||
week: selectedYear === currentDate.year ? currentDate.weekNumber : 1,
|
weekPosition: 0,
|
||||||
}),
|
}),
|
||||||
toPreviousActive: computed(() => calendar.currents.month !== 1),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendarState = reactive({
|
const calendarState = reactive({
|
||||||
viewMode: {
|
viewMode: {
|
||||||
isWeek: computed(() => calendar.viewMode.value === 'week'),
|
isWeek: computed(() => calendar.viewMode.value === 'week'),
|
||||||
isMonth: computed(() => calendar.viewMode.value === 'month'),
|
isMonth: computed(() => calendar.viewMode.value === 'month'),
|
||||||
details: computed(() => viewModes.find(calendar.viewMode.value)),
|
details: computed(() => findViewMode(calendar.viewMode.value)),
|
||||||
},
|
},
|
||||||
monthName: computed(() => months[calendar.currents.month - 1]?.name),
|
monthName: computed(() => months[calendar.currents.month - 1]?.name),
|
||||||
isActualYear: computed(() => calendar.currents.year !== selectedYear),
|
isActualYear: computed(() => calendar.currents.year !== selectedYear),
|
||||||
@ -176,51 +175,57 @@ const calendarState = reactive({
|
|||||||
isNext: computed(() => calendar.currents.month !== 12),
|
isNext: computed(() => calendar.currents.month !== 12),
|
||||||
})
|
})
|
||||||
|
|
||||||
watch([calendar.viewMode, calendar.currents], () => {
|
const customCalendar = {
|
||||||
loadCalendar()
|
loadCalendar() {
|
||||||
})
|
const date = DateTime.fromObject({
|
||||||
|
|
||||||
loadCalendar()
|
|
||||||
|
|
||||||
function loadCalendar() {
|
|
||||||
console.log('Loaded')
|
|
||||||
let focusDate = DateTime.fromObject({
|
|
||||||
year: calendar.currents.year,
|
year: calendar.currents.year,
|
||||||
month: calendar.currents.month,
|
month: calendar.currents.month,
|
||||||
})
|
})
|
||||||
let start, end
|
days.value = this.generateCalendarData(date)
|
||||||
|
},
|
||||||
|
generateCalendarData(date) {
|
||||||
|
const firstMonthDay = date.startOf('month')
|
||||||
|
const lastMonthDay = date.endOf('month')
|
||||||
|
|
||||||
if (calendar.viewMode.value === 'week') {
|
if (calendarState.viewMode.isWeek) {
|
||||||
if (currentDate.year === selectedYear)
|
return this.generateWeekData(firstMonthDay.startOf('week'))
|
||||||
focusDate = DateTime.fromObject({ weekNumber: calendar.currents.week })
|
} else if (calendarState.viewMode.isMonth) {
|
||||||
else
|
return this.generateMonthData(firstMonthDay.startOf('week'), lastMonthDay.endOf('week'))
|
||||||
focusDate = focusDate.plus({ week: calendar.currents.week - 1 })
|
}
|
||||||
start = focusDate.startOf('week')
|
return []
|
||||||
end = focusDate.endOf('week')
|
},
|
||||||
} else if (calendar.viewMode.value === 'month') {
|
generateWeekData(start) {
|
||||||
focusDate = focusDate.startOf('month')
|
let days = []
|
||||||
start = focusDate.startOf('week')
|
const startWeek = start.plus({ week: calendar.currents.weekPosition })
|
||||||
end = focusDate.endOf('month').endOf('week')
|
const endWeek = startWeek.endOf('week')
|
||||||
|
for (let day = startWeek; day < endWeek; day = day.plus({ day: 1 })) {
|
||||||
|
days.push(this.prepareDay(day))
|
||||||
|
}
|
||||||
|
return days
|
||||||
|
},
|
||||||
|
generateMonthData(start, end) {
|
||||||
|
let days = []
|
||||||
|
for (let day = start; day < end; day = day.plus({ day: 1 })) {
|
||||||
|
days.push(this.prepareDay(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
generateCalendarData(start, end, focusDate)
|
return days
|
||||||
}
|
},
|
||||||
|
prepareDay(day) {
|
||||||
function generateCalendarData(startDate, endDate, focusDate) {
|
return {
|
||||||
const tmpDays = []
|
|
||||||
for (let day = startDate; day < endDate; day = day.plus({ day: 1 })) {
|
|
||||||
const isCurrentMonth = isInCurrentMonth(day, focusDate)
|
|
||||||
const okIsToday = isToday(day)
|
|
||||||
|
|
||||||
tmpDays.push({
|
|
||||||
date: day.toISODate(),
|
date: day.toISODate(),
|
||||||
isCurrentMonth,
|
dayNumber: day.day,
|
||||||
isToday: okIsToday,
|
isCurrentMonth: isInCurrentMonth(day),
|
||||||
})
|
isToday: isToday(day),
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
days.value = tmpDays
|
watch([calendar.viewMode, calendar.currents], () => {
|
||||||
}
|
customCalendar.loadCalendar()
|
||||||
|
})
|
||||||
|
|
||||||
|
customCalendar.loadCalendar()
|
||||||
|
|
||||||
function toLast() {
|
function toLast() {
|
||||||
if (calendar.viewMode.value === 'week')
|
if (calendar.viewMode.value === 'week')
|
||||||
@ -239,11 +244,21 @@ function toNext() {
|
|||||||
function resetCalendar() {
|
function resetCalendar() {
|
||||||
calendar.currents.year = selectedYear
|
calendar.currents.year = selectedYear
|
||||||
calendar.currents.month = selectedYear === currentDate.year ? currentDate.month : 1
|
calendar.currents.month = selectedYear === currentDate.year ? currentDate.month : 1
|
||||||
calendar.currents.week = selectedYear === currentDate.year ? currentDate.weekNumber : 1
|
calendar.currents.weekPosition = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function addWeeks(howMany = 1) {
|
function addWeeks(howMany = 1) {
|
||||||
calendar.currents.week += howMany
|
const date = DateTime.fromObject({
|
||||||
|
year: calendar.currents.year,
|
||||||
|
month: calendar.currents.month,
|
||||||
|
})
|
||||||
|
const nextMonth = date.plus({ week: calendar.currents.weekPosition }).month
|
||||||
|
if (calendarState.viewMode.isWeek && nextMonth !== calendar.currents.month) {
|
||||||
|
calendar.currents.month = nextMonth
|
||||||
|
calendar.currents.weekPosition = 0
|
||||||
|
return
|
||||||
|
}
|
||||||
|
calendar.currents.weekPosition += howMany
|
||||||
}
|
}
|
||||||
|
|
||||||
function addMonths(howMany = 1) {
|
function addMonths(howMany = 1) {
|
||||||
@ -255,8 +270,8 @@ function updateViewMode(type) {
|
|||||||
calendar.viewMode.value = type
|
calendar.viewMode.value = type
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInCurrentMonth(date, currentMonth) {
|
function isInCurrentMonth(date) {
|
||||||
return currentMonth.hasSame(date, 'month')
|
return calendar.currents.month === date.month
|
||||||
}
|
}
|
||||||
|
|
||||||
function isToday(date) {
|
function isToday(date) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user