Improves performance by caching DOM elements

Caches frequently accessed DOM elements in NavigationManager and
EventRenderer to reduce redundant queries, improving performance.

Updates the event renderer to trigger all-day height animations and
introduces a destroy method for resource management.

Refactors time formatting in EventRenderer to handle both total
minutes and Date objects using a unified method.
This commit is contained in:
Janus Knudsen 2025-09-03 18:15:33 +02:00
parent 77592278d3
commit 0da875a224
3 changed files with 126 additions and 37 deletions

View file

@ -16,6 +16,10 @@ export class NavigationManager {
private currentWeek: Date;
private targetWeek: Date;
private animationQueue: number = 0;
// Cached DOM elements to avoid redundant queries
private cachedCalendarContainer: HTMLElement | null = null;
private cachedCurrentGrid: HTMLElement | null = null;
constructor(eventBus: IEventBus, eventRenderer: EventRenderingService) {
this.eventBus = eventBus;
@ -31,6 +35,37 @@ export class NavigationManager {
// Don't update week info immediately - wait for DOM to be ready
}
/**
* Get cached calendar container element
*/
private getCalendarContainer(): HTMLElement | null {
if (!this.cachedCalendarContainer) {
this.cachedCalendarContainer = document.querySelector('swp-calendar-container');
}
return this.cachedCalendarContainer;
}
/**
* Get cached current grid element
*/
private getCurrentGrid(): HTMLElement | null {
const container = this.getCalendarContainer();
if (!container) return null;
if (!this.cachedCurrentGrid) {
this.cachedCurrentGrid = container.querySelector('swp-grid-container:not([data-prerendered])');
}
return this.cachedCurrentGrid;
}
/**
* Clear cached DOM elements (call when DOM structure changes)
*/
private clearCache(): void {
this.cachedCalendarContainer = null;
this.cachedCurrentGrid = null;
}
private setupEventListeners(): void {
// Initial DOM update when calendar is initialized
this.eventBus.on(CoreEvents.INITIALIZED, () => {
@ -137,8 +172,8 @@ export class NavigationManager {
* Animation transition using pre-rendered containers when available
*/
private animateTransition(direction: 'prev' | 'next', targetWeek: Date): void {
const container = document.querySelector('swp-calendar-container');
const currentGrid = container?.querySelector('swp-grid-container:not([data-prerendered])');
const container = this.getCalendarContainer();
const currentGrid = this.getCurrentGrid();
if (!container || !currentGrid) {
return;
@ -148,7 +183,7 @@ export class NavigationManager {
let newGrid: HTMLElement;
// Always create a fresh container for consistent behavior
newGrid = this.navigationRenderer.renderContainer(container as HTMLElement, targetWeek);
newGrid = this.navigationRenderer.renderContainer(container, targetWeek);
// Clear any existing transforms before animation
@ -187,6 +222,9 @@ export class NavigationManager {
newGrid.style.position = 'relative';
newGrid.removeAttribute('data-prerendered');
// Clear cache since DOM structure changed
this.clearCache();
// Update state
this.currentWeek = new Date(targetWeek);
this.animationQueue--;