2025-07-24 22:17:38 +02:00
|
|
|
import { EventBus } from '../core/EventBus';
|
|
|
|
|
import { IEventBus, CalendarEvent } from '../types/CalendarTypes';
|
|
|
|
|
import { EventTypes } from '../constants/EventTypes';
|
|
|
|
|
import { calendarConfig } from '../core/CalendarConfig';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { CalendarTypeFactory } from '../factories/CalendarTypeFactory';
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-08-07 00:15:44 +02:00
|
|
|
* EventRenderer - Render events i DOM med positionering using Strategy Pattern
|
2025-07-24 22:17:38 +02:00
|
|
|
* Håndterer event positioning og overlap detection
|
|
|
|
|
*/
|
|
|
|
|
export class EventRenderer {
|
|
|
|
|
private eventBus: IEventBus;
|
2025-08-07 00:15:44 +02:00
|
|
|
private pendingEvents: CalendarEvent[] = [];
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
constructor(eventBus: IEventBus) {
|
|
|
|
|
this.eventBus = eventBus;
|
|
|
|
|
this.setupEventListeners();
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
// Initialize the factory (if not already done)
|
|
|
|
|
CalendarTypeFactory.initialize();
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setupEventListeners(): void {
|
|
|
|
|
this.eventBus.on(EventTypes.EVENTS_LOADED, (event: Event) => {
|
|
|
|
|
const customEvent = event as CustomEvent;
|
|
|
|
|
const { events } = customEvent.detail;
|
2025-08-02 00:28:45 +02:00
|
|
|
console.log('EventRenderer: Received EVENTS_LOADED with', events.length, 'events');
|
2025-07-25 23:31:25 +02:00
|
|
|
// Store events but don't render yet - wait for grid to be ready
|
|
|
|
|
this.pendingEvents = events;
|
|
|
|
|
this.tryRenderEvents();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.eventBus.on(EventTypes.GRID_RENDERED, () => {
|
|
|
|
|
// Grid is ready, now we can render events
|
|
|
|
|
this.tryRenderEvents();
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.eventBus.on(EventTypes.VIEW_RENDERED, () => {
|
|
|
|
|
// Clear existing events when view changes
|
|
|
|
|
this.clearEvents();
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Handle calendar type changes
|
|
|
|
|
this.eventBus.on(EventTypes.CALENDAR_TYPE_CHANGED, () => {
|
|
|
|
|
// Re-render events with new strategy
|
|
|
|
|
this.tryRenderEvents();
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-25 23:31:25 +02:00
|
|
|
|
|
|
|
|
private tryRenderEvents(): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
// Only render if we have both events and appropriate columns are ready
|
2025-08-02 00:28:45 +02:00
|
|
|
console.log('EventRenderer: tryRenderEvents called, pending events:', this.pendingEvents.length);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-07-25 23:31:25 +02:00
|
|
|
if (this.pendingEvents.length > 0) {
|
2025-08-07 00:15:44 +02:00
|
|
|
const calendarType = calendarConfig.getCalendarType();
|
|
|
|
|
let columnsSelector = calendarType === 'resource' ? 'swp-resource-column' : 'swp-day-column';
|
|
|
|
|
const columns = document.querySelectorAll(columnsSelector);
|
|
|
|
|
|
|
|
|
|
console.log(`EventRenderer: Found ${columns.length} ${columnsSelector} elements for ${calendarType} calendar`);
|
|
|
|
|
|
|
|
|
|
if (columns.length > 0) {
|
2025-07-25 23:31:25 +02:00
|
|
|
this.renderEvents(this.pendingEvents);
|
|
|
|
|
this.pendingEvents = []; // Clear pending events after rendering
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
private renderEvents(events: CalendarEvent[]): void {
|
2025-08-02 23:59:52 +02:00
|
|
|
console.log('EventRenderer: renderEvents called with', events.length, 'events');
|
2025-08-05 00:41:59 +02:00
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Get the appropriate event renderer strategy
|
|
|
|
|
const calendarType = calendarConfig.getCalendarType();
|
|
|
|
|
const eventRenderer = CalendarTypeFactory.getEventRenderer(calendarType);
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
console.log(`EventRenderer: Using ${calendarType} event renderer strategy`);
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Use strategy to render events
|
|
|
|
|
eventRenderer.renderEvents(events, calendarConfig);
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
// Emit event rendered
|
|
|
|
|
this.eventBus.emit(EventTypes.EVENT_RENDERED, {
|
|
|
|
|
count: events.filter(e => !e.allDay).length
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private clearEvents(): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
const calendarType = calendarConfig.getCalendarType();
|
|
|
|
|
const eventRenderer = CalendarTypeFactory.getEventRenderer(calendarType);
|
|
|
|
|
eventRenderer.clearEvents();
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public refresh(): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
this.tryRenderEvents();
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy(): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
this.pendingEvents = [];
|
2025-07-24 22:17:38 +02:00
|
|
|
this.clearEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|