Calendar/src/managers/EventRenderer.ts

100 lines
3.7 KiB
TypeScript
Raw Normal View History

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-08-07 00:15:44 +02:00
* EventRenderer - Render events i DOM med positionering using Strategy Pattern
* Håndterer event positioning og overlap detection
*/
export class EventRenderer {
private eventBus: IEventBus;
2025-08-07 00:15:44 +02:00
private pendingEvents: CalendarEvent[] = [];
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();
}
private setupEventListeners(): void {
this.eventBus.on(EventTypes.EVENTS_LOADED, (event: Event) => {
const customEvent = event as CustomEvent;
const { events } = customEvent.detail;
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();
});
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
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
}
}
}
private renderEvents(events: CalendarEvent[]): void {
2025-08-02 23:59:52 +02:00
console.log('EventRenderer: renderEvents called with', events.length, 'events');
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
});
}
private clearEvents(): void {
2025-08-07 00:15:44 +02:00
const calendarType = calendarConfig.getCalendarType();
const eventRenderer = CalendarTypeFactory.getEventRenderer(calendarType);
eventRenderer.clearEvents();
}
public refresh(): void {
2025-08-07 00:15:44 +02:00
this.tryRenderEvents();
}
public destroy(): void {
2025-08-07 00:15:44 +02:00
this.pendingEvents = [];
this.clearEvents();
}
}