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 { IEventBus } from '../types/CalendarTypes';
|
|||
import { EventTypes } from '../constants/EventTypes';
|
||||
import { DateUtils } from '../utils/DateUtils';
|
||||
import { CalendarConfig } from '../core/CalendarConfig';
|
||||
import { DateCalculator } from '../utils/DateCalculator';
|
||||
|
||||
/**
|
||||
* NavigationRenderer - Handles DOM rendering for navigation containers
|
||||
|
|
@ -10,10 +11,12 @@ import { CalendarConfig } from '../core/CalendarConfig';
|
|||
export class NavigationRenderer {
|
||||
private eventBus: IEventBus;
|
||||
private config: CalendarConfig;
|
||||
private dateCalculator: DateCalculator;
|
||||
|
||||
constructor(eventBus: IEventBus, config: CalendarConfig) {
|
||||
this.eventBus = eventBus;
|
||||
this.config = config;
|
||||
this.dateCalculator = new DateCalculator(config);
|
||||
this.setupEventListeners();
|
||||
}
|
||||
|
||||
|
|
@ -99,17 +102,14 @@ export class NavigationRenderer {
|
|||
header.innerHTML = '';
|
||||
dayColumns.innerHTML = '';
|
||||
|
||||
// Render headers for target week
|
||||
// Get dates using DateCalculator
|
||||
const dates = this.dateCalculator.getWorkWeekDates(weekStart);
|
||||
const workWeekSettings = this.config.getWorkWeekSettings();
|
||||
workWeekSettings.workDays.forEach((dayOfWeek, i) => {
|
||||
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);
|
||||
|
||||
|
||||
// Render headers for target week
|
||||
dates.forEach((date, i) => {
|
||||
const headerElement = document.createElement('swp-day-header');
|
||||
if (this.isToday(date)) {
|
||||
if (this.dateCalculator.isToday(date)) {
|
||||
headerElement.dataset.today = 'true';
|
||||
}
|
||||
|
||||
|
|
@ -117,20 +117,15 @@ export class NavigationRenderer {
|
|||
<swp-day-name>${workWeekSettings.dayNames[i]}</swp-day-name>
|
||||
<swp-day-date>${date.getDate()}</swp-day-date>
|
||||
`;
|
||||
headerElement.dataset.date = this.formatDate(date);
|
||||
headerElement.dataset.date = this.dateCalculator.formatISODate(date);
|
||||
|
||||
header.appendChild(headerElement);
|
||||
});
|
||||
|
||||
// Render day columns for target week
|
||||
workWeekSettings.workDays.forEach(dayOfWeek => {
|
||||
dates.forEach(date => {
|
||||
const column = document.createElement('swp-day-column');
|
||||
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);
|
||||
column.dataset.date = this.formatDate(date);
|
||||
column.dataset.date = this.dateCalculator.formatISODate(date);
|
||||
|
||||
const eventsLayer = document.createElement('swp-events-layer');
|
||||
column.appendChild(eventsLayer);
|
||||
|
|
@ -139,18 +134,4 @@ export class NavigationRenderer {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to format date
|
||||
*/
|
||||
private formatDate(date: Date): string {
|
||||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if date is today
|
||||
*/
|
||||
private isToday(date: Date): boolean {
|
||||
const today = new Date();
|
||||
return date.toDateString() === today.toDateString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue