Refactors event extraction to utility function
Moves event extraction logic to a shared utility function for reusability. Removes redundant code and improves consistency in event handling. The original duration is now mandatory.
This commit is contained in:
parent
0d33b51ff8
commit
8b5420f367
2 changed files with 12 additions and 76 deletions
|
|
@ -136,25 +136,17 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
// Snap to interval
|
||||
const snappedStartMinutes = Math.round(actualStartMinutes / snapInterval) * snapInterval;
|
||||
|
||||
// Use cached original duration (no recalculation)
|
||||
const cachedDuration = parseInt(clone.dataset.originalDuration || '60');
|
||||
const endTotalMinutes = snappedStartMinutes + cachedDuration;
|
||||
|
||||
if(!clone.dataset.originalDuration)
|
||||
throw new DOMException("missing clone.dataset.originalDuration")
|
||||
|
||||
// Update dataset with reference date for performance
|
||||
const referenceDate = new Date('1970-01-01T00:00:00');
|
||||
const startDate = new Date(referenceDate);
|
||||
startDate.setMinutes(startDate.getMinutes() + snappedStartMinutes);
|
||||
const endTotalMinutes = snappedStartMinutes + parseInt(clone.dataset.originalDuration);
|
||||
|
||||
const endDate = new Date(referenceDate);
|
||||
endDate.setMinutes(endDate.getMinutes() + endTotalMinutes);
|
||||
|
||||
clone.dataset.start = startDate.toISOString();
|
||||
clone.dataset.end = endDate.toISOString();
|
||||
// Update display
|
||||
// Update visual time display only
|
||||
const timeElement = clone.querySelector('swp-event-time');
|
||||
if (timeElement) {
|
||||
const startTime = TimeFormatter.formatTimeFromMinutes(snappedStartMinutes);
|
||||
const endTime = TimeFormatter.formatTimeFromMinutes(endTotalMinutes);
|
||||
let startTime = TimeFormatter.formatTimeFromMinutes(snappedStartMinutes);
|
||||
let endTime = TimeFormatter.formatTimeFromMinutes(endTotalMinutes);
|
||||
timeElement.textContent = `${startTime} - ${endTime}`;
|
||||
}
|
||||
}
|
||||
|
|
@ -289,7 +281,7 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
container = element.closest('swp-events-layer') as HTMLElement;
|
||||
}
|
||||
|
||||
const event = this.elementToCalendarEvent(element);
|
||||
const event = SwpEventElement.extractCalendarEventFromElement(element);
|
||||
if (event) {
|
||||
stackEvents.push(event);
|
||||
}
|
||||
|
|
@ -326,7 +318,7 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
|
||||
// Update dataset with new times after successful drop (only for timed events)
|
||||
if (draggedClone.dataset.displayType !== 'allday') {
|
||||
const newEvent = this.elementToCalendarEvent(draggedClone);
|
||||
const newEvent = SwpEventElement.extractCalendarEventFromElement(draggedClone);
|
||||
if (newEvent) {
|
||||
draggedClone.dataset.start = newEvent.start.toISOString();
|
||||
draggedClone.dataset.end = newEvent.end.toISOString();
|
||||
|
|
@ -390,7 +382,7 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
if (!eventsLayer) return;
|
||||
|
||||
// Convert dropped element to CalendarEvent with new position
|
||||
const droppedEvent = this.elementToCalendarEvent(droppedElement);
|
||||
const droppedEvent = SwpEventElement.extractCalendarEventFromElement(droppedElement);
|
||||
if (!droppedEvent) return;
|
||||
|
||||
// Get existing events in the column (excluding the dropped element)
|
||||
|
|
@ -434,7 +426,7 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
return;
|
||||
}
|
||||
|
||||
const event = this.elementToCalendarEvent(element);
|
||||
const event = SwpEventElement.extractCalendarEventFromElement(element);
|
||||
if (event) {
|
||||
events.push(event);
|
||||
}
|
||||
|
|
@ -452,62 +444,6 @@ export abstract class BaseEventRenderer implements EventRendererStrategy {
|
|||
// No need to manually track and remove from groups
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DOM element to CalendarEvent - handles both normal and 1970 reference dates
|
||||
*/
|
||||
private elementToCalendarEvent(element: HTMLElement): CalendarEvent | null {
|
||||
const eventId = element.dataset.eventId;
|
||||
const title = element.dataset.title;
|
||||
const type = element.dataset.type;
|
||||
const start = element.dataset.start;
|
||||
const end = element.dataset.end;
|
||||
|
||||
if (!eventId || !title || !type || !start || !end) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let startDate = new Date(start);
|
||||
let endDate = new Date(end);
|
||||
|
||||
// Check if we have 1970 reference date (from drag operations)
|
||||
if (startDate.getFullYear() === 1970) {
|
||||
// Find the parent column to get the actual date
|
||||
const columnElement = element.closest('swp-day-column') as HTMLElement;
|
||||
if (columnElement && columnElement.dataset.date) {
|
||||
const columnDate = new Date(columnElement.dataset.date);
|
||||
|
||||
// Keep the time portion from the 1970 dates, but use the column's date
|
||||
startDate = new Date(
|
||||
columnDate.getFullYear(),
|
||||
columnDate.getMonth(),
|
||||
columnDate.getDate(),
|
||||
startDate.getHours(),
|
||||
startDate.getMinutes()
|
||||
);
|
||||
|
||||
endDate = new Date(
|
||||
columnDate.getFullYear(),
|
||||
columnDate.getMonth(),
|
||||
columnDate.getDate(),
|
||||
endDate.getHours(),
|
||||
endDate.getMinutes()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: eventId,
|
||||
title: title,
|
||||
start: startDate,
|
||||
end: endDate,
|
||||
type: type,
|
||||
allDay: false,
|
||||
syncStatus: 'synced',
|
||||
metadata: {
|
||||
duration: element.dataset.duration
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle conversion to all-day event
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue