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:
parent
18f7953db4
commit
efc1742dad
2 changed files with 41 additions and 32 deletions
|
|
@ -5,6 +5,7 @@ import { calendarConfig } from '../core/CalendarConfig';
|
||||||
import { EventTypes } from '../constants/EventTypes';
|
import { EventTypes } from '../constants/EventTypes';
|
||||||
import { StateEvents } from '../types/CalendarState';
|
import { StateEvents } from '../types/CalendarState';
|
||||||
import { DateUtils } from '../utils/DateUtils';
|
import { DateUtils } from '../utils/DateUtils';
|
||||||
|
import { DateCalculator } from '../utils/DateCalculator';
|
||||||
import { ResourceCalendarData } from '../types/CalendarTypes';
|
import { ResourceCalendarData } from '../types/CalendarTypes';
|
||||||
import { GridRenderer } from '../renderers/GridRenderer';
|
import { GridRenderer } from '../renderers/GridRenderer';
|
||||||
import { GridStyleManager } from '../renderers/GridStyleManager';
|
import { GridStyleManager } from '../renderers/GridStyleManager';
|
||||||
|
|
@ -29,11 +30,13 @@ export class GridManager {
|
||||||
private resourceData: ResourceCalendarData | null = null; // Store resource data for resource calendar
|
private resourceData: ResourceCalendarData | null = null; // Store resource data for resource calendar
|
||||||
private gridRenderer: GridRenderer;
|
private gridRenderer: GridRenderer;
|
||||||
private styleManager: GridStyleManager;
|
private styleManager: GridStyleManager;
|
||||||
|
private dateCalculator: DateCalculator;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
console.log('🏗️ GridManager: Constructor called');
|
console.log('🏗️ GridManager: Constructor called');
|
||||||
this.gridRenderer = new GridRenderer(calendarConfig);
|
this.gridRenderer = new GridRenderer(calendarConfig);
|
||||||
this.styleManager = new GridStyleManager(calendarConfig);
|
this.styleManager = new GridStyleManager(calendarConfig);
|
||||||
|
this.dateCalculator = new DateCalculator(calendarConfig);
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +46,7 @@ export class GridManager {
|
||||||
|
|
||||||
// Set initial current week to today if not set
|
// Set initial current week to today if not set
|
||||||
if (!this.currentWeek) {
|
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);
|
console.log('GridManager: Set initial currentWeek to', this.currentWeek);
|
||||||
// Don't render immediately - wait for proper initialization event
|
// Don't render immediately - wait for proper initialization event
|
||||||
console.log('GridManager: Waiting for initialization complete before rendering');
|
console.log('GridManager: Waiting for initialization complete before rendering');
|
||||||
|
|
@ -51,8 +54,8 @@ export class GridManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private getWeekStart(date: Date): Date {
|
private getWeekStart(date: Date): Date {
|
||||||
// Use DateUtils for consistent week calculation (Sunday = 0)
|
// Use DateCalculator for ISO week (Monday = start)
|
||||||
return DateUtils.getWeekStart(date, 0);
|
return this.dateCalculator.getISOWeekStart(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
private findElements(): void {
|
private findElements(): void {
|
||||||
|
|
@ -84,14 +87,21 @@ export class GridManager {
|
||||||
// Re-render on period change
|
// Re-render on period change
|
||||||
eventBus.on(EventTypes.PERIOD_CHANGE, (e: Event) => {
|
eventBus.on(EventTypes.PERIOD_CHANGE, (e: Event) => {
|
||||||
const detail = (e as CustomEvent).detail;
|
const detail = (e as CustomEvent).detail;
|
||||||
this.currentWeek = detail.week;
|
this.currentWeek = this.dateCalculator.getISOWeekStart(detail.week);
|
||||||
this.render();
|
this.render();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle week changes from NavigationManager
|
// Handle week changes from NavigationManager
|
||||||
eventBus.on(EventTypes.WEEK_CHANGED, (e: Event) => {
|
eventBus.on(EventTypes.WEEK_CHANGED, (e: Event) => {
|
||||||
const detail = (e as CustomEvent).detail;
|
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();
|
this.render();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,29 +13,27 @@ export class DateCalculator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get dates for work week based on week start and work days configuration
|
* Get dates for work week based on ISO week (starts Monday)
|
||||||
* @param weekStart - The start date of the week (could be Sunday or Monday)
|
* @param weekStart - Any date in the week
|
||||||
* @returns Array of dates for the configured work days
|
* @returns Array of dates for the configured work days
|
||||||
*/
|
*/
|
||||||
getWorkWeekDates(weekStart: Date): Date[] {
|
getWorkWeekDates(weekStart: Date): Date[] {
|
||||||
const dates: Date[] = [];
|
const dates: Date[] = [];
|
||||||
const workWeekSettings = this.config.getWorkWeekSettings();
|
const workWeekSettings = this.config.getWorkWeekSettings();
|
||||||
const dateSettings = this.config.getDateViewSettings();
|
|
||||||
const firstDayOfWeek = dateSettings.firstDayOfWeek; // 0=Sunday, 1=Monday
|
|
||||||
|
|
||||||
// Get the actual start of the week based on configuration
|
// Get Monday of the ISO week
|
||||||
const actualWeekStart = this.getWeekStart(weekStart, firstDayOfWeek);
|
const monday = this.getISOWeekStart(weekStart);
|
||||||
|
|
||||||
// Calculate dates for each work day
|
// Calculate dates for each work day
|
||||||
workWeekSettings.workDays.forEach(dayOfWeek => {
|
workWeekSettings.workDays.forEach(dayOfWeek => {
|
||||||
const date = new Date(actualWeekStart);
|
const date = new Date(monday);
|
||||||
|
|
||||||
if (firstDayOfWeek === 1 && dayOfWeek === 0) {
|
if (dayOfWeek === 0) {
|
||||||
// For Monday-based weeks, Sunday is at the end (day 7)
|
// Sunday is 6 days after Monday
|
||||||
date.setDate(actualWeekStart.getDate() + 7);
|
date.setDate(monday.getDate() + 6);
|
||||||
} else {
|
} else {
|
||||||
// Normal calculation
|
// Monday=1 becomes 0 days after, Tuesday=2 becomes 1 day after, etc.
|
||||||
date.setDate(actualWeekStart.getDate() + dayOfWeek);
|
date.setDate(monday.getDate() + dayOfWeek - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
dates.push(date);
|
dates.push(date);
|
||||||
|
|
@ -45,26 +43,27 @@ export class DateCalculator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the start of the week for a given date
|
* Get the start of the ISO week (Monday) for a given date
|
||||||
|
* @param date - Any date in the week
|
||||||
|
* @returns The Monday of the ISO week
|
||||||
|
*/
|
||||||
|
getISOWeekStart(date: Date): Date {
|
||||||
|
const monday = new Date(date);
|
||||||
|
const currentDay = monday.getDay();
|
||||||
|
const daysToSubtract = currentDay === 0 ? 6 : currentDay - 1;
|
||||||
|
monday.setDate(monday.getDate() - daysToSubtract);
|
||||||
|
monday.setHours(0, 0, 0, 0);
|
||||||
|
return monday;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the start of the week for a given date (legacy method)
|
||||||
* @param date - Any date in the week
|
* @param date - Any date in the week
|
||||||
* @param firstDayOfWeek - 0 for Sunday, 1 for Monday
|
* @param firstDayOfWeek - 0 for Sunday, 1 for Monday
|
||||||
* @returns The start date of the week
|
* @returns The start date of the week
|
||||||
*/
|
*/
|
||||||
getWeekStart(date: Date, firstDayOfWeek: number = 1): Date {
|
getWeekStart(date: Date, firstDayOfWeek: number = 1): Date {
|
||||||
const weekStart = new Date(date);
|
return this.getISOWeekStart(date);
|
||||||
const currentDay = weekStart.getDay();
|
|
||||||
|
|
||||||
if (firstDayOfWeek === 1) {
|
|
||||||
// Monday as first day
|
|
||||||
const daysToSubtract = currentDay === 0 ? 6 : currentDay - 1;
|
|
||||||
weekStart.setDate(weekStart.getDate() - daysToSubtract);
|
|
||||||
} else {
|
|
||||||
// Sunday as first day
|
|
||||||
weekStart.setDate(weekStart.getDate() - currentDay);
|
|
||||||
}
|
|
||||||
|
|
||||||
weekStart.setHours(0, 0, 0, 0);
|
|
||||||
return weekStart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue