2025-07-24 22:17:38 +02:00
|
|
|
import { IEventBus } from '../types/CalendarTypes.js';
|
|
|
|
|
import { DateUtils } from '../utils/DateUtils.js';
|
|
|
|
|
import { EventTypes } from '../constants/EventTypes.js';
|
2025-08-17 22:54:00 +02:00
|
|
|
import { NavigationRenderer } from '../renderers/NavigationRenderer.js';
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* NavigationManager handles calendar navigation (prev/next/today buttons)
|
2025-07-25 23:31:25 +02:00
|
|
|
* with simplified CSS Grid approach
|
2025-07-24 22:17:38 +02:00
|
|
|
*/
|
|
|
|
|
export class NavigationManager {
|
|
|
|
|
private eventBus: IEventBus;
|
2025-08-17 22:54:00 +02:00
|
|
|
private navigationRenderer: NavigationRenderer;
|
2025-07-24 22:17:38 +02:00
|
|
|
private currentWeek: Date;
|
|
|
|
|
private targetWeek: Date;
|
|
|
|
|
private animationQueue: number = 0;
|
|
|
|
|
|
|
|
|
|
constructor(eventBus: IEventBus) {
|
2025-08-09 00:31:44 +02:00
|
|
|
console.log('🧭 NavigationManager: Constructor called');
|
2025-07-24 22:17:38 +02:00
|
|
|
this.eventBus = eventBus;
|
2025-08-17 22:54:00 +02:00
|
|
|
this.navigationRenderer = new NavigationRenderer(eventBus);
|
2025-07-24 22:17:38 +02:00
|
|
|
this.currentWeek = DateUtils.getWeekStart(new Date(), 0); // Sunday start like POC
|
|
|
|
|
this.targetWeek = new Date(this.currentWeek);
|
|
|
|
|
this.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private init(): void {
|
|
|
|
|
this.setupEventListeners();
|
2025-08-09 00:31:44 +02:00
|
|
|
// Don't update week info immediately - wait for DOM to be ready
|
|
|
|
|
console.log('NavigationManager: Waiting for CALENDAR_INITIALIZED before updating DOM');
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setupEventListeners(): void {
|
2025-08-09 00:31:44 +02:00
|
|
|
// Initial DOM update when calendar is initialized
|
|
|
|
|
this.eventBus.on(EventTypes.CALENDAR_INITIALIZED, () => {
|
|
|
|
|
console.log('NavigationManager: Received CALENDAR_INITIALIZED, updating week info');
|
|
|
|
|
this.updateWeekInfo();
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-24 22:17:38 +02:00
|
|
|
// Listen for navigation button clicks
|
|
|
|
|
document.addEventListener('click', (e) => {
|
|
|
|
|
const target = e.target as HTMLElement;
|
|
|
|
|
const navButton = target.closest('[data-action]') as HTMLElement;
|
|
|
|
|
|
|
|
|
|
if (!navButton) return;
|
|
|
|
|
|
|
|
|
|
const action = navButton.dataset.action;
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case 'prev':
|
|
|
|
|
this.navigateToPreviousWeek();
|
|
|
|
|
break;
|
|
|
|
|
case 'next':
|
|
|
|
|
this.navigateToNextWeek();
|
|
|
|
|
break;
|
|
|
|
|
case 'today':
|
|
|
|
|
this.navigateToToday();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Listen for external navigation requests
|
|
|
|
|
this.eventBus.on(EventTypes.NAVIGATE_TO_DATE, (event: Event) => {
|
|
|
|
|
const customEvent = event as CustomEvent;
|
|
|
|
|
const targetDate = new Date(customEvent.detail.date);
|
|
|
|
|
this.navigateToDate(targetDate);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private navigateToPreviousWeek(): void {
|
|
|
|
|
this.targetWeek.setDate(this.targetWeek.getDate() - 7);
|
|
|
|
|
const weekToShow = new Date(this.targetWeek);
|
|
|
|
|
this.animationQueue++;
|
|
|
|
|
this.animateTransition('prev', weekToShow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private navigateToNextWeek(): void {
|
|
|
|
|
this.targetWeek.setDate(this.targetWeek.getDate() + 7);
|
|
|
|
|
const weekToShow = new Date(this.targetWeek);
|
|
|
|
|
this.animationQueue++;
|
|
|
|
|
this.animateTransition('next', weekToShow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private navigateToToday(): void {
|
|
|
|
|
const today = new Date();
|
|
|
|
|
const todayWeekStart = DateUtils.getWeekStart(today, 0);
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
const weekStart = DateUtils.getWeekStart(date, 0);
|
|
|
|
|
this.targetWeek = new Date(weekStart);
|
|
|
|
|
|
|
|
|
|
const currentTime = this.currentWeek.getTime();
|
|
|
|
|
const targetTime = weekStart.getTime();
|
|
|
|
|
|
|
|
|
|
if (currentTime < targetTime) {
|
|
|
|
|
this.animationQueue++;
|
|
|
|
|
this.animateTransition('next', weekStart);
|
|
|
|
|
} else if (currentTime > targetTime) {
|
|
|
|
|
this.animationQueue++;
|
|
|
|
|
this.animateTransition('prev', weekStart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 00:07:39 +02:00
|
|
|
/**
|
2025-08-16 00:51:12 +02:00
|
|
|
* Animation transition using pre-rendered containers when available
|
2025-08-12 00:07:39 +02:00
|
|
|
*/
|
2025-07-24 22:17:38 +02:00
|
|
|
private animateTransition(direction: 'prev' | 'next', targetWeek: Date): void {
|
2025-08-12 00:07:39 +02:00
|
|
|
const container = document.querySelector('swp-calendar-container');
|
2025-08-16 00:51:12 +02:00
|
|
|
const currentGrid = container?.querySelector('swp-grid-container:not([data-prerendered])');
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-12 00:07:39 +02:00
|
|
|
if (!container || !currentGrid) {
|
|
|
|
|
console.warn('NavigationManager: Required DOM elements not found');
|
2025-07-24 22:17:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2025-08-12 00:07:39 +02:00
|
|
|
|
|
|
|
|
console.log(`NavigationManager: Starting ${direction} animation to ${targetWeek.toDateString()}`);
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
let newGrid: HTMLElement;
|
2025-08-12 00:07:39 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Always create a fresh container for consistent behavior
|
|
|
|
|
console.log('NavigationManager: Creating new container');
|
2025-08-17 22:54:00 +02:00
|
|
|
newGrid = this.navigationRenderer.renderContainer(container as HTMLElement, targetWeek);
|
2025-08-12 00:07:39 +02:00
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Clear any existing transforms before animation
|
|
|
|
|
newGrid.style.transform = '';
|
|
|
|
|
(currentGrid as HTMLElement).style.transform = '';
|
2025-08-12 00:07:39 +02:00
|
|
|
|
2025-08-13 23:37:23 +02:00
|
|
|
// Animate transition using Web Animations API
|
|
|
|
|
const slideOutAnimation = (currentGrid as HTMLElement).animate([
|
|
|
|
|
{ transform: 'translateX(0)', opacity: '1' },
|
|
|
|
|
{ transform: direction === 'next' ? 'translateX(-100%)' : 'translateX(100%)', opacity: '0.5' }
|
|
|
|
|
], {
|
|
|
|
|
duration: 400,
|
|
|
|
|
easing: 'ease-in-out',
|
|
|
|
|
fill: 'forwards'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const slideInAnimation = newGrid.animate([
|
|
|
|
|
{ transform: direction === 'next' ? 'translateX(100%)' : 'translateX(-100%)' },
|
|
|
|
|
{ transform: 'translateX(0)' }
|
|
|
|
|
], {
|
|
|
|
|
duration: 400,
|
|
|
|
|
easing: 'ease-in-out',
|
|
|
|
|
fill: 'forwards'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle animation completion
|
|
|
|
|
slideInAnimation.addEventListener('finish', () => {
|
|
|
|
|
// Cleanup: Remove all old grids except the new one
|
|
|
|
|
const allGrids = container.querySelectorAll('swp-grid-container');
|
|
|
|
|
for (let i = 0; i < allGrids.length - 1; i++) {
|
|
|
|
|
allGrids[i].remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset positioning
|
|
|
|
|
newGrid.style.position = 'relative';
|
2025-08-16 00:51:12 +02:00
|
|
|
newGrid.removeAttribute('data-prerendered');
|
2025-08-13 23:37:23 +02:00
|
|
|
|
|
|
|
|
// Update state
|
|
|
|
|
this.currentWeek = new Date(targetWeek);
|
|
|
|
|
this.animationQueue--;
|
2025-07-24 22:17:38 +02:00
|
|
|
|
2025-08-13 23:37:23 +02:00
|
|
|
// If this was the last queued animation, ensure we're in sync
|
|
|
|
|
if (this.animationQueue === 0) {
|
|
|
|
|
this.currentWeek = new Date(this.targetWeek);
|
|
|
|
|
}
|
2025-08-12 00:31:02 +02:00
|
|
|
|
2025-08-13 23:37:23 +02:00
|
|
|
// Update week info and notify other managers
|
|
|
|
|
this.updateWeekInfo();
|
|
|
|
|
this.eventBus.emit(EventTypes.WEEK_CHANGED, {
|
|
|
|
|
weekStart: this.currentWeek,
|
|
|
|
|
weekEnd: DateUtils.addDays(this.currentWeek, 6)
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
|
2025-08-13 23:37:23 +02:00
|
|
|
// Emit animation complete event for ScrollManager
|
|
|
|
|
this.eventBus.emit(EventTypes.NAVIGATION_ANIMATION_COMPLETE, {
|
|
|
|
|
direction,
|
|
|
|
|
weekStart: this.currentWeek
|
|
|
|
|
});
|
2025-08-12 00:07:39 +02:00
|
|
|
|
2025-08-13 23:37:23 +02:00
|
|
|
console.log(`NavigationManager: Completed ${direction} animation`);
|
2025-08-12 00:07:39 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Utility functions (from POC) - moved formatting to NavigationRenderer
|
2025-07-24 22:17:38 +02:00
|
|
|
|
|
|
|
|
private updateWeekInfo(): void {
|
|
|
|
|
const weekNumber = DateUtils.getWeekNumber(this.currentWeek);
|
|
|
|
|
const weekEnd = DateUtils.addDays(this.currentWeek, 6);
|
|
|
|
|
const dateRange = DateUtils.formatDateRange(this.currentWeek, weekEnd);
|
|
|
|
|
|
|
|
|
|
// Update week info in DOM
|
|
|
|
|
const weekNumberElement = document.querySelector('swp-week-number');
|
|
|
|
|
const dateRangeElement = document.querySelector('swp-date-range');
|
|
|
|
|
|
2025-08-17 22:09:50 +02:00
|
|
|
if (weekNumberElement)
|
2025-07-24 22:17:38 +02:00
|
|
|
weekNumberElement.textContent = `Week ${weekNumber}`;
|
|
|
|
|
|
2025-08-17 22:09:50 +02:00
|
|
|
|
|
|
|
|
if (dateRangeElement)
|
2025-07-24 22:17:38 +02:00
|
|
|
dateRangeElement.textContent = dateRange;
|
|
|
|
|
|
|
|
|
|
// Notify other managers about week info update
|
|
|
|
|
this.eventBus.emit(EventTypes.WEEK_INFO_UPDATED, {
|
|
|
|
|
weekNumber,
|
|
|
|
|
dateRange,
|
|
|
|
|
weekStart: this.currentWeek,
|
|
|
|
|
weekEnd
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current week start date
|
|
|
|
|
*/
|
|
|
|
|
getCurrentWeek(): Date {
|
|
|
|
|
return new Date(this.currentWeek);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get target week (where navigation is heading)
|
|
|
|
|
*/
|
|
|
|
|
getTargetWeek(): Date {
|
|
|
|
|
return new Date(this.targetWeek);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if navigation animation is in progress
|
|
|
|
|
*/
|
|
|
|
|
isAnimating(): boolean {
|
|
|
|
|
return this.animationQueue > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Force navigation to specific week without animation
|
|
|
|
|
*/
|
|
|
|
|
setWeek(weekStart: Date): void {
|
|
|
|
|
this.currentWeek = new Date(weekStart);
|
|
|
|
|
this.targetWeek = new Date(weekStart);
|
|
|
|
|
this.updateWeekInfo();
|
|
|
|
|
|
|
|
|
|
this.eventBus.emit(EventTypes.WEEK_CHANGED, {
|
|
|
|
|
weekStart: this.currentWeek,
|
|
|
|
|
weekEnd: DateUtils.addDays(this.currentWeek, 6)
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-16 00:51:12 +02:00
|
|
|
|
2025-08-17 22:54:00 +02:00
|
|
|
// Rendering methods moved to NavigationRenderer for better separation of concerns
|
2025-07-24 22:17:38 +02:00
|
|
|
}
|