Files
petriandClaude Sonnet 5.5 3faa4cfbc6 Open only the info popover when a calendar bar is tapped
On touch screens a tap on a task bar both showed its info popover and
opened the schedule dialog. A tap now only opens the popover, which stays
open until something else is tapped, and the popover's own "Open schedule"
button opens the dialog. Mouse clicks and Enter still open the dialog
directly. The popover is no longer a tooltip since it holds a button, and
the bar's aria-describedby points only at the details.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-26 09:35:53 +02:00

310 lines
8.8 KiB
JavaScript

/**
* Drag and drop for the task calendar (jobs::calendar).
*
* A bar is dragged by its body (move in time or to another person's row)
* or by its edges (change the start or the end). Unscheduled tasks are
* dropped on a day to place them. The day and the person come from the
* grid cell under the pointer (data-calendar-day / data-calendar-user).
* Dragging is only a shortcut: every bar is a button whose dialog makes
* the same changes with the keyboard. Escape cancels a drag.
*
* Hovering or focusing a bar shows its info popover (the job's basic
* details); the pointer may move onto it, and Escape closes it. A mouse
* click or Enter on a bar opens its dialog. On a touch screen a tap only
* opens the popover (a tap would otherwise both show it and open the
* dialog), and the popover's own button opens the dialog.
*/
const DRAG_THRESHOLD_PX = 5;
const DAY_MS = 24 * 60 * 60 * 1000;
function toDate(value) {
const [year, month, day] = value.split('-').map(Number);
return Date.UTC(year, month - 1, day);
}
function shift(value, days) {
return new Date(toDate(value) + days * DAY_MS).toISOString().slice(0, 10);
}
function daysBetween(from, to) {
return Math.round((toDate(to) - toDate(from)) / DAY_MS);
}
function userOf(value) {
return value === '' || value === undefined || value === null ? null : Number(value);
}
export function taskCalendar(canManage) {
return {
drag: null,
target: null,
dragged: false,
info: null,
infoTimer: null,
infoHeld: false,
touched: false,
/**
* Shows the info popover of a bar next to it. Not while dragging.
*/
showInfo(bar, id) {
if (this.drag?.moved) {
return;
}
this.keepInfo();
const popover = document.getElementById(id);
if (!popover) {
return;
}
if (this.info && this.info !== popover) {
this.closeInfo();
}
if (!popover.matches(':popover-open')) {
popover.showPopover();
}
this.info = popover;
const anchor = bar.getBoundingClientRect();
const box = popover.getBoundingClientRect();
const gap = 4;
const below = anchor.bottom + gap;
const top = below + box.height <= window.innerHeight || anchor.top - gap - box.height < 0 ? below : anchor.top - gap - box.height;
const left = Math.min(Math.max(gap, anchor.left), window.innerWidth - box.width - gap);
popover.style.top = `${Math.max(gap, top)}px`;
popover.style.left = `${Math.max(gap, left)}px`;
},
/**
* Hides the popover after a moment, so that the pointer can move onto it.
*/
hideInfo(id, delay = 150) {
this.keepInfo();
this.infoTimer = setTimeout(() => {
const popover = document.getElementById(id);
if (this.infoHeld && this.info === popover) {
return;
}
if (popover?.matches(':popover-open')) {
popover.hidePopover();
}
if (this.info === popover) {
this.info = null;
}
}, delay);
},
keepInfo() {
clearTimeout(this.infoTimer);
this.infoTimer = null;
},
closeInfo() {
this.keepInfo();
this.infoHeld = false;
if (this.info?.matches(':popover-open')) {
this.info.hidePopover();
}
this.info = null;
},
/**
* A tap on a popover keeps it open until something outside it is tapped.
*/
holdInfo() {
this.keepInfo();
this.infoHeld = true;
},
/**
* Closes the popover when a tap or click lands outside it and its bar.
*/
dismissInfo(event) {
if (this.info && !this.info.contains(event.target) && !event.target.closest?.('[data-calendar-bar]')) {
this.closeInfo();
}
},
/**
* Keyboard focus leaving the popover (not back to its bar) closes it.
*/
leaveInfo(event, id) {
const next = event.relatedTarget;
if (next && (next.closest?.(`#${id}`) || next.getAttribute?.('aria-describedby') === `${id}-details`)) {
return;
}
this.hideInfo(id, 0);
},
/**
* A bar's own blur closes its popover unless focus moves into it.
*/
blurBar(event, id) {
if (event.relatedTarget?.closest?.(`#${id}`)) {
return;
}
this.hideInfo(id, 0);
},
/**
* A mouse click or Enter opens the dialog; a tap shows the popover,
* whose button opens the dialog.
*/
clickBar(bar, id, open) {
const touched = this.touched;
this.touched = false;
if (this.consumeDrag()) {
return;
}
if (touched) {
this.showInfo(bar, id);
this.holdInfo();
return;
}
open();
},
/**
* The popover's button: close it and open the dialog.
*/
openFromInfo(open) {
this.closeInfo();
open();
},
begin(event, mode, bar) {
this.closeInfo();
this.touched = event.pointerType === 'touch' || event.pointerType === 'pen';
if (!canManage || event.button !== 0 || event.pointerType === 'touch') {
return;
}
const cell = this.cellAt(event);
this.drag = {
mode,
bar,
x: event.clientX,
y: event.clientY,
originDay: cell?.day ?? bar.from,
moved: false,
};
this.target = null;
this.dragged = false;
},
track(event) {
if (!this.drag) {
return;
}
if (!this.drag.moved && Math.hypot(event.clientX - this.drag.x, event.clientY - this.drag.y) < DRAG_THRESHOLD_PX) {
return;
}
this.drag.moved = true;
this.target = this.cellAt(event);
},
drop(event) {
const drag = this.drag;
this.drag = null;
if (!drag || !drag.moved) {
this.target = null;
return;
}
const cell = this.cellAt(event) ?? this.target;
this.target = null;
this.dragged = true;
if (!cell) {
return;
}
const bar = drag.bar;
let from = bar.from;
let to = bar.to;
let toUser = bar.user;
if (drag.mode === 'move') {
const delta = daysBetween(drag.originDay, cell.day);
from = shift(bar.from, delta);
to = shift(bar.to, delta);
toUser = cell.user;
} else if (drag.mode === 'start') {
from = cell.day > bar.to ? bar.to : cell.day;
} else if (drag.mode === 'end') {
to = cell.day < bar.from ? bar.from : cell.day;
} else if (drag.mode === 'place') {
from = cell.day;
to = null;
toUser = cell.user;
}
if (drag.mode !== 'place' && from === bar.from && to === bar.to && toUser === bar.user) {
return;
}
this.$wire.reschedule(bar.task, bar.job, bar.user, toUser, from, to);
},
cancel() {
if (this.drag) {
this.drag = null;
this.target = null;
this.dragged = true;
}
},
/**
* A click right after a drag is the end of the drag, not a request to open the dialog.
*/
consumeDrag() {
const dragged = this.dragged;
this.dragged = false;
return dragged;
},
isTarget(day, user) {
return this.target !== null && this.target.day === day && this.target.user === userOf(user);
},
cellAt(event) {
const element = document
.elementsFromPoint(event.clientX, event.clientY)
.find((candidate) => candidate.dataset && candidate.dataset.calendarDay !== undefined);
return element ? { day: element.dataset.calendarDay, user: userOf(element.dataset.calendarUser) } : null;
},
};
}
document.addEventListener('alpine:init', () => {
window.Alpine.data('taskCalendar', taskCalendar);
});