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
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import { CalendarConfig } from '../core/CalendarConfig';
|
||||
import { ResourceCalendarData } from '../types/CalendarTypes';
|
||||
import { DateCalculator } from '../utils/DateCalculator';
|
||||
|
||||
/**
|
||||
* Interface for column rendering strategies
|
||||
|
|
@ -23,10 +24,15 @@ export interface ColumnRenderContext {
|
|||
* Date-based column renderer (original functionality)
|
||||
*/
|
||||
export class DateColumnRenderer implements ColumnRenderer {
|
||||
private dateCalculator: DateCalculator;
|
||||
|
||||
render(columnContainer: HTMLElement, context: ColumnRenderContext): void {
|
||||
const { currentWeek, config } = context;
|
||||
|
||||
const dates = this.getWeekDates(currentWeek, config);
|
||||
// Initialize date calculator
|
||||
this.dateCalculator = new DateCalculator(config);
|
||||
|
||||
const dates = this.dateCalculator.getWorkWeekDates(currentWeek);
|
||||
const dateSettings = config.getDateViewSettings();
|
||||
const daysToShow = dates.slice(0, dateSettings.weekDays);
|
||||
|
||||
|
|
@ -34,7 +40,7 @@ export class DateColumnRenderer implements ColumnRenderer {
|
|||
|
||||
daysToShow.forEach((date) => {
|
||||
const column = document.createElement('swp-day-column');
|
||||
(column as any).dataset.date = this.formatDate(date);
|
||||
(column as any).dataset.date = this.dateCalculator.formatISODate(date);
|
||||
|
||||
const eventsLayer = document.createElement('swp-events-layer');
|
||||
column.appendChild(eventsLayer);
|
||||
|
|
@ -43,26 +49,6 @@ export class DateColumnRenderer implements ColumnRenderer {
|
|||
});
|
||||
}
|
||||
|
||||
private getWeekDates(weekStart: Date, config: CalendarConfig): Date[] {
|
||||
const dates: Date[] = [];
|
||||
const workWeekSettings = config.getWorkWeekSettings();
|
||||
|
||||
// Calculate dates based on actual work days (e.g., [1,2,3,4] for Mon-Thu)
|
||||
workWeekSettings.workDays.forEach(dayOfWeek => {
|
||||
const date = new Date(weekStart);
|
||||
// Set to the start of the week (Sunday = 0)
|
||||
date.setDate(weekStart.getDate() - weekStart.getDay());
|
||||
// Add the specific day of week
|
||||
date.setDate(date.getDate() + dayOfWeek);
|
||||
dates.push(date);
|
||||
});
|
||||
|
||||
return dates;
|
||||
}
|
||||
|
||||
private formatDate(date: Date): string {
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue