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-25 17:46:49 +02:00
|
|
|
import { DateCalculator } from '../utils/DateCalculator';
|
|
|
|
|
import { CoreEvents } from '../constants/CoreEvents';
|
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-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-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-22 21:53:18 +02:00
|
|
|
console.log('✅ GridRenderer: Created grid container with header');
|
|
|
|
|
|
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-09-25 17:46:49 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create navigation grid container for slide animations
|
|
|
|
|
* Moved from NavigationRenderer to centralize grid creation
|
|
|
|
|
*/
|
|
|
|
|
public createNavigationGrid(parentContainer: HTMLElement, weekStart: Date): HTMLElement {
|
|
|
|
|
console.group('🔧 GridRenderer.createNavigationGrid');
|
|
|
|
|
console.log('Week start:', weekStart);
|
|
|
|
|
console.log('Parent container:', parentContainer);
|
|
|
|
|
|
|
|
|
|
const weekEnd = DateCalculator.addDays(weekStart, 6);
|
|
|
|
|
|
|
|
|
|
// Create new grid container
|
|
|
|
|
const newGrid = document.createElement('swp-grid-container');
|
|
|
|
|
newGrid.innerHTML = `
|
|
|
|
|
<swp-calendar-header></swp-calendar-header>
|
|
|
|
|
<swp-scrollable-content>
|
|
|
|
|
<swp-time-grid>
|
|
|
|
|
<swp-grid-lines></swp-grid-lines>
|
|
|
|
|
<swp-day-columns></swp-day-columns>
|
|
|
|
|
</swp-time-grid>
|
|
|
|
|
</swp-scrollable-content>
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
// Position new grid - NO transform here, let Animation API handle it
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
this.renderWeekContentInNavigationGrid(newGrid, weekStart);
|
|
|
|
|
|
|
|
|
|
console.log('Grid created:', newGrid);
|
|
|
|
|
console.log('Emitting GRID_RENDERED');
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.groupEnd();
|
|
|
|
|
return newGrid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render week content in navigation grid container
|
|
|
|
|
* Moved from NavigationRenderer
|
|
|
|
|
*/
|
|
|
|
|
private renderWeekContentInNavigationGrid(gridContainer: HTMLElement, weekStart: Date): void {
|
|
|
|
|
console.group('🔧 GridRenderer.renderWeekContentInNavigationGrid');
|
|
|
|
|
console.log('Grid container:', gridContainer);
|
|
|
|
|
console.log('Week start:', weekStart);
|
|
|
|
|
|
|
|
|
|
const header = gridContainer.querySelector('swp-calendar-header');
|
|
|
|
|
const dayColumns = gridContainer.querySelector('swp-day-columns');
|
|
|
|
|
|
|
|
|
|
if (!header || !dayColumns) {
|
|
|
|
|
console.log('Missing header or dayColumns');
|
|
|
|
|
console.groupEnd();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear existing content
|
|
|
|
|
header.innerHTML = '';
|
|
|
|
|
dayColumns.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
// Get dates using DateCalculator
|
|
|
|
|
const dates = DateCalculator.getWorkWeekDates(weekStart);
|
|
|
|
|
|
|
|
|
|
// Render headers for target week
|
|
|
|
|
dates.forEach((date, i) => {
|
|
|
|
|
const headerElement = document.createElement('swp-day-header');
|
|
|
|
|
if (DateCalculator.isToday(date)) {
|
|
|
|
|
headerElement.dataset.today = 'true';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dayName = DateCalculator.getDayName(date, 'short');
|
|
|
|
|
|
|
|
|
|
headerElement.innerHTML = `
|
|
|
|
|
<swp-day-name>${dayName}</swp-day-name>
|
|
|
|
|
<swp-day-date>${date.getDate()}</swp-day-date>
|
|
|
|
|
`;
|
|
|
|
|
headerElement.dataset.date = DateCalculator.formatISODate(date);
|
|
|
|
|
|
|
|
|
|
header.appendChild(headerElement);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Render day columns for target week
|
|
|
|
|
dates.forEach(date => {
|
|
|
|
|
const column = document.createElement('swp-day-column');
|
|
|
|
|
column.dataset.date = DateCalculator.formatISODate(date);
|
|
|
|
|
|
|
|
|
|
const eventsLayer = document.createElement('swp-events-layer');
|
|
|
|
|
column.appendChild(eventsLayer);
|
|
|
|
|
|
|
|
|
|
dayColumns.appendChild(column);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('Week content rendered');
|
|
|
|
|
console.groupEnd();
|
|
|
|
|
}
|
2025-08-17 22:54:00 +02:00
|
|
|
}
|