2025-08-07 00:15:44 +02:00
|
|
|
// Grid structure management - Simple CSS Grid Implementation with Strategy Pattern
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
import { eventBus } from '../core/EventBus';
|
|
|
|
|
import { calendarConfig } from '../core/CalendarConfig';
|
|
|
|
|
import { EventTypes } from '../constants/EventTypes';
|
2025-08-09 00:31:44 +02:00
|
|
|
import { StateEvents } from '../types/CalendarState';
|
2025-07-24 22:17:38 +02:00
|
|
|
import { DateUtils } from '../utils/DateUtils';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { ResourceCalendarData } from '../types/CalendarTypes';
|
2025-08-17 22:54:00 +02:00
|
|
|
import { GridRenderer } from '../renderers/GridRenderer';
|
|
|
|
|
import { GridStyleManager } from '../renderers/GridStyleManager';
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Grid position interface
|
|
|
|
|
*/
|
|
|
|
|
interface GridPosition {
|
|
|
|
|
minutes: number;
|
|
|
|
|
time: string;
|
|
|
|
|
y: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-07 00:15:44 +02:00
|
|
|
* Manages the calendar grid structure using simple CSS Grid with Strategy Pattern
|
2025-07-24 22:17:38 +02:00
|
|
|
*/
|
|
|
|
|
export class GridManager {
|
|
|
|
|
private container: HTMLElement | null = null;
|
2025-07-25 23:31:25 +02:00
|
|
|
private grid: HTMLElement | null = null;
|
2025-07-24 22:17:38 +02:00
|
|
|
private currentWeek: Date | null = null;
|
2025-08-04 23:55:04 +02:00
|
|
|
private allDayEvents: any[] = []; // Store all-day events for current week
|
2025-08-07 00:15:44 +02:00
|
|
|
private resourceData: ResourceCalendarData | null = null; // Store resource data for resource calendar
|
2025-08-17 22:54:00 +02:00
|
|
|
private gridRenderer: GridRenderer;
|
|
|
|
|
private styleManager: GridStyleManager;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
constructor() {
|
2025-08-09 00:31:44 +02:00
|
|
|
console.log('🏗️ GridManager: Constructor called');
|
2025-08-17 22:54:00 +02:00
|
|
|
this.gridRenderer = new GridRenderer(calendarConfig);
|
|
|
|
|
this.styleManager = new GridStyleManager(calendarConfig);
|
2025-07-24 22:17:38 +02:00
|
|
|
this.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private init(): void {
|
|
|
|
|
this.findElements();
|
|
|
|
|
this.subscribeToEvents();
|
2025-07-25 23:31:25 +02:00
|
|
|
|
|
|
|
|
// Set initial current week to today if not set
|
|
|
|
|
if (!this.currentWeek) {
|
|
|
|
|
this.currentWeek = this.getWeekStart(new Date());
|
|
|
|
|
console.log('GridManager: Set initial currentWeek to', this.currentWeek);
|
2025-08-09 00:31:44 +02:00
|
|
|
// Don't render immediately - wait for proper initialization event
|
|
|
|
|
console.log('GridManager: Waiting for initialization complete before rendering');
|
2025-07-25 23:31:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getWeekStart(date: Date): Date {
|
2025-08-02 23:59:52 +02:00
|
|
|
// Use DateUtils for consistent week calculation (Sunday = 0)
|
|
|
|
|
return DateUtils.getWeekStart(date, 0);
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private findElements(): void {
|
2025-07-25 23:31:25 +02:00
|
|
|
this.grid = document.querySelector('swp-calendar-container');
|
2025-08-09 01:16:04 +02:00
|
|
|
console.log('GridManager: findElements called, found swp-calendar-container:', !!this.grid);
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private subscribeToEvents(): void {
|
2025-08-09 01:16:04 +02:00
|
|
|
// State-driven events removed - render() is now called directly by CalendarManager
|
2025-08-09 00:31:44 +02:00
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
// Re-render grid on config changes
|
|
|
|
|
eventBus.on(EventTypes.CONFIG_UPDATE, (e: Event) => {
|
|
|
|
|
const detail = (e as CustomEvent).detail;
|
2025-08-05 21:56:06 +02:00
|
|
|
if (['dayStartHour', 'dayEndHour', 'hourHeight', 'view', 'weekDays', 'fitToWidth'].includes(detail.key)) {
|
2025-07-24 22:17:38 +02:00
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Re-render on calendar type change
|
|
|
|
|
eventBus.on(EventTypes.CALENDAR_TYPE_CHANGED, () => {
|
|
|
|
|
this.render();
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
// Re-render on view change
|
|
|
|
|
eventBus.on(EventTypes.VIEW_CHANGE, () => {
|
|
|
|
|
this.render();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Re-render on period change
|
|
|
|
|
eventBus.on(EventTypes.PERIOD_CHANGE, (e: Event) => {
|
|
|
|
|
const detail = (e as CustomEvent).detail;
|
|
|
|
|
this.currentWeek = detail.week;
|
2025-07-25 23:31:25 +02:00
|
|
|
this.render();
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
|
2025-07-25 00:24:15 +02:00
|
|
|
// Handle week changes from NavigationManager
|
|
|
|
|
eventBus.on(EventTypes.WEEK_CHANGED, (e: Event) => {
|
|
|
|
|
const detail = (e as CustomEvent).detail;
|
|
|
|
|
this.currentWeek = detail.weekStart;
|
|
|
|
|
this.render();
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-04 23:55:04 +02:00
|
|
|
// Handle events loaded
|
|
|
|
|
eventBus.on(EventTypes.EVENTS_LOADED, (e: Event) => {
|
|
|
|
|
const detail = (e as CustomEvent).detail;
|
|
|
|
|
this.updateAllDayEvents(detail.events);
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
// Handle data loaded for resource mode
|
|
|
|
|
eventBus.on(StateEvents.DATA_LOADED, (e: Event) => {
|
2025-08-07 00:15:44 +02:00
|
|
|
const detail = (e as CustomEvent).detail;
|
2025-08-09 00:31:44 +02:00
|
|
|
console.log(`GridManager: Received DATA_LOADED`);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
if (detail.data && detail.data.calendarMode === 'resource') {
|
|
|
|
|
// Resource data will be passed in the state event
|
|
|
|
|
// For now just update grid styles
|
2025-08-17 22:54:00 +02:00
|
|
|
this.styleManager.updateGridStyles(this.resourceData);
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
// Handle grid clicks
|
|
|
|
|
this.setupGridInteractions();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 23:06:03 +02:00
|
|
|
/**
|
|
|
|
|
* Set resource data for resource calendar mode
|
|
|
|
|
*/
|
|
|
|
|
public setResourceData(resourceData: ResourceCalendarData | null): void {
|
|
|
|
|
this.resourceData = resourceData;
|
|
|
|
|
console.log('GridManager: Set resource data:', resourceData ? `${resourceData.resources.length} resources` : 'null');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
/**
|
2025-08-09 01:16:04 +02:00
|
|
|
* Render the complete grid structure - now returns Promise for direct calls
|
2025-07-24 22:17:38 +02:00
|
|
|
*/
|
2025-08-09 01:16:04 +02:00
|
|
|
async render(): Promise<void> {
|
|
|
|
|
if (!this.grid) {
|
|
|
|
|
console.warn('GridManager: render() called but this.grid is null, re-finding elements');
|
|
|
|
|
this.findElements();
|
|
|
|
|
if (!this.grid) {
|
|
|
|
|
throw new Error('GridManager: swp-calendar-container not found, cannot render');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-25 23:31:25 +02:00
|
|
|
|
2025-08-09 01:16:04 +02:00
|
|
|
console.log('GridManager: Starting render with grid element:', this.grid);
|
2025-08-17 22:54:00 +02:00
|
|
|
this.styleManager.updateGridStyles(this.resourceData);
|
|
|
|
|
this.gridRenderer.renderGrid(this.grid, this.currentWeek!, this.resourceData, this.allDayEvents);
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
const columnCount = this.styleManager.getColumnCount(this.resourceData);
|
2025-08-09 01:16:04 +02:00
|
|
|
console.log(`GridManager: Render complete - created ${columnCount} columns`);
|
2025-08-16 00:51:12 +02:00
|
|
|
|
|
|
|
|
// Emit GRID_RENDERED event to trigger event rendering
|
|
|
|
|
const weekEnd = this.currentWeek ? new Date(this.currentWeek.getTime() + 6 * 24 * 60 * 60 * 1000) : null;
|
|
|
|
|
eventBus.emit(EventTypes.GRID_RENDERED, {
|
|
|
|
|
container: this.grid,
|
|
|
|
|
currentWeek: this.currentWeek,
|
|
|
|
|
startDate: this.currentWeek,
|
|
|
|
|
endDate: weekEnd,
|
|
|
|
|
columnCount: columnCount
|
|
|
|
|
});
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Column count calculation moved to GridStyleManager
|
2025-07-25 23:31:25 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Grid rendering methods moved to GridRenderer
|
2025-08-05 00:41:59 +02:00
|
|
|
|
2025-08-04 23:55:04 +02:00
|
|
|
/**
|
|
|
|
|
* Update all-day events data and re-render if needed
|
|
|
|
|
*/
|
|
|
|
|
private updateAllDayEvents(events: any[]): void {
|
|
|
|
|
if (!this.currentWeek) return;
|
|
|
|
|
|
|
|
|
|
// Filter all-day events for current week
|
|
|
|
|
const weekStart = this.currentWeek;
|
|
|
|
|
const weekEnd = new Date(weekStart);
|
|
|
|
|
weekEnd.setDate(weekStart.getDate() + 6);
|
|
|
|
|
|
|
|
|
|
this.allDayEvents = events.filter(event => {
|
|
|
|
|
if (!event.allDay) return false;
|
|
|
|
|
|
|
|
|
|
const eventDate = new Date(event.start);
|
|
|
|
|
return eventDate >= weekStart && eventDate <= weekEnd;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('GridManager: Updated all-day events:', this.allDayEvents.length);
|
|
|
|
|
|
2025-08-07 00:26:33 +02:00
|
|
|
// Update only the calendar header if grid is already rendered
|
2025-08-04 23:55:04 +02:00
|
|
|
if (this.grid && this.grid.children.length > 0) {
|
2025-08-17 22:54:00 +02:00
|
|
|
this.gridRenderer.renderGrid(this.grid, this.currentWeek!, this.resourceData, this.allDayEvents);
|
2025-08-04 23:55:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// CSS management methods moved to GridStyleManager
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-07-25 23:31:25 +02:00
|
|
|
* Setup grid interaction handlers for POC structure
|
2025-07-24 22:17:38 +02:00
|
|
|
*/
|
|
|
|
|
private setupGridInteractions(): void {
|
2025-07-25 23:31:25 +02:00
|
|
|
if (!this.grid) return;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Click handler for day columns (works for both date and resource columns)
|
2025-07-25 23:31:25 +02:00
|
|
|
this.grid.addEventListener('click', (e: MouseEvent) => {
|
2025-07-24 22:17:38 +02:00
|
|
|
// Ignore if clicking on an event
|
|
|
|
|
if ((e.target as Element).closest('swp-event')) return;
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
const dayColumn = (e.target as Element).closest('swp-day-column, swp-resource-column') as HTMLElement;
|
2025-07-25 23:31:25 +02:00
|
|
|
if (!dayColumn) return;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
const position = this.getClickPosition(e, dayColumn);
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
eventBus.emit(EventTypes.GRID_CLICK, {
|
2025-07-25 23:31:25 +02:00
|
|
|
date: (dayColumn as any).dataset.date,
|
2025-08-07 00:15:44 +02:00
|
|
|
resource: (dayColumn as any).dataset.resource,
|
|
|
|
|
employeeId: (dayColumn as any).dataset.employeeId,
|
2025-07-24 22:17:38 +02:00
|
|
|
time: position.time,
|
2025-07-26 00:00:03 +02:00
|
|
|
minutes: position.minutes
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
// Double click handler for day columns
|
|
|
|
|
this.grid.addEventListener('dblclick', (e: MouseEvent) => {
|
2025-07-24 22:17:38 +02:00
|
|
|
// Ignore if clicking on an event
|
|
|
|
|
if ((e.target as Element).closest('swp-event')) return;
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
const dayColumn = (e.target as Element).closest('swp-day-column, swp-resource-column') as HTMLElement;
|
2025-07-25 23:31:25 +02:00
|
|
|
if (!dayColumn) return;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
const position = this.getClickPosition(e, dayColumn);
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
eventBus.emit(EventTypes.GRID_DBLCLICK, {
|
2025-07-25 23:31:25 +02:00
|
|
|
date: (dayColumn as any).dataset.date,
|
2025-08-07 00:15:44 +02:00
|
|
|
resource: (dayColumn as any).dataset.resource,
|
|
|
|
|
employeeId: (dayColumn as any).dataset.employeeId,
|
2025-07-24 22:17:38 +02:00
|
|
|
time: position.time,
|
2025-07-26 00:00:03 +02:00
|
|
|
minutes: position.minutes
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-25 23:31:25 +02:00
|
|
|
* Get click position in day column (POC structure)
|
2025-07-24 22:17:38 +02:00
|
|
|
*/
|
2025-07-25 23:31:25 +02:00
|
|
|
private getClickPosition(event: MouseEvent, dayColumn: HTMLElement): GridPosition {
|
|
|
|
|
const rect = dayColumn.getBoundingClientRect();
|
|
|
|
|
const y = event.clientY - rect.top;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
|
|
|
|
const hourHeight = gridSettings.hourHeight;
|
2025-07-25 23:31:25 +02:00
|
|
|
const minuteHeight = hourHeight / 60;
|
2025-08-09 00:31:44 +02:00
|
|
|
const snapInterval = gridSettings.snapInterval;
|
|
|
|
|
const dayStartHour = gridSettings.dayStartHour;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
// Calculate total minutes from day start
|
|
|
|
|
let totalMinutes = Math.floor(y / minuteHeight);
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
// Snap to interval
|
2025-07-25 23:31:25 +02:00
|
|
|
totalMinutes = Math.round(totalMinutes / snapInterval) * snapInterval;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
// Add day start offset
|
2025-07-25 23:31:25 +02:00
|
|
|
totalMinutes += dayStartHour * 60;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
minutes: totalMinutes,
|
|
|
|
|
time: this.minutesToTime(totalMinutes),
|
2025-07-25 23:31:25 +02:00
|
|
|
y: y
|
2025-07-24 22:17:38 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
/**
|
|
|
|
|
* Scroll to specific hour
|
|
|
|
|
*/
|
|
|
|
|
scrollToHour(hour: number): void {
|
|
|
|
|
if (!this.grid) return;
|
|
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
|
|
|
|
const hourHeight = gridSettings.hourHeight;
|
|
|
|
|
const dayStartHour = gridSettings.dayStartHour;
|
2025-07-25 23:31:25 +02:00
|
|
|
const headerHeight = 80; // Header row height
|
|
|
|
|
const scrollTop = headerHeight + ((hour - dayStartHour) * hourHeight);
|
|
|
|
|
|
|
|
|
|
this.grid.scrollTop = scrollTop;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
/**
|
|
|
|
|
* Utility methods
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
private minutesToTime(totalMinutes: number): string {
|
|
|
|
|
const hours = Math.floor(totalMinutes / 60);
|
|
|
|
|
const minutes = totalMinutes % 60;
|
|
|
|
|
const period = hours >= 12 ? 'PM' : 'AM';
|
|
|
|
|
const displayHour = hours > 12 ? hours - 12 : (hours === 0 ? 12 : hours);
|
|
|
|
|
|
|
|
|
|
return `${displayHour}:${minutes.toString().padStart(2, '0')} ${period}`;
|
|
|
|
|
}
|
|
|
|
|
}
|