Refactors date calculations into DateCalculator
Centralizes date calculation logic into a dedicated DateCalculator class for improved maintainability and testability. Removes redundant date calculation methods from CalendarManager, ColumnRenderer, HeaderRenderer and NavigationRenderer, and utilizes the DateCalculator for these calculations. Updates components to use DateCalculator for consistent date handling, including week start determination, date formatting, and "is today" checks.
This commit is contained in:
parent
2f854b2c64
commit
18f7953db4
5 changed files with 233 additions and 124 deletions
|
|
@ -6,6 +6,7 @@ import { EventManager } from './EventManager.js';
|
|||
import { GridManager } from './GridManager.js';
|
||||
import { EventRenderingService } from '../renderers/EventRendererManager.js';
|
||||
import { ScrollManager } from './ScrollManager.js';
|
||||
import { DateCalculator } from '../utils/DateCalculator.js';
|
||||
|
||||
/**
|
||||
* CalendarManager - Main coordinator for all calendar managers
|
||||
|
|
@ -18,6 +19,7 @@ export class CalendarManager {
|
|||
private gridManager: GridManager;
|
||||
private eventRenderer: EventRenderingService;
|
||||
private scrollManager: ScrollManager;
|
||||
private dateCalculator: DateCalculator;
|
||||
private currentView: CalendarView = 'week';
|
||||
private currentDate: Date = new Date();
|
||||
private isInitialized: boolean = false;
|
||||
|
|
@ -36,6 +38,7 @@ export class CalendarManager {
|
|||
this.gridManager = gridManager;
|
||||
this.eventRenderer = eventRenderer;
|
||||
this.scrollManager = scrollManager;
|
||||
this.dateCalculator = new DateCalculator(config);
|
||||
this.setupEventListeners();
|
||||
console.log('📋 CalendarManager: Created with proper dependency injection');
|
||||
}
|
||||
|
|
@ -428,8 +431,8 @@ export class CalendarManager {
|
|||
// Trigger event rendering for the current date range using correct method
|
||||
this.eventRenderer.renderEvents({
|
||||
container: container as HTMLElement,
|
||||
startDate: new Date(periodData.startDate),
|
||||
endDate: new Date(periodData.endDate)
|
||||
startDate: new Date(periodData.start),
|
||||
endDate: new Date(periodData.end)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -471,10 +474,10 @@ export class CalendarManager {
|
|||
const lastDate = new Date(lastDateStr);
|
||||
|
||||
// Calculate week number from first date
|
||||
const weekNumber = this.getWeekNumber(firstDate);
|
||||
const weekNumber = this.dateCalculator.getWeekNumber(firstDate);
|
||||
|
||||
// Format date range
|
||||
const dateRange = this.formatDateRange(firstDate, lastDate);
|
||||
const dateRange = this.dateCalculator.formatDateRange(firstDate, lastDate);
|
||||
|
||||
console.log('CalendarManager: Week info from columns:', {
|
||||
firstDate: firstDateStr,
|
||||
|
|
@ -492,41 +495,5 @@ export class CalendarManager {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to get week number
|
||||
*/
|
||||
private getWeekNumber(date: Date): number {
|
||||
const start = new Date(date.getFullYear(), 0, 1);
|
||||
const days = Math.floor((date.getTime() - start.getTime()) / (24 * 60 * 60 * 1000));
|
||||
return Math.ceil((days + start.getDay() + 1) / 7);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to get week start (Sunday)
|
||||
*/
|
||||
private getWeekStart(date: Date): Date {
|
||||
const weekStart = new Date(date);
|
||||
weekStart.setDate(date.getDate() - date.getDay());
|
||||
return weekStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to format date range
|
||||
*/
|
||||
private formatDateRange(start: Date, end: Date): string {
|
||||
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
const startMonth = months[start.getMonth()];
|
||||
const endMonth = months[end.getMonth()];
|
||||
const startDay = start.getDate();
|
||||
const endDay = end.getDate();
|
||||
const year = start.getFullYear();
|
||||
|
||||
if (startMonth === endMonth) {
|
||||
return `${startMonth} ${startDay} - ${endDay}, ${year}`;
|
||||
} else {
|
||||
return `${startMonth} ${startDay} - ${endMonth} ${endDay}, ${year}`;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue