Files
xenia-pos-local/terminal/src/apps/kds/logic/layout.js
T
bonaminandClaude Opus 5.5 5f6d4ab428 feat(terminal): kitchen display on paired screens, live without polling (platform-v2 3.2b)
Port of the manager's KdsPage.jsx into terminal/src/apps/kds, split into pure logic
(layout packing, age, buckets, rounds, totals, status cycle, settings; unit-tested) and
components (card, item, action bars, dialogs, item totals, rails, settings panel).
- Settings are the device's config; the settings panel saves them (managers only),
  zoom changes the size for the session.
- Orders reload on live events and every reconnect; polling only while the live
  connection is down (DeviceProvider exposes liveUp; useLiveEvents helper).
- "Tap to start" unlocks sound; venue clock via useFormat; English-first texts.
- Undo of an item recomputes the order status at once; undo/redo clear the selection.
- Tests: terminal unit +15, terminal.mjs +12 KDS checks (no polling, live order + chime,
  item/undo/prepare/decline, settings permission, change made elsewhere), backend
  regression for an item back to pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:27:11 +02:00

146 lines
6.1 KiB
JavaScript

// The kitchen board's layout engine (ported from the manager's KdsPage.jsx, plan step 3.2b).
// Pure: unit-tested in Node (tests/kds.test.mjs).
//
// THE RULE: no vertical scrolling. Every card must fit within maxH (the available board
// height). Cards widen into extra columns until they fit. If even MAX_UNITS columns aren't
// enough, the last column overflows: the only acceptable compromise, and only with extremely
// tall items. Cards are absolutely positioned; their height is set from the layout.
export const COL_GAP = 14
export const CARD_GAP_Y = 14
export const BOARD_PAD = 14
export const BLOCK_GAP = 4 // vertical gap between item blocks
export const BODY_PAD_T = 8
export const BODY_PAD_B = 10 // a bit extra so the last item clears the footer
export const BODY_PAD_H = 10
export const MAX_UNITS = 3
// Base chrome heights at font scale 1; cardChrome() scales them so the layout stays exact
// at any text size.
export const CARD_HEAD_H_BASE = 46
export const CARD_SUB_H_BASE = 28
export const CARD_FOOT_H_BASE = 32
export const CARD_NOTE_H_BASE = 35 // spacer (6) + icon line (≈20) + bottom pad (8) + border (1)
export const COURSE_HEADER_H_BASE = 18
export const BATCH_DIVIDER_H_BASE = 22
/** Fixed rows of a card (header, subheader, waiter footer, order note) + body padding. */
export function cardChrome(fs, hasWaiter, hasNote) {
const head = Math.round(CARD_HEAD_H_BASE * fs)
const sub = Math.round(CARD_SUB_H_BASE * fs)
const foot = hasWaiter ? Math.round(CARD_FOOT_H_BASE * fs) : 0
const note = hasNote ? Math.round(CARD_NOTE_H_BASE * fs) : 0
return head + sub + foot + note + BODY_PAD_T + BODY_PAD_B
}
export function colHeight(col, blocks, gap) {
if (!col.length) return 0
let h = 0
for (const idx of col) h += blocks[idx]
return h + (col.length - 1) * gap
}
/** Items into columns of at most bodyH each (block indexes per column). */
export function distribute(blocks, gap, bodyH) {
if (!blocks.length) return [[]]
const cols = []
let cur = [], curH = 0
for (let i = 0; i < blocks.length; i++) {
const h = blocks[i]
const tentative = cur.length ? curH + gap + h : h
if (cur.length && tentative > bodyH) {
cols.push(cur); cur = [i]; curH = h
} else {
cur.push(i); curH = tentative
}
}
if (cur.length) cols.push(cur)
return cols
}
function foldInto(cols, units) {
if (cols.length <= units) return cols
const head = cols.slice(0, units - 1)
const tail = []
for (let i = units - 1; i < cols.length; i++) tail.push(...cols[i])
return [...head, tail]
}
/** A card's size: more columns (units) until it fits in maxH. Each attempt divides the body
* height by the number of columns, so more columns = shorter columns = a shorter card. */
export function sizeCard({ blocks, blockGap, chrome, maxH, base, maxUnits = MAX_UNITS }) {
const availBodyH = Math.max(40, maxH - chrome)
for (let units = 1; units <= maxUnits; units++) {
const cols = foldInto(distribute(blocks, blockGap, availBodyH / units), units)
const actualUnits = cols.length
let maxColH = 0
for (const c of cols) maxColH = Math.max(maxColH, colHeight(c, blocks, blockGap))
const cardH = chrome + maxColH
const cardW = actualUnits * base + (actualUnits - 1) * COL_GAP
if (cardH <= maxH + 0.5) return { units: actualUnits, cols, cardW, cardH }
}
// Extreme content: maxUnits wide, height capped (content clipped only as a last resort)
const cols = foldInto(distribute(blocks, blockGap, availBodyH / maxUnits), maxUnits)
return { units: maxUnits, cols, cardW: maxUnits * base + (maxUnits - 1) * COL_GAP, cardH: maxH }
}
/** Skyline packing: cards stack in a column until the next would exceed maxH, then a new
* column starts to the right. Oldest orders are leftmost. */
export function packBoard(cards, colW, maxH) {
const tracks = [] // tracks[i] = current bottom y of track column i
const positions = {}
const trackW = colW + COL_GAP
const topOf = (s, u) => {
let y = 0
for (let k = 0; k < u; k++) y = Math.max(y, tracks[s + k] || 0)
return y
}
for (const card of cards) {
const u = card.units
let placed = null
for (let s = 0; s + u <= tracks.length; s++) {
const y = topOf(s, u)
if (y + card.cardH <= maxH + 0.5) { placed = { s, y }; break }
}
if (!placed) placed = { s: tracks.length, y: 0 }
const { s, y } = placed
const bottom = y + card.cardH + CARD_GAP_Y
for (let k = 0; k < u; k++) tracks[s + k] = bottom
positions[card.id] = { x: s * trackW, y }
}
let totalW = 0
for (const card of cards) {
const p = positions[card.id]
if (p) totalW = Math.max(totalW, p.x + card.cardW)
}
return { positions, totalW }
}
/** Every visible card's place on the board: { map: {id: {cols, x, y, w, h}}, totalW }.
* measured[id].blocks = item heights from the measure layer (an estimate until then);
* course headers and batch dividers add rows. */
export function layoutBoard({ orders, measured, boardH, colW, fontScale, groupByCourse, courses, batchMode }) {
if (!orders.length || !boardH) return { map: {}, totalW: BOARD_PAD * 2 }
const maxH = boardH - BOARD_PAD * 2
const sized = orders.map(o => {
const chrome = cardChrome(fontScale, o.waiters?.length > 0, !!o.notes)
const m = measured[o.id]
let blocks = m ? m.blocks : o.items.map(() => Math.round(28 * fontScale))
if (groupByCourse && courses?.length > 0 && o.items?.length > 0) {
const groups = new Set(o.items.map(it => (it.course_id != null ? String(it.course_id) : '__none__')))
blocks = [...Array(groups.size).fill(Math.round(COURSE_HEADER_H_BASE * fontScale)), ...blocks]
}
if (batchMode === 'grouped' && o._batches?.length > 1) {
blocks = [...Array(o._batches.length).fill(Math.round(BATCH_DIVIDER_H_BASE * fontScale)), ...blocks]
}
return { id: o.id, ...sizeCard({ blocks, blockGap: BLOCK_GAP, chrome, maxH, base: colW }) }
})
const { positions, totalW } = packBoard(sized, colW, maxH)
const map = {}
for (const s of sized) {
const p = positions[s.id]
if (p) map[s.id] = { cols: s.cols, x: p.x + BOARD_PAD, y: p.y + BOARD_PAD, w: s.cardW, h: s.cardH }
}
return { map, totalW: totalW + BOARD_PAD * 2 }
}