2025-09-03 20:04:47 +02:00
|
|
|
import { calendarConfig } from '../core/CalendarConfig';
|
2025-09-03 18:51:19 +02:00
|
|
|
import { ResourceCalendarData, CalendarView } from '../types/CalendarTypes';
|
2025-08-17 22:54:00 +02:00
|
|
|
import { CalendarTypeFactory } from '../factories/CalendarTypeFactory';
|
|
|
|
|
import { ColumnRenderContext } from './ColumnRenderer';
|
2025-08-24 23:31:11 +02:00
|
|
|
import { eventBus } from '../core/EventBus';
|
2025-10-03 20:50:40 +02:00
|
|
|
import { DateService } from '../utils/DateService';
|
2025-09-25 17:46:49 +02:00
|
|
|
import { CoreEvents } from '../constants/CoreEvents';
|
2025-10-06 22:29:31 +02:00
|
|
|
import { TimeFormatter } from '../utils/TimeFormatter';
|
2025-09-03 18:51:19 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
/**
|
2025-09-03 18:51:19 +02:00
|
|
|
* GridRenderer - Centralized DOM rendering for calendar grid
|
|
|
|
|
* Optimized to reduce redundant DOM operations and improve performance
|
2025-08-17 22:54:00 +02:00
|
|
|
*/
|
|
|
|
|
export class GridRenderer {
|
2025-09-03 18:51:19 +02:00
|
|
|
private cachedGridContainer: HTMLElement | null = null;
|
|
|
|
|
private cachedTimeAxis: HTMLElement | null = null;
|
2025-10-03 20:50:40 +02:00
|
|
|
private dateService: DateService;
|
2025-08-17 22:54:00 +02:00
|
|
|
|
2025-09-03 20:04:47 +02:00
|
|
|
constructor() {
|
2025-10-04 00:32:26 +02:00
|
|
|
const timezone = calendarConfig.getTimezone?.();
|
2025-10-03 20:50:40 +02:00
|
|
|
this.dateService = new DateService(timezone);
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public renderGrid(
|
2025-09-03 18:51:19 +02:00
|
|
|
grid: HTMLElement,
|
|
|
|
|
currentDate: Date,
|
|
|
|
|
resourceData: ResourceCalendarData | null,
|
|
|
|
|
view: CalendarView = 'week'
|
2025-08-17 22:54:00 +02:00
|
|
|
): void {
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
if (!grid || !currentDate) {
|
2025-08-17 22:54:00 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Cache grid reference for performance
|
|
|
|
|
this.cachedGridContainer = grid;
|
|
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Only clear and rebuild if grid is empty (first render)
|
|
|
|
|
if (grid.children.length === 0) {
|
2025-09-03 18:51:19 +02:00
|
|
|
this.createCompleteGridStructure(grid, currentDate, resourceData, view);
|
2025-09-17 23:39:29 +02:00
|
|
|
// Setup grid-related event listeners on first render
|
2025-10-06 22:29:31 +02:00
|
|
|
// this.setupGridEventListeners();
|
2025-08-17 22:54:00 +02:00
|
|
|
} else {
|
2025-09-03 18:51:19 +02:00
|
|
|
// Optimized update - only refresh dynamic content
|
|
|
|
|
this.updateGridContent(grid, currentDate, resourceData, view);
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 18:51:19 +02:00
|
|
|
* Create complete grid structure in one operation
|
2025-08-17 22:54:00 +02:00
|
|
|
*/
|
2025-09-03 18:51:19 +02:00
|
|
|
private createCompleteGridStructure(
|
|
|
|
|
grid: HTMLElement,
|
|
|
|
|
currentDate: Date,
|
|
|
|
|
resourceData: ResourceCalendarData | null,
|
|
|
|
|
view: CalendarView
|
|
|
|
|
): void {
|
|
|
|
|
// Create all elements in memory first for better performance
|
|
|
|
|
const fragment = document.createDocumentFragment();
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Create header spacer
|
2025-08-17 22:54:00 +02:00
|
|
|
const headerSpacer = document.createElement('swp-header-spacer');
|
2025-09-03 18:51:19 +02:00
|
|
|
fragment.appendChild(headerSpacer);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Create time axis with caching
|
|
|
|
|
const timeAxis = this.createOptimizedTimeAxis();
|
|
|
|
|
this.cachedTimeAxis = timeAxis;
|
|
|
|
|
fragment.appendChild(timeAxis);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Create grid container with caching
|
|
|
|
|
const gridContainer = this.createOptimizedGridContainer(currentDate, resourceData, view);
|
|
|
|
|
this.cachedGridContainer = gridContainer;
|
|
|
|
|
fragment.appendChild(gridContainer);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Append all at once to minimize reflows
|
|
|
|
|
grid.appendChild(fragment);
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
private createOptimizedTimeAxis(): HTMLElement {
|
2025-08-17 22:54:00 +02:00
|
|
|
const timeAxis = document.createElement('swp-time-axis');
|
|
|
|
|
const timeAxisContent = document.createElement('swp-time-axis-content');
|
2025-09-03 20:04:47 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
2025-08-17 22:54:00 +02:00
|
|
|
const startHour = gridSettings.dayStartHour;
|
|
|
|
|
const endHour = gridSettings.dayEndHour;
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
const fragment = document.createDocumentFragment();
|
2025-08-17 22:54:00 +02:00
|
|
|
for (let hour = startHour; hour < endHour; hour++) {
|
|
|
|
|
const marker = document.createElement('swp-hour-marker');
|
2025-10-06 22:29:31 +02:00
|
|
|
const date = new Date(2024, 0, 1, hour, 0);
|
|
|
|
|
marker.textContent = TimeFormatter.formatTime(date);
|
2025-09-03 18:51:19 +02:00
|
|
|
fragment.appendChild(marker);
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
timeAxisContent.appendChild(fragment);
|
2025-10-06 22:06:00 +02:00
|
|
|
timeAxisContent.style.top = '-1px';
|
2025-08-17 22:54:00 +02:00
|
|
|
timeAxis.appendChild(timeAxisContent);
|
2025-09-03 18:51:19 +02:00
|
|
|
return timeAxis;
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
private createOptimizedGridContainer(
|
|
|
|
|
currentDate: Date,
|
|
|
|
|
resourceData: ResourceCalendarData | null,
|
|
|
|
|
view: CalendarView
|
|
|
|
|
): HTMLElement {
|
2025-08-17 22:54:00 +02:00
|
|
|
const gridContainer = document.createElement('swp-grid-container');
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-22 21:53:18 +02:00
|
|
|
// Create calendar header as first child - always exists now!
|
|
|
|
|
const calendarHeader = document.createElement('swp-calendar-header');
|
|
|
|
|
gridContainer.appendChild(calendarHeader);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Create scrollable content structure
|
2025-08-17 22:54:00 +02:00
|
|
|
const scrollableContent = document.createElement('swp-scrollable-content');
|
|
|
|
|
const timeGrid = document.createElement('swp-time-grid');
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Add grid lines
|
|
|
|
|
const gridLines = document.createElement('swp-grid-lines');
|
|
|
|
|
timeGrid.appendChild(gridLines);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
// Create column container
|
2025-08-17 22:54:00 +02:00
|
|
|
const columnContainer = document.createElement('swp-day-columns');
|
2025-09-03 18:51:19 +02:00
|
|
|
this.renderColumnContainer(columnContainer, currentDate, resourceData, view);
|
2025-08-17 22:54:00 +02:00
|
|
|
timeGrid.appendChild(columnContainer);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
scrollableContent.appendChild(timeGrid);
|
|
|
|
|
gridContainer.appendChild(scrollableContent);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-22 21:53:18 +02:00
|
|
|
console.log('✅ GridRenderer: Created grid container with header');
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-03 18:51:19 +02:00
|
|
|
return gridContainer;
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 18:51:19 +02:00
|
|
|
* Render column container with view awareness
|
2025-08-17 22:54:00 +02:00
|
|
|
*/
|
|
|
|
|
private renderColumnContainer(
|
|
|
|
|
columnContainer: HTMLElement,
|
2025-09-03 18:51:19 +02:00
|
|
|
currentDate: Date,
|
|
|
|
|
resourceData: ResourceCalendarData | null,
|
|
|
|
|
view: CalendarView
|
2025-08-17 22:54:00 +02:00
|
|
|
): void {
|
2025-09-03 20:04:47 +02:00
|
|
|
const calendarType = calendarConfig.getCalendarMode();
|
2025-08-17 22:54:00 +02:00
|
|
|
const columnRenderer = CalendarTypeFactory.getColumnRenderer(calendarType);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
const context: ColumnRenderContext = {
|
2025-09-03 18:51:19 +02:00
|
|
|
currentWeek: currentDate, // ColumnRenderer expects currentWeek property
|
2025-09-03 20:04:47 +02:00
|
|
|
config: calendarConfig,
|
2025-08-17 22:54:00 +02:00
|
|
|
resourceData: resourceData
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
columnRenderer.render(columnContainer, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 18:51:19 +02:00
|
|
|
* Optimized update of grid content without full rebuild
|
2025-08-17 22:54:00 +02:00
|
|
|
*/
|
2025-09-03 18:51:19 +02:00
|
|
|
private updateGridContent(
|
2025-08-17 22:54:00 +02:00
|
|
|
grid: HTMLElement,
|
2025-09-03 18:51:19 +02:00
|
|
|
currentDate: Date,
|
|
|
|
|
resourceData: ResourceCalendarData | null,
|
|
|
|
|
view: CalendarView
|
2025-08-17 22:54:00 +02:00
|
|
|
): void {
|
2025-09-03 18:51:19 +02:00
|
|
|
// Update column container if needed
|
|
|
|
|
const columnContainer = grid.querySelector('swp-day-columns');
|
|
|
|
|
if (columnContainer) {
|
|
|
|
|
columnContainer.innerHTML = '';
|
|
|
|
|
this.renderColumnContainer(columnContainer as HTMLElement, currentDate, resourceData, view);
|
|
|
|
|
}
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
2025-09-25 17:46:49 +02:00
|
|
|
/**
|
|
|
|
|
* Create navigation grid container for slide animations
|
2025-09-25 17:55:13 +02:00
|
|
|
* Now uses same implementation as initial load for consistency
|
2025-09-25 17:46:49 +02:00
|
|
|
*/
|
|
|
|
|
public createNavigationGrid(parentContainer: HTMLElement, weekStart: Date): HTMLElement {
|
|
|
|
|
console.group('🔧 GridRenderer.createNavigationGrid');
|
|
|
|
|
console.log('Week start:', weekStart);
|
|
|
|
|
console.log('Parent container:', parentContainer);
|
2025-09-25 17:55:13 +02:00
|
|
|
console.log('Using same grid creation as initial load');
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-10-03 20:50:40 +02:00
|
|
|
const weekEnd = this.dateService.addDays(weekStart, 6);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-25 17:55:13 +02:00
|
|
|
// Use SAME method as initial load - respects workweek and resource settings
|
|
|
|
|
const newGrid = this.createOptimizedGridContainer(weekStart, null, 'week');
|
2025-09-25 17:46:49 +02:00
|
|
|
|
2025-09-25 17:55:13 +02:00
|
|
|
// Position new grid for animation - NO transform here, let Animation API handle it
|
2025-09-25 17:46:49 +02:00
|
|
|
newGrid.style.position = 'absolute';
|
|
|
|
|
newGrid.style.top = '0';
|
|
|
|
|
newGrid.style.left = '0';
|
|
|
|
|
newGrid.style.width = '100%';
|
|
|
|
|
newGrid.style.height = '100%';
|
|
|
|
|
|
|
|
|
|
// Add to parent container
|
|
|
|
|
parentContainer.appendChild(newGrid);
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-25 17:55:13 +02:00
|
|
|
console.log('Grid created using createOptimizedGridContainer:', newGrid);
|
2025-09-25 17:46:49 +02:00
|
|
|
console.log('Emitting GRID_RENDERED');
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-25 17:46:49 +02:00
|
|
|
eventBus.emit(CoreEvents.GRID_RENDERED, {
|
|
|
|
|
container: newGrid, // Specific grid container, not parent
|
|
|
|
|
currentDate: weekStart,
|
|
|
|
|
startDate: weekStart,
|
|
|
|
|
endDate: weekEnd,
|
|
|
|
|
isNavigation: true // Flag to indicate this is navigation rendering
|
|
|
|
|
});
|
2025-10-06 22:29:31 +02:00
|
|
|
|
2025-09-25 17:46:49 +02:00
|
|
|
console.groupEnd();
|
|
|
|
|
return newGrid;
|
|
|
|
|
}
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|