2025-07-24 22:17:38 +02:00
|
|
|
import { EventBus } from '../core/EventBus';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { IEventBus, CalendarEvent, ResourceCalendarData } from '../types/CalendarTypes';
|
2025-08-20 20:22:51 +02:00
|
|
|
import { CoreEvents } from '../constants/CoreEvents';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { calendarConfig } from '../core/CalendarConfig';
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* EventManager - Administrerer event lifecycle og CRUD operationer
|
|
|
|
|
* Håndterer mock data og event synchronization
|
|
|
|
|
*/
|
|
|
|
|
export class EventManager {
|
|
|
|
|
private eventBus: IEventBus;
|
|
|
|
|
private events: CalendarEvent[] = [];
|
|
|
|
|
|
|
|
|
|
constructor(eventBus: IEventBus) {
|
2025-08-07 00:15:44 +02:00
|
|
|
console.log('EventManager: Constructor called');
|
2025-07-24 22:17:38 +02:00
|
|
|
this.eventBus = eventBus;
|
|
|
|
|
this.setupEventListeners();
|
2025-08-09 00:31:44 +02:00
|
|
|
console.log('EventManager: Waiting for CALENDAR_INITIALIZED before loading data');
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setupEventListeners(): void {
|
2025-08-12 00:07:39 +02:00
|
|
|
// NOTE: Removed POC event listener to prevent interference with production code
|
|
|
|
|
// POC sliding animation should not trigger separate event rendering
|
2025-08-20 20:22:51 +02:00
|
|
|
// this.eventBus.on(CoreEvents.WEEK_CONTENT_RENDERED, ...);
|
2025-08-09 01:16:04 +02:00
|
|
|
}
|
2025-08-09 00:31:44 +02:00
|
|
|
|
2025-08-09 01:16:04 +02:00
|
|
|
/**
|
|
|
|
|
* Public method to load data - called directly by CalendarManager
|
|
|
|
|
*/
|
|
|
|
|
public async loadData(): Promise<void> {
|
|
|
|
|
console.log('EventManager: Loading data via direct call');
|
|
|
|
|
await this.loadMockData();
|
|
|
|
|
console.log(`EventManager: Data loaded successfully - ${this.events.length} events`);
|
|
|
|
|
|
|
|
|
|
// Debug: Log first few events
|
|
|
|
|
if (this.events.length > 0) {
|
|
|
|
|
console.log('EventManager: First event:', {
|
|
|
|
|
title: this.events[0].title,
|
|
|
|
|
start: this.events[0].start,
|
|
|
|
|
end: this.events[0].end
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
private async loadMockData(): Promise<void> {
|
|
|
|
|
try {
|
2025-08-09 00:31:44 +02:00
|
|
|
const calendarType = calendarConfig.getCalendarMode();
|
2025-08-07 00:15:44 +02:00
|
|
|
let jsonFile: string;
|
|
|
|
|
|
|
|
|
|
console.log(`EventManager: Calendar type detected: '${calendarType}'`);
|
|
|
|
|
|
|
|
|
|
if (calendarType === 'resource') {
|
|
|
|
|
jsonFile = '/src/data/mock-resource-events.json';
|
|
|
|
|
} else {
|
|
|
|
|
jsonFile = '/src/data/mock-events.json';
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
console.log(`EventManager: Loading ${calendarType} calendar data from ${jsonFile}`);
|
|
|
|
|
|
|
|
|
|
const response = await fetch(jsonFile);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to load mock events: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
const data = await response.json();
|
|
|
|
|
console.log(`EventManager: Loaded data for ${calendarType} calendar`);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-10 23:06:03 +02:00
|
|
|
// Store raw data for GridManager
|
|
|
|
|
this.rawData = data;
|
2025-08-09 00:31:44 +02:00
|
|
|
|
|
|
|
|
// Process data for internal use
|
|
|
|
|
this.processCalendarData(calendarType, data);
|
2025-08-07 00:15:44 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('EventManager: Failed to load mock events:', error);
|
|
|
|
|
this.events = []; // Fallback to empty array
|
|
|
|
|
}
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
private processCalendarData(calendarType: string, data: any): void {
|
|
|
|
|
if (calendarType === 'resource') {
|
|
|
|
|
const resourceData = data as ResourceCalendarData;
|
|
|
|
|
this.events = resourceData.resources.flatMap(resource =>
|
|
|
|
|
resource.events.map(event => ({
|
|
|
|
|
...event,
|
|
|
|
|
resourceName: resource.name,
|
|
|
|
|
resourceDisplayName: resource.displayName,
|
|
|
|
|
resourceEmployeeId: resource.employeeId
|
|
|
|
|
}))
|
|
|
|
|
);
|
|
|
|
|
console.log(`EventManager: Processed ${this.events.length} events from ${resourceData.resources.length} resources`);
|
|
|
|
|
} else {
|
|
|
|
|
this.events = data as CalendarEvent[];
|
|
|
|
|
console.log(`EventManager: Processed ${this.events.length} date events`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-09 00:31:44 +02:00
|
|
|
private syncEvents(): void {
|
2025-08-20 19:42:13 +02:00
|
|
|
// Events are synced during initialization
|
2025-08-09 00:31:44 +02:00
|
|
|
// This method maintained for internal state management only
|
|
|
|
|
console.log(`EventManager: Internal sync - ${this.events.length} events in memory`);
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getEvents(): CalendarEvent[] {
|
|
|
|
|
return [...this.events];
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 23:06:03 +02:00
|
|
|
/**
|
|
|
|
|
* Get raw resource data for resource calendar mode
|
|
|
|
|
*/
|
|
|
|
|
public getResourceData(): any {
|
|
|
|
|
return this.rawData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private rawData: any = null;
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
public getEventById(id: string): CalendarEvent | undefined {
|
|
|
|
|
return this.events.find(event => event.id === id);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
/**
|
|
|
|
|
* Get events for a specific time period
|
|
|
|
|
*/
|
|
|
|
|
public getEventsForPeriod(startDate: Date, endDate: Date): CalendarEvent[] {
|
|
|
|
|
return this.events.filter(event => {
|
|
|
|
|
const eventStart = new Date(event.start);
|
|
|
|
|
const eventEnd = new Date(event.end);
|
|
|
|
|
|
|
|
|
|
// Event overlaps period if it starts before period ends AND ends after period starts
|
|
|
|
|
return eventStart <= endDate && eventEnd >= startDate;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
public addEvent(event: Omit<CalendarEvent, 'id'>): CalendarEvent {
|
|
|
|
|
const newEvent: CalendarEvent = {
|
|
|
|
|
...event,
|
|
|
|
|
id: Date.now().toString()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.events.push(newEvent);
|
|
|
|
|
this.syncEvents();
|
|
|
|
|
|
2025-08-20 20:22:51 +02:00
|
|
|
this.eventBus.emit(CoreEvents.EVENT_CREATED, {
|
2025-07-24 22:17:38 +02:00
|
|
|
event: newEvent
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return newEvent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateEvent(id: string, updates: Partial<CalendarEvent>): CalendarEvent | null {
|
|
|
|
|
const eventIndex = this.events.findIndex(event => event.id === id);
|
|
|
|
|
if (eventIndex === -1) return null;
|
|
|
|
|
|
|
|
|
|
const updatedEvent = { ...this.events[eventIndex], ...updates };
|
|
|
|
|
this.events[eventIndex] = updatedEvent;
|
|
|
|
|
|
|
|
|
|
this.syncEvents();
|
|
|
|
|
|
2025-08-20 20:22:51 +02:00
|
|
|
this.eventBus.emit(CoreEvents.EVENT_UPDATED, {
|
2025-07-24 22:17:38 +02:00
|
|
|
event: updatedEvent
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return updatedEvent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public deleteEvent(id: string): boolean {
|
|
|
|
|
const eventIndex = this.events.findIndex(event => event.id === id);
|
|
|
|
|
if (eventIndex === -1) return false;
|
|
|
|
|
|
|
|
|
|
const deletedEvent = this.events[eventIndex];
|
|
|
|
|
this.events.splice(eventIndex, 1);
|
|
|
|
|
|
|
|
|
|
this.syncEvents();
|
|
|
|
|
|
2025-08-20 20:22:51 +02:00
|
|
|
this.eventBus.emit(CoreEvents.EVENT_DELETED, {
|
2025-07-24 22:17:38 +02:00
|
|
|
event: deletedEvent
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public refresh(): void {
|
|
|
|
|
this.syncEvents();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 00:07:39 +02:00
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
public destroy(): void {
|
|
|
|
|
this.events = [];
|
|
|
|
|
}
|
|
|
|
|
}
|