2025-09-26 22:11:57 +02:00
|
|
|
/**
|
|
|
|
|
* ColumnDetectionUtils - Shared utility for column detection and caching
|
|
|
|
|
* Used by both DragDropManager and AllDayManager for consistent column detection
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
import { MousePosition } from "../types/DragDropTypes";
|
|
|
|
|
|
|
|
|
|
|
2025-09-26 22:11:57 +02:00
|
|
|
export interface ColumnBounds {
|
2025-09-28 13:25:09 +02:00
|
|
|
date: string;
|
|
|
|
|
left: number;
|
|
|
|
|
right: number;
|
|
|
|
|
boundingClientRect: DOMRect,
|
|
|
|
|
element : HTMLElement,
|
|
|
|
|
index: number
|
2025-09-26 22:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ColumnDetectionUtils {
|
2025-09-28 13:25:09 +02:00
|
|
|
private static columnBoundsCache: ColumnBounds[] = [];
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
/**
|
|
|
|
|
* Update column bounds cache for coordinate-based column detection
|
|
|
|
|
*/
|
|
|
|
|
public static updateColumnBoundsCache(): void {
|
|
|
|
|
// Reset cache
|
|
|
|
|
this.columnBoundsCache = [];
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
// Find alle kolonner
|
|
|
|
|
const columns = document.querySelectorAll('swp-day-column');
|
2025-09-29 20:50:52 +02:00
|
|
|
let index = 1;
|
2025-09-28 13:25:09 +02:00
|
|
|
// Cache hver kolonnes x-grænser
|
|
|
|
|
columns.forEach(column => {
|
|
|
|
|
const rect = column.getBoundingClientRect();
|
|
|
|
|
const date = (column as HTMLElement).dataset.date;
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
if (date) {
|
|
|
|
|
this.columnBoundsCache.push({
|
|
|
|
|
boundingClientRect : rect,
|
|
|
|
|
element: column as HTMLElement,
|
|
|
|
|
date,
|
|
|
|
|
left: rect.left,
|
|
|
|
|
right: rect.right,
|
|
|
|
|
index: index++
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-09-26 22:11:57 +02:00
|
|
|
});
|
|
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
// Sorter efter x-position (fra venstre til højre)
|
|
|
|
|
this.columnBoundsCache.sort((a, b) => a.left - b.left);
|
2025-09-26 22:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
/**
|
|
|
|
|
* Get column date from X coordinate using cached bounds
|
|
|
|
|
*/
|
|
|
|
|
public static getColumnBounds(position: MousePosition): ColumnBounds | null{
|
|
|
|
|
if (this.columnBoundsCache.length === 0) {
|
|
|
|
|
this.updateColumnBoundsCache();
|
|
|
|
|
}
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
// Find den kolonne hvor x-koordinaten er indenfor grænserne
|
|
|
|
|
let column = this.columnBoundsCache.find(col =>
|
|
|
|
|
position.x >= col.left && position.x <= col.right
|
|
|
|
|
);
|
|
|
|
|
if (column)
|
|
|
|
|
return column;
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
return null;
|
2025-09-26 22:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
/**
|
|
|
|
|
* Clear cache (useful for testing or when DOM structure changes)
|
|
|
|
|
*/
|
|
|
|
|
public static clearCache(): void {
|
|
|
|
|
this.columnBoundsCache = [];
|
|
|
|
|
}
|
2025-09-26 22:11:57 +02:00
|
|
|
|
2025-09-28 13:25:09 +02:00
|
|
|
/**
|
|
|
|
|
* Get current cache for debugging
|
|
|
|
|
*/
|
|
|
|
|
public static getCache(): ColumnBounds[] {
|
|
|
|
|
return [...this.columnBoundsCache];
|
|
|
|
|
}
|
2025-09-26 22:11:57 +02:00
|
|
|
}
|