Task calendar (/jobs/calendar) replaces per-unit task Kanban columns (K34): a two-week Gantt of tasks per person. Bars move, resize and change person by dragging or through a keyboard dialog. Each task's estimate is booked evenly over working days into task_bookings, and what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves to the next working day. Tasks get planned_from/planned_to on the form. Holidays (/settings/holidays): Finnish public holidays calculated per year, which the admin can take out of use, plus company days off. Also includes the earlier uncommitted work: workflow templates with adding tasks from a template, and removing customers no module refers to. Co-Authored-By: Claude Opus 5.5 <[email protected]>
150 lines
4.3 KiB
JavaScript
150 lines
4.3 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.
|
|
*/
|
|
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,
|
|
|
|
begin(event, mode, bar) {
|
|
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);
|
|
});
|