2025-07-24 22:17:38 +02:00
|
|
|
import { EventBus } from '../core/EventBus';
|
2025-08-16 00:51:12 +02:00
|
|
|
import { IEventBus, CalendarEvent, RenderContext } from '../types/CalendarTypes';
|
2025-07-24 22:17:38 +02:00
|
|
|
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 { calendarConfig } from '../core/CalendarConfig';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { CalendarTypeFactory } from '../factories/CalendarTypeFactory';
|
2025-08-16 00:51:12 +02:00
|
|
|
import { EventManager } from './EventManager';
|
|
|
|
|
import { EventRendererStrategy } from '../renderers/EventRenderer';
|
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-16 00:51:12 +02:00
|
|
|
private eventManager: EventManager;
|
|
|
|
|
private strategy: EventRendererStrategy;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
constructor(eventBus: IEventBus, eventManager: EventManager) {
|
2025-07-24 22:17:38 +02:00
|
|
|
this.eventBus = eventBus;
|
2025-08-16 00:51:12 +02:00
|
|
|
this.eventManager = eventManager;
|
|
|
|
|
|
|
|
|
|
// Cache strategy at initialization
|
|
|
|
|
const calendarType = calendarConfig.getCalendarMode();
|
|
|
|
|
this.strategy = CalendarTypeFactory.getEventRenderer(calendarType);
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
this.setupEventListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 01:16:04 +02:00
|
|
|
/**
|
2025-08-16 00:51:12 +02:00
|
|
|
* Render events in a specific container for a given period
|
2025-08-09 01:16:04 +02:00
|
|
|
*/
|
2025-08-16 00:51:12 +02:00
|
|
|
public renderEvents(context: RenderContext): void {
|
|
|
|
|
console.log('EventRenderer: Rendering events for period', {
|
|
|
|
|
startDate: context.startDate,
|
|
|
|
|
endDate: context.endDate,
|
|
|
|
|
container: context.container
|
2025-08-09 01:16:04 +02:00
|
|
|
});
|
2025-08-05 00:41:59 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Get events from EventManager for the period
|
|
|
|
|
const events = this.eventManager.getEventsForPeriod(
|
|
|
|
|
context.startDate,
|
|
|
|
|
context.endDate
|
|
|
|
|
);
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
console.log(`EventRenderer: Found ${events.length} events for period`);
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
if (events.length === 0) {
|
|
|
|
|
console.log('EventRenderer: No events to render for this period');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-09 01:16:04 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Use cached strategy to render events in the specific container
|
|
|
|
|
this.strategy.renderEvents(events, context.container, calendarConfig);
|
2025-08-09 01:16:04 +02:00
|
|
|
|
|
|
|
|
console.log(`EventRenderer: Successfully rendered ${events.length} events`);
|
|
|
|
|
}
|
2025-08-02 23:59:52 +02:00
|
|
|
|
2025-08-09 01:16:04 +02:00
|
|
|
private setupEventListeners(): void {
|
2025-08-16 00:51:12 +02:00
|
|
|
// Event-driven rendering: React to grid and container events
|
|
|
|
|
this.eventBus.on(EventTypes.GRID_RENDERED, (event: Event) => {
|
|
|
|
|
console.log('EventRenderer: Received GRID_RENDERED event');
|
|
|
|
|
this.handleGridRendered(event as CustomEvent);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.eventBus.on(EventTypes.CONTAINER_READY_FOR_EVENTS, (event: Event) => {
|
|
|
|
|
console.log('EventRenderer: Received CONTAINER_READY_FOR_EVENTS event');
|
|
|
|
|
this.handleContainerReady(event as CustomEvent);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.eventBus.on(EventTypes.VIEW_CHANGED, (event: Event) => {
|
|
|
|
|
console.log('EventRenderer: Received VIEW_CHANGED event');
|
|
|
|
|
this.handleViewChanged(event as CustomEvent);
|
2025-08-09 01:16:04 +02:00
|
|
|
});
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Handle calendar type changes - update cached strategy
|
2025-08-09 01:16:04 +02:00
|
|
|
this.eventBus.on(EventTypes.CALENDAR_TYPE_CHANGED, () => {
|
2025-08-16 00:51:12 +02:00
|
|
|
const calendarType = calendarConfig.getCalendarMode();
|
|
|
|
|
this.strategy = CalendarTypeFactory.getEventRenderer(calendarType);
|
|
|
|
|
console.log(`EventRenderer: Updated strategy to ${calendarType}`);
|
2025-07-24 22:17:38 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 01:16:04 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
/**
|
|
|
|
|
* Handle GRID_RENDERED event - render events in the current grid
|
|
|
|
|
*/
|
|
|
|
|
private handleGridRendered(event: CustomEvent): void {
|
|
|
|
|
const { container, startDate, endDate } = event.detail;
|
|
|
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
|
console.error('EventRenderer: No container in GRID_RENDERED event', event.detail);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use period from event or fallback to calculated period
|
|
|
|
|
const periodStart = startDate;
|
|
|
|
|
const periodEnd = endDate;
|
|
|
|
|
|
|
|
|
|
this.renderEvents({
|
|
|
|
|
container: container,
|
|
|
|
|
startDate: periodStart,
|
|
|
|
|
endDate: periodEnd
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle CONTAINER_READY_FOR_EVENTS event - render events in pre-rendered container
|
|
|
|
|
*/
|
|
|
|
|
private handleContainerReady(event: CustomEvent): void {
|
|
|
|
|
const { container, startDate, endDate } = event.detail;
|
|
|
|
|
|
|
|
|
|
if (!container || !startDate || !endDate) {
|
|
|
|
|
console.error('EventRenderer: Invalid CONTAINER_READY_FOR_EVENTS event data', event.detail);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.renderEvents({
|
|
|
|
|
container: container,
|
|
|
|
|
startDate: new Date(startDate),
|
|
|
|
|
endDate: new Date(endDate)
|
|
|
|
|
});
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
/**
|
|
|
|
|
* Handle VIEW_CHANGED event - clear and re-render for new view
|
|
|
|
|
*/
|
|
|
|
|
private handleViewChanged(event: CustomEvent): void {
|
|
|
|
|
// Clear all existing events since view structure may have changed
|
2025-08-09 01:16:04 +02:00
|
|
|
this.clearEvents();
|
2025-08-16 00:51:12 +02:00
|
|
|
|
|
|
|
|
// New rendering will be triggered by subsequent GRID_RENDERED event
|
|
|
|
|
console.log('EventRenderer: Cleared events for view change, waiting for GRID_RENDERED');
|
|
|
|
|
}
|
|
|
|
|
private clearEvents(container?: HTMLElement): void {
|
|
|
|
|
console.log(`EventRenderer: Clearing events`, container ? 'in container' : 'globally');
|
|
|
|
|
this.strategy.clearEvents(container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public refresh(container?: HTMLElement): void {
|
|
|
|
|
// Clear events in specific container or globally
|
|
|
|
|
this.clearEvents(container);
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
this.clearEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|