Refactors date calculations to use ISO week

Adopts ISO week date calculation (Monday-based) for consistency
and accuracy across calendar components. This change ensures
standardized week determination, resolving potential discrepancies
in week-based operations.
This commit is contained in:
Janus Knudsen 2025-08-19 23:04:56 +02:00
parent 18f7953db4
commit efc1742dad
2 changed files with 41 additions and 32 deletions

View file

@ -5,6 +5,7 @@ import { calendarConfig } from '../core/CalendarConfig';
import { EventTypes } from '../constants/EventTypes';
import { StateEvents } from '../types/CalendarState';
import { DateUtils } from '../utils/DateUtils';
import { DateCalculator } from '../utils/DateCalculator';
import { ResourceCalendarData } from '../types/CalendarTypes';
import { GridRenderer } from '../renderers/GridRenderer';
import { GridStyleManager } from '../renderers/GridStyleManager';
@ -29,11 +30,13 @@ export class GridManager {
private resourceData: ResourceCalendarData | null = null; // Store resource data for resource calendar
private gridRenderer: GridRenderer;
private styleManager: GridStyleManager;
private dateCalculator: DateCalculator;
constructor() {
console.log('🏗️ GridManager: Constructor called');
this.gridRenderer = new GridRenderer(calendarConfig);
this.styleManager = new GridStyleManager(calendarConfig);
this.dateCalculator = new DateCalculator(calendarConfig);
this.init();
}
@ -43,7 +46,7 @@ export class GridManager {
// Set initial current week to today if not set
if (!this.currentWeek) {
this.currentWeek = this.getWeekStart(new Date());
this.currentWeek = this.dateCalculator.getISOWeekStart(new Date());
console.log('GridManager: Set initial currentWeek to', this.currentWeek);
// Don't render immediately - wait for proper initialization event
console.log('GridManager: Waiting for initialization complete before rendering');
@ -51,8 +54,8 @@ export class GridManager {
}
private getWeekStart(date: Date): Date {
// Use DateUtils for consistent week calculation (Sunday = 0)
return DateUtils.getWeekStart(date, 0);
// Use DateCalculator for ISO week (Monday = start)
return this.dateCalculator.getISOWeekStart(date);
}
private findElements(): void {
@ -84,14 +87,21 @@ export class GridManager {
// Re-render on period change
eventBus.on(EventTypes.PERIOD_CHANGE, (e: Event) => {
const detail = (e as CustomEvent).detail;
this.currentWeek = detail.week;
this.currentWeek = this.dateCalculator.getISOWeekStart(detail.week);
this.render();
});
// Handle week changes from NavigationManager
eventBus.on(EventTypes.WEEK_CHANGED, (e: Event) => {
const detail = (e as CustomEvent).detail;
this.currentWeek = detail.weekStart;
this.currentWeek = this.dateCalculator.getISOWeekStart(detail.weekStart);
this.render();
});
// Handle date changes from CalendarManager
eventBus.on(EventTypes.DATE_CHANGED, (e: Event) => {
const detail = (e as CustomEvent).detail;
this.currentWeek = this.dateCalculator.getISOWeekStart(detail.currentDate);
this.render();
});