2025-08-07 00:15:44 +02:00
|
|
|
// Header rendering strategy interface and implementations
|
|
|
|
|
|
2025-08-24 23:31:11 +02:00
|
|
|
import { CalendarConfig, ALL_DAY_CONSTANTS } from '../core/CalendarConfig';
|
2025-08-25 21:20:51 +02:00
|
|
|
import { eventBus } from '../core/EventBus';
|
2025-08-07 00:15:44 +02:00
|
|
|
import { ResourceCalendarData } from '../types/CalendarTypes';
|
2025-08-18 23:42:03 +02:00
|
|
|
import { DateCalculator } from '../utils/DateCalculator';
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interface for header rendering strategies
|
|
|
|
|
*/
|
|
|
|
|
export interface HeaderRenderer {
|
2025-08-07 00:26:33 +02:00
|
|
|
render(calendarHeader: HTMLElement, context: HeaderRenderContext): void;
|
2025-08-24 23:31:11 +02:00
|
|
|
addToAllDay(dayHeader: HTMLElement): void;
|
2025-08-25 22:05:57 +02:00
|
|
|
ensureAllDayContainers(calendarHeader: HTMLElement): void;
|
2025-09-01 23:37:47 +02:00
|
|
|
checkAndAnimateAllDayHeight(): void;
|
2025-08-24 23:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class with shared addToAllDay implementation
|
|
|
|
|
*/
|
|
|
|
|
export abstract class BaseHeaderRenderer implements HeaderRenderer {
|
2025-09-03 00:25:05 +02:00
|
|
|
// Cached DOM elements to avoid redundant queries
|
|
|
|
|
private cachedCalendarHeader: HTMLElement | null = null;
|
|
|
|
|
private cachedAllDayContainer: HTMLElement | null = null;
|
|
|
|
|
private cachedHeaderSpacer: HTMLElement | null = null;
|
|
|
|
|
|
2025-08-24 23:31:11 +02:00
|
|
|
abstract render(calendarHeader: HTMLElement, context: HeaderRenderContext): void;
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 00:25:05 +02:00
|
|
|
* Get cached calendar header element
|
2025-08-24 23:31:11 +02:00
|
|
|
*/
|
2025-09-03 00:25:05 +02:00
|
|
|
private getCalendarHeader(): HTMLElement | null {
|
|
|
|
|
if (!this.cachedCalendarHeader) {
|
|
|
|
|
this.cachedCalendarHeader = document.querySelector('swp-calendar-header');
|
|
|
|
|
}
|
|
|
|
|
return this.cachedCalendarHeader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cached all-day container element
|
|
|
|
|
*/
|
|
|
|
|
private getAllDayContainer(): HTMLElement | null {
|
|
|
|
|
if (!this.cachedAllDayContainer) {
|
|
|
|
|
const calendarHeader = this.getCalendarHeader();
|
|
|
|
|
if (calendarHeader) {
|
|
|
|
|
this.cachedAllDayContainer = calendarHeader.querySelector('swp-allday-container');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return this.cachedAllDayContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get cached header spacer element
|
|
|
|
|
*/
|
|
|
|
|
private getHeaderSpacer(): HTMLElement | null {
|
|
|
|
|
if (!this.cachedHeaderSpacer) {
|
|
|
|
|
this.cachedHeaderSpacer = document.querySelector('swp-header-spacer');
|
|
|
|
|
}
|
|
|
|
|
return this.cachedHeaderSpacer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate all-day height based on number of rows
|
|
|
|
|
*/
|
|
|
|
|
private calculateAllDayHeight(targetRows: number): {
|
|
|
|
|
targetHeight: number;
|
|
|
|
|
currentHeight: number;
|
|
|
|
|
heightDifference: number;
|
|
|
|
|
} {
|
2025-08-24 23:31:11 +02:00
|
|
|
const root = document.documentElement;
|
2025-09-03 00:25:05 +02:00
|
|
|
const targetHeight = targetRows * ALL_DAY_CONSTANTS.SINGLE_ROW_HEIGHT;
|
2025-08-24 23:31:11 +02:00
|
|
|
const currentHeight = parseInt(getComputedStyle(root).getPropertyValue('--all-day-row-height') || '0');
|
2025-09-03 00:25:05 +02:00
|
|
|
const heightDifference = targetHeight - currentHeight;
|
|
|
|
|
|
|
|
|
|
return { targetHeight, currentHeight, heightDifference };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear cached DOM elements (call when DOM structure changes)
|
|
|
|
|
*/
|
|
|
|
|
private clearCache(): void {
|
|
|
|
|
this.cachedCalendarHeader = null;
|
|
|
|
|
this.cachedAllDayContainer = null;
|
|
|
|
|
this.cachedHeaderSpacer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expand header to show all-day row
|
|
|
|
|
*/
|
|
|
|
|
addToAllDay(dayHeader: HTMLElement): void {
|
|
|
|
|
const { currentHeight } = this.calculateAllDayHeight(0);
|
2025-08-24 23:31:11 +02:00
|
|
|
|
|
|
|
|
if (currentHeight === 0) {
|
|
|
|
|
// Find the calendar header element to animate
|
|
|
|
|
const calendarHeader = dayHeader.closest('swp-calendar-header') as HTMLElement;
|
|
|
|
|
if (calendarHeader) {
|
2025-08-28 00:13:11 +02:00
|
|
|
// Ensure container exists BEFORE animation
|
|
|
|
|
this.createAllDayMainStructure(calendarHeader);
|
2025-09-01 23:37:47 +02:00
|
|
|
this.checkAndAnimateAllDayHeight();
|
2025-08-24 23:31:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 22:05:57 +02:00
|
|
|
/**
|
2025-08-31 23:48:34 +02:00
|
|
|
* Ensure all-day containers exist - always create them during header rendering
|
2025-08-25 22:05:57 +02:00
|
|
|
*/
|
|
|
|
|
ensureAllDayContainers(calendarHeader: HTMLElement): void {
|
2025-08-31 23:48:34 +02:00
|
|
|
this.createAllDayMainStructure(calendarHeader);
|
2025-08-25 22:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
checkAndAnimateAllDayHeight(): void {
|
2025-09-03 00:25:05 +02:00
|
|
|
const container = this.getAllDayContainer();
|
2025-09-01 23:37:47 +02:00
|
|
|
if (!container) return;
|
|
|
|
|
|
|
|
|
|
const allDayEvents = container.querySelectorAll('swp-allday-event');
|
|
|
|
|
|
|
|
|
|
// Calculate required rows - 0 if no events (will collapse)
|
|
|
|
|
let maxRows = 0;
|
|
|
|
|
|
|
|
|
|
if (allDayEvents.length > 0) {
|
|
|
|
|
// Expand events to all dates they span and group by date
|
|
|
|
|
const expandedEventsByDate: Record<string, string[]> = {};
|
|
|
|
|
|
|
|
|
|
(Array.from(allDayEvents) as HTMLElement[]).forEach((event: HTMLElement) => {
|
|
|
|
|
const startISO = event.dataset.start || '';
|
|
|
|
|
const endISO = event.dataset.end || startISO;
|
|
|
|
|
const eventId = event.dataset.eventId || '';
|
|
|
|
|
|
|
|
|
|
// Extract dates from ISO strings
|
|
|
|
|
const startDate = startISO.split('T')[0]; // YYYY-MM-DD
|
|
|
|
|
const endDate = endISO.split('T')[0]; // YYYY-MM-DD
|
|
|
|
|
|
|
|
|
|
// Loop through all dates from start to end
|
|
|
|
|
let current = new Date(startDate);
|
|
|
|
|
const end = new Date(endDate);
|
|
|
|
|
|
|
|
|
|
while (current <= end) {
|
|
|
|
|
const dateStr = current.toISOString().split('T')[0]; // YYYY-MM-DD format
|
|
|
|
|
|
|
|
|
|
if (!expandedEventsByDate[dateStr]) {
|
|
|
|
|
expandedEventsByDate[dateStr] = [];
|
|
|
|
|
}
|
|
|
|
|
expandedEventsByDate[dateStr].push(eventId);
|
|
|
|
|
|
|
|
|
|
// Move to next day
|
|
|
|
|
current.setDate(current.getDate() + 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Find max rows needed
|
|
|
|
|
maxRows = Math.max(
|
|
|
|
|
...Object.values(expandedEventsByDate).map(ids => ids?.length || 0),
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Animate to required rows (0 = collapse, >0 = expand)
|
|
|
|
|
this.animateToRows(maxRows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Animate all-day container to specific number of rows
|
|
|
|
|
*/
|
|
|
|
|
animateToRows(targetRows: number): void {
|
2025-09-03 00:25:05 +02:00
|
|
|
const { targetHeight, currentHeight, heightDifference } = this.calculateAllDayHeight(targetRows);
|
2025-09-01 23:37:47 +02:00
|
|
|
|
|
|
|
|
if (targetHeight === currentHeight) return; // No animation needed
|
2025-08-24 23:31:11 +02:00
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
console.log(`🎬 All-day height animation starting: ${currentHeight}px → ${targetHeight}px (${Math.ceil(currentHeight / ALL_DAY_CONSTANTS.SINGLE_ROW_HEIGHT)} → ${targetRows} rows)`);
|
|
|
|
|
|
2025-09-03 00:25:05 +02:00
|
|
|
// Get cached elements
|
|
|
|
|
const calendarHeader = this.getCalendarHeader();
|
|
|
|
|
const headerSpacer = this.getHeaderSpacer();
|
|
|
|
|
const allDayContainer = this.getAllDayContainer();
|
2025-08-24 23:31:11 +02:00
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
if (!calendarHeader || !allDayContainer) return;
|
2025-08-28 00:13:11 +02:00
|
|
|
|
2025-09-03 00:12:47 +02:00
|
|
|
// Get current parent height for animation
|
|
|
|
|
const currentParentHeight = parseFloat(getComputedStyle(calendarHeader).height);
|
|
|
|
|
const targetParentHeight = currentParentHeight + heightDifference;
|
|
|
|
|
|
2025-08-24 23:31:11 +02:00
|
|
|
const animations = [
|
2025-09-03 00:12:47 +02:00
|
|
|
calendarHeader.animate([
|
|
|
|
|
{ height: `${currentParentHeight}px` },
|
|
|
|
|
{ height: `${targetParentHeight}px` }
|
2025-08-28 00:13:11 +02:00
|
|
|
], {
|
2025-09-03 00:12:47 +02:00
|
|
|
duration: 300,
|
2025-08-28 00:13:11 +02:00
|
|
|
easing: 'ease-out',
|
|
|
|
|
fill: 'forwards'
|
2025-08-24 23:31:11 +02:00
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-28 00:13:11 +02:00
|
|
|
// Add spacer animation if spacer exists
|
2025-08-24 23:31:11 +02:00
|
|
|
if (headerSpacer) {
|
2025-09-03 00:25:05 +02:00
|
|
|
const root = document.documentElement;
|
2025-09-01 23:37:47 +02:00
|
|
|
const currentSpacerHeight = parseInt(getComputedStyle(root).getPropertyValue('--header-height')) + currentHeight;
|
|
|
|
|
const targetSpacerHeight = parseInt(getComputedStyle(root).getPropertyValue('--header-height')) + targetHeight;
|
|
|
|
|
|
2025-08-24 23:31:11 +02:00
|
|
|
animations.push(
|
|
|
|
|
headerSpacer.animate([
|
2025-09-01 23:37:47 +02:00
|
|
|
{ height: `${currentSpacerHeight}px` },
|
|
|
|
|
{ height: `${targetSpacerHeight}px` }
|
2025-08-24 23:31:11 +02:00
|
|
|
], {
|
2025-09-03 00:12:47 +02:00
|
|
|
duration: 300,
|
2025-08-24 23:31:11 +02:00
|
|
|
easing: 'ease-out',
|
|
|
|
|
fill: 'forwards'
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
// Update CSS variable after animation
|
2025-08-24 23:31:11 +02:00
|
|
|
Promise.all(animations.map(anim => anim.finished)).then(() => {
|
2025-09-03 00:25:05 +02:00
|
|
|
const root = document.documentElement;
|
2025-09-01 23:37:47 +02:00
|
|
|
root.style.setProperty('--all-day-row-height', `${targetHeight}px`);
|
2025-08-25 21:20:51 +02:00
|
|
|
eventBus.emit('header:height-changed');
|
2025-08-24 23:31:11 +02:00
|
|
|
});
|
|
|
|
|
}
|
2025-08-25 21:20:51 +02:00
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
private createAllDayMainStructure(calendarHeader: HTMLElement): void {
|
|
|
|
|
// Check if container already exists
|
|
|
|
|
let container = calendarHeader.querySelector('swp-allday-container');
|
2025-08-25 21:20:51 +02:00
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
if (!container) {
|
2025-08-28 00:13:11 +02:00
|
|
|
// Create simple all-day container (initially hidden)
|
2025-08-26 00:05:42 +02:00
|
|
|
container = document.createElement('swp-allday-container');
|
|
|
|
|
calendarHeader.appendChild(container);
|
2025-09-03 00:25:05 +02:00
|
|
|
// Clear cache since DOM structure changed
|
|
|
|
|
this.clearCache();
|
2025-08-26 00:05:42 +02:00
|
|
|
}
|
2025-08-25 21:20:51 +02:00
|
|
|
}
|
2025-09-03 00:25:05 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Public cleanup method for cached elements
|
|
|
|
|
*/
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
this.clearCache();
|
|
|
|
|
}
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Context for header rendering
|
|
|
|
|
*/
|
|
|
|
|
export interface HeaderRenderContext {
|
|
|
|
|
currentWeek: Date;
|
|
|
|
|
config: CalendarConfig;
|
|
|
|
|
resourceData?: ResourceCalendarData | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Date-based header renderer (original functionality)
|
|
|
|
|
*/
|
2025-08-24 23:31:11 +02:00
|
|
|
export class DateHeaderRenderer extends BaseHeaderRenderer {
|
2025-08-20 21:38:54 +02:00
|
|
|
private dateCalculator!: DateCalculator;
|
2025-08-18 23:42:03 +02:00
|
|
|
|
2025-08-07 00:26:33 +02:00
|
|
|
render(calendarHeader: HTMLElement, context: HeaderRenderContext): void {
|
2025-08-25 22:05:57 +02:00
|
|
|
const { currentWeek, config } = context;
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-18 23:42:03 +02:00
|
|
|
// Initialize date calculator with config
|
|
|
|
|
this.dateCalculator = new DateCalculator(config);
|
|
|
|
|
|
|
|
|
|
const dates = this.dateCalculator.getWorkWeekDates(currentWeek);
|
2025-08-20 21:38:54 +02:00
|
|
|
const weekDays = config.getDateViewSettings().weekDays;
|
2025-08-07 00:15:44 +02:00
|
|
|
const daysToShow = dates.slice(0, weekDays);
|
|
|
|
|
|
2025-08-18 22:27:17 +02:00
|
|
|
daysToShow.forEach((date, index) => {
|
2025-08-07 00:15:44 +02:00
|
|
|
const header = document.createElement('swp-day-header');
|
2025-08-18 23:42:03 +02:00
|
|
|
if (this.dateCalculator.isToday(date)) {
|
2025-08-07 00:15:44 +02:00
|
|
|
(header as any).dataset.today = 'true';
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 00:39:31 +02:00
|
|
|
const dayName = this.dateCalculator.getDayName(date, 'short');
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
header.innerHTML = `
|
2025-08-20 00:39:31 +02:00
|
|
|
<swp-day-name>${dayName}</swp-day-name>
|
2025-08-07 00:15:44 +02:00
|
|
|
<swp-day-date>${date.getDate()}</swp-day-date>
|
|
|
|
|
`;
|
2025-08-18 23:42:03 +02:00
|
|
|
(header as any).dataset.date = this.dateCalculator.formatISODate(date);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-07 00:26:33 +02:00
|
|
|
calendarHeader.appendChild(header);
|
2025-08-07 00:15:44 +02:00
|
|
|
});
|
2025-08-31 23:48:34 +02:00
|
|
|
|
|
|
|
|
// Always create all-day container after rendering headers
|
|
|
|
|
this.ensureAllDayContainers(calendarHeader);
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resource-based header renderer
|
|
|
|
|
*/
|
2025-08-24 23:31:11 +02:00
|
|
|
export class ResourceHeaderRenderer extends BaseHeaderRenderer {
|
2025-08-07 00:26:33 +02:00
|
|
|
render(calendarHeader: HTMLElement, context: HeaderRenderContext): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
const { resourceData } = context;
|
|
|
|
|
|
|
|
|
|
if (!resourceData) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resourceData.resources.forEach((resource) => {
|
|
|
|
|
const header = document.createElement('swp-resource-header');
|
|
|
|
|
header.setAttribute('data-resource', resource.name);
|
|
|
|
|
header.setAttribute('data-employee-id', resource.employeeId);
|
|
|
|
|
|
|
|
|
|
header.innerHTML = `
|
|
|
|
|
<swp-resource-avatar>
|
|
|
|
|
<img src="${resource.avatarUrl}" alt="${resource.displayName}" onerror="this.style.display='none'">
|
|
|
|
|
</swp-resource-avatar>
|
|
|
|
|
<swp-resource-name>${resource.displayName}</swp-resource-name>
|
|
|
|
|
`;
|
|
|
|
|
|
2025-08-07 00:26:33 +02:00
|
|
|
calendarHeader.appendChild(header);
|
2025-08-07 00:15:44 +02:00
|
|
|
});
|
|
|
|
|
|
2025-08-31 23:48:34 +02:00
|
|
|
// Always create all-day container after rendering headers
|
|
|
|
|
this.ensureAllDayContainers(calendarHeader);
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
}
|