2025-11-13 20:12:05 +01:00
|
|
|
import { IEventBus, CalendarView } from '../types/CalendarTypes';
|
2025-11-07 23:23:19 +01:00
|
|
|
import { CoreEvents } from '../constants/CoreEvents';
|
2025-11-13 20:12:05 +01:00
|
|
|
import { DateService } from '../utils/DateService';
|
|
|
|
|
import { Configuration } from '../configurations/CalendarConfig';
|
2025-11-07 23:23:19 +01:00
|
|
|
|
|
|
|
|
/**
|
2025-11-13 20:12:05 +01:00
|
|
|
* NavigationButtons - Manages navigation button UI and navigation logic
|
2025-11-07 23:23:19 +01:00
|
|
|
*
|
|
|
|
|
* RESPONSIBILITY:
|
|
|
|
|
* ===============
|
2025-11-13 20:12:05 +01:00
|
|
|
* This manager owns all logic related to the <swp-nav-group> UI element
|
|
|
|
|
* and performs the actual navigation calculations.
|
2025-11-07 23:23:19 +01:00
|
|
|
*
|
|
|
|
|
* RESPONSIBILITIES:
|
|
|
|
|
* - Handles button clicks on swp-nav-button elements
|
|
|
|
|
* - Validates navigation actions (prev, next, today)
|
2025-11-13 20:12:05 +01:00
|
|
|
* - Calculates next/previous dates based on current view
|
|
|
|
|
* - Emits NAVIGATION_COMPLETED events with new date
|
2025-11-07 23:23:19 +01:00
|
|
|
* - Manages button UI listeners
|
|
|
|
|
*
|
|
|
|
|
* EVENT FLOW:
|
|
|
|
|
* ===========
|
2025-11-13 20:12:05 +01:00
|
|
|
* User clicks button → calculateNewDate() → emit NAVIGATION_COMPLETED → GridManager re-renders
|
2025-11-07 23:23:19 +01:00
|
|
|
*/
|
2025-11-08 14:30:18 +01:00
|
|
|
export class NavigationButtons {
|
2025-11-07 23:23:19 +01:00
|
|
|
private eventBus: IEventBus;
|
|
|
|
|
private buttonListeners: Map<Element, EventListener> = new Map();
|
2025-11-13 20:12:05 +01:00
|
|
|
private dateService: DateService;
|
|
|
|
|
private config: Configuration;
|
|
|
|
|
private currentDate: Date = new Date();
|
|
|
|
|
private currentView: CalendarView = 'week';
|
2025-11-07 23:23:19 +01:00
|
|
|
|
2025-11-13 20:12:05 +01:00
|
|
|
constructor(
|
|
|
|
|
eventBus: IEventBus,
|
|
|
|
|
dateService: DateService,
|
|
|
|
|
config: Configuration
|
|
|
|
|
) {
|
2025-11-07 23:23:19 +01:00
|
|
|
this.eventBus = eventBus;
|
2025-11-13 20:12:05 +01:00
|
|
|
this.dateService = dateService;
|
|
|
|
|
this.config = config;
|
2025-11-07 23:23:19 +01:00
|
|
|
this.setupButtonListeners();
|
2025-11-13 20:12:05 +01:00
|
|
|
this.subscribeToEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subscribe to events
|
|
|
|
|
*/
|
|
|
|
|
private subscribeToEvents(): void {
|
|
|
|
|
// Listen for view changes
|
|
|
|
|
this.eventBus.on(CoreEvents.VIEW_CHANGED, (e: Event) => {
|
|
|
|
|
const detail = (e as CustomEvent).detail;
|
|
|
|
|
this.currentView = detail.currentView;
|
|
|
|
|
});
|
2025-11-07 23:23:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setup click listeners on all navigation buttons
|
|
|
|
|
*/
|
|
|
|
|
private setupButtonListeners(): void {
|
|
|
|
|
const buttons = document.querySelectorAll('swp-nav-button[data-action]');
|
|
|
|
|
|
|
|
|
|
buttons.forEach(button => {
|
|
|
|
|
const clickHandler = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const action = button.getAttribute('data-action');
|
|
|
|
|
if (action && this.isValidAction(action)) {
|
|
|
|
|
this.handleNavigation(action);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
button.addEventListener('click', clickHandler);
|
|
|
|
|
this.buttonListeners.set(button, clickHandler);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle navigation action
|
|
|
|
|
*/
|
|
|
|
|
private handleNavigation(action: string): void {
|
2025-11-13 20:12:05 +01:00
|
|
|
switch (action) {
|
|
|
|
|
case 'prev':
|
|
|
|
|
this.navigatePrevious();
|
|
|
|
|
break;
|
|
|
|
|
case 'next':
|
|
|
|
|
this.navigateNext();
|
|
|
|
|
break;
|
|
|
|
|
case 'today':
|
|
|
|
|
this.navigateToday();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Navigate in specified direction
|
|
|
|
|
*/
|
|
|
|
|
private navigate(direction: 'next' | 'prev'): void {
|
|
|
|
|
const offset = direction === 'next' ? 1 : -1;
|
|
|
|
|
let newDate: Date;
|
|
|
|
|
|
|
|
|
|
switch (this.currentView) {
|
|
|
|
|
case 'week':
|
|
|
|
|
newDate = this.dateService.addWeeks(this.currentDate, offset);
|
|
|
|
|
break;
|
|
|
|
|
case 'month':
|
|
|
|
|
newDate = this.dateService.addMonths(this.currentDate, offset);
|
|
|
|
|
break;
|
|
|
|
|
case 'day':
|
|
|
|
|
newDate = this.dateService.addDays(this.currentDate, offset);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
newDate = this.dateService.addWeeks(this.currentDate, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentDate = newDate;
|
|
|
|
|
|
|
|
|
|
this.eventBus.emit(CoreEvents.NAVIGATION_COMPLETED, {
|
|
|
|
|
direction: direction === 'next' ? 'next' : 'previous',
|
|
|
|
|
newDate: newDate,
|
|
|
|
|
periodLabel: this.getCurrentPeriodLabel()
|
2025-11-07 23:23:19 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 20:12:05 +01:00
|
|
|
/**
|
|
|
|
|
* Navigate to next period
|
|
|
|
|
*/
|
|
|
|
|
private navigateNext(): void {
|
|
|
|
|
this.navigate('next');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Navigate to previous period
|
|
|
|
|
*/
|
|
|
|
|
private navigatePrevious(): void {
|
|
|
|
|
this.navigate('prev');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Navigate to today
|
|
|
|
|
*/
|
|
|
|
|
private navigateToday(): void {
|
|
|
|
|
this.currentDate = new Date();
|
|
|
|
|
|
|
|
|
|
this.eventBus.emit(CoreEvents.NAVIGATION_COMPLETED, {
|
|
|
|
|
direction: 'today',
|
|
|
|
|
newDate: this.currentDate,
|
|
|
|
|
periodLabel: this.getCurrentPeriodLabel()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 23:23:19 +01:00
|
|
|
/**
|
|
|
|
|
* Validate if string is a valid navigation action
|
|
|
|
|
*/
|
|
|
|
|
private isValidAction(action: string): boolean {
|
|
|
|
|
return ['prev', 'next', 'today'].includes(action);
|
|
|
|
|
}
|
|
|
|
|
}
|