Dispatch board in jQuery
I rebuilt the Magic Dragon board with jQuery and no build step. It has HTML5 drag and drop, keyboard control for people who can’t use a mouse, and ETAs that update as stops move.
- Type
- Working demo, 2026
- Built with
- jQuery 3.7, HTML5 Drag and Drop, Ajax
- Saves to
- localStorage, with undo
- Tests
- 11 QUnit tests for the ETA math
How it's put together
One state object holds the lanes and orders. Every change, whether a drag, a key press, a menu pick or a new order, goes through one commit() function that snapshots the old state for Undo, saves to localStorage, and re-renders from state. Cards that changed position glide there using the FLIP technique: measure, re-render, then animate the transform from the old spot to the new one.
- The board and new orders load with
$.getJSON, as they would from a server. - Order data only ever goes into the page through
.text(), never as HTML. - Undo works from the toast, the toolbar or Ctrl/⌘+Z.
Mouse: HTML5 drag and drop
Drag events are delegated on the board, so cards that are re-rendered never need new handlers. While you drag, a marker shows exactly where the card will land:
// Where would the card land? Above the first card whose middle is below the pointer.
function placeMarker($list, y) {
var before = null;
$list.children('.card').not('.is-dragging').each(function () {
var r = this.getBoundingClientRect();
if (y < r.top + r.height / 2) {
before = this;
return false;
}
});
if (before) {
if ($marker[0].nextSibling !== before) $marker.insertBefore(before);
} else if ($marker[0].parentNode !== $list[0] || $marker[0].nextSibling) {
$list.append($marker);
}
}
Holding a card near the edge of the board or the window scrolls it, which helps on a laptop screen with four drivers.
Keyboard: pick up, move, drop
Drag and drop is useless to someone on a keyboard or a screen reader, so every card is a focusable control. Space picks it up, the arrow keys move it between drivers and stops, Space drops it and Escape puts it back. Each step is announced through an aria-live region: “Ana R., stop 2 of 3, ETA 8:06 PM.”
switch (e.key) {
case 'ArrowUp': e.preventDefault(); nudge(0, -1); break;
case 'ArrowDown': e.preventDefault(); nudge(0, 1); break;
case 'ArrowLeft': e.preventDefault(); nudge(-1, 0); break;
case 'ArrowRight': e.preventDefault(); nudge(1, 0); break;
case ' ':
case 'Enter': e.preventDefault(); drop(); break;
case 'Escape': e.preventDefault(); cancelLift(); break;
case 'Tab': cancelLift(); break;
}
Touch: a menu on every card
HTML5 drag and drop is unreliable on phones, so each card also has a menu with Assign, Unassign and Mark ready. It follows the ARIA menu button pattern: arrow keys, Home and End move through items, and Escape closes it and returns focus to the button. On small screens you swipe from one driver to the next.
ETA math and tests
The route timing lives in eta.js, which has no DOM code, so the page and a QUnit suite run the same functions. A driver who is still out leaves when they get back; a stop with a delivery window is never reached early; a stop reached after its window closes is flagged late.
for (var i = 0; i < stops.length; i++) {
var stop = stops[i];
var arrive = t + travel(opts.matrix, here, stop.town);
var win = parseWindow(stop.window);
// Never early: wait for the delivery window to open.
var eta = win && arrive < win.start ? win.start : arrive;
planned.push({ ref: stop.ref, eta: eta, waited: eta - arrive, late: !!win && eta > win.end });
t = eta + SERVICE_MIN;
here = stop.town;
}
Why jQuery
Plenty of production sites still run on jQuery and Bootstrap, and drag and drop is where frameworks usually hide the hard parts. Writing it in ES5-style JavaScript with no build step shows the browser APIs underneath: DataTransfer, drag events, focus management and live regions.
Next: Field Reports