2025-08-07 00:15:44 +02:00
|
|
|
// Event rendering strategy interface and implementations
|
|
|
|
|
|
|
|
|
|
import { CalendarEvent } from '../types/CalendarTypes';
|
2025-09-03 20:04:47 +02:00
|
|
|
import { calendarConfig } from '../core/CalendarConfig';
|
2025-08-20 00:39:31 +02:00
|
|
|
import { DateCalculator } from '../utils/DateCalculator';
|
2025-08-27 22:50:13 +02:00
|
|
|
import { eventBus } from '../core/EventBus';
|
2025-09-01 23:37:47 +02:00
|
|
|
import { CoreEvents } from '../constants/CoreEvents';
|
2025-09-09 14:35:21 +02:00
|
|
|
import { OverlapDetector, OverlapResult, EventId } from '../utils/OverlapDetector';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resize state interface
|
|
|
|
|
*/
|
|
|
|
|
interface ResizeState {
|
|
|
|
|
element: HTMLElement;
|
|
|
|
|
handle: 'top' | 'bottom';
|
|
|
|
|
startY: number;
|
|
|
|
|
originalTop: number;
|
|
|
|
|
originalHeight: number;
|
|
|
|
|
originalStartTime: Date;
|
|
|
|
|
originalEndTime: Date;
|
|
|
|
|
minHeightPx: number;
|
|
|
|
|
}
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interface for event rendering strategies
|
|
|
|
|
*/
|
|
|
|
|
export interface EventRendererStrategy {
|
2025-09-03 20:04:47 +02:00
|
|
|
renderEvents(events: CalendarEvent[], container: HTMLElement): void;
|
2025-08-16 00:51:12 +02:00
|
|
|
clearEvents(container?: HTMLElement): void;
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for event renderers with common functionality
|
|
|
|
|
*/
|
|
|
|
|
export abstract class BaseEventRenderer implements EventRendererStrategy {
|
2025-08-20 00:39:31 +02:00
|
|
|
protected dateCalculator: DateCalculator;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
// Drag and drop state
|
|
|
|
|
private draggedClone: HTMLElement | null = null;
|
|
|
|
|
private originalEvent: HTMLElement | null = null;
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
// Resize state
|
|
|
|
|
private resizeState: ResizeState | null = null;
|
|
|
|
|
private readonly MIN_EVENT_DURATION_MINUTES = 30;
|
2025-08-20 00:39:31 +02:00
|
|
|
|
2025-09-03 20:04:47 +02:00
|
|
|
constructor(dateCalculator?: DateCalculator) {
|
2025-09-03 18:38:52 +02:00
|
|
|
if (!dateCalculator) {
|
2025-09-03 20:04:47 +02:00
|
|
|
DateCalculator.initialize(calendarConfig);
|
2025-09-03 18:38:52 +02:00
|
|
|
}
|
|
|
|
|
this.dateCalculator = dateCalculator || new DateCalculator();
|
2025-08-27 22:50:13 +02:00
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// NEW OVERLAP DETECTION SYSTEM
|
|
|
|
|
// All new functions prefixed with new_
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
protected overlapDetector = new OverlapDetector();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ny hovedfunktion til at håndtere event overlaps
|
|
|
|
|
* @param events - Events der skal renderes i kolonnen
|
|
|
|
|
* @param container - Container element at rendere i
|
|
|
|
|
*/
|
|
|
|
|
protected new_handleEventOverlaps(events: CalendarEvent[], container: HTMLElement): void {
|
|
|
|
|
if (events.length === 0) return;
|
|
|
|
|
|
|
|
|
|
if (events.length === 1) {
|
|
|
|
|
const element = this.renderEvent(events[0]);
|
|
|
|
|
container.appendChild(element);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 17:15:06 +02:00
|
|
|
// Track hvilke events der allerede er blevet processeret
|
|
|
|
|
const processedEvents = new Set<string>();
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Gå gennem hvert event og find overlaps
|
|
|
|
|
events.forEach((currentEvent, index) => {
|
2025-09-09 17:15:06 +02:00
|
|
|
// Skip events der allerede er processeret som del af en overlap gruppe
|
|
|
|
|
if (processedEvents.has(currentEvent.id)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
const remainingEvents = events.slice(index + 1);
|
|
|
|
|
const overlappingEvents = this.overlapDetector.resolveOverlap(currentEvent, remainingEvents);
|
|
|
|
|
|
|
|
|
|
if (overlappingEvents.length > 0) {
|
|
|
|
|
// Der er overlaps - opret stack links
|
|
|
|
|
const result = this.overlapDetector.decorateWithStackLinks(currentEvent, overlappingEvents);
|
|
|
|
|
this.new_renderOverlappingEvents(result, container);
|
2025-09-09 17:15:06 +02:00
|
|
|
|
|
|
|
|
// Marker alle events i overlap gruppen som processeret
|
|
|
|
|
overlappingEvents.forEach(event => processedEvents.add(event.id));
|
2025-09-09 14:35:21 +02:00
|
|
|
} else {
|
|
|
|
|
// Intet overlap - render normalt
|
|
|
|
|
const element = this.renderEvent(currentEvent);
|
|
|
|
|
container.appendChild(element);
|
2025-09-09 17:15:06 +02:00
|
|
|
processedEvents.add(currentEvent.id);
|
2025-09-09 14:35:21 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
/**
|
|
|
|
|
* Setup listeners for drag events from DragDropManager
|
|
|
|
|
*/
|
2025-08-27 23:56:38 +02:00
|
|
|
protected setupDragEventListeners(): void {
|
2025-08-27 22:50:13 +02:00
|
|
|
// Handle drag start
|
|
|
|
|
eventBus.on('drag:start', (event) => {
|
|
|
|
|
const { originalElement, eventId, mouseOffset, column } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleDragStart(originalElement, eventId, mouseOffset, column);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle drag move
|
|
|
|
|
eventBus.on('drag:move', (event) => {
|
|
|
|
|
const { eventId, snappedY, column, mouseOffset } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleDragMove(eventId, snappedY, column, mouseOffset);
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-03 20:13:56 +02:00
|
|
|
// Handle drag auto-scroll (when dragging near edges triggers scroll)
|
|
|
|
|
eventBus.on('drag:auto-scroll', (event) => {
|
|
|
|
|
const { eventId, snappedY } = (event as CustomEvent).detail;
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
|
|
|
|
// Update position directly using the calculated snapped position
|
|
|
|
|
this.draggedClone.style.top = snappedY + 'px';
|
|
|
|
|
|
|
|
|
|
// Update timestamp display
|
|
|
|
|
this.updateCloneTimestamp(this.draggedClone, snappedY);
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
// Handle drag end
|
|
|
|
|
eventBus.on('drag:end', (event) => {
|
|
|
|
|
const { eventId, originalElement, finalColumn, finalY } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleDragEnd(eventId, originalElement, finalColumn, finalY);
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Handle click (when drag threshold not reached)
|
|
|
|
|
eventBus.on('event:click', (event) => {
|
|
|
|
|
const { eventId, originalElement } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleEventClick(eventId, originalElement);
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
// Handle column change
|
|
|
|
|
eventBus.on('drag:column-change', (event) => {
|
|
|
|
|
const { eventId, newColumn } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleColumnChange(eventId, newColumn);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Handle convert to all-day
|
|
|
|
|
eventBus.on('drag:convert-to-allday', (event) => {
|
|
|
|
|
const { eventId, targetDate, headerRenderer } = (event as CustomEvent).detail;
|
|
|
|
|
this.handleConvertToAllDay(eventId, targetDate, headerRenderer);
|
|
|
|
|
});
|
2025-09-01 23:37:47 +02:00
|
|
|
|
|
|
|
|
// Handle navigation period change (when slide animation completes)
|
2025-09-03 20:04:47 +02:00
|
|
|
eventBus.on(CoreEvents.NAVIGATION_COMPLETED, () => {
|
2025-09-01 23:37:47 +02:00
|
|
|
// Animate all-day height after navigation completes
|
2025-09-03 18:15:33 +02:00
|
|
|
this.triggerAllDayHeightAnimation();
|
2025-09-01 23:37:47 +02:00
|
|
|
});
|
2025-08-27 22:50:13 +02:00
|
|
|
}
|
2025-09-03 18:15:33 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Trigger all-day height animation without creating new renderer instance
|
|
|
|
|
*/
|
|
|
|
|
private triggerAllDayHeightAnimation(): void {
|
|
|
|
|
import('./HeaderRenderer').then(({ DateHeaderRenderer }) => {
|
|
|
|
|
const headerRenderer = new DateHeaderRenderer();
|
|
|
|
|
headerRenderer.checkAndAnimateAllDayHeight();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cleanup method for proper resource management
|
|
|
|
|
*/
|
|
|
|
|
public destroy(): void {
|
|
|
|
|
this.draggedClone = null;
|
|
|
|
|
this.originalEvent = null;
|
|
|
|
|
}
|
2025-08-31 23:48:34 +02:00
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
/**
|
|
|
|
|
* Get original event duration from data-duration attribute
|
|
|
|
|
*/
|
|
|
|
|
private getOriginalEventDuration(originalEvent: HTMLElement): number {
|
|
|
|
|
// Find the swp-event-time element with data-duration attribute
|
|
|
|
|
const timeElement = originalEvent.querySelector('swp-event-time');
|
|
|
|
|
if (timeElement) {
|
|
|
|
|
const duration = timeElement.getAttribute('data-duration');
|
|
|
|
|
if (duration) {
|
|
|
|
|
const durationMinutes = parseInt(duration);
|
|
|
|
|
return durationMinutes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback to 60 minutes if attribute not found
|
|
|
|
|
return 60;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a clone of an event for dragging
|
|
|
|
|
*/
|
|
|
|
|
private createEventClone(originalEvent: HTMLElement): HTMLElement {
|
|
|
|
|
const clone = originalEvent.cloneNode(true) as HTMLElement;
|
|
|
|
|
|
|
|
|
|
// Prefix ID with "clone-"
|
|
|
|
|
const originalId = originalEvent.dataset.eventId;
|
|
|
|
|
if (originalId) {
|
|
|
|
|
clone.dataset.eventId = `clone-${originalId}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get and cache original duration from data-duration attribute
|
|
|
|
|
const originalDurationMinutes = this.getOriginalEventDuration(originalEvent);
|
|
|
|
|
clone.dataset.originalDuration = originalDurationMinutes.toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Style for dragging
|
|
|
|
|
clone.style.position = 'absolute';
|
|
|
|
|
clone.style.zIndex = '999999';
|
|
|
|
|
clone.style.pointerEvents = 'none';
|
|
|
|
|
clone.style.opacity = '0.8';
|
|
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
// Dragged event skal have fuld kolonne bredde
|
|
|
|
|
clone.style.left = '2px';
|
|
|
|
|
clone.style.right = '2px';
|
|
|
|
|
clone.style.width = '';
|
|
|
|
|
clone.style.height = originalEvent.style.height || `${originalEvent.getBoundingClientRect().height}px`;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update clone timestamp based on new position
|
|
|
|
|
*/
|
|
|
|
|
private updateCloneTimestamp(clone: HTMLElement, snappedY: number): void {
|
2025-09-03 20:04:47 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
2025-08-27 22:50:13 +02:00
|
|
|
const hourHeight = gridSettings.hourHeight;
|
|
|
|
|
const dayStartHour = gridSettings.dayStartHour;
|
2025-09-03 20:48:23 +02:00
|
|
|
const snapInterval = gridSettings.snapInterval;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
2025-09-03 20:48:23 +02:00
|
|
|
// Calculate minutes from grid start (not from midnight)
|
|
|
|
|
const minutesFromGridStart = (snappedY / hourHeight) * 60;
|
|
|
|
|
|
|
|
|
|
// Add dayStartHour offset to get actual time
|
|
|
|
|
const actualStartMinutes = (dayStartHour * 60) + minutesFromGridStart;
|
|
|
|
|
|
|
|
|
|
// Snap to interval
|
|
|
|
|
const snappedStartMinutes = Math.round(actualStartMinutes / snapInterval) * snapInterval;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
// Use cached original duration (no recalculation)
|
|
|
|
|
const cachedDuration = parseInt(clone.dataset.originalDuration || '60');
|
2025-09-03 20:48:23 +02:00
|
|
|
const endTotalMinutes = snappedStartMinutes + cachedDuration;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
// Update display
|
|
|
|
|
const timeElement = clone.querySelector('swp-event-time');
|
|
|
|
|
if (timeElement) {
|
2025-09-03 20:48:23 +02:00
|
|
|
const newTimeText = `${this.formatTime(snappedStartMinutes)} - ${this.formatTime(endTotalMinutes)}`;
|
2025-08-27 22:50:13 +02:00
|
|
|
timeElement.textContent = newTimeText;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate event duration in minutes from element height
|
|
|
|
|
*/
|
|
|
|
|
private getEventDuration(element: HTMLElement): number {
|
2025-09-03 20:04:47 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
2025-08-27 22:50:13 +02:00
|
|
|
const hourHeight = gridSettings.hourHeight;
|
|
|
|
|
|
|
|
|
|
// Get height from style or computed
|
2025-09-09 14:35:21 +02:00
|
|
|
let heightPx = parseInt(element.style.height) || 0;
|
2025-08-27 22:50:13 +02:00
|
|
|
if (!heightPx) {
|
|
|
|
|
const rect = element.getBoundingClientRect();
|
|
|
|
|
heightPx = rect.height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Math.round((heightPx / hourHeight) * 60);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-03 18:15:33 +02:00
|
|
|
* Unified time formatting method - handles both total minutes and Date objects
|
2025-08-27 22:50:13 +02:00
|
|
|
*/
|
2025-09-03 18:15:33 +02:00
|
|
|
private formatTime(input: number | Date | string): string {
|
|
|
|
|
let hours: number, minutes: number;
|
|
|
|
|
|
|
|
|
|
if (typeof input === 'number') {
|
|
|
|
|
// Total minutes input
|
|
|
|
|
hours = Math.floor(input / 60) % 24;
|
|
|
|
|
minutes = input % 60;
|
|
|
|
|
} else {
|
|
|
|
|
// Date or ISO string input
|
|
|
|
|
const date = typeof input === 'string' ? new Date(input) : input;
|
|
|
|
|
hours = date.getHours();
|
|
|
|
|
minutes = date.getMinutes();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
const period = hours >= 12 ? 'PM' : 'AM';
|
2025-09-03 18:15:33 +02:00
|
|
|
const displayHours = hours > 12 ? hours - 12 : (hours === 0 ? 12 : hours);
|
2025-08-27 22:50:13 +02:00
|
|
|
return `${displayHours}:${minutes.toString().padStart(2, '0')} ${period}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle drag start event
|
|
|
|
|
*/
|
|
|
|
|
private handleDragStart(originalElement: HTMLElement, eventId: string, mouseOffset: any, column: string): void {
|
2025-09-04 23:35:19 +02:00
|
|
|
console.log('handleDragStart:', eventId);
|
2025-08-27 22:50:13 +02:00
|
|
|
this.originalEvent = originalElement;
|
|
|
|
|
|
2025-09-09 17:30:44 +02:00
|
|
|
// Remove stacking styling during drag will be handled by new system
|
2025-09-04 19:22:26 +02:00
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
// Create clone
|
|
|
|
|
this.draggedClone = this.createEventClone(originalElement);
|
|
|
|
|
|
2025-09-04 00:16:35 +02:00
|
|
|
// Add to current column's events layer (not directly to column)
|
2025-08-27 22:50:13 +02:00
|
|
|
const columnElement = document.querySelector(`swp-day-column[data-date="${column}"]`);
|
|
|
|
|
if (columnElement) {
|
2025-09-04 00:16:35 +02:00
|
|
|
const eventsLayer = columnElement.querySelector('swp-events-layer');
|
|
|
|
|
if (eventsLayer) {
|
|
|
|
|
eventsLayer.appendChild(this.draggedClone);
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback to column if events layer not found
|
|
|
|
|
columnElement.appendChild(this.draggedClone);
|
|
|
|
|
}
|
2025-08-27 22:50:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make original semi-transparent
|
|
|
|
|
originalElement.style.opacity = '0.3';
|
|
|
|
|
originalElement.style.userSelect = 'none';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle drag move event
|
|
|
|
|
*/
|
|
|
|
|
private handleDragMove(eventId: string, snappedY: number, column: string, mouseOffset: any): void {
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
|
|
|
|
// Update position
|
|
|
|
|
this.draggedClone.style.top = snappedY + 'px';
|
|
|
|
|
|
|
|
|
|
// Update timestamp display
|
|
|
|
|
this.updateCloneTimestamp(this.draggedClone, snappedY);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle column change during drag
|
|
|
|
|
*/
|
|
|
|
|
private handleColumnChange(eventId: string, newColumn: string): void {
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
2025-09-04 00:16:35 +02:00
|
|
|
// Move clone to new column's events layer
|
2025-08-27 22:50:13 +02:00
|
|
|
const newColumnElement = document.querySelector(`swp-day-column[data-date="${newColumn}"]`);
|
2025-09-04 00:16:35 +02:00
|
|
|
if (newColumnElement) {
|
|
|
|
|
const eventsLayer = newColumnElement.querySelector('swp-events-layer');
|
|
|
|
|
if (eventsLayer && this.draggedClone.parentElement !== eventsLayer) {
|
|
|
|
|
eventsLayer.appendChild(this.draggedClone);
|
|
|
|
|
} else if (!eventsLayer && this.draggedClone.parentElement !== newColumnElement) {
|
|
|
|
|
// Fallback to column if events layer not found
|
|
|
|
|
newColumnElement.appendChild(this.draggedClone);
|
|
|
|
|
}
|
2025-08-27 22:50:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle drag end event
|
|
|
|
|
*/
|
|
|
|
|
private handleDragEnd(eventId: string, originalElement: HTMLElement, finalColumn: string, finalY: number): void {
|
2025-09-04 23:35:19 +02:00
|
|
|
console.log('handleDragEnd:', eventId);
|
2025-08-27 23:56:38 +02:00
|
|
|
|
|
|
|
|
if (!this.draggedClone || !this.originalEvent) {
|
2025-09-04 23:35:19 +02:00
|
|
|
console.log('Missing draggedClone or originalEvent');
|
2025-08-27 23:56:38 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2025-08-27 22:50:13 +02:00
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
// Remove original event from any existing groups first
|
|
|
|
|
this.removeEventFromExistingGroups(this.originalEvent);
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
// Fade out original
|
|
|
|
|
this.fadeOutAndRemove(this.originalEvent);
|
|
|
|
|
|
2025-08-27 23:56:38 +02:00
|
|
|
// Remove clone prefix and normalize clone to be a regular event
|
2025-08-27 22:50:13 +02:00
|
|
|
const cloneId = this.draggedClone.dataset.eventId;
|
|
|
|
|
if (cloneId && cloneId.startsWith('clone-')) {
|
|
|
|
|
this.draggedClone.dataset.eventId = cloneId.replace('clone-', '');
|
|
|
|
|
}
|
2025-08-27 23:56:38 +02:00
|
|
|
|
|
|
|
|
// Fully normalize the clone to be a regular event
|
2025-08-27 22:50:13 +02:00
|
|
|
this.draggedClone.style.pointerEvents = '';
|
|
|
|
|
this.draggedClone.style.opacity = '';
|
2025-08-27 23:56:38 +02:00
|
|
|
this.draggedClone.style.userSelect = '';
|
2025-09-04 19:22:26 +02:00
|
|
|
// Behold z-index hvis det er et stacked event
|
2025-08-27 23:56:38 +02:00
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Detect overlaps with other events in the target column and reposition if needed
|
2025-09-09 17:30:44 +02:00
|
|
|
this.handleDragDropOverlaps(this.draggedClone, finalColumn);
|
2025-08-27 22:50:13 +02:00
|
|
|
// Clean up
|
|
|
|
|
this.draggedClone = null;
|
|
|
|
|
this.originalEvent = null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Handle event click (when drag threshold not reached)
|
2025-09-04 19:22:26 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private handleEventClick(eventId: string, originalElement: HTMLElement): void {
|
|
|
|
|
console.log('handleEventClick:', eventId);
|
2025-09-04 20:32:25 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Clean up any drag artifacts from failed drag attempt
|
|
|
|
|
if (this.draggedClone) {
|
|
|
|
|
this.draggedClone.remove();
|
|
|
|
|
this.draggedClone = null;
|
|
|
|
|
}
|
2025-09-04 20:32:25 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Restore original element styling if it was modified
|
|
|
|
|
if (this.originalEvent) {
|
|
|
|
|
this.originalEvent.style.opacity = '';
|
|
|
|
|
this.originalEvent.style.userSelect = '';
|
|
|
|
|
this.originalEvent = null;
|
2025-09-04 20:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Emit a clean click event for other components to handle
|
|
|
|
|
eventBus.emit('event:clicked', {
|
|
|
|
|
eventId: eventId,
|
|
|
|
|
element: originalElement
|
|
|
|
|
});
|
2025-09-04 20:32:25 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-04 23:35:19 +02:00
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Handle event double-click for text selection
|
2025-09-04 23:35:19 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private handleEventDoubleClick(eventElement: HTMLElement): void {
|
|
|
|
|
console.log('handleEventDoubleClick:', eventElement.dataset.eventId);
|
|
|
|
|
|
|
|
|
|
// Enable text selection temporarily
|
|
|
|
|
eventElement.classList.add('text-selectable');
|
|
|
|
|
|
|
|
|
|
// Auto-select the event text
|
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
if (selection) {
|
|
|
|
|
const range = document.createRange();
|
|
|
|
|
range.selectNodeContents(eventElement);
|
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
selection.addRange(range);
|
|
|
|
|
}
|
2025-09-04 23:35:19 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Remove text selection mode when clicking outside
|
|
|
|
|
const removeSelectable = (e: Event) => {
|
|
|
|
|
// Don't remove if clicking within the same event
|
|
|
|
|
if (e.target && eventElement.contains(e.target as Node)) {
|
|
|
|
|
return;
|
2025-09-04 23:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
eventElement.classList.remove('text-selectable');
|
|
|
|
|
document.removeEventListener('click', removeSelectable);
|
|
|
|
|
|
|
|
|
|
// Clear selection
|
|
|
|
|
if (selection) {
|
|
|
|
|
selection.removeAllRanges();
|
2025-09-04 23:35:19 +02:00
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add click outside listener after a short delay
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
document.addEventListener('click', removeSelectable);
|
|
|
|
|
}, 100);
|
2025-09-04 23:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 17:30:44 +02:00
|
|
|
/**
|
|
|
|
|
* Handle overlap detection and re-rendering after drag-drop
|
|
|
|
|
*/
|
|
|
|
|
private handleDragDropOverlaps(droppedElement: HTMLElement, targetColumn: string): void {
|
|
|
|
|
const targetColumnElement = document.querySelector(`swp-day-column[data-date="${targetColumn}"]`);
|
|
|
|
|
if (!targetColumnElement) return;
|
|
|
|
|
|
|
|
|
|
const eventsLayer = targetColumnElement.querySelector('swp-events-layer') as HTMLElement;
|
|
|
|
|
if (!eventsLayer) return;
|
|
|
|
|
|
|
|
|
|
// Convert dropped element to CalendarEvent with new position
|
|
|
|
|
const droppedEvent = this.elementToCalendarEventWithNewPosition(droppedElement, targetColumn);
|
|
|
|
|
if (!droppedEvent) return;
|
|
|
|
|
|
|
|
|
|
// Get existing events in the column (excluding the dropped element)
|
|
|
|
|
const existingEvents = this.getEventsInColumn(eventsLayer, droppedElement.dataset.eventId);
|
|
|
|
|
|
|
|
|
|
// Find overlaps with the dropped event
|
|
|
|
|
const overlappingEvents = this.overlapDetector.resolveOverlap(droppedEvent, existingEvents);
|
|
|
|
|
|
|
|
|
|
if (overlappingEvents.length > 0) {
|
|
|
|
|
// Remove only affected events from DOM
|
|
|
|
|
const affectedEventIds = [droppedEvent.id, ...overlappingEvents.map(e => e.id)];
|
|
|
|
|
eventsLayer.querySelectorAll('swp-event').forEach(el => {
|
|
|
|
|
const eventId = (el as HTMLElement).dataset.eventId;
|
|
|
|
|
if (eventId && affectedEventIds.includes(eventId)) {
|
|
|
|
|
el.remove();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Re-render affected events with overlap handling
|
|
|
|
|
const affectedEvents = [droppedEvent, ...overlappingEvents];
|
|
|
|
|
this.new_handleEventOverlaps(affectedEvents, eventsLayer);
|
|
|
|
|
}
|
|
|
|
|
// If no overlaps, the dropped element stays as is
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all events in a column as CalendarEvent objects
|
|
|
|
|
*/
|
|
|
|
|
private getEventsInColumn(eventsLayer: HTMLElement, excludeEventId?: string): CalendarEvent[] {
|
|
|
|
|
const eventElements = eventsLayer.querySelectorAll('swp-event');
|
|
|
|
|
const events: CalendarEvent[] = [];
|
|
|
|
|
|
|
|
|
|
eventElements.forEach(el => {
|
|
|
|
|
const element = el as HTMLElement;
|
|
|
|
|
const eventId = element.dataset.eventId;
|
|
|
|
|
|
|
|
|
|
// Skip the excluded event (e.g., the dropped event)
|
|
|
|
|
if (excludeEventId && eventId === excludeEventId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const event = this.elementToCalendarEvent(element);
|
|
|
|
|
if (event) {
|
|
|
|
|
events.push(event);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Remove event from any existing groups and cleanup empty containers
|
2025-09-09 17:30:44 +02:00
|
|
|
* In the new system, this is handled automatically by re-rendering overlaps
|
2025-09-04 19:22:26 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private removeEventFromExistingGroups(eventElement: HTMLElement): void {
|
2025-09-09 17:30:44 +02:00
|
|
|
// With the new system, overlap relationships are recalculated on drop
|
|
|
|
|
// No need to manually track and remove from groups
|
2025-09-04 19:22:26 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
/**
|
|
|
|
|
* Restore normal event styling (full column width)
|
|
|
|
|
*/
|
|
|
|
|
private restoreNormalEventStyling(eventElement: HTMLElement): void {
|
|
|
|
|
eventElement.style.position = 'absolute';
|
|
|
|
|
eventElement.style.left = '2px';
|
|
|
|
|
eventElement.style.right = '2px';
|
|
|
|
|
eventElement.style.width = '';
|
|
|
|
|
// Behold z-index for stacked events
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
/**
|
|
|
|
|
* Update element's dataset with new times after successful drop
|
|
|
|
|
*/
|
|
|
|
|
private updateElementDataset(element: HTMLElement, event: CalendarEvent): void {
|
2025-09-09 14:35:21 +02:00
|
|
|
element.dataset.start = event.start.toISOString();
|
|
|
|
|
element.dataset.end = event.end.toISOString();
|
2025-09-04 19:22:26 +02:00
|
|
|
|
|
|
|
|
// Update the time display
|
|
|
|
|
const timeElement = element.querySelector('swp-event-time');
|
|
|
|
|
if (timeElement) {
|
|
|
|
|
const startTime = this.formatTime(event.start);
|
|
|
|
|
const endTime = this.formatTime(event.end);
|
|
|
|
|
timeElement.textContent = `${startTime} - ${endTime}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert DOM element to CalendarEvent using its NEW position after drag
|
|
|
|
|
*/
|
|
|
|
|
private elementToCalendarEventWithNewPosition(element: HTMLElement, targetColumn: string): CalendarEvent | null {
|
|
|
|
|
const eventId = element.dataset.eventId;
|
|
|
|
|
const title = element.dataset.title;
|
|
|
|
|
const type = element.dataset.type;
|
|
|
|
|
const originalDuration = element.dataset.originalDuration;
|
|
|
|
|
|
|
|
|
|
if (!eventId || !title || !type) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate new start/end times based on current position
|
2025-09-09 14:35:21 +02:00
|
|
|
const currentTop = parseInt(element.style.top) || 0;
|
2025-09-04 19:22:26 +02:00
|
|
|
const durationMinutes = originalDuration ? parseInt(originalDuration) : 60;
|
|
|
|
|
|
|
|
|
|
// Convert position to time
|
|
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
|
|
|
|
const hourHeight = gridSettings.hourHeight;
|
|
|
|
|
const dayStartHour = gridSettings.dayStartHour;
|
|
|
|
|
|
|
|
|
|
// Calculate minutes from grid start
|
|
|
|
|
const minutesFromGridStart = (currentTop / hourHeight) * 60;
|
|
|
|
|
const actualStartMinutes = (dayStartHour * 60) + minutesFromGridStart;
|
|
|
|
|
const actualEndMinutes = actualStartMinutes + durationMinutes;
|
|
|
|
|
|
|
|
|
|
// Create ISO date strings for the target column
|
|
|
|
|
const targetDate = new Date(targetColumn + 'T00:00:00');
|
|
|
|
|
const startDate = new Date(targetDate);
|
|
|
|
|
startDate.setMinutes(startDate.getMinutes() + actualStartMinutes);
|
|
|
|
|
|
|
|
|
|
const endDate = new Date(targetDate);
|
|
|
|
|
endDate.setMinutes(endDate.getMinutes() + actualEndMinutes);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: eventId,
|
|
|
|
|
title: title,
|
2025-09-09 14:35:21 +02:00
|
|
|
start: startDate,
|
|
|
|
|
end: endDate,
|
2025-09-04 19:22:26 +02:00
|
|
|
type: type,
|
|
|
|
|
allDay: false,
|
|
|
|
|
syncStatus: 'synced',
|
|
|
|
|
metadata: {
|
|
|
|
|
duration: durationMinutes
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 23:35:19 +02:00
|
|
|
|
2025-09-04 19:22:26 +02:00
|
|
|
/**
|
|
|
|
|
* Convert DOM element to CalendarEvent for overlap detection
|
|
|
|
|
*/
|
|
|
|
|
private elementToCalendarEvent(element: HTMLElement): CalendarEvent | null {
|
|
|
|
|
const eventId = element.dataset.eventId;
|
|
|
|
|
const title = element.dataset.title;
|
|
|
|
|
const start = element.dataset.start;
|
|
|
|
|
const end = element.dataset.end;
|
|
|
|
|
const type = element.dataset.type;
|
|
|
|
|
const duration = element.dataset.duration;
|
|
|
|
|
|
|
|
|
|
if (!eventId || !title || !start || !end || !type) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: eventId,
|
|
|
|
|
title: title,
|
2025-09-09 14:35:21 +02:00
|
|
|
start: new Date(start),
|
|
|
|
|
end: new Date(end),
|
2025-09-04 19:22:26 +02:00
|
|
|
type: type,
|
|
|
|
|
allDay: false,
|
|
|
|
|
syncStatus: 'synced', // Default to synced for existing events
|
|
|
|
|
metadata: {
|
|
|
|
|
duration: duration ? parseInt(duration) : 60
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
/**
|
|
|
|
|
* Handle conversion to all-day event
|
|
|
|
|
*/
|
|
|
|
|
private handleConvertToAllDay(eventId: string, targetDate: string, headerRenderer: any): void {
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
|
|
|
|
// Only convert once
|
|
|
|
|
if (this.draggedClone.tagName === 'SWP-ALLDAY-EVENT') return;
|
|
|
|
|
|
|
|
|
|
// Transform clone to all-day format
|
|
|
|
|
this.transformCloneToAllDay(this.draggedClone, targetDate);
|
|
|
|
|
|
|
|
|
|
// Expand header if needed
|
|
|
|
|
headerRenderer.addToAllDay(this.draggedClone.parentElement);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Transform clone from timed to all-day event
|
|
|
|
|
*/
|
|
|
|
|
private transformCloneToAllDay(clone: HTMLElement, targetDate: string): void {
|
|
|
|
|
const calendarHeader = document.querySelector('swp-calendar-header');
|
|
|
|
|
if (!calendarHeader) return;
|
|
|
|
|
|
|
|
|
|
// Find all-day container
|
|
|
|
|
const allDayContainer = calendarHeader.querySelector('swp-allday-container');
|
|
|
|
|
if (!allDayContainer) return;
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
// Extract all original event data
|
2025-08-27 22:50:13 +02:00
|
|
|
const titleElement = clone.querySelector('swp-event-title');
|
|
|
|
|
const eventTitle = titleElement ? titleElement.textContent || 'Untitled' : 'Untitled';
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
const timeElement = clone.querySelector('swp-event-time');
|
|
|
|
|
const eventTime = timeElement ? timeElement.textContent || '' : '';
|
|
|
|
|
const eventDuration = timeElement ? timeElement.getAttribute('data-duration') || '' : '';
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
// Calculate column index
|
|
|
|
|
const dayHeaders = document.querySelectorAll('swp-day-header');
|
|
|
|
|
let columnIndex = 1;
|
|
|
|
|
dayHeaders.forEach((header, index) => {
|
|
|
|
|
if ((header as HTMLElement).dataset.date === targetDate) {
|
|
|
|
|
columnIndex = index + 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
// Create all-day event with standardized data attributes
|
2025-08-27 22:50:13 +02:00
|
|
|
const allDayEvent = document.createElement('swp-allday-event');
|
|
|
|
|
allDayEvent.dataset.eventId = clone.dataset.eventId || '';
|
2025-09-01 23:37:47 +02:00
|
|
|
allDayEvent.dataset.title = eventTitle;
|
|
|
|
|
allDayEvent.dataset.start = `${targetDate}T${eventTime.split(' - ')[0]}:00`;
|
|
|
|
|
allDayEvent.dataset.end = `${targetDate}T${eventTime.split(' - ')[1]}:00`;
|
2025-08-27 22:50:13 +02:00
|
|
|
allDayEvent.dataset.type = clone.dataset.type || 'work';
|
2025-09-01 23:37:47 +02:00
|
|
|
allDayEvent.dataset.duration = eventDuration;
|
2025-08-27 22:50:13 +02:00
|
|
|
allDayEvent.textContent = eventTitle;
|
|
|
|
|
|
|
|
|
|
// Position in grid
|
|
|
|
|
(allDayEvent as HTMLElement).style.gridColumn = columnIndex.toString();
|
2025-09-01 23:37:47 +02:00
|
|
|
// grid-row will be set by checkAndAnimateAllDayHeight() based on actual position
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
// Remove original clone
|
|
|
|
|
if (clone.parentElement) {
|
|
|
|
|
clone.parentElement.removeChild(clone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add to all-day container
|
|
|
|
|
allDayContainer.appendChild(allDayEvent);
|
|
|
|
|
|
|
|
|
|
// Update reference
|
|
|
|
|
this.draggedClone = allDayEvent;
|
2025-09-01 23:37:47 +02:00
|
|
|
|
|
|
|
|
// Check if height animation is needed
|
2025-09-03 18:15:33 +02:00
|
|
|
this.triggerAllDayHeightAnimation();
|
2025-08-27 22:50:13 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 23:37:47 +02:00
|
|
|
|
|
|
|
|
|
2025-08-27 22:50:13 +02:00
|
|
|
/**
|
|
|
|
|
* Fade out and remove element
|
|
|
|
|
*/
|
|
|
|
|
private fadeOutAndRemove(element: HTMLElement): void {
|
|
|
|
|
element.style.transition = 'opacity 0.3s ease-out';
|
|
|
|
|
element.style.opacity = '0';
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
element.remove();
|
|
|
|
|
}, 300);
|
2025-08-20 00:39:31 +02:00
|
|
|
}
|
2025-08-29 22:49:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert dragged clone to all-day event preview
|
|
|
|
|
*/
|
|
|
|
|
private convertToAllDayPreview(targetDate: string): void {
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
|
|
|
|
// Only convert once
|
|
|
|
|
if (this.draggedClone.tagName === 'SWP-ALLDAY-EVENT') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Transform clone to all-day format
|
|
|
|
|
this.transformCloneToAllDay(this.draggedClone, targetDate);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move all-day event to a new date container
|
|
|
|
|
*/
|
|
|
|
|
private moveAllDayToNewDate(targetDate: string): void {
|
|
|
|
|
if (!this.draggedClone) return;
|
|
|
|
|
|
|
|
|
|
const calendarHeader = document.querySelector('swp-calendar-header');
|
|
|
|
|
if (!calendarHeader) return;
|
|
|
|
|
|
|
|
|
|
// Find the all-day container
|
|
|
|
|
const allDayContainer = calendarHeader.querySelector('swp-allday-container');
|
|
|
|
|
if (!allDayContainer) return;
|
|
|
|
|
|
|
|
|
|
// Calculate new column index
|
|
|
|
|
const dayHeaders = document.querySelectorAll('swp-day-header');
|
|
|
|
|
let columnIndex = 1;
|
|
|
|
|
dayHeaders.forEach((header, index) => {
|
|
|
|
|
if ((header as HTMLElement).dataset.date === targetDate) {
|
|
|
|
|
columnIndex = index + 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Update grid column position
|
|
|
|
|
(this.draggedClone as HTMLElement).style.gridColumn = columnIndex.toString();
|
|
|
|
|
|
|
|
|
|
// Move to all-day container if not already there
|
|
|
|
|
if (this.draggedClone.parentElement !== allDayContainer) {
|
|
|
|
|
allDayContainer.appendChild(this.draggedClone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-03 20:04:47 +02:00
|
|
|
renderEvents(events: CalendarEvent[], container: HTMLElement): void {
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-12 00:31:02 +02:00
|
|
|
// NOTE: Removed clearEvents() to support sliding animation
|
|
|
|
|
// With sliding animation, multiple grid containers exist simultaneously
|
|
|
|
|
// clearEvents() would remove events from all containers, breaking the animation
|
|
|
|
|
// Events are now rendered directly into the new container without clearing
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-24 00:13:07 +02:00
|
|
|
// Separate all-day events from regular events
|
|
|
|
|
const allDayEvents = events.filter(event => event.allDay);
|
|
|
|
|
const regularEvents = events.filter(event => !event.allDay);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Always call renderAllDayEvents to ensure height is set correctly (even to 0)
|
2025-09-03 20:04:47 +02:00
|
|
|
this.renderAllDayEvents(allDayEvents, container);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-24 00:13:07 +02:00
|
|
|
// Find columns in the specific container for regular events
|
2025-08-16 00:51:12 +02:00
|
|
|
const columns = this.getColumns(container);
|
2025-08-13 23:05:58 +02:00
|
|
|
|
|
|
|
|
columns.forEach(column => {
|
2025-08-24 00:13:07 +02:00
|
|
|
const columnEvents = this.getEventsForColumn(column, regularEvents);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-13 23:05:58 +02:00
|
|
|
const eventsLayer = column.querySelector('swp-events-layer');
|
|
|
|
|
if (eventsLayer) {
|
2025-09-09 14:35:21 +02:00
|
|
|
// NY TILGANG: Kald vores nye overlap handling
|
|
|
|
|
this.new_handleEventOverlaps(columnEvents, eventsLayer as HTMLElement);
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
// Abstract methods that subclasses must implement
|
|
|
|
|
protected abstract getColumns(container: HTMLElement): HTMLElement[];
|
2025-08-13 23:05:58 +02:00
|
|
|
protected abstract getEventsForColumn(column: HTMLElement, events: CalendarEvent[]): CalendarEvent[];
|
|
|
|
|
|
2025-08-24 00:13:07 +02:00
|
|
|
/**
|
|
|
|
|
* Render all-day events in the header row 2
|
|
|
|
|
*/
|
2025-09-03 20:04:47 +02:00
|
|
|
protected renderAllDayEvents(allDayEvents: CalendarEvent[], container: HTMLElement): void {
|
2025-08-24 00:13:07 +02:00
|
|
|
|
|
|
|
|
// Find the calendar header
|
|
|
|
|
const calendarHeader = container.querySelector('swp-calendar-header');
|
|
|
|
|
if (!calendarHeader) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 23:48:34 +02:00
|
|
|
// Find the all-day container (should always exist now)
|
2025-08-26 00:05:42 +02:00
|
|
|
const allDayContainer = calendarHeader.querySelector('swp-allday-container') as HTMLElement;
|
|
|
|
|
if (!allDayContainer) {
|
2025-08-31 23:48:34 +02:00
|
|
|
console.warn('All-day container not found - this should not happen');
|
2025-08-26 00:05:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear existing events
|
|
|
|
|
allDayContainer.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
if (allDayEvents.length === 0) {
|
2025-08-31 23:48:34 +02:00
|
|
|
// No events - container exists but is empty and hidden
|
2025-08-26 00:05:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build date to column mapping
|
2025-08-24 00:13:07 +02:00
|
|
|
const dayHeaders = calendarHeader.querySelectorAll('swp-day-header');
|
|
|
|
|
const dateToColumnMap = new Map<string, number>();
|
|
|
|
|
|
|
|
|
|
dayHeaders.forEach((header, index) => {
|
|
|
|
|
const dateStr = (header as any).dataset.date;
|
|
|
|
|
if (dateStr) {
|
|
|
|
|
dateToColumnMap.set(dateStr, index + 1); // 1-based column index
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
// Calculate grid spans for all events
|
|
|
|
|
const eventSpans = allDayEvents.map(event => ({
|
|
|
|
|
event,
|
|
|
|
|
span: this.calculateEventGridSpan(event, dateToColumnMap)
|
|
|
|
|
})).filter(item => item.span.columnSpan > 0); // Remove events outside visible range
|
|
|
|
|
|
|
|
|
|
// Simple row assignment using overlap detection
|
|
|
|
|
const eventPlacements: Array<{ event: CalendarEvent, span: { startColumn: number, columnSpan: number }, row: number }> = [];
|
2025-08-24 00:13:07 +02:00
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
eventSpans.forEach(eventItem => {
|
|
|
|
|
let assignedRow = 1;
|
2025-08-24 00:13:07 +02:00
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
// Find first row where this event doesn't overlap with any existing event
|
|
|
|
|
while (true) {
|
|
|
|
|
const rowEvents = eventPlacements.filter(item => item.row === assignedRow);
|
2025-09-09 14:35:21 +02:00
|
|
|
const hasOverlap = rowEvents.some(rowEvent =>
|
|
|
|
|
this.spansOverlap(eventItem.span, rowEvent.span)
|
2025-08-26 00:05:42 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!hasOverlap) {
|
|
|
|
|
break; // Found available row
|
|
|
|
|
}
|
|
|
|
|
assignedRow++;
|
2025-08-24 00:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
eventPlacements.push({
|
|
|
|
|
event: eventItem.event,
|
|
|
|
|
span: eventItem.span,
|
|
|
|
|
row: assignedRow
|
|
|
|
|
});
|
2025-08-24 00:13:07 +02:00
|
|
|
});
|
|
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
// Get max row needed
|
|
|
|
|
const maxRow = Math.max(...eventPlacements.map(item => item.row), 1);
|
2025-08-25 22:05:57 +02:00
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
// Place events directly in the single container
|
|
|
|
|
eventPlacements.forEach(({ event, span, row }) => {
|
|
|
|
|
// Create the all-day event element
|
|
|
|
|
const allDayEvent = document.createElement('swp-allday-event');
|
|
|
|
|
allDayEvent.textContent = event.title;
|
2025-09-01 23:37:47 +02:00
|
|
|
|
|
|
|
|
// Set data attributes directly from CalendarEvent
|
|
|
|
|
allDayEvent.dataset.eventId = event.id;
|
|
|
|
|
allDayEvent.dataset.title = event.title;
|
2025-09-09 14:35:21 +02:00
|
|
|
allDayEvent.dataset.start = event.start.toISOString();
|
|
|
|
|
allDayEvent.dataset.end = event.end.toISOString();
|
2025-09-01 23:37:47 +02:00
|
|
|
allDayEvent.dataset.type = event.type;
|
|
|
|
|
allDayEvent.dataset.duration = event.metadata?.duration?.toString() || '60';
|
2025-08-26 00:05:42 +02:00
|
|
|
|
|
|
|
|
// Set grid position (column and row)
|
|
|
|
|
(allDayEvent as HTMLElement).style.gridColumn = span.columnSpan > 1
|
|
|
|
|
? `${span.startColumn} / span ${span.columnSpan}`
|
|
|
|
|
: `${span.startColumn}`;
|
|
|
|
|
(allDayEvent as HTMLElement).style.gridRow = row.toString();
|
|
|
|
|
|
|
|
|
|
// Use event metadata for color if available
|
|
|
|
|
if (event.metadata?.color) {
|
|
|
|
|
(allDayEvent as HTMLElement).style.backgroundColor = event.metadata.color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allDayContainer.appendChild(allDayEvent);
|
|
|
|
|
|
2025-08-24 00:13:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
protected renderEvent(event: CalendarEvent): HTMLElement {
|
2025-08-07 00:15:44 +02:00
|
|
|
const eventElement = document.createElement('swp-event');
|
|
|
|
|
eventElement.dataset.eventId = event.id;
|
2025-09-01 23:37:47 +02:00
|
|
|
eventElement.dataset.title = event.title;
|
2025-09-09 14:35:21 +02:00
|
|
|
eventElement.dataset.start = event.start.toISOString();
|
|
|
|
|
eventElement.dataset.end = event.end.toISOString();
|
2025-08-07 00:15:44 +02:00
|
|
|
eventElement.dataset.type = event.type;
|
2025-09-01 23:37:47 +02:00
|
|
|
eventElement.dataset.duration = event.metadata?.duration?.toString() || '60';
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
// Calculate position based on time
|
2025-09-03 20:04:47 +02:00
|
|
|
const position = this.calculateEventPosition(event);
|
2025-08-07 00:15:44 +02:00
|
|
|
eventElement.style.position = 'absolute';
|
|
|
|
|
eventElement.style.top = `${position.top + 1}px`;
|
2025-08-22 22:57:35 +02:00
|
|
|
eventElement.style.height = `${position.height - 3}px`; //adjusted so bottom does not cover horizontal time lines.
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-08-21 22:09:15 +02:00
|
|
|
// Color is now handled by CSS classes based on data-type attribute
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-09-03 18:15:33 +02:00
|
|
|
// Format time for display using unified method
|
|
|
|
|
const startTime = this.formatTime(event.start);
|
|
|
|
|
const endTime = this.formatTime(event.end);
|
2025-08-27 22:50:13 +02:00
|
|
|
|
|
|
|
|
// Calculate duration in minutes
|
2025-09-09 14:35:21 +02:00
|
|
|
const durationMinutes = (event.end.getTime() - event.start.getTime()) / (1000 * 60);
|
2025-08-07 00:15:44 +02:00
|
|
|
|
|
|
|
|
// Create event content
|
|
|
|
|
eventElement.innerHTML = `
|
2025-08-27 22:50:13 +02:00
|
|
|
<swp-event-time data-duration="${durationMinutes}">${startTime} - ${endTime}</swp-event-time>
|
2025-08-07 00:15:44 +02:00
|
|
|
<swp-event-title>${event.title}</swp-event-title>
|
|
|
|
|
`;
|
2025-08-27 22:50:13 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Setup resize handles on first mouseover only
|
|
|
|
|
eventElement.addEventListener('mouseover', () => {
|
|
|
|
|
if (eventElement.dataset.hasResizeHandlers !== 'true') {
|
|
|
|
|
this.setupDynamicResizeHandles(eventElement);
|
|
|
|
|
eventElement.dataset.hasResizeHandlers = 'true';
|
|
|
|
|
}
|
|
|
|
|
}, { once: true });
|
|
|
|
|
|
|
|
|
|
// Setup double-click for text selection
|
|
|
|
|
eventElement.addEventListener('dblclick', (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
this.handleEventDoubleClick(eventElement);
|
|
|
|
|
});
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
return eventElement;
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-03 20:04:47 +02:00
|
|
|
protected calculateEventPosition(event: CalendarEvent): { top: number; height: number } {
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-09-03 20:04:47 +02:00
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
2025-08-09 01:16:04 +02:00
|
|
|
const dayStartHour = gridSettings.dayStartHour;
|
|
|
|
|
const hourHeight = gridSettings.hourHeight;
|
2025-08-07 00:15:44 +02:00
|
|
|
|
2025-09-03 20:48:23 +02:00
|
|
|
// Calculate minutes from midnight
|
2025-09-09 14:35:21 +02:00
|
|
|
const startMinutes = event.start.getHours() * 60 + event.start.getMinutes();
|
|
|
|
|
const endMinutes = event.end.getHours() * 60 + event.end.getMinutes();
|
2025-08-07 00:15:44 +02:00
|
|
|
const dayStartMinutes = dayStartHour * 60;
|
|
|
|
|
|
2025-09-03 20:48:23 +02:00
|
|
|
// Calculate top position relative to visible grid start
|
|
|
|
|
// If dayStartHour=6 and event starts at 09:00 (540 min), then:
|
|
|
|
|
// top = ((540 - 360) / 60) * hourHeight = 3 * hourHeight (3 hours from grid start)
|
2025-08-07 00:15:44 +02:00
|
|
|
const top = ((startMinutes - dayStartMinutes) / 60) * hourHeight;
|
|
|
|
|
|
2025-09-03 20:48:23 +02:00
|
|
|
// Calculate height based on event duration
|
2025-08-07 00:15:44 +02:00
|
|
|
const durationMinutes = endMinutes - startMinutes;
|
|
|
|
|
const height = (durationMinutes / 60) * hourHeight;
|
|
|
|
|
|
|
|
|
|
return { top, height };
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 00:05:42 +02:00
|
|
|
/**
|
|
|
|
|
* Calculate grid column span for event
|
|
|
|
|
*/
|
|
|
|
|
private calculateEventGridSpan(event: CalendarEvent, dateToColumnMap: Map<string, number>): { startColumn: number, columnSpan: number } {
|
2025-09-09 14:35:21 +02:00
|
|
|
const startDateKey = DateCalculator.formatISODate(event.start);
|
2025-08-26 00:05:42 +02:00
|
|
|
const startColumn = dateToColumnMap.get(startDateKey);
|
|
|
|
|
|
|
|
|
|
if (!startColumn) {
|
|
|
|
|
return { startColumn: 0, columnSpan: 0 }; // Event outside visible range
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate span by checking each day
|
|
|
|
|
let endColumn = startColumn;
|
2025-09-09 14:35:21 +02:00
|
|
|
const currentDate = new Date(event.start);
|
2025-08-26 00:05:42 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
while (currentDate <= event.end) {
|
2025-08-26 00:05:42 +02:00
|
|
|
currentDate.setDate(currentDate.getDate() + 1);
|
2025-09-03 18:38:52 +02:00
|
|
|
const dateKey = DateCalculator.formatISODate(currentDate);
|
2025-08-26 00:05:42 +02:00
|
|
|
const col = dateToColumnMap.get(dateKey);
|
|
|
|
|
if (col) {
|
|
|
|
|
endColumn = col;
|
|
|
|
|
} else {
|
|
|
|
|
break; // Event extends beyond visible range
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnSpan = endColumn - startColumn + 1;
|
|
|
|
|
return { startColumn, columnSpan };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Check if two column spans overlap (for all-day events)
|
2025-08-26 00:05:42 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private spansOverlap(event1Span: { startColumn: number, columnSpan: number }, event2Span: { startColumn: number, columnSpan: number }): boolean {
|
2025-08-26 00:05:42 +02:00
|
|
|
const event1End = event1Span.startColumn + event1Span.columnSpan - 1;
|
|
|
|
|
const event2End = event2Span.startColumn + event2Span.columnSpan - 1;
|
|
|
|
|
|
|
|
|
|
return !(event1End < event2Span.startColumn || event2End < event1Span.startColumn);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 20:32:25 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
|
2025-09-04 00:16:35 +02:00
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Setup dynamic resize handles that are only created when needed
|
2025-09-04 00:16:35 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private setupDynamicResizeHandles(eventElement: HTMLElement): void {
|
|
|
|
|
let topHandle: HTMLElement | null = null;
|
|
|
|
|
let bottomHandle: HTMLElement | null = null;
|
|
|
|
|
|
|
|
|
|
console.log('Setting up dynamic resize handles for event:', eventElement.dataset.eventId);
|
|
|
|
|
|
|
|
|
|
// Create handles on mouse enter
|
|
|
|
|
eventElement.addEventListener('mouseenter', () => {
|
|
|
|
|
console.log('Mouse ENTER event:', eventElement.dataset.eventId);
|
|
|
|
|
// Only create if they don't already exist
|
|
|
|
|
if (!topHandle || !bottomHandle) {
|
|
|
|
|
topHandle = document.createElement('swp-resize-handle');
|
|
|
|
|
topHandle.setAttribute('data-position', 'top');
|
|
|
|
|
topHandle.style.opacity = '0';
|
|
|
|
|
|
|
|
|
|
bottomHandle = document.createElement('swp-resize-handle');
|
|
|
|
|
bottomHandle.setAttribute('data-position', 'bottom');
|
|
|
|
|
bottomHandle.style.opacity = '0';
|
|
|
|
|
|
|
|
|
|
// Add mousedown listeners for resize functionality
|
|
|
|
|
topHandle.addEventListener('mousedown', (e: MouseEvent) => {
|
|
|
|
|
e.stopPropagation(); // Forhindre normal drag
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.startResize(eventElement, 'top', e);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bottomHandle.addEventListener('mousedown', (e: MouseEvent) => {
|
|
|
|
|
e.stopPropagation(); // Forhindre normal drag
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.startResize(eventElement, 'bottom', e);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Insert handles at beginning and end
|
|
|
|
|
eventElement.insertBefore(topHandle, eventElement.firstChild);
|
|
|
|
|
eventElement.appendChild(bottomHandle);
|
|
|
|
|
console.log('Created resize handles for event:', eventElement.dataset.eventId);
|
|
|
|
|
}
|
2025-09-04 00:16:35 +02:00
|
|
|
});
|
|
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Mouse move handler for smart visibility
|
|
|
|
|
eventElement.addEventListener('mousemove', (e: MouseEvent) => {
|
|
|
|
|
if (!topHandle || !bottomHandle) return;
|
|
|
|
|
|
|
|
|
|
const rect = eventElement.getBoundingClientRect();
|
|
|
|
|
const y = e.clientY - rect.top;
|
|
|
|
|
const height = rect.height;
|
|
|
|
|
|
|
|
|
|
// Show top handle if mouse is in top 12px
|
|
|
|
|
if (y <= 12) {
|
|
|
|
|
topHandle.style.opacity = '1';
|
|
|
|
|
bottomHandle.style.opacity = '0';
|
|
|
|
|
}
|
|
|
|
|
// Show bottom handle if mouse is in bottom 12px
|
|
|
|
|
else if (y >= height - 12) {
|
|
|
|
|
topHandle.style.opacity = '0';
|
|
|
|
|
bottomHandle.style.opacity = '1';
|
|
|
|
|
}
|
|
|
|
|
// Hide both if mouse is in middle
|
|
|
|
|
else {
|
|
|
|
|
topHandle.style.opacity = '0';
|
|
|
|
|
bottomHandle.style.opacity = '0';
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Hide handles when mouse leaves event (men kun hvis ikke i resize mode)
|
|
|
|
|
eventElement.addEventListener('mouseleave', () => {
|
|
|
|
|
console.log('Mouse LEAVE event:', eventElement.dataset.eventId);
|
|
|
|
|
if (!this.resizeState && topHandle && bottomHandle) {
|
|
|
|
|
topHandle.style.opacity = '0';
|
|
|
|
|
bottomHandle.style.opacity = '0';
|
|
|
|
|
console.log('Hidden resize handles for event:', eventElement.dataset.eventId);
|
|
|
|
|
}
|
2025-09-04 00:16:35 +02:00
|
|
|
});
|
|
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start resize operation
|
|
|
|
|
*/
|
|
|
|
|
private startResize(eventElement: HTMLElement, handle: 'top' | 'bottom', e: MouseEvent): void {
|
|
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
|
|
|
|
const minHeightPx = (this.MIN_EVENT_DURATION_MINUTES / 60) * gridSettings.hourHeight;
|
|
|
|
|
|
|
|
|
|
this.resizeState = {
|
|
|
|
|
element: eventElement,
|
|
|
|
|
handle: handle,
|
|
|
|
|
startY: e.clientY,
|
|
|
|
|
originalTop: parseFloat(eventElement.style.top),
|
|
|
|
|
originalHeight: parseFloat(eventElement.style.height),
|
|
|
|
|
originalStartTime: new Date(eventElement.dataset.start || ''),
|
|
|
|
|
originalEndTime: new Date(eventElement.dataset.end || ''),
|
|
|
|
|
minHeightPx: minHeightPx
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Global listeners for resize
|
|
|
|
|
document.addEventListener('mousemove', this.handleResize);
|
|
|
|
|
document.addEventListener('mouseup', this.endResize);
|
|
|
|
|
|
|
|
|
|
// Add resize cursor to body
|
|
|
|
|
document.body.style.cursor = handle === 'top' ? 'n-resize' : 's-resize';
|
|
|
|
|
|
|
|
|
|
console.log('Starting resize:', handle, 'element:', eventElement.dataset.eventId);
|
|
|
|
|
}
|
2025-09-04 00:16:35 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Handle resize drag
|
2025-09-04 00:16:35 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private handleResize = (e: MouseEvent): void => {
|
|
|
|
|
if (!this.resizeState) return;
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
const deltaY = e.clientY - this.resizeState.startY;
|
|
|
|
|
const snappedDelta = this.snapToGrid(deltaY);
|
|
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
if (this.resizeState.handle === 'top') {
|
|
|
|
|
// Resize fra toppen
|
|
|
|
|
const newTop = this.resizeState.originalTop + snappedDelta;
|
|
|
|
|
const newHeight = this.resizeState.originalHeight - snappedDelta;
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Check minimum højde
|
|
|
|
|
if (newHeight >= this.resizeState.minHeightPx && newTop >= 0) {
|
|
|
|
|
this.resizeState.element.style.top = newTop + 'px';
|
|
|
|
|
this.resizeState.element.style.height = newHeight + 'px';
|
|
|
|
|
|
|
|
|
|
// Opdater tidspunkter
|
|
|
|
|
const minutesDelta = (snappedDelta / gridSettings.hourHeight) * 60;
|
|
|
|
|
const newStartTime = this.addMinutes(this.resizeState.originalStartTime, minutesDelta);
|
|
|
|
|
this.updateEventDisplay(this.resizeState.element, newStartTime, this.resizeState.originalEndTime);
|
2025-09-04 00:16:35 +02:00
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
} else {
|
|
|
|
|
// Resize fra bunden
|
|
|
|
|
const newHeight = this.resizeState.originalHeight + snappedDelta;
|
|
|
|
|
|
|
|
|
|
// Check minimum højde
|
|
|
|
|
if (newHeight >= this.resizeState.minHeightPx) {
|
|
|
|
|
this.resizeState.element.style.height = newHeight + 'px';
|
|
|
|
|
|
|
|
|
|
// Opdater tidspunkter
|
|
|
|
|
const minutesDelta = (snappedDelta / gridSettings.hourHeight) * 60;
|
|
|
|
|
const newEndTime = this.addMinutes(this.resizeState.originalEndTime, minutesDelta);
|
|
|
|
|
this.updateEventDisplay(this.resizeState.element, this.resizeState.originalStartTime, newEndTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End resize operation
|
|
|
|
|
*/
|
|
|
|
|
private endResize = (): void => {
|
|
|
|
|
if (!this.resizeState) return;
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Få finale tider fra element
|
|
|
|
|
const finalStart = this.resizeState.element.dataset.start;
|
|
|
|
|
const finalEnd = this.resizeState.element.dataset.end;
|
|
|
|
|
|
|
|
|
|
console.log('Ending resize:', this.resizeState.element.dataset.eventId, 'New times:', finalStart, finalEnd);
|
|
|
|
|
|
|
|
|
|
// Emit event med nye tider
|
|
|
|
|
eventBus.emit('event:resized', {
|
|
|
|
|
eventId: this.resizeState.element.dataset.eventId,
|
|
|
|
|
newStart: finalStart,
|
|
|
|
|
newEnd: finalEnd
|
2025-09-04 00:16:35 +02:00
|
|
|
});
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
document.removeEventListener('mousemove', this.handleResize);
|
|
|
|
|
document.removeEventListener('mouseup', this.endResize);
|
|
|
|
|
document.body.style.cursor = '';
|
|
|
|
|
this.resizeState = null;
|
2025-09-04 00:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Snap delta to grid intervals
|
2025-09-04 00:16:35 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private snapToGrid(deltaY: number): number {
|
|
|
|
|
const gridSettings = calendarConfig.getGridSettings();
|
|
|
|
|
const snapInterval = gridSettings.snapInterval;
|
|
|
|
|
const hourHeight = gridSettings.hourHeight;
|
|
|
|
|
const snapDistancePx = (snapInterval / 60) * hourHeight;
|
|
|
|
|
return Math.round(deltaY / snapDistancePx) * snapDistancePx;
|
|
|
|
|
}
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
/**
|
|
|
|
|
* Update event display during resize
|
|
|
|
|
*/
|
|
|
|
|
private updateEventDisplay(element: HTMLElement, startTime: Date, endTime: Date): void {
|
|
|
|
|
// Beregn ny duration i minutter
|
|
|
|
|
const durationMinutes = (endTime.getTime() - startTime.getTime()) / (1000 * 60);
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Opdater dataset
|
|
|
|
|
element.dataset.start = startTime.toISOString();
|
|
|
|
|
element.dataset.end = endTime.toISOString();
|
|
|
|
|
element.dataset.duration = durationMinutes.toString();
|
2025-09-04 00:16:35 +02:00
|
|
|
|
2025-09-09 14:35:21 +02:00
|
|
|
// Opdater visual tid
|
|
|
|
|
const timeElement = element.querySelector('swp-event-time');
|
|
|
|
|
if (timeElement) {
|
|
|
|
|
const startStr = this.formatTime(startTime.toISOString());
|
|
|
|
|
const endStr = this.formatTime(endTime.toISOString());
|
|
|
|
|
timeElement.textContent = `${startStr} - ${endStr}`;
|
|
|
|
|
|
|
|
|
|
// Opdater også data-duration attribut på time elementet
|
|
|
|
|
timeElement.setAttribute('data-duration', durationMinutes.toString());
|
|
|
|
|
}
|
2025-09-04 00:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-09 14:35:21 +02:00
|
|
|
* Add minutes to a date
|
2025-09-04 00:16:35 +02:00
|
|
|
*/
|
2025-09-09 14:35:21 +02:00
|
|
|
private addMinutes(date: Date, minutes: number): Date {
|
|
|
|
|
return new Date(date.getTime() + minutes * 60000);
|
2025-09-04 00:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
clearEvents(container?: HTMLElement): void {
|
2025-09-04 19:22:26 +02:00
|
|
|
const selector = 'swp-event, swp-event-group';
|
2025-09-04 00:16:35 +02:00
|
|
|
const existingEvents = container
|
2025-08-16 00:51:12 +02:00
|
|
|
? container.querySelectorAll(selector)
|
|
|
|
|
: document.querySelectorAll(selector);
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
existingEvents.forEach(event => event.remove());
|
|
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renderer overlappende events baseret på OverlapResult
|
|
|
|
|
* @param result - OverlapResult med events og stack links
|
|
|
|
|
* @param container - Container at rendere i
|
|
|
|
|
*/
|
|
|
|
|
protected new_renderOverlappingEvents(result: OverlapResult, container: HTMLElement): void {
|
|
|
|
|
// Iterate direkte gennem stackLinks - allerede sorteret fra decorateWithStackLinks
|
|
|
|
|
for (const [eventId, stackLink] of result.stackLinks.entries()) {
|
|
|
|
|
const event = result.overlappingEvents.find(e => e.id === eventId);
|
|
|
|
|
if (!event) continue;
|
|
|
|
|
|
|
|
|
|
const element = this.renderEvent(event);
|
|
|
|
|
|
|
|
|
|
// Check om dette event deler kolonne med foregående (samme start tid)
|
|
|
|
|
if (stackLink.prev) {
|
|
|
|
|
const prevEvent = result.overlappingEvents.find(e => e.id === stackLink.prev);
|
|
|
|
|
if (prevEvent && prevEvent.start.getTime() === event.start.getTime()) {
|
|
|
|
|
// Samme start tid - del kolonne (side by side)
|
|
|
|
|
this.new_applyColumnSharingStyling([element]);
|
|
|
|
|
} else {
|
|
|
|
|
// Forskellige start tider - stack vertikalt
|
|
|
|
|
this.new_applyStackStyling(element, stackLink.stackLevel);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Første event i stack
|
|
|
|
|
this.new_applyStackStyling(element, stackLink.stackLevel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container.appendChild(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Applicerer stack styling (margin-left og z-index)
|
|
|
|
|
* @param element - Event element
|
|
|
|
|
* @param stackLevel - Stack niveau
|
|
|
|
|
*/
|
|
|
|
|
protected new_applyStackStyling(element: HTMLElement, stackLevel: number): void {
|
|
|
|
|
element.style.marginLeft = `${stackLevel * 15}px`;
|
|
|
|
|
element.style.zIndex = `${100 + stackLevel}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Applicerer column sharing styling (flexbox)
|
|
|
|
|
* @param elements - Event elements der skal dele plads
|
|
|
|
|
*/
|
|
|
|
|
protected new_applyColumnSharingStyling(elements: HTMLElement[]): void {
|
|
|
|
|
elements.forEach(element => {
|
|
|
|
|
element.style.flex = '1';
|
|
|
|
|
element.style.minWidth = '50px';
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Date-based event renderer
|
|
|
|
|
*/
|
|
|
|
|
export class DateEventRenderer extends BaseEventRenderer {
|
2025-09-03 20:04:47 +02:00
|
|
|
constructor(dateCalculator?: DateCalculator) {
|
|
|
|
|
super(dateCalculator);
|
2025-08-27 23:56:38 +02:00
|
|
|
this.setupDragEventListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 00:51:12 +02:00
|
|
|
protected getColumns(container: HTMLElement): HTMLElement[] {
|
|
|
|
|
const columns = container.querySelectorAll('swp-day-column');
|
2025-08-13 23:05:58 +02:00
|
|
|
return Array.from(columns) as HTMLElement[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getEventsForColumn(column: HTMLElement, events: CalendarEvent[]): CalendarEvent[] {
|
|
|
|
|
const columnDate = column.dataset.date;
|
2025-08-20 00:39:31 +02:00
|
|
|
if (!columnDate) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2025-08-13 23:05:58 +02:00
|
|
|
|
|
|
|
|
const columnEvents = events.filter(event => {
|
2025-09-09 14:35:21 +02:00
|
|
|
const eventDateStr = DateCalculator.formatISODate(event.start);
|
2025-08-20 00:39:31 +02:00
|
|
|
const matches = eventDateStr === columnDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return matches;
|
2025-08-13 23:05:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return columnEvents;
|
|
|
|
|
}
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resource-based event renderer
|
|
|
|
|
*/
|
|
|
|
|
export class ResourceEventRenderer extends BaseEventRenderer {
|
2025-08-16 00:51:12 +02:00
|
|
|
protected getColumns(container: HTMLElement): HTMLElement[] {
|
|
|
|
|
const columns = container.querySelectorAll('swp-resource-column');
|
2025-08-13 23:05:58 +02:00
|
|
|
return Array.from(columns) as HTMLElement[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getEventsForColumn(column: HTMLElement, events: CalendarEvent[]): CalendarEvent[] {
|
|
|
|
|
const resourceName = column.dataset.resource;
|
|
|
|
|
if (!resourceName) return [];
|
|
|
|
|
|
|
|
|
|
const columnEvents = events.filter(event => {
|
|
|
|
|
return event.resource?.name === resourceName;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return columnEvents;
|
|
|
|
|
}
|
2025-09-09 14:35:21 +02:00
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// NEW OVERLAP DETECTION SYSTEM
|
|
|
|
|
// All new functions prefixed with new_
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
protected overlapDetector = new OverlapDetector();
|
|
|
|
|
|
2025-08-07 00:15:44 +02:00
|
|
|
}
|