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

@ -4,6 +4,7 @@ import { DateService } from '../utils/DateService';
import { CoreEvents } from '../constants/CoreEvents';
import { WeekInfoRenderer } from '../renderers/WeekInfoRenderer';
import { GridRenderer } from '../renderers/GridRenderer';
import { INavButtonClickedEventPayload } from '../types/EventTypes';
export class NavigationManager {
private eventBus: IEventBus;
@ -57,21 +58,12 @@ export class NavigationManager {
this.weekInfoRenderer.applyFilterToPreRenderedGrids(detail);
});
// Listen for navigation button clicks from NavigationButtonsManager
// Listen for navigation button clicks from NavigationButtons
this.eventBus.on(CoreEvents.NAV_BUTTON_CLICKED, (event: Event) => {
const { action } = (event as CustomEvent).detail;
const { direction, newDate } = (event as CustomEvent<INavButtonClickedEventPayload>).detail;
switch (action) {
case 'prev':
this.navigateToPreviousWeek();
break;
case 'next':
this.navigateToNextWeek();
break;
case 'today':
this.navigateToToday();
break;
}
// Navigate to the new date with animation
this.navigateToDate(newDate, direction);
});
// Listen for external navigation requests
@ -145,52 +137,32 @@ export class NavigationManager {
}
}
private navigateToPreviousWeek(): void {
this.targetWeek = this.dateService.addWeeks(this.targetWeek, -1);
const weekToShow = new Date(this.targetWeek);
this.animationQueue++;
this.animateTransition('prev', weekToShow);
}
private navigateToNextWeek(): void {
this.targetWeek = this.dateService.addWeeks(this.targetWeek, 1);
const weekToShow = new Date(this.targetWeek);
this.animationQueue++;
this.animateTransition('next', weekToShow);
}
private navigateToToday(): void {
const today = new Date();
const todayWeekStart = this.getISOWeekStart(today);
// Reset to today
this.targetWeek = new Date(todayWeekStart);
const currentTime = this.currentWeek.getTime();
const targetTime = todayWeekStart.getTime();
if (currentTime < targetTime) {
this.animationQueue++;
this.animateTransition('next', todayWeekStart);
} else if (currentTime > targetTime) {
this.animationQueue++;
this.animateTransition('prev', todayWeekStart);
}
}
private navigateToDate(date: Date): void {
private navigateToDate(date: Date, direction?: 'next' | 'previous' | 'today'): void {
const weekStart = this.getISOWeekStart(date);
this.targetWeek = new Date(weekStart);
const currentTime = this.currentWeek.getTime();
const targetTime = weekStart.getTime();
if (currentTime < targetTime) {
// Use provided direction or calculate based on time comparison
let animationDirection: 'next' | 'prev';
if (direction === 'next') {
animationDirection = 'next';
} else if (direction === 'previous') {
animationDirection = 'prev';
} else if (direction === 'today') {
// For "today", determine direction based on current position
animationDirection = currentTime < targetTime ? 'next' : 'prev';
} else {
// Fallback: calculate direction
animationDirection = currentTime < targetTime ? 'next' : 'prev';
}
if (currentTime !== targetTime) {
this.animationQueue++;
this.animateTransition('next', weekStart);
} else if (currentTime > targetTime) {
this.animationQueue++;
this.animateTransition('prev', weekStart);
this.animateTransition(animationDirection, weekStart);
}
}