Refactors event system to use CoreEvents
Migrates the application to use a new CoreEvents system. This change removes the legacy EventTypes constant file and updates all managers, renderers, and core components to use the CoreEvents constant file for event emission and subscription. This improves code maintainability and promotes a consistent eventing strategy across the application. Adds validation to EventBus emit and extractCategory functions.
This commit is contained in:
parent
414ef1caaf
commit
4b4dbdc0d6
11 changed files with 76 additions and 228 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { EventBus } from '../core/EventBus.js';
|
||||
import { EventTypes } from '../constants/EventTypes.js';
|
||||
import { CoreEvents } from '../constants/CoreEvents.js';
|
||||
import { CalendarConfig } from '../core/CalendarConfig.js';
|
||||
import { CalendarEvent, CalendarView, IEventBus } from '../types/CalendarTypes.js';
|
||||
import { EventManager } from './EventManager.js';
|
||||
|
|
@ -87,7 +87,7 @@ export class CalendarManager {
|
|||
console.log('✅ CalendarManager: Simple initialization complete');
|
||||
|
||||
// Emit initialization complete event
|
||||
this.eventBus.emit(EventTypes.CALENDAR_INITIALIZED, {
|
||||
this.eventBus.emit(CoreEvents.INITIALIZED, {
|
||||
currentDate: this.currentDate,
|
||||
currentView: this.currentView
|
||||
});
|
||||
|
|
@ -112,7 +112,7 @@ export class CalendarManager {
|
|||
console.log(`Changing view from ${previousView} to ${view}`);
|
||||
|
||||
// Emit view change event
|
||||
this.eventBus.emit(EventTypes.VIEW_CHANGED, {
|
||||
this.eventBus.emit(CoreEvents.VIEW_CHANGED, {
|
||||
previousView,
|
||||
currentView: view,
|
||||
date: this.currentDate
|
||||
|
|
@ -125,13 +125,23 @@ export class CalendarManager {
|
|||
* Sæt aktuel dato
|
||||
*/
|
||||
public setCurrentDate(date: Date): void {
|
||||
// Validate input date
|
||||
if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
|
||||
console.error('CalendarManager.setCurrentDate: Invalid date provided', date);
|
||||
return;
|
||||
}
|
||||
|
||||
const previousDate = this.currentDate;
|
||||
this.currentDate = new Date(date);
|
||||
|
||||
console.log(`Changing date from ${previousDate.toISOString()} to ${date.toISOString()}`);
|
||||
// Validate that both dates are valid before logging
|
||||
const prevDateStr = previousDate && !isNaN(previousDate.getTime()) ? previousDate.toISOString() : 'Invalid Date';
|
||||
const newDateStr = this.currentDate.toISOString();
|
||||
|
||||
console.log(`Changing date from ${prevDateStr} to ${newDateStr}`);
|
||||
|
||||
// Emit date change event
|
||||
this.eventBus.emit(EventTypes.DATE_CHANGED, {
|
||||
this.eventBus.emit(CoreEvents.DATE_CHANGED, {
|
||||
previousDate,
|
||||
currentDate: this.currentDate,
|
||||
view: this.currentView
|
||||
|
|
@ -209,7 +219,7 @@ export class CalendarManager {
|
|||
public refresh(): void {
|
||||
console.log('Refreshing calendar...');
|
||||
|
||||
this.eventBus.emit(EventTypes.CALENDAR_REFRESH_REQUESTED, {
|
||||
this.eventBus.emit(CoreEvents.REFRESH_REQUESTED, {
|
||||
view: this.currentView,
|
||||
date: this.currentDate
|
||||
});
|
||||
|
|
@ -224,7 +234,7 @@ export class CalendarManager {
|
|||
this.currentView = 'week';
|
||||
this.currentDate = new Date();
|
||||
|
||||
this.eventBus.emit(EventTypes.CALENDAR_RESET, {
|
||||
this.eventBus.emit(CoreEvents.REFRESH_REQUESTED, {
|
||||
view: this.currentView,
|
||||
date: this.currentDate
|
||||
});
|
||||
|
|
@ -234,41 +244,8 @@ export class CalendarManager {
|
|||
* Setup event listeners for at håndtere events fra andre managers
|
||||
*/
|
||||
private setupEventListeners(): void {
|
||||
// Lyt efter navigation events
|
||||
this.eventBus.on(EventTypes.NAVIGATE_TO_DATE, (event) => {
|
||||
const customEvent = event as CustomEvent;
|
||||
const { date } = customEvent.detail;
|
||||
this.setCurrentDate(new Date(date));
|
||||
});
|
||||
|
||||
// Lyt efter view change requests
|
||||
this.eventBus.on(EventTypes.VIEW_CHANGE_REQUESTED, (event) => {
|
||||
const customEvent = event as CustomEvent;
|
||||
const { view } = customEvent.detail;
|
||||
this.setView(view);
|
||||
});
|
||||
|
||||
// Lyt efter today navigation
|
||||
this.eventBus.on(EventTypes.NAVIGATE_TO_TODAY, () => {
|
||||
this.goToToday();
|
||||
});
|
||||
|
||||
// Lyt efter next/previous navigation
|
||||
this.eventBus.on(EventTypes.NAVIGATE_NEXT, () => {
|
||||
this.goToNext();
|
||||
});
|
||||
|
||||
this.eventBus.on(EventTypes.NAVIGATE_PREVIOUS, () => {
|
||||
this.goToPrevious();
|
||||
});
|
||||
|
||||
// Lyt efter refresh requests
|
||||
this.eventBus.on(EventTypes.REFRESH_REQUESTED, () => {
|
||||
this.refresh();
|
||||
});
|
||||
|
||||
// Lyt efter workweek changes
|
||||
this.eventBus.on(EventTypes.WORKWEEK_CHANGED, (event: Event) => {
|
||||
// Listen for workweek changes only
|
||||
this.eventBus.on(CoreEvents.WORKWEEK_CHANGED, (event: Event) => {
|
||||
const customEvent = event as CustomEvent;
|
||||
console.log('CalendarManager: Workweek changed to', customEvent.detail.workWeekId);
|
||||
this.handleWorkweekChange();
|
||||
|
|
@ -276,16 +253,6 @@ export class CalendarManager {
|
|||
// Also update week info display since workweek affects date range display
|
||||
this.updateWeekInfoForWorkweekChange();
|
||||
});
|
||||
|
||||
// Lyt efter reset requests
|
||||
this.eventBus.on(EventTypes.RESET_REQUESTED, () => {
|
||||
this.reset();
|
||||
});
|
||||
|
||||
// Update week info when grid is rendered with actual column dates
|
||||
this.eventBus.on(EventTypes.GRID_RENDERED, () => {
|
||||
this.updateWeekInfoFromRenderedColumns();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -487,7 +454,7 @@ export class CalendarManager {
|
|||
});
|
||||
|
||||
// Emit week info update
|
||||
this.eventBus.emit(EventTypes.WEEK_INFO_UPDATED, {
|
||||
this.eventBus.emit(CoreEvents.WEEK_CHANGED, {
|
||||
weekNumber,
|
||||
dateRange,
|
||||
weekStart: firstDate,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue