Refactors date handling with DateService
Replaces DateCalculator with DateService for improved date and time operations, including timezone handling. This change enhances the calendar's accuracy and flexibility in managing dates, especially concerning timezone configurations. It also corrects a typo in the `allDay` dataset attribute.
This commit is contained in:
parent
4859f42450
commit
6bbf2d8adb
17 changed files with 159 additions and 749 deletions
|
|
@ -4,16 +4,15 @@
|
|||
*/
|
||||
|
||||
import { ViewStrategy, ViewContext, ViewLayoutConfig } from './ViewStrategy';
|
||||
import { DateCalculator } from '../utils/DateCalculator';
|
||||
import { DateService } from '../utils/DateService';
|
||||
import { calendarConfig } from '../core/CalendarConfig';
|
||||
import { CalendarEvent } from '../types/CalendarTypes';
|
||||
|
||||
export class MonthViewStrategy implements ViewStrategy {
|
||||
private dateCalculator: DateCalculator;
|
||||
private dateService: DateService;
|
||||
|
||||
constructor() {
|
||||
DateCalculator.initialize(calendarConfig);
|
||||
this.dateCalculator = new DateCalculator();
|
||||
this.dateService = new DateService('Europe/Copenhagen');
|
||||
}
|
||||
|
||||
getLayoutConfig(): ViewLayoutConfig {
|
||||
|
|
@ -74,7 +73,7 @@ export class MonthViewStrategy implements ViewStrategy {
|
|||
dates.forEach(date => {
|
||||
const cell = document.createElement('div');
|
||||
cell.className = 'month-day-cell';
|
||||
cell.dataset.date = DateCalculator.formatISODate(date);
|
||||
cell.dataset.date = this.dateService.formatISODate(date);
|
||||
cell.style.border = '1px solid #e0e0e0';
|
||||
cell.style.minHeight = '100px';
|
||||
cell.style.padding = '4px';
|
||||
|
|
@ -88,7 +87,7 @@ export class MonthViewStrategy implements ViewStrategy {
|
|||
dayNumber.style.marginBottom = '4px';
|
||||
|
||||
// Check if today
|
||||
if (DateCalculator.isToday(date)) {
|
||||
if (this.dateService.isSameDay(date, new Date())) {
|
||||
dayNumber.style.color = '#1976d2';
|
||||
cell.style.backgroundColor = '#f5f5f5';
|
||||
}
|
||||
|
|
@ -103,12 +102,13 @@ export class MonthViewStrategy implements ViewStrategy {
|
|||
const firstOfMonth = new Date(monthDate.getFullYear(), monthDate.getMonth(), 1);
|
||||
|
||||
// Get Monday of the week containing first day
|
||||
const startDate = DateCalculator.getISOWeekStart(firstOfMonth);
|
||||
const weekBounds = this.dateService.getWeekBounds(firstOfMonth);
|
||||
const startDate = this.dateService.startOfDay(weekBounds.start);
|
||||
|
||||
// Generate 42 days (6 weeks)
|
||||
const dates: Date[] = [];
|
||||
for (let i = 0; i < 42; i++) {
|
||||
dates.push(DateCalculator.addDays(startDate, i));
|
||||
dates.push(this.dateService.addDays(startDate, i));
|
||||
}
|
||||
|
||||
return dates;
|
||||
|
|
@ -143,10 +143,11 @@ export class MonthViewStrategy implements ViewStrategy {
|
|||
const firstOfMonth = new Date(baseDate.getFullYear(), baseDate.getMonth(), 1);
|
||||
|
||||
// Get Monday of the week containing first day
|
||||
const startDate = DateCalculator.getISOWeekStart(firstOfMonth);
|
||||
const weekBounds = this.dateService.getWeekBounds(firstOfMonth);
|
||||
const startDate = this.dateService.startOfDay(weekBounds.start);
|
||||
|
||||
// End date is 41 days after start (42 total days)
|
||||
const endDate = DateCalculator.addDays(startDate, 41);
|
||||
const endDate = this.dateService.addDays(startDate, 41);
|
||||
|
||||
return {
|
||||
startDate,
|
||||
|
|
|
|||
|
|
@ -4,19 +4,19 @@
|
|||
*/
|
||||
|
||||
import { ViewStrategy, ViewContext, ViewLayoutConfig } from './ViewStrategy';
|
||||
import { DateCalculator } from '../utils/DateCalculator';
|
||||
import { DateService } from '../utils/DateService';
|
||||
import { calendarConfig } from '../core/CalendarConfig';
|
||||
import { GridRenderer } from '../renderers/GridRenderer';
|
||||
import { GridStyleManager } from '../renderers/GridStyleManager';
|
||||
|
||||
export class WeekViewStrategy implements ViewStrategy {
|
||||
private dateCalculator: DateCalculator;
|
||||
private dateService: DateService;
|
||||
private gridRenderer: GridRenderer;
|
||||
private styleManager: GridStyleManager;
|
||||
|
||||
constructor() {
|
||||
DateCalculator.initialize(calendarConfig);
|
||||
this.dateCalculator = new DateCalculator();
|
||||
const timezone = calendarConfig.getTimezone?.() || 'Europe/Copenhagen';
|
||||
this.dateService = new DateService(timezone);
|
||||
this.gridRenderer = new GridRenderer();
|
||||
this.styleManager = new GridStyleManager();
|
||||
}
|
||||
|
|
@ -43,28 +43,31 @@ export class WeekViewStrategy implements ViewStrategy {
|
|||
}
|
||||
|
||||
getNextPeriod(currentDate: Date): Date {
|
||||
return DateCalculator.addWeeks(currentDate, 1);
|
||||
return this.dateService.addWeeks(currentDate, 1);
|
||||
}
|
||||
|
||||
getPreviousPeriod(currentDate: Date): Date {
|
||||
return DateCalculator.addWeeks(currentDate, -1);
|
||||
return this.dateService.addWeeks(currentDate, -1);
|
||||
}
|
||||
|
||||
getPeriodLabel(date: Date): string {
|
||||
const weekStart = DateCalculator.getISOWeekStart(date);
|
||||
const weekEnd = DateCalculator.addDays(weekStart, 6);
|
||||
const weekNumber = DateCalculator.getWeekNumber(date);
|
||||
const weekBounds = this.dateService.getWeekBounds(date);
|
||||
const weekStart = this.dateService.startOfDay(weekBounds.start);
|
||||
const weekEnd = this.dateService.addDays(weekStart, 6);
|
||||
const weekNumber = this.dateService.getWeekNumber(date);
|
||||
|
||||
return `Week ${weekNumber}: ${DateCalculator.formatDateRange(weekStart, weekEnd)}`;
|
||||
return `Week ${weekNumber}: ${this.dateService.formatDateRange(weekStart, weekEnd)}`;
|
||||
}
|
||||
|
||||
getDisplayDates(baseDate: Date): Date[] {
|
||||
return DateCalculator.getWorkWeekDates(baseDate);
|
||||
const workWeekSettings = calendarConfig.getWorkWeekSettings();
|
||||
return this.dateService.getWorkWeekDates(baseDate, workWeekSettings.workDays);
|
||||
}
|
||||
|
||||
getPeriodRange(baseDate: Date): { startDate: Date; endDate: Date } {
|
||||
const weekStart = DateCalculator.getISOWeekStart(baseDate);
|
||||
const weekEnd = DateCalculator.addDays(weekStart, 6);
|
||||
const weekBounds = this.dateService.getWeekBounds(baseDate);
|
||||
const weekStart = this.dateService.startOfDay(weekBounds.start);
|
||||
const weekEnd = this.dateService.addDays(weekStart, 6);
|
||||
|
||||
return {
|
||||
startDate: weekStart,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue