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-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-08-17 22:54:00 +02:00
|
|
|
|
2025-09-03 20:04:47 +02:00
|
|
|
constructor() {
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 18:51:19 +02:00
|
|
|
* Render the complete grid structure with view-aware optimization
|
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-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-09-21 15:48:13 +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();
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// Create time axis with caching
|
|
|
|
|
const timeAxis = this.createOptimizedTimeAxis();
|
|
|
|
|
this.cachedTimeAxis = timeAxis;
|
|
|
|
|
fragment.appendChild(timeAxis);
|
|
|
|
|
|
|
|
|
|
// Create grid container with caching
|
|
|
|
|
const gridContainer = this.createOptimizedGridContainer(currentDate, resourceData, view);
|
|
|
|
|
this.cachedGridContainer = gridContainer;
|
|
|
|
|
fragment.appendChild(gridContainer);
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
* Create optimized time axis with caching
|
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-09-03 18:51:19 +02:00
|
|
|
// Create all hour markers in memory first
|
|
|
|
|
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');
|
|
|
|
|
const period = hour >= 12 ? 'PM' : 'AM';
|
|
|
|
|
const displayHour = hour > 12 ? hour - 12 : (hour === 0 ? 12 : hour);
|
|
|
|
|
marker.textContent = `${displayHour} ${period}`;
|
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-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
|
|
|
* Create optimized grid container with header and scrollable content
|
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-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');
|
|
|
|
|
|
|
|
|
|
// Add grid lines
|
|
|
|
|
const gridLines = document.createElement('swp-grid-lines');
|
|
|
|
|
timeGrid.appendChild(gridLines);
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
scrollableContent.appendChild(timeGrid);
|
|
|
|
|
gridContainer.appendChild(scrollableContent);
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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-08-25 22:05:57 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-09-10 22:07:40 +02:00
|
|
|
* Setup grid-only event listeners (column events)
|
2025-09-21 15:48:13 +02:00
|
|
|
|
2025-09-10 22:07:40 +02:00
|
|
|
private setupGridEventListeners(): void {
|
|
|
|
|
// Setup grid body mouseover listener for all-day to timed conversion
|
|
|
|
|
this.setupGridBodyMouseOver();
|
|
|
|
|
}
|
2025-09-21 15:48:13 +02:00
|
|
|
*/
|
2025-09-10 22:07:40 +02:00
|
|
|
/**
|
|
|
|
|
* Setup grid body mouseover listener for all-day to timed conversion
|
2025-09-21 15:48:13 +02:00
|
|
|
|
2025-09-10 22:07:40 +02:00
|
|
|
private setupGridBodyMouseOver(): void {
|
|
|
|
|
const grid = this.cachedGridContainer;
|
|
|
|
|
if (!grid) return;
|
|
|
|
|
|
|
|
|
|
const columnContainer = grid.querySelector('swp-day-columns');
|
|
|
|
|
if (!columnContainer) return;
|
|
|
|
|
|
|
|
|
|
// Throttle for better performance
|
2025-09-03 18:51:19 +02:00
|
|
|
let lastEmitTime = 0;
|
|
|
|
|
const throttleDelay = 16; // ~60fps
|
2025-09-10 22:07:40 +02:00
|
|
|
|
|
|
|
|
const gridBodyEventListener = (event: Event) => {
|
2025-09-03 18:51:19 +02:00
|
|
|
const now = Date.now();
|
|
|
|
|
if (now - lastEmitTime < throttleDelay) {
|
2025-09-10 22:07:40 +02:00
|
|
|
return;
|
2025-09-03 18:51:19 +02:00
|
|
|
}
|
|
|
|
|
lastEmitTime = now;
|
2025-08-25 22:05:57 +02:00
|
|
|
|
2025-09-10 22:07:40 +02:00
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
const dayColumn = target.closest('swp-day-column');
|
2025-08-25 22:05:57 +02:00
|
|
|
|
2025-09-10 22:07:40 +02:00
|
|
|
if (dayColumn) {
|
|
|
|
|
const targetColumn = (dayColumn as HTMLElement).dataset.date;
|
|
|
|
|
if (targetColumn) {
|
|
|
|
|
// Calculate Y position relative to the column
|
|
|
|
|
const columnRect = dayColumn.getBoundingClientRect();
|
|
|
|
|
const mouseY = (event as MouseEvent).clientY;
|
|
|
|
|
const targetY = mouseY - columnRect.top;
|
2025-08-26 00:05:42 +02:00
|
|
|
|
2025-09-10 22:07:40 +02:00
|
|
|
eventBus.emit('column:mouseover', {
|
|
|
|
|
targetColumn,
|
|
|
|
|
targetY
|
|
|
|
|
});
|
2025-08-26 00:05:42 +02:00
|
|
|
}
|
2025-08-25 22:05:57 +02:00
|
|
|
}
|
|
|
|
|
};
|
2025-09-10 22:07:40 +02:00
|
|
|
|
|
|
|
|
columnContainer.addEventListener('mouseover', gridBodyEventListener);
|
|
|
|
|
|
|
|
|
|
// Store reference for cleanup
|
|
|
|
|
(this as any).gridBodyEventListener = gridBodyEventListener;
|
|
|
|
|
(this as any).cachedColumnContainer = columnContainer;
|
2025-08-25 22:05:57 +02:00
|
|
|
}
|
2025-09-21 15:48:13 +02:00
|
|
|
*/
|
2025-09-03 18:51:19 +02:00
|
|
|
/**
|
|
|
|
|
* Clean up cached elements and event listeners
|
|
|
|
|
*/
|
|
|
|
|
public destroy(): void {
|
2025-09-10 22:07:40 +02:00
|
|
|
// Clean up grid-only event listeners
|
2025-09-21 15:48:13 +02:00
|
|
|
// if ((this as any).gridBodyEventListener && (this as any).cachedColumnContainer) {
|
|
|
|
|
// (this as any).cachedColumnContainer.removeEventListener('mouseover', (this as any).gridBodyEventListener);
|
|
|
|
|
//}
|
2025-09-03 18:51:19 +02:00
|
|
|
|
|
|
|
|
// Clear cached references
|
|
|
|
|
this.cachedGridContainer = null;
|
|
|
|
|
this.cachedTimeAxis = null;
|
2025-09-10 22:07:40 +02:00
|
|
|
(this as any).gridBodyEventListener = null;
|
|
|
|
|
(this as any).cachedColumnContainer = null;
|
2025-09-03 18:51:19 +02:00
|
|
|
}
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|