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-30 23:45:07 +02:00
|
|
|
/**
|
|
|
|
|
* Get column bounds by Date
|
|
|
|
|
*/
|
|
|
|
|
public static getColumnBoundsByDate(date: Date): ColumnBounds | null {
|
|
|
|
|
if (this.columnBoundsCache.length === 0) {
|
|
|
|
|
this.updateColumnBoundsCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert Date to YYYY-MM-DD format
|
|
|
|
|
var dateString = date.toISOString().split('T')[0];
|
|
|
|
|
|
|
|
|
|
// Find column that matches the date
|
|
|
|
|
var column = this.columnBoundsCache.find(col => col.date === dateString);
|
|
|
|
|
return column || null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:41:28 +02:00
|
|
|
|
|
|
|
|
public static getColumns(): ColumnBounds[] {
|
2025-09-28 13:25:09 +02:00
|
|
|
return [...this.columnBoundsCache];
|
|
|
|
|
}
|
2025-10-01 21:27:13 +02:00
|
|
|
public static getHeaderColumns(): ColumnBounds[] {
|
|
|
|
|
|
|
|
|
|
let dayHeaders: ColumnBounds[] = [];
|
|
|
|
|
|
|
|
|
|
const dayColumns = document.querySelectorAll('swp-calendar-header swp-day-header');
|
|
|
|
|
let index = 1;
|
|
|
|
|
// Cache hver kolonnes x-grænser
|
|
|
|
|
dayColumns.forEach(column => {
|
|
|
|
|
const rect = column.getBoundingClientRect();
|
|
|
|
|
const date = (column as HTMLElement).dataset.date;
|
|
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
|
dayHeaders.push({
|
|
|
|
|
boundingClientRect : rect,
|
|
|
|
|
element: column as HTMLElement,
|
|
|
|
|
date,
|
|
|
|
|
left: rect.left,
|
|
|
|
|
right: rect.right,
|
|
|
|
|
index: index++
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Sorter efter x-position (fra venstre til højre)
|
|
|
|
|
dayHeaders.sort((a, b) => a.left - b.left);
|
|
|
|
|
return dayHeaders;
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-26 22:11:57 +02:00
|
|
|
}
|