feat: grids, radar and document lines (#28)

Lists need one component for the whole app (SPEC §10.6-§10.7): a list
query model that turns search, filters, sort and paging into SQL or runs
over rows in memory, a KhGrid that pages 100,000 rows while scrolling
(or in pages) with sort, selection, keyboard, inline edit, totals, row
menu, saved column layout and CSV/XLSX export, the KhRadar around it
with search, quick filters, a filter builder and saved filters, a radar
window for F4 lookups, and KhLinesGrid for document lines typed from the
keyboard.

Checked in the browser on the component catalog and the sample document.
Fixed on the way: the grid height lost to the flex basis, a new search
kept the old scroll position, Save of a filter needed a second click,
page shortcuts (Ctrl+Canc) reached the record behind a modal window,
Escape did nothing once focus left a dialog, totals did not follow an
inline edit, and amounts exported as 0 instead of 0,00.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-11 18:47:38 +01:00
1 parent d0f9d0e651
commit 75d60ee4c4
18 files changed
+3776 -28

No files matched your search

@@ -218,3 +218,84 @@ button.ctx { cursor: pointer; }
.kh-catalogrow { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.kh-catalogbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 11px; }
.kh-resizable { resize: horizontal; overflow: hidden; min-width: 240px; max-width: 100%; padding: 2px 6px 12px 2px; border: 1px dashed var(--kh-border); border-radius: var(--kh-radius-lg); }
/* ---- Grids, radar and document lines (issue #28) ---- */
.kh-sr { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.kh-grid { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.kh-gridwrap { position: relative; min-height: 120px; }
.kh-gridwrap:focus-visible { outline: 2px solid var(--kh-accent); outline-offset: -2px; }
.kh-th { border: 0; background: none; padding: 0; font: inherit; color: inherit; cursor: pointer; display: inline-flex; align-items: center; gap: 3px; }
.kh-th:hover { color: var(--kh-text); }
.kh-th[data-sort="asc"]::after { content: "\25B2"; color: var(--kh-accent); font-size: 9px; }
.kh-th[data-sort="desc"]::after { content: "\25BC"; color: var(--kh-accent); font-size: 9px; }
.grid th.num .kh-th { flex-direction: row-reverse; }
.grid tbody tr { cursor: default; }
.grid tbody tr:hover td { background: var(--kh-row-sel); }
.grid tr.kh-cur td { box-shadow: inset 0 1px 0 var(--kh-accent), inset 0 -1px 0 var(--kh-accent); }
.grid tr.kh-cur td:first-child { box-shadow: inset 2px 0 0 var(--kh-accent), inset 0 1px 0 var(--kh-accent), inset 0 -1px 0 var(--kh-accent); }
.grid tr.kh-ph td { color: transparent; }
.grid tr.kh-empty td { text-align: center; color: var(--kh-text-3); height: 64px; white-space: normal; }
.grid td.num, .grid td .num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.neg { color: var(--kh-err); }
.grid td.ed { padding: 0 2px; }
.kh-bxbtn { border: 0; background: none; padding: 4px; cursor: pointer; display: grid; place-items: center; margin: 0 auto; }
.kh-bxbtn .bx svg.i { width: 10px; height: 10px; color: #fff; }
.grid .kh-rowmenu { width: 32px; padding: 0 4px; text-align: center; }
.kh-rowbtn { border: 0; background: none; color: var(--kh-text-3); width: 24px; height: 24px; border-radius: 5px; display: grid; place-items: center; cursor: pointer; opacity: 0; }
.grid tr:hover .kh-rowbtn, .grid tr.kh-cur .kh-rowbtn, .kh-rowbtn:focus-visible { opacity: 1; }
.kh-rowbtn:hover { background: var(--kh-surface-3); color: var(--kh-text); }
.kh-rowpop { z-index: 21; max-height: 70vh; overflow-y: auto; }
.kh-celledit { width: 100%; height: calc(var(--kh-row-h) - 6px); border: 0; background: transparent; font: inherit; color: var(--kh-text); outline: none; padding: 0 8px; }
.kh-celledit.num { text-align: right; }
.gridfoot .kh-pager { display: inline-flex; gap: 2px; align-items: center; margin-left: auto; }
.kh-pg { border: 0; background: none; font: inherit; font-size: 12px; color: var(--kh-text-2); min-width: 26px; height: 24px; padding: 0 6px; border-radius: 5px; cursor: pointer; display: inline-grid; place-items: center; }
.kh-pg:hover:not(:disabled) { background: var(--kh-surface-3); }
.kh-pg.on { background: var(--kh-accent); color: #fff; }
.kh-pg:disabled { opacity: .35; cursor: default; }
.kh-pgdots { padding: 0 4px; color: var(--kh-text-3); }
.kh-colchooser { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 2px; max-height: 50vh; overflow: auto; }
.kh-colchooser li { display: flex; align-items: center; gap: 4px; padding: 2px 4px; border-radius: 6px; }
.kh-colchooser li:hover { background: var(--kh-surface-2); }
.kh-colcheck { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; font-size: 12.5px; cursor: pointer; padding: 4px 0; }
.kh-colcheck input { accent-color: var(--kh-accent); width: 15px; height: 15px; margin: 0; }
.kh-radar { display: flex; flex-direction: column; gap: 10px; min-height: 0; min-width: 0; }
.kh-radarbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.kh-radarbar .grow { flex: 1; }
.kh-radarpanel { display: flex; flex-direction: column; min-height: 0; overflow: visible; }
.kh-radarpanel .kh-grid { flex: 1; }
.kh-radarpanel .kh-gridwrap { flex: none; height: calc(100vh - 330px); }
.kh-radar.compact .filters { padding: 8px 0 10px; }
.kh-rsearch { display: inline-flex; align-items: center; gap: 6px; height: 28px; min-width: 220px; flex: 0 1 280px; padding: 0 8px; cursor: text; }
.kh-rsearch input { border: 0; outline: none; background: transparent; font: inherit; color: var(--kh-text); flex: 1; min-width: 0; height: 100%; }
.kh-rsearch svg.i { color: var(--kh-text-3); flex: none; }
.kh-rsearch:focus-within { border-color: var(--kh-accent); box-shadow: 0 0 0 3px var(--kh-accent-soft); }
.filters .grow { flex: 1; }
.btn.kh-addfilter { height: 28px; }
.kh-savedlist { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.kh-savedlist li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 2px 4px 2px 8px; border-radius: 6px; }
.kh-savedlist li:hover { background: var(--kh-surface-2); }
.kh-choices { border: 0; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 2px; }
.kh-choices legend { font-size: 12px; font-weight: 600; color: var(--kh-text-2); margin-bottom: 4px; padding: 0; }
.kh-filterform { gap: 10px; }
.kh-grow { flex: 1; }
.kh-linesgrid { display: flex; flex-direction: column; min-width: 0; }
.kh-linesgrid .gridwrap { overflow-x: auto; }
.grid.kh-lines td { overflow: visible; }
.grid.kh-lines td.kh-ce { padding: 0 2px; }
.grid .kh-ln { width: 34px; text-align: right; color: var(--kh-text-3); font-size: 11px; padding-right: 8px; }
input.kh-cell { width: 100%; height: calc(var(--kh-row-h) - 4px); border: 1px solid transparent; border-radius: 4px; background: transparent; font: inherit; color: var(--kh-text); padding: 0 8px; outline: none; min-width: 3ch; }
input.kh-cell:hover { border-color: var(--kh-border); }
input.kh-cell:focus { border-color: var(--kh-accent); background: var(--kh-input); box-shadow: 0 0 0 2px var(--kh-accent-soft); }
input.kh-cell.num { text-align: right; font-variant-numeric: tabular-nums; }
input.kh-cell.code { font-family: var(--kh-mono); font-size: 12px; font-weight: 600; color: var(--kh-accent-text); }
input.kh-cell.err { border-color: var(--kh-err); background: var(--kh-err-soft); }
.grid tr.kh-desc td.desc-row input.kh-cell { font-style: italic; color: var(--kh-text-2); }
.grid tr.kh-pending td { background: var(--kh-surface) !important; }
.grid tr.kh-pending input.kh-cell { border-style: dashed; border-color: var(--kh-border); }
.grid tr.kh-pending input.kh-cell:focus { border-style: solid; border-color: var(--kh-accent); }
.kh-linesbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 12px; border-top: 1px solid var(--kh-border); }
.kh-linesbar .grow { flex: 1; }
.kbdhint { display: inline-flex; gap: 3px; align-items: center; font: 10.5px var(--kh-mono); color: var(--kh-text-3); white-space: nowrap; }
.kbdhint kbd { border: 1px solid var(--kh-border-strong); border-bottom-width: 2px; padding: 0 4px; border-radius: 3px; background: var(--kh-surface); font: inherit; color: var(--kh-text-2); }
+104
View File
@@ -32,11 +32,23 @@
if (!shortcutTarget || e.isComposing) return;
const combo = comboOf(e);
if (!combo || !shortcutCombos.has(combo) || preventedBy(e.target, combo)) return;
// A modal window is open: the page behind it does not react (Ctrl+Canc in a search box must not delete the record).
if (document.querySelector(".overlay")) return;
e.preventDefault();
e.stopPropagation();
shortcutTarget.invokeMethodAsync("OnShortcut", combo).catch(function () { /* circuit gone */ });
}
// Escape closes the topmost dialog even when the focus fell out of it (after a re-render, a click on the page).
// Inside the dialog its own handler already does it.
document.addEventListener("keydown", function (e) {
if (e.key !== "Escape" || e.defaultPrevented) return;
const overlays = document.querySelectorAll(".overlay");
if (overlays.length === 0 || (e.target.closest && e.target.closest(".overlay"))) return;
const close = overlays[overlays.length - 1].querySelector(".kh-close");
if (close) close.click();
});
function onBeforeUnload(e) {
if (!unloadGuard) return;
e.preventDefault();
@@ -107,6 +119,8 @@
element.dispatchEvent(new KeyboardEvent("keydown", { key: e.key === "+" ? "ArrowUp" : "ArrowDown", bubbles: true }));
return;
}
// Keys typed in a field inside the element (a cell being edited) are the field's own.
if (e.target !== element && e.target.matches && e.target.matches("input, textarea, select, [contenteditable]")) return;
const combo = comboOf(e);
if (combo && preventedBy(element, combo)) e.preventDefault();
});
@@ -134,6 +148,96 @@
menu.style.overflowY = "auto";
},
// Brings a row of a grid into view: the row when it is rendered, otherwise its estimated place (virtual rows).
scrollGridRow: function (wrap, index) {
if (!wrap) return;
const row = wrap.querySelector('tr[data-i="' + index + '"]');
if (row) {
const head = wrap.querySelector("thead");
const top = row.offsetTop - (head ? head.offsetHeight : 0);
if (top < wrap.scrollTop) wrap.scrollTop = top;
else if (row.offsetTop + row.offsetHeight > wrap.scrollTop + wrap.clientHeight) wrap.scrollTop = row.offsetTop + row.offsetHeight - wrap.clientHeight;
return;
}
const sample = wrap.querySelector("tbody tr[data-i]");
const height = sample ? sample.offsetHeight : 30;
wrap.scrollTop = Math.max(0, index * height - wrap.clientHeight / 2);
},
// Moves a fixed menu back inside the window when it would come out of the right or bottom edge.
keepInWindow: function (element) {
if (!element) return;
const r = element.getBoundingClientRect();
if (r.right > window.innerWidth - 8) element.style.left = Math.max(8, window.innerWidth - r.width - 8) + "px";
if (r.bottom > window.innerHeight - 8) element.style.top = Math.max(8, window.innerHeight - r.height - 8) + "px";
},
// Hands a file written by the server to the browser as a download.
download: async function (fileName, mediaType, streamReference) {
const buffer = await streamReference.arrayBuffer();
const url = URL.createObjectURL(new Blob([buffer], { type: mediaType }));
const link = document.createElement("a");
link.href = url;
link.download = fileName;
document.body.appendChild(link);
link.click();
link.remove();
setTimeout(function () { URL.revokeObjectURL(url); }, 2000);
},
// The keyboard of the lines of a document: Enter next cell, Shift+Enter previous, arrows the same cell of the line
// above or below, Ctrl+Down new line, Ctrl+Canc removes the line, F4 lookup. Rows and cells are data-r and data-c.
linesGrid: function (table, target) {
if (!table || table.khLines) return;
table.khLines = true;
const cellOf = function (r, c) { return table.querySelector('input.kh-cell[data-r="' + r + '"][data-c="' + c + '"]'); };
const focusCell = function (r, c) {
let tries = 0;
(function attempt() {
const el = cellOf(r, c) || table.querySelector('input.kh-cell[data-r="' + r + '"]');
if (el) { el.focus(); el.select(); } else if (tries++ < 20) setTimeout(attempt, 40);
})();
};
table.khFocus = focusCell;
const flush = function (el) {
if (el.khDirty) { el.khDirty = false; el.dispatchEvent(new Event("change", { bubbles: true })); }
};
table.addEventListener("input", function (e) { if (e.target.classList.contains("kh-cell")) e.target.khDirty = true; });
table.addEventListener("change", function (e) { if (e.target.classList.contains("kh-cell")) e.target.khDirty = false; }, true);
table.addEventListener("keydown", function (e) {
const el = e.target;
if (!el.classList || !el.classList.contains("kh-cell") || e.isComposing) return;
const r = Number(el.dataset.r), c = Number(el.dataset.c);
const combo = comboOf(e);
const call = function (method, args, then) {
e.preventDefault();
e.stopPropagation();
flush(el);
target.invokeMethodAsync.apply(target, [method].concat(args)).then(then).catch(function () { /* circuit gone */ });
};
if (combo === "Enter" || combo === "Shift+Enter") {
e.preventDefault();
const cells = Array.prototype.slice.call(table.querySelectorAll("input.kh-cell:not([readonly])"));
const next = cells[cells.indexOf(el) + (combo === "Enter" ? 1 : -1)];
if (next) { next.focus(); next.select(); }
} else if (combo === "ArrowDown" || combo === "ArrowUp") {
const other = cellOf(r + (combo === "ArrowDown" ? 1 : -1), c);
if (other) { e.preventDefault(); other.focus(); other.select(); }
} else if (combo === "Ctrl+ArrowDown") {
call("InsertAfterAsync", [r], function () { focusCell(r + 1, 0); });
} else if (combo === "Ctrl+Delete") {
call("RemoveAtAsync", [r], function () { focusCell(r, c); });
} else if (combo === "F4") {
call("LookupAsync", [r, c], function () { });
}
});
},
// Moves the focus to a cell of the lines of a document.
focusLineCell: function (table, r, c) {
if (table && table.khFocus) table.khFocus(r, Math.max(0, c));
},
// Moves the focus to the first field or button inside an element (dialogs, side panels).
focusFirst: function (element) {
if (!element) return;