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:
1 parent
d0f9d0e651
commit
75d60ee4c4
18 files changed
+3776
-28
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user