- A task now has one optional assignee (tasks.assignee_user_id), whose calendar row it is on and whose days its estimate books, and members (task_members) who see it among their own jobs and in the weekly sheet but get no bookings. A migration moves existing assignees over: the one with most booked time becomes the assignee, the others members. - Adding tasks from a workflow template asks for each step's assignee and, by default, schedules tasks with a due date to end on it, booking the estimate backwards from the due date (not before today); what does not fit runs past the due date and is reported as late. - The task calendar leaves quick sale tasks out of the unscheduled list, shows a bar's job details in a popover on hover and focus, and shows the day load on its own grid line. - The template table stacks its cells on narrow screens to reflow at 320 px. Co-Authored-By: Claude Opus 5.5 <[email protected]>
228 lines
6.5 KiB
JavaScript
228 lines
6.5 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.
|
|
*/
|
|
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,
|
|
|
|
/**
|
|
* 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 (popover?.matches(':popover-open')) {
|
|
popover.hidePopover();
|
|
}
|
|
|
|
if (this.info === popover) {
|
|
this.info = null;
|
|
}
|
|
}, delay);
|
|
},
|
|
|
|
keepInfo() {
|
|
clearTimeout(this.infoTimer);
|
|
this.infoTimer = null;
|
|
},
|
|
|
|
closeInfo() {
|
|
this.keepInfo();
|
|
|
|
if (this.info?.matches(':popover-open')) {
|
|
this.info.hidePopover();
|
|
}
|
|
|
|
this.info = null;
|
|
},
|
|
|
|
begin(event, mode, bar) {
|
|
this.closeInfo();
|
|
|
|
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);
|
|
});
|