Calendar/src/renderers/HeaderRenderer.ts

201 lines
6.5 KiB
TypeScript
Raw Normal View History

2025-08-07 00:15:44 +02:00
// Header rendering strategy interface and implementations
import { CalendarConfig, ALL_DAY_CONSTANTS } from '../core/CalendarConfig';
import { eventBus } from '../core/EventBus';
2025-08-07 00:15:44 +02:00
import { ResourceCalendarData } from '../types/CalendarTypes';
import { DateCalculator } from '../utils/DateCalculator';
2025-08-07 00:15:44 +02:00
/**
* Interface for header rendering strategies
*/
export interface HeaderRenderer {
render(calendarHeader: HTMLElement, context: HeaderRenderContext): void;
addToAllDay(dayHeader: HTMLElement): void;
ensureAllDayContainers(calendarHeader: HTMLElement): void;
}
/**
* Base class with shared addToAllDay implementation
*/
export abstract class BaseHeaderRenderer implements HeaderRenderer {
abstract render(calendarHeader: HTMLElement, context: HeaderRenderContext): void;
/**
* Expand header to show all-day row
*/
addToAllDay(dayHeader: HTMLElement): void {
const root = document.documentElement;
const currentHeight = parseInt(getComputedStyle(root).getPropertyValue('--all-day-row-height') || '0');
if (currentHeight === 0) {
// Find the calendar header element to animate
const calendarHeader = dayHeader.closest('swp-calendar-header') as HTMLElement;
if (calendarHeader) {
// Ensure container exists BEFORE animation
this.createAllDayMainStructure(calendarHeader);
this.animateHeaderExpansion(calendarHeader);
}
}
}
/**
* Ensure all-day containers exist - always create them during header rendering
*/
ensureAllDayContainers(calendarHeader: HTMLElement): void {
this.createAllDayMainStructure(calendarHeader);
}
private animateHeaderExpansion(calendarHeader: HTMLElement): void {
const root = document.documentElement;
const currentHeaderHeight = parseInt(getComputedStyle(root).getPropertyValue('--header-height'));
const targetHeight = currentHeaderHeight + ALL_DAY_CONSTANTS.SINGLE_ROW_HEIGHT;
// Find header spacer
const headerSpacer = document.querySelector('swp-header-spacer') as HTMLElement;
// Find or create all-day container (it should exist but be hidden)
let allDayContainer = calendarHeader.querySelector('swp-allday-container') as HTMLElement;
if (!allDayContainer) {
// Create container if it doesn't exist
allDayContainer = document.createElement('swp-allday-container');
calendarHeader.appendChild(allDayContainer);
}
// Animate header, spacer, and container simultaneously
const animations = [
// Header height animation
calendarHeader.animate([
{ height: `${currentHeaderHeight}px` },
{ height: `${targetHeight}px` }
], {
duration: 150,
easing: 'ease-out',
fill: 'forwards'
}),
// Container visibility and height animation
allDayContainer.animate([
{ height: '0px', opacity: '0' },
{ height: `${ALL_DAY_CONSTANTS.SINGLE_ROW_HEIGHT}px`, opacity: '1' }
], {
duration: 150,
easing: 'ease-out',
fill: 'forwards'
})
];
// Add spacer animation if spacer exists
if (headerSpacer) {
animations.push(
headerSpacer.animate([
{ height: `${currentHeaderHeight}px` },
{ height: `${targetHeight}px` }
], {
duration: 150,
easing: 'ease-out',
fill: 'forwards'
})
);
}
// Wait for all animations to finish
Promise.all(animations.map(anim => anim.finished)).then(() => {
// Set the CSS variable after animation
root.style.setProperty('--all-day-row-height', `${ALL_DAY_CONSTANTS.SINGLE_ROW_HEIGHT}px`);
// Notify ScrollManager about header height change
eventBus.emit('header:height-changed');
});
}
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-26 00:05:42 +02:00
if (!container) {
// Create simple all-day container (initially hidden)
2025-08-26 00:05:42 +02:00
container = document.createElement('swp-allday-container');
calendarHeader.appendChild(container);
} else {
2025-08-26 00:05:42 +02:00
}
}
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)
*/
export class DateHeaderRenderer extends BaseHeaderRenderer {
private dateCalculator!: DateCalculator;
render(calendarHeader: HTMLElement, context: HeaderRenderContext): void {
const { currentWeek, config } = context;
2025-08-07 00:15:44 +02:00
// Initialize date calculator with config
this.dateCalculator = new DateCalculator(config);
const dates = this.dateCalculator.getWorkWeekDates(currentWeek);
const weekDays = config.getDateViewSettings().weekDays;
2025-08-07 00:15:44 +02:00
const daysToShow = dates.slice(0, weekDays);
daysToShow.forEach((date, index) => {
2025-08-07 00:15:44 +02:00
const header = document.createElement('swp-day-header');
if (this.dateCalculator.isToday(date)) {
2025-08-07 00:15:44 +02:00
(header as any).dataset.today = 'true';
}
const dayName = this.dateCalculator.getDayName(date, 'short');
2025-08-07 00:15:44 +02:00
header.innerHTML = `
<swp-day-name>${dayName}</swp-day-name>
2025-08-07 00:15:44 +02:00
<swp-day-date>${date.getDate()}</swp-day-date>
`;
(header as any).dataset.date = this.dateCalculator.formatISODate(date);
2025-08-07 00:15:44 +02:00
calendarHeader.appendChild(header);
2025-08-07 00:15:44 +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
*/
export class ResourceHeaderRenderer extends BaseHeaderRenderer {
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>
`;
calendarHeader.appendChild(header);
2025-08-07 00:15:44 +02:00
});
// Always create all-day container after rendering headers
this.ensureAllDayContainers(calendarHeader);
2025-08-07 00:15:44 +02:00
}
}