Refactors DateCalculator to be a static class

This change refactors the DateCalculator class to be a static class.
This removes the need to instantiate DateCalculator in multiple
managers and renderers, simplifying dependency management and
ensuring consistent date calculations across the application.
The configuration is now initialized once at the application start.
This commit is contained in:
Janus Knudsen 2025-09-03 18:38:52 +02:00
parent 0da875a224
commit b8b44ddae8
11 changed files with 107 additions and 83 deletions

View file

@ -11,7 +11,8 @@ export class MonthViewStrategy implements ViewStrategy {
private dateCalculator: DateCalculator;
constructor() {
this.dateCalculator = new DateCalculator(calendarConfig);
DateCalculator.initialize(calendarConfig);
this.dateCalculator = new DateCalculator();
}
getLayoutConfig(): ViewLayoutConfig {
@ -72,7 +73,7 @@ export class MonthViewStrategy implements ViewStrategy {
dates.forEach(date => {
const cell = document.createElement('div');
cell.className = 'month-day-cell';
cell.dataset.date = this.dateCalculator.formatISODate(date);
cell.dataset.date = DateCalculator.formatISODate(date);
cell.style.border = '1px solid #e0e0e0';
cell.style.minHeight = '100px';
cell.style.padding = '4px';
@ -86,7 +87,7 @@ export class MonthViewStrategy implements ViewStrategy {
dayNumber.style.marginBottom = '4px';
// Check if today
if (this.dateCalculator.isToday(date)) {
if (DateCalculator.isToday(date)) {
dayNumber.style.color = '#1976d2';
cell.style.backgroundColor = '#f5f5f5';
}
@ -101,12 +102,12 @@ export class MonthViewStrategy implements ViewStrategy {
const firstOfMonth = new Date(monthDate.getFullYear(), monthDate.getMonth(), 1);
// Get Monday of the week containing first day
const startDate = this.dateCalculator.getISOWeekStart(firstOfMonth);
const startDate = DateCalculator.getISOWeekStart(firstOfMonth);
// Generate 42 days (6 weeks)
const dates: Date[] = [];
for (let i = 0; i < 42; i++) {
dates.push(this.dateCalculator.addDays(startDate, i));
dates.push(DateCalculator.addDays(startDate, i));
}
return dates;
@ -141,10 +142,10 @@ export class MonthViewStrategy implements ViewStrategy {
const firstOfMonth = new Date(baseDate.getFullYear(), baseDate.getMonth(), 1);
// Get Monday of the week containing first day
const startDate = this.dateCalculator.getISOWeekStart(firstOfMonth);
const startDate = DateCalculator.getISOWeekStart(firstOfMonth);
// End date is 41 days after start (42 total days)
const endDate = this.dateCalculator.addDays(startDate, 41);
const endDate = DateCalculator.addDays(startDate, 41);
return {
startDate,

View file

@ -15,7 +15,8 @@ export class WeekViewStrategy implements ViewStrategy {
private styleManager: GridStyleManager;
constructor() {
this.dateCalculator = new DateCalculator(calendarConfig);
DateCalculator.initialize(calendarConfig);
this.dateCalculator = new DateCalculator();
this.gridRenderer = new GridRenderer(calendarConfig);
this.styleManager = new GridStyleManager(calendarConfig);
}
@ -42,28 +43,28 @@ export class WeekViewStrategy implements ViewStrategy {
}
getNextPeriod(currentDate: Date): Date {
return this.dateCalculator.addWeeks(currentDate, 1);
return DateCalculator.addWeeks(currentDate, 1);
}
getPreviousPeriod(currentDate: Date): Date {
return this.dateCalculator.addWeeks(currentDate, -1);
return DateCalculator.addWeeks(currentDate, -1);
}
getPeriodLabel(date: Date): string {
const weekStart = this.dateCalculator.getISOWeekStart(date);
const weekEnd = this.dateCalculator.addDays(weekStart, 6);
const weekNumber = this.dateCalculator.getWeekNumber(date);
const weekStart = DateCalculator.getISOWeekStart(date);
const weekEnd = DateCalculator.addDays(weekStart, 6);
const weekNumber = DateCalculator.getWeekNumber(date);
return `Week ${weekNumber}: ${this.dateCalculator.formatDateRange(weekStart, weekEnd)}`;
return `Week ${weekNumber}: ${DateCalculator.formatDateRange(weekStart, weekEnd)}`;
}
getDisplayDates(baseDate: Date): Date[] {
return this.dateCalculator.getWorkWeekDates(baseDate);
return DateCalculator.getWorkWeekDates(baseDate);
}
getPeriodRange(baseDate: Date): { startDate: Date; endDate: Date } {
const weekStart = this.dateCalculator.getISOWeekStart(baseDate);
const weekEnd = this.dateCalculator.addDays(weekStart, 6);
const weekStart = DateCalculator.getISOWeekStart(baseDate);
const weekEnd = DateCalculator.addDays(weekStart, 6);
return {
startDate: weekStart,