Refactors navigation event handling and payload

Updates navigation button event emission to use a more consistent payload type

Simplifies navigation logic by centralizing date navigation in NavigationManager
Removes redundant method implementations and standardizes event handling
This commit is contained in:
Janus C. H. Knudsen 2025-11-13 20:41:33 +01:00
parent a666a632bd
commit 5075b71eb2
3 changed files with 42 additions and 96 deletions

View file

@ -2,6 +2,7 @@ import { IEventBus, CalendarView } from '../types/CalendarTypes';
import { CoreEvents } from '../constants/CoreEvents';
import { DateService } from '../utils/DateService';
import { Configuration } from '../configurations/CalendarConfig';
import { INavButtonClickedEventPayload } from '../types/EventTypes';
/**
* NavigationButtons - Manages navigation button UI and navigation logic
@ -93,7 +94,7 @@ export class NavigationButtons {
/**
* Navigate in specified direction
*/
private navigate(direction: 'next' | 'prev'): void {
private navigate(direction: 'next' | 'previous'): void {
const offset = direction === 'next' ? 1 : -1;
let newDate: Date;
@ -113,11 +114,12 @@ export class NavigationButtons {
this.currentDate = newDate;
this.eventBus.emit(CoreEvents.NAVIGATION_COMPLETED, {
direction: direction === 'next' ? 'next' : 'previous',
newDate: newDate,
periodLabel: this.getCurrentPeriodLabel()
});
const payload: INavButtonClickedEventPayload = {
direction: direction,
newDate: newDate
};
this.eventBus.emit(CoreEvents.NAV_BUTTON_CLICKED, payload);
}
/**
@ -131,7 +133,7 @@ export class NavigationButtons {
* Navigate to previous period
*/
private navigatePrevious(): void {
this.navigate('prev');
this.navigate('previous');
}
/**
@ -140,46 +142,12 @@ export class NavigationButtons {
private navigateToday(): void {
this.currentDate = new Date();
this.eventBus.emit(CoreEvents.NAVIGATION_COMPLETED, {
const payload: INavButtonClickedEventPayload = {
direction: 'today',
newDate: this.currentDate,
periodLabel: this.getCurrentPeriodLabel()
});
}
newDate: this.currentDate
};
/**
* Get ISO week start (Monday)
*/
private getISOWeekStart(date: Date): Date {
const weekBounds = this.dateService.getWeekBounds(date);
return this.dateService.startOfDay(weekBounds.start);
}
/**
* Get week end (Sunday)
*/
private getWeekEnd(date: Date): Date {
const weekBounds = this.dateService.getWeekBounds(date);
return this.dateService.endOfDay(weekBounds.end);
}
/**
* Get current period label
*/
private getCurrentPeriodLabel(): string {
switch (this.currentView) {
case 'week':
case 'day':
const weekStart = this.getISOWeekStart(this.currentDate);
const weekEnd = this.getWeekEnd(this.currentDate);
return this.dateService.formatDateRange(weekStart, weekEnd);
case 'month':
return this.dateService.formatMonthYear(this.currentDate);
default:
const defaultWeekStart = this.getISOWeekStart(this.currentDate);
const defaultWeekEnd = this.getWeekEnd(this.currentDate);
return this.dateService.formatDateRange(defaultWeekStart, defaultWeekEnd);
}
this.eventBus.emit(CoreEvents.NAV_BUTTON_CLICKED, payload);
}
/**