athena-ui-js 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -60
- package/docs/migration-2.0.md +83 -0
- package/docs/styling.md +86 -0
- package/package.json +109 -24
- package/src/athena-ui.js +61 -81
- package/src/chart.js +6 -0
- package/src/classic.js +11 -0
- package/src/components.js +6 -0
- package/src/controls.js +8 -0
- package/src/csv.js +6 -0
- package/src/dialog.js +7 -0
- package/src/dom.js +6 -0
- package/src/feedback.js +6 -0
- package/src/format.js +6 -0
- package/src/forms.js +7 -0
- package/src/i18n.js +6 -0
- package/src/inputs.js +9 -0
- package/src/libs/config/config.js +1 -2
- package/src/libs/core/controls.js +145 -0
- package/src/libs/core/cookies.js +55 -0
- package/src/libs/core/dateRange.js +121 -0
- package/src/libs/core/dom.js +58 -8
- package/src/libs/{hephaestus/HTMLElementGenerator/src/HTMLElementGenerator.js → core/elementGenerator.js} +23 -21
- package/src/libs/{hephaestus/HTMLElementGenerator/src → core}/errors.js +7 -5
- package/src/libs/core/format.js +51 -0
- package/src/libs/core/hungarianControls.js +41 -0
- package/src/libs/core/i18n.js +104 -6
- package/src/libs/core/theme.js +131 -0
- package/src/libs/core/validation.js +2 -2
- package/src/libs/hephaestus/LayoutGenerator/basicLayoutGenerator.js +13 -7
- package/src/libs/hephaestus/LayoutGenerator/footerContentGenerator.js +2 -4
- package/src/libs/hephaestus/LayoutGenerator/headerContentGenerator.js +12 -5
- package/src/libs/hephaestus/MenuGenerator/basicMenuGenerator.js +3 -3
- package/src/libs/hephaestus/MenuGenerator/menuGenerator.js +265 -0
- package/src/libs/hephaestus/auth/authState.js +58 -0
- package/src/libs/hephaestus/auth/session.js +1 -1
- package/src/libs/hephaestus/chartGenerator/chartGenerator.js +519 -87
- package/src/libs/hephaestus/componentGenerator/componentGenerator.js +121 -4
- package/src/libs/hephaestus/componentGenerator/themePicker.js +73 -0
- package/src/libs/hephaestus/dialogGenerator/dialogGenerator.js +113 -25
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +21 -0
- package/src/libs/hephaestus/formGenerator/authForms.js +130 -0
- package/src/libs/hephaestus/formGenerator/changePasswordFormGenerator.js +4 -4
- package/src/libs/hephaestus/formGenerator/formUtils.js +10 -13
- package/src/libs/hephaestus/formGenerator/loginFormGenerator.js +10 -11
- package/src/libs/hephaestus/formGenerator/registrationFormGenerator.js +3 -3
- package/src/libs/hephaestus/hephaestus.js +0 -5
- package/src/libs/hephaestus/inputGenerator/dropZoneGenerator.js +1 -1
- package/src/libs/hephaestus/inputGenerator/inputGenerator.js +30 -1
- package/src/libs/hephaestus/modalGenerator/modalGenerator.js +25 -12
- package/src/libs/hephaestus/navigationGenerator/navigationGenerator.js +20 -24
- package/src/libs/hephaestus/router/router.js +54 -5
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +290 -16
- package/src/menu.js +6 -0
- package/src/navigation.js +6 -0
- package/src/res/themes/appearance.css +154 -0
- package/src/res/themes/default/style.css +688 -82
- package/src/res/themes/tokens.css +96 -0
- package/src/router.js +6 -0
- package/src/sanitize.js +6 -0
- package/src/session.js +8 -0
- package/src/table.js +6 -0
- package/src/testing.js +11 -1
- package/src/theme-boot.js +25 -0
- package/src/theme.js +7 -0
- package/src/validation.js +6 -0
- package/types/athena-ui.d.ts +96 -0
- package/types/chart.d.ts +1 -0
- package/types/classic.d.ts +1 -0
- package/types/components.d.ts +1 -0
- package/types/controls.d.ts +3 -0
- package/types/csv.d.ts +1 -0
- package/types/dialog.d.ts +2 -0
- package/types/dom.d.ts +1 -0
- package/types/feedback.d.ts +1 -0
- package/types/format.d.ts +1 -0
- package/types/forms.d.ts +2 -0
- package/types/i18n.d.ts +1 -0
- package/types/inputs.d.ts +4 -0
- package/types/libs/config/config.d.ts +96 -0
- package/types/libs/config/hooks.d.ts +8 -0
- package/types/libs/core/combobox.d.ts +10 -0
- package/types/libs/core/controls.d.ts +28 -0
- package/types/libs/core/cookies.d.ts +27 -0
- package/types/libs/core/csv.d.ts +12 -0
- package/types/libs/core/dateRange.d.ts +72 -0
- package/types/libs/core/dom.d.ts +65 -0
- package/types/libs/core/elementGenerator.d.ts +30 -0
- package/types/libs/core/errors.d.ts +29 -0
- package/types/libs/core/focus.d.ts +6 -0
- package/types/libs/core/format.d.ts +21 -0
- package/types/libs/core/hungarianControls.d.ts +2 -0
- package/types/libs/core/i18n.d.ts +17 -0
- package/types/libs/core/pagination.d.ts +5 -0
- package/types/libs/core/sanitize.d.ts +6 -0
- package/types/libs/core/tableMath.d.ts +26 -0
- package/types/libs/core/theme.d.ts +39 -0
- package/types/libs/core/validation.d.ts +56 -0
- package/types/libs/hephaestus/LayoutGenerator/basicLayoutGenerator.d.ts +10 -0
- package/types/libs/hephaestus/LayoutGenerator/footerContentGenerator.d.ts +4 -0
- package/types/libs/hephaestus/LayoutGenerator/headerContentGenerator.d.ts +20 -0
- package/types/libs/hephaestus/LayoutGenerator/wrapperContentGenerator.d.ts +1 -0
- package/types/libs/hephaestus/MenuGenerator/basicMenuGenerator.d.ts +14 -0
- package/types/libs/hephaestus/MenuGenerator/menuGenerator.d.ts +65 -0
- package/types/libs/hephaestus/auth/authState.d.ts +26 -0
- package/types/libs/hephaestus/auth/session.d.ts +26 -0
- package/types/libs/hephaestus/chartGenerator/chartGenerator.d.ts +91 -0
- package/types/libs/hephaestus/componentGenerator/componentGenerator.d.ts +222 -0
- package/types/libs/hephaestus/componentGenerator/themePicker.d.ts +24 -0
- package/types/libs/hephaestus/dialogGenerator/dialogGenerator.d.ts +120 -0
- package/types/libs/hephaestus/feedbackGenerator/feedbackGenerator.d.ts +86 -0
- package/types/libs/hephaestus/formGenerator/authForms.d.ts +122 -0
- package/types/libs/hephaestus/formGenerator/changePasswordFormGenerator.d.ts +6 -0
- package/types/libs/hephaestus/formGenerator/formBuilder.d.ts +113 -0
- package/types/libs/hephaestus/formGenerator/formUtils.d.ts +8 -0
- package/types/libs/hephaestus/formGenerator/loginFormGenerator.d.ts +11 -0
- package/types/libs/hephaestus/formGenerator/lostPasswordFormGenerator.d.ts +4 -0
- package/types/libs/hephaestus/formGenerator/registrationFormGenerator.d.ts +7 -0
- package/types/libs/hephaestus/hephaestus.d.ts +3 -0
- package/types/libs/hephaestus/inputGenerator/dropZoneGenerator.d.ts +48 -0
- package/types/libs/hephaestus/inputGenerator/inputGenerator.d.ts +185 -0
- package/types/libs/hephaestus/inputGenerator/richTextGenerator.d.ts +49 -0
- package/types/libs/hephaestus/modalGenerator/modalGenerator.d.ts +2 -0
- package/types/libs/hephaestus/navigationGenerator/navigationGenerator.d.ts +150 -0
- package/types/libs/hephaestus/router/matchRoute.d.ts +12 -0
- package/types/libs/hephaestus/router/router.d.ts +67 -0
- package/types/libs/hephaestus/tableGenerator/tableGenerator.d.ts +120 -0
- package/types/menu.d.ts +1 -0
- package/types/navigation.d.ts +1 -0
- package/types/res/themes/themes.d.ts +7 -0
- package/types/router.d.ts +1 -0
- package/types/sanitize.d.ts +1 -0
- package/types/session.d.ts +3 -0
- package/types/table.d.ts +1 -0
- package/types/testing.d.ts +27 -0
- package/types/theme-boot.d.ts +0 -0
- package/types/theme.d.ts +2 -0
- package/types/validation.d.ts +1 -0
- package/src/libs/crypto-js/LICENSE +0 -24
- package/src/libs/crypto-js/crypto-js.js +0 -6657
- package/src/libs/hephaestus/HTMLControlGenerator/LICENSE +0 -21
- package/src/libs/hephaestus/HTMLControlGenerator/src/HTMLControlGenerator.min.js +0 -5
- package/src/libs/hephaestus/HTMLElementGenerator/LICENSE +0 -21
- package/src/libs/js-cookie/LICENSE +0 -21
- package/src/libs/js-cookie/src/api.mjs +0 -103
- package/src/libs/js-cookie/src/assign.mjs +0 -10
- package/src/libs/js-cookie/src/converter.mjs +0 -14
- package/src/libs/libs.js +0 -13
|
@@ -56,6 +56,18 @@ const compare = (a, b) =>
|
|
|
56
56
|
* @param {(row: object) => string} [options.rowLabel] aria-label of a focusable row
|
|
57
57
|
* @param {"normal"|"compact"} [options.density="normal"]
|
|
58
58
|
* @param {boolean} [options.wrap] true lets long text wrap, false keeps one line with an ellipsis (default: wrap, except in virtual tables)
|
|
59
|
+
* @param {(query: {page: number, pageSize: number, cursor: *, sort: ({key: string, descending: boolean}|null), filter: string}) =>
|
|
60
|
+
* Promise<{rows: object[], total?: number, nextCursor?: *}>} [options.dataSource] load the rows from a server instead of
|
|
61
|
+
* keeping them all in the browser. Sorting, the filter box and the pager call it again. Answer with `total` for numbered pages
|
|
62
|
+
* (`page` is 0-based), or with `nextCursor` for a "Load more" button (`cursor` is what the last answer gave, undefined for the
|
|
63
|
+
* first request; the rows are appended). A failed request shows the error with a Retry button.
|
|
64
|
+
* Together with `virtual` the list scrolls like one of `total` rows (every answer must carry `total`): the rows in view are asked for
|
|
65
|
+
* in chunks of `chunkSize` (`page` is the chunk number), rows that have not arrived yet show a placeholder, and sorting or the filter
|
|
66
|
+
* start again from the top.
|
|
67
|
+
* @param {number} [options.chunkSize=50] rows per request of a data source with `virtual`
|
|
68
|
+
* @param {number} [options.filterDelay=300] ms to wait after typing before the data source is asked
|
|
69
|
+
* @param {{label: string, onclick: (row: object) => void, danger?: boolean, hidden?: (row: object) => boolean, ariaLabel?: (row: object) => string}[]} [options.rowActions]
|
|
70
|
+
* a column of buttons at the end of every row
|
|
59
71
|
* Column extras: `render(row, value)` returns text or a Node (built by the caller, so safe by construction), `align`
|
|
60
72
|
* ("left" | "center" | "right"), `title` (false switches the tooltip with the full text off).
|
|
61
73
|
* @returns {{element: HTMLElement, update: Function, destroy: Function, setRows: Function, getSelected: () => object[], clearSelection: Function,
|
|
@@ -66,10 +78,19 @@ export function generateTable(options = {}) {
|
|
|
66
78
|
parent = "wrapper", id = "table", columns = [], sortable = true,
|
|
67
79
|
filter = false, selectable = false, resizableColumns = false, onRowClick, onSelectionChange
|
|
68
80
|
} = options;
|
|
81
|
+
const remote = typeof options.dataSource === "function";
|
|
82
|
+
const rowActions = options.rowActions?.length ? options.rowActions : null;
|
|
69
83
|
const virtual = options.virtual
|
|
70
84
|
? { height: 400, rowHeight: 36, overscan: 5, ...(options.virtual === true ? {} : options.virtual) }
|
|
71
85
|
: null;
|
|
72
|
-
const
|
|
86
|
+
const remoteVirtual = remote && Boolean(virtual);
|
|
87
|
+
const chunkSize = Math.max(1, options.chunkSize ?? 50);
|
|
88
|
+
const chunks = new Map(); // chunk number -> "loading" | "done" | "failed" (remote virtual)
|
|
89
|
+
let flushTimer = 0;
|
|
90
|
+
const pageSize = virtual ? 0 : (options.pageSize ?? (remote ? 25 : 0));
|
|
91
|
+
// What the data source told us: { loading, error, total, nextCursor, cursorMode }
|
|
92
|
+
const server = { loading: false, error: null, total: null, nextCursor: undefined, cursorMode: false, request: 0 };
|
|
93
|
+
const colCount = () => columns.length + (selectable ? 1 : 0) + (rowActions ? 1 : 0);
|
|
73
94
|
const host = resolveElement(parent) ?? document.body;
|
|
74
95
|
let rows = [...(options.rows ?? [])];
|
|
75
96
|
let current = rows; // filtered and sorted rows
|
|
@@ -101,11 +122,14 @@ export function generateTable(options = {}) {
|
|
|
101
122
|
filterInput.type = "search";
|
|
102
123
|
filterInput.placeholder = translate("filter");
|
|
103
124
|
filterInput.setAttribute("aria-label", translate("filter"));
|
|
125
|
+
let filterTimer = 0;
|
|
104
126
|
filterInput.addEventListener("input", () => {
|
|
105
|
-
state.query = filterInput.value.toLowerCase();
|
|
127
|
+
state.query = remote ? filterInput.value.trim() : filterInput.value.toLowerCase();
|
|
106
128
|
state.page = 0;
|
|
107
129
|
if (scroller) scroller.scrollTop = 0;
|
|
108
|
-
render();
|
|
130
|
+
if (!remote) return render();
|
|
131
|
+
clearTimeout(filterTimer);
|
|
132
|
+
filterTimer = setTimeout(() => load({ reset: true }), options.filterDelay ?? 300);
|
|
109
133
|
});
|
|
110
134
|
root.append(filterInput);
|
|
111
135
|
}
|
|
@@ -120,6 +144,7 @@ export function generateTable(options = {}) {
|
|
|
120
144
|
host.append(root);
|
|
121
145
|
|
|
122
146
|
function visibleRows() {
|
|
147
|
+
if (remote) return rows; // the server filtered, sorted and paged them
|
|
123
148
|
let result = rows;
|
|
124
149
|
if (state.query) {
|
|
125
150
|
result = result.filter(row =>
|
|
@@ -260,7 +285,8 @@ export function generateTable(options = {}) {
|
|
|
260
285
|
const sort = () => {
|
|
261
286
|
state.descending = state.sortKey === col.key && !state.descending;
|
|
262
287
|
state.sortKey = col.key;
|
|
263
|
-
|
|
288
|
+
if (remote) load({ reset: true });
|
|
289
|
+
else render();
|
|
264
290
|
};
|
|
265
291
|
th.addEventListener("click", sort);
|
|
266
292
|
th.addEventListener("keydown", e => {
|
|
@@ -277,6 +303,12 @@ export function generateTable(options = {}) {
|
|
|
277
303
|
if (resizableColumns) makeResizer(th, col);
|
|
278
304
|
tr.append(th);
|
|
279
305
|
}
|
|
306
|
+
if (rowActions) {
|
|
307
|
+
const th = el("th", "athena-table-actions");
|
|
308
|
+
th.scope = "col";
|
|
309
|
+
th.append(el("span", "athena-visually-hidden", translate("actions")));
|
|
310
|
+
tr.append(th);
|
|
311
|
+
}
|
|
280
312
|
const thead = el("thead");
|
|
281
313
|
thead.append(tr);
|
|
282
314
|
return thead;
|
|
@@ -318,7 +350,7 @@ export function generateTable(options = {}) {
|
|
|
318
350
|
tr.setAttribute("aria-hidden", "true");
|
|
319
351
|
tr.style.height = `${height}px`;
|
|
320
352
|
const td = el("td");
|
|
321
|
-
td.colSpan =
|
|
353
|
+
td.colSpan = colCount();
|
|
322
354
|
tr.append(td);
|
|
323
355
|
return tr;
|
|
324
356
|
}
|
|
@@ -326,6 +358,15 @@ export function generateTable(options = {}) {
|
|
|
326
358
|
function renderRow(row, index) {
|
|
327
359
|
const tr = el("tr");
|
|
328
360
|
if (virtual) tr.setAttribute("aria-rowindex", String(index + 2));
|
|
361
|
+
if (row === undefined) {
|
|
362
|
+
// A row of a data source that has not arrived yet
|
|
363
|
+
tr.className = "athena-table-placeholder";
|
|
364
|
+
tr.setAttribute("aria-busy", "true");
|
|
365
|
+
const td = el("td", null, "…");
|
|
366
|
+
td.colSpan = colCount();
|
|
367
|
+
tr.append(td);
|
|
368
|
+
return tr;
|
|
369
|
+
}
|
|
329
370
|
addClasses(tr, options.rowClass?.(row));
|
|
330
371
|
if (onRowClick) {
|
|
331
372
|
tr.classList.add("clickable");
|
|
@@ -384,17 +425,52 @@ export function generateTable(options = {}) {
|
|
|
384
425
|
}
|
|
385
426
|
tr.append(td);
|
|
386
427
|
}
|
|
428
|
+
if (rowActions) {
|
|
429
|
+
const td = el("td", "athena-table-actions");
|
|
430
|
+
for (const action of rowActions) {
|
|
431
|
+
if (action.hidden?.(row)) continue;
|
|
432
|
+
const button = el("button", `athena-button button-${action.danger ? "danger" : "secondary"} button-small`, action.label);
|
|
433
|
+
button.type = "button";
|
|
434
|
+
if (action.ariaLabel) button.setAttribute("aria-label", action.ariaLabel(row));
|
|
435
|
+
button.addEventListener("click", event => {
|
|
436
|
+
event.stopPropagation();
|
|
437
|
+
action.onclick(row);
|
|
438
|
+
});
|
|
439
|
+
td.append(button);
|
|
440
|
+
}
|
|
441
|
+
tr.append(td);
|
|
442
|
+
}
|
|
443
|
+
return tr;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function messageRow(className, text, retry) {
|
|
447
|
+
const td = el("td", className, text);
|
|
448
|
+
td.colSpan = Math.max(colCount(), 1);
|
|
449
|
+
if (retry) {
|
|
450
|
+
const button = el("button", "athena-button button-secondary button-small", translate("retry"));
|
|
451
|
+
button.type = "button";
|
|
452
|
+
button.addEventListener("click", () => load({}));
|
|
453
|
+
td.append(" ", button);
|
|
454
|
+
}
|
|
455
|
+
const tr = el("tr");
|
|
456
|
+
tr.append(td);
|
|
387
457
|
return tr;
|
|
388
458
|
}
|
|
389
459
|
|
|
390
460
|
function renderBody() {
|
|
391
461
|
const tbody = el("tbody");
|
|
462
|
+
if (remote && server.error && !current.length) {
|
|
463
|
+
const row = messageRow("athena-table-error", server.error, true);
|
|
464
|
+
row.firstChild.setAttribute("role", "alert");
|
|
465
|
+
tbody.append(row);
|
|
466
|
+
return tbody;
|
|
467
|
+
}
|
|
468
|
+
if (remote && server.loading && !current.length) {
|
|
469
|
+
tbody.append(messageRow("athena-table-loading", translate("loading")));
|
|
470
|
+
return tbody;
|
|
471
|
+
}
|
|
392
472
|
if (!current.length) {
|
|
393
|
-
|
|
394
|
-
td.colSpan = Math.max(columns.length + (selectable ? 1 : 0), 1);
|
|
395
|
-
const tr = el("tr");
|
|
396
|
-
tr.append(td);
|
|
397
|
-
tbody.append(tr);
|
|
473
|
+
tbody.append(messageRow("athena-table-empty", options.emptyText ?? translate("noData")));
|
|
398
474
|
return tbody;
|
|
399
475
|
}
|
|
400
476
|
if (virtual) {
|
|
@@ -403,18 +479,58 @@ export function generateTable(options = {}) {
|
|
|
403
479
|
rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
|
|
404
480
|
});
|
|
405
481
|
windowKey = `${win.start}:${win.end}`;
|
|
482
|
+
if (remoteVirtual) ensureChunks();
|
|
406
483
|
if (win.offsetTop) tbody.append(spacer(win.offsetTop));
|
|
407
484
|
for (let i = win.start; i < win.end; i++) tbody.append(renderRow(current[i], i));
|
|
408
485
|
if (win.offsetBottom) tbody.append(spacer(win.offsetBottom));
|
|
409
486
|
return tbody;
|
|
410
487
|
}
|
|
411
|
-
const pageRows = pageSize > 0 ? current.slice(state.page * pageSize, (state.page + 1) * pageSize) : current;
|
|
488
|
+
const pageRows = remote ? current : pageSize > 0 ? current.slice(state.page * pageSize, (state.page + 1) * pageSize) : current;
|
|
412
489
|
for (const row of pageRows) tbody.append(renderRow(row));
|
|
413
490
|
return tbody;
|
|
414
491
|
}
|
|
415
492
|
|
|
493
|
+
function renderRemotePager() {
|
|
494
|
+
if (server.cursorMode) {
|
|
495
|
+
pager.hidden = !server.nextCursor && !server.error;
|
|
496
|
+
const more = el("button", "athena-button button-secondary", translate("loadMore"));
|
|
497
|
+
more.type = "button";
|
|
498
|
+
more.disabled = server.loading;
|
|
499
|
+
more.addEventListener("click", () => load({ more: true }));
|
|
500
|
+
pager.replaceChildren(more);
|
|
501
|
+
showPagerError();
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const pages = server.total == null ? state.page + 2 : Math.max(1, Math.ceil(server.total / pageSize));
|
|
505
|
+
pager.hidden = pages === 1;
|
|
506
|
+
const go = delta => () => {
|
|
507
|
+
state.page += delta;
|
|
508
|
+
load({});
|
|
509
|
+
};
|
|
510
|
+
const prev = el("button", null, `‹ ${translate("previous")}`);
|
|
511
|
+
prev.type = "button";
|
|
512
|
+
prev.disabled = state.page === 0 || server.loading;
|
|
513
|
+
prev.addEventListener("click", go(-1));
|
|
514
|
+
const next = el("button", null, `${translate("next")} ›`);
|
|
515
|
+
next.type = "button";
|
|
516
|
+
next.disabled = state.page >= pages - 1 || server.loading;
|
|
517
|
+
next.addEventListener("click", go(1));
|
|
518
|
+
pager.replaceChildren(prev, el("span", null, ` ${translate("page", { page: state.page + 1, pages: server.total == null ? "?" : pages })} `), next);
|
|
519
|
+
showPagerError();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// A failed request while rows are on the screen (the next page, "Load more"): say so next to the buttons.
|
|
523
|
+
function showPagerError() {
|
|
524
|
+
if (!server.error || !current.length) return;
|
|
525
|
+
pager.hidden = false;
|
|
526
|
+
const message = el("span", "athena-table-error", server.error);
|
|
527
|
+
message.setAttribute("role", "alert");
|
|
528
|
+
pager.append(" ", message);
|
|
529
|
+
}
|
|
530
|
+
|
|
416
531
|
function renderPager(total) {
|
|
417
532
|
if (!pager) return;
|
|
533
|
+
if (remote) return renderRemotePager();
|
|
418
534
|
const pages = Math.max(1, Math.ceil(total / pageSize));
|
|
419
535
|
state.page = Math.min(state.page, pages - 1);
|
|
420
536
|
pager.hidden = pages === 1;
|
|
@@ -438,6 +554,8 @@ export function generateTable(options = {}) {
|
|
|
438
554
|
function render() {
|
|
439
555
|
const scrollTop = scroller?.scrollTop ?? 0;
|
|
440
556
|
current = visibleRows();
|
|
557
|
+
root.toggleAttribute("aria-busy", remote && server.loading);
|
|
558
|
+
root.classList.toggle("athena-table-is-loading", remote && server.loading);
|
|
441
559
|
renderPager(current.length);
|
|
442
560
|
if (virtual) table.setAttribute("aria-rowcount", String(current.length + 1));
|
|
443
561
|
const parts = [renderHead(current), renderBody()];
|
|
@@ -453,12 +571,13 @@ export function generateTable(options = {}) {
|
|
|
453
571
|
// starts empty (a tiny scroll area when the rows are set) still draws a full window.
|
|
454
572
|
const viewportHeight = () => Math.max(scroller.clientHeight, Math.min(virtual.height, current.length * virtual.rowHeight));
|
|
455
573
|
|
|
456
|
-
function refreshWindow() {
|
|
574
|
+
function refreshWindow(force = false) {
|
|
575
|
+
if (remoteVirtual) ensureChunks();
|
|
457
576
|
const win = virtualWindow({
|
|
458
577
|
scrollTop: scroller.scrollTop, viewportHeight: viewportHeight(),
|
|
459
578
|
rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
|
|
460
579
|
});
|
|
461
|
-
if (`${win.start}:${win.end}` === windowKey) return;
|
|
580
|
+
if (!force && `${win.start}:${win.end}` === windowKey) return;
|
|
462
581
|
const focused = document.activeElement;
|
|
463
582
|
const keepFocus = focused && table.tBodies[0]?.contains(focused);
|
|
464
583
|
table.tBodies[0].replaceWith(renderBody());
|
|
@@ -470,13 +589,165 @@ export function generateTable(options = {}) {
|
|
|
470
589
|
scroller.addEventListener("scroll", () => {
|
|
471
590
|
if (queued) return;
|
|
472
591
|
queued = true;
|
|
473
|
-
|
|
592
|
+
// Once per frame; the timer is for pages where frames are paused or late (a hidden tab, a busy page).
|
|
593
|
+
let timer = 0;
|
|
594
|
+
const run = () => {
|
|
595
|
+
if (!queued) return;
|
|
474
596
|
queued = false;
|
|
597
|
+
clearTimeout(timer);
|
|
475
598
|
refreshWindow();
|
|
599
|
+
};
|
|
600
|
+
requestAnimationFrame(run);
|
|
601
|
+
timer = setTimeout(run, 100);
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* Asks the data source. { reset } starts again from the first page, { more } appends the next cursor page.
|
|
607
|
+
* An answer that arrives after a newer request was made is ignored.
|
|
608
|
+
*/
|
|
609
|
+
async function load({ reset = false, more = false } = {}) {
|
|
610
|
+
if (remoteVirtual) return loadVirtual();
|
|
611
|
+
if (reset) {
|
|
612
|
+
state.page = 0;
|
|
613
|
+
server.nextCursor = undefined;
|
|
614
|
+
}
|
|
615
|
+
const request = ++server.request;
|
|
616
|
+
server.loading = true;
|
|
617
|
+
server.error = null;
|
|
618
|
+
render();
|
|
619
|
+
try {
|
|
620
|
+
const answer = await options.dataSource({
|
|
621
|
+
page: state.page,
|
|
622
|
+
pageSize,
|
|
623
|
+
cursor: more ? server.nextCursor : undefined,
|
|
624
|
+
sort: state.sortKey ? { key: state.sortKey, descending: state.descending } : null,
|
|
625
|
+
filter: state.query
|
|
476
626
|
});
|
|
627
|
+
if (request !== server.request) return;
|
|
628
|
+
const loaded = answer?.rows ?? [];
|
|
629
|
+
server.cursorMode = answer?.nextCursor !== undefined || (more && server.cursorMode);
|
|
630
|
+
server.nextCursor = answer?.nextCursor ?? undefined;
|
|
631
|
+
server.total = answer?.total ?? null;
|
|
632
|
+
rows = more ? [...rows, ...loaded] : [...loaded];
|
|
633
|
+
if (!more) selected.clear();
|
|
634
|
+
} catch (error) {
|
|
635
|
+
if (request !== server.request) return;
|
|
636
|
+
server.error = error?.message ?? String(error);
|
|
637
|
+
if (!more) rows = [];
|
|
638
|
+
} finally {
|
|
639
|
+
if (request === server.request) {
|
|
640
|
+
server.loading = false;
|
|
641
|
+
render();
|
|
642
|
+
if (!more) selectionChanged();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/* ------------------REMOTE + VIRTUAL------------------- */
|
|
648
|
+
|
|
649
|
+
const status = remoteVirtual ? el("div", "athena-table-status") : null;
|
|
650
|
+
if (status) {
|
|
651
|
+
status.setAttribute("role", "status");
|
|
652
|
+
root.append(status);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function updateStatus() {
|
|
656
|
+
if (!status) return;
|
|
657
|
+
const failed = [...chunks.values()].includes("failed");
|
|
658
|
+
status.replaceChildren();
|
|
659
|
+
status.hidden = !failed;
|
|
660
|
+
if (!failed) return;
|
|
661
|
+
status.append(el("span", "athena-table-error", server.error ?? translate("noData")), " ");
|
|
662
|
+
const retry = el("button", "athena-button button-secondary button-small", translate("retry"));
|
|
663
|
+
retry.type = "button";
|
|
664
|
+
retry.addEventListener("click", () => {
|
|
665
|
+
for (const [chunk, value] of chunks) if (value === "failed") chunks.delete(chunk);
|
|
666
|
+
server.error = null;
|
|
667
|
+
updateStatus();
|
|
668
|
+
ensureChunks();
|
|
669
|
+
});
|
|
670
|
+
status.append(retry);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const query = () => ({ pageSize: chunkSize, cursor: undefined, sort: state.sortKey ? { key: state.sortKey, descending: state.descending } : null, filter: state.query });
|
|
674
|
+
|
|
675
|
+
function place(chunk, list) {
|
|
676
|
+
list.forEach((row, i) => {
|
|
677
|
+
const index = chunk * chunkSize + i;
|
|
678
|
+
if (index < rows.length) rows[index] = row;
|
|
477
679
|
});
|
|
478
680
|
}
|
|
479
681
|
|
|
682
|
+
// Starts again: the first chunk tells how many rows there are, the others come while the person scrolls.
|
|
683
|
+
async function loadVirtual() {
|
|
684
|
+
const generation = ++server.request;
|
|
685
|
+
clearTimeout(flushTimer);
|
|
686
|
+
chunks.clear();
|
|
687
|
+
rows = [];
|
|
688
|
+
selected.clear();
|
|
689
|
+
if (scroller) scroller.scrollTop = 0;
|
|
690
|
+
server.loading = true;
|
|
691
|
+
server.error = null;
|
|
692
|
+
render();
|
|
693
|
+
try {
|
|
694
|
+
const answer = await options.dataSource({ page: 0, ...query() });
|
|
695
|
+
if (generation !== server.request) return;
|
|
696
|
+
if (!Number.isInteger(answer?.total) || answer.total < 0) throw new Error("A data source with virtual scrolling has to answer with total");
|
|
697
|
+
rows = new Array(answer.total);
|
|
698
|
+
place(0, answer.rows ?? []);
|
|
699
|
+
chunks.set(0, "done");
|
|
700
|
+
server.total = answer.total;
|
|
701
|
+
} catch (error) {
|
|
702
|
+
if (generation !== server.request) return;
|
|
703
|
+
server.error = error?.message ?? String(error);
|
|
704
|
+
rows = [];
|
|
705
|
+
} finally {
|
|
706
|
+
if (generation === server.request) {
|
|
707
|
+
server.loading = false;
|
|
708
|
+
render();
|
|
709
|
+
selectionChanged();
|
|
710
|
+
updateStatus();
|
|
711
|
+
if (!server.error) ensureChunks();
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
async function loadChunk(chunk, generation) {
|
|
717
|
+
chunks.set(chunk, "loading");
|
|
718
|
+
try {
|
|
719
|
+
const answer = await options.dataSource({ page: chunk, ...query() });
|
|
720
|
+
if (generation !== server.request) return;
|
|
721
|
+
place(chunk, answer?.rows ?? []);
|
|
722
|
+
chunks.set(chunk, "done");
|
|
723
|
+
} catch (error) {
|
|
724
|
+
if (generation !== server.request) return;
|
|
725
|
+
chunks.set(chunk, "failed");
|
|
726
|
+
server.error = error?.message ?? String(error);
|
|
727
|
+
} finally {
|
|
728
|
+
if (generation === server.request) {
|
|
729
|
+
refreshWindow(true);
|
|
730
|
+
updateStatus();
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Asks for the chunks under the rows in view (and the rows around them). Waits a moment, so scrolling past does not ask for everything.
|
|
736
|
+
function ensureChunks() {
|
|
737
|
+
clearTimeout(flushTimer);
|
|
738
|
+
const generation = server.request;
|
|
739
|
+
flushTimer = setTimeout(() => {
|
|
740
|
+
if (generation !== server.request || !rows.length) return;
|
|
741
|
+
const win = virtualWindow({
|
|
742
|
+
scrollTop: scroller.scrollTop, viewportHeight: viewportHeight(),
|
|
743
|
+
rowHeight: virtual.rowHeight, total: rows.length, overscan: virtual.overscan
|
|
744
|
+
});
|
|
745
|
+
const first = Math.floor(win.start / chunkSize);
|
|
746
|
+
const last = Math.floor((win.end - 1) / chunkSize);
|
|
747
|
+
for (let chunk = first; chunk <= last; chunk++) if (!chunks.has(chunk)) loadChunk(chunk, generation);
|
|
748
|
+
}, 40);
|
|
749
|
+
}
|
|
750
|
+
|
|
480
751
|
// The window of rows depends on the height of the scroll area, which is unknown while the list is empty or
|
|
481
752
|
// not on screen yet: measure again whenever it changes size.
|
|
482
753
|
if (scroller && typeof ResizeObserver !== "undefined") {
|
|
@@ -490,8 +761,11 @@ export function generateTable(options = {}) {
|
|
|
490
761
|
}
|
|
491
762
|
|
|
492
763
|
render();
|
|
764
|
+
if (remote) load({ reset: true });
|
|
493
765
|
return asHandle({
|
|
494
766
|
element: root,
|
|
767
|
+
/** Asks the data source again (the first page, keeping sort and filter). Resolves when the answer is shown. */
|
|
768
|
+
reload: () => (remote ? load({ reset: true }) : Promise.resolve()),
|
|
495
769
|
// update() draws the table again; update({ rows }) also replaces the rows.
|
|
496
770
|
update(next) {
|
|
497
771
|
if (next?.rows) this.setRows(next.rows);
|
|
@@ -514,9 +788,9 @@ export function generateTable(options = {}) {
|
|
|
514
788
|
},
|
|
515
789
|
getColumnWidths: () => ({ ...widths }),
|
|
516
790
|
// The filtered and sorted rows as CSV text
|
|
517
|
-
toCsv: () => toCsv(columns, current),
|
|
791
|
+
toCsv: () => toCsv(columns, current.filter(row => row !== undefined)),
|
|
518
792
|
exportCsv(filename = "table.csv") {
|
|
519
|
-
downloadFile(filename, toCsv(columns, current));
|
|
793
|
+
downloadFile(filename, toCsv(columns, current.filter(row => row !== undefined)));
|
|
520
794
|
}
|
|
521
795
|
});
|
|
522
796
|
}
|
package/src/menu.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//navigation entry of Athena UI: tabs, breadcrumbs, pagination, accordion, dropdown, sidebar
|
|
2
|
+
//Import only what you use: `import { generateTable } from "athena-ui-js/navigation"`.
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
export * from "./libs/hephaestus/navigationGenerator/navigationGenerator.js";
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/* Athena UI appearance: themes and modes (light / dark).
|
|
2
|
+
|
|
3
|
+
THE CONTRACT
|
|
4
|
+
------------
|
|
5
|
+
createThemeManager() (and theme-boot.js, before the page is drawn) put two attributes on <html>:
|
|
6
|
+
|
|
7
|
+
<html data-theme="athena" data-mode="dark">
|
|
8
|
+
|
|
9
|
+
data-theme is the id of a theme, data-mode is "light" or "dark" (the choice "Like the system" is resolved to one of them).
|
|
10
|
+
A theme is a pair of blocks that set the design tokens below. Nothing else is needed:
|
|
11
|
+
|
|
12
|
+
[data-theme="ocean"][data-mode="light"] { --athena-bg: #f4f9fb; ... }
|
|
13
|
+
[data-theme="ocean"][data-mode="dark"] { --athena-bg: #0b1c24; ... }
|
|
14
|
+
|
|
15
|
+
The selectors also match any other element that has the two attributes, so a preview (generateThemePicker) or one dark panel on a
|
|
16
|
+
light page is just an element with data-theme and data-mode.
|
|
17
|
+
|
|
18
|
+
TOKENS
|
|
19
|
+
------
|
|
20
|
+
--athena-bg / --athena-fg page background and text
|
|
21
|
+
--athena-muted secondary text (keep 4.5 : 1 against --athena-bg)
|
|
22
|
+
--athena-line / --athena-border borders and dividers
|
|
23
|
+
--athena-surface / -alt cards, table heads, striped rows
|
|
24
|
+
--athena-hover hovered and selected rows, menu items
|
|
25
|
+
--athena-overlay the dim layer behind a dialog
|
|
26
|
+
--athena-accent / -fg primary buttons, the active menu item, and the text on them
|
|
27
|
+
--athena-focus outline of the element that has the keyboard focus
|
|
28
|
+
--athena-danger / success / warning / info states
|
|
29
|
+
--athena-shadow elevation of dialogs and menus
|
|
30
|
+
--series-1 ... --series-8 the colours of charts, in a fixed order
|
|
31
|
+
|
|
32
|
+
To use it: load this file after style.css, put the attributes on <html> (theme-boot.js + createThemeManager), and add your own
|
|
33
|
+
themes as more pairs of blocks. Without the script the page follows the system (the media query at the end). */
|
|
34
|
+
|
|
35
|
+
/* ------------------------------------------------------------ Athena */
|
|
36
|
+
|
|
37
|
+
[data-theme="athena"][data-mode="light"] {
|
|
38
|
+
color-scheme: light;
|
|
39
|
+
--athena-bg: #fff;
|
|
40
|
+
--athena-fg: #1a1a1a;
|
|
41
|
+
--athena-muted: #5c5c5c;
|
|
42
|
+
--athena-border: #000;
|
|
43
|
+
--athena-line: #ccc;
|
|
44
|
+
--athena-surface: #f2f2f2;
|
|
45
|
+
--athena-surface-alt: #fafafa;
|
|
46
|
+
--athena-hover: #e8f0fe;
|
|
47
|
+
--athena-overlay: rgb(0 0 0 / 40%);
|
|
48
|
+
--athena-accent: #2f6db5;
|
|
49
|
+
--athena-accent-fg: #fff;
|
|
50
|
+
--athena-focus: #1b4f8f;
|
|
51
|
+
--athena-danger: #c0392b;
|
|
52
|
+
--athena-success: #1e8449;
|
|
53
|
+
--athena-warning: #9a6700;
|
|
54
|
+
--athena-info: #2471a3;
|
|
55
|
+
--athena-shadow: 0 4px 14px rgb(0 0 0 / 18%);
|
|
56
|
+
--series-1: #4e79a7; --series-2: #d9731a; --series-3: #d0393b; --series-4: #3f9a95;
|
|
57
|
+
--series-5: #3f8a3a; --series-6: #a98a00; --series-7: #94598c; --series-8: #5f6878;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
[data-theme="athena"][data-mode="dark"] {
|
|
61
|
+
color-scheme: dark;
|
|
62
|
+
--athena-bg: #16181d;
|
|
63
|
+
--athena-fg: #e6e8eb;
|
|
64
|
+
--athena-muted: #9aa3ad;
|
|
65
|
+
--athena-border: #3a3f47;
|
|
66
|
+
--athena-line: #3a3f47;
|
|
67
|
+
--athena-surface: #23272e;
|
|
68
|
+
--athena-surface-alt: #1c1f25;
|
|
69
|
+
--athena-hover: #2b3340;
|
|
70
|
+
--athena-overlay: rgb(0 0 0 / 65%);
|
|
71
|
+
--athena-accent: #5b9bd5;
|
|
72
|
+
--athena-accent-fg: #0b1118;
|
|
73
|
+
--athena-focus: #8cc0ee;
|
|
74
|
+
--athena-danger: #ef6b5e;
|
|
75
|
+
--athena-success: #4cc38a;
|
|
76
|
+
--athena-warning: #e0b04a;
|
|
77
|
+
--athena-info: #5b9bd5;
|
|
78
|
+
--athena-shadow: 0 4px 14px rgb(0 0 0 / 55%);
|
|
79
|
+
--series-1: #6fa3d6; --series-2: #f0a04b; --series-3: #ef7b7d; --series-4: #6fcfc8;
|
|
80
|
+
--series-5: #7bc576; --series-6: #f1d36a; --series-7: #c79bc0; --series-8: #a9b1c0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* ------------------------------------------------------------ High contrast */
|
|
84
|
+
|
|
85
|
+
[data-theme="contrast"][data-mode="light"] {
|
|
86
|
+
color-scheme: light;
|
|
87
|
+
--athena-bg: #fff;
|
|
88
|
+
--athena-fg: #000;
|
|
89
|
+
--athena-muted: #333;
|
|
90
|
+
--athena-border: #000;
|
|
91
|
+
--athena-line: #000;
|
|
92
|
+
--athena-surface: #fff;
|
|
93
|
+
--athena-surface-alt: #f0f0f0;
|
|
94
|
+
--athena-hover: #ffe97a;
|
|
95
|
+
--athena-overlay: rgb(0 0 0 / 70%);
|
|
96
|
+
--athena-accent: #00389a;
|
|
97
|
+
--athena-accent-fg: #fff;
|
|
98
|
+
--athena-focus: #b00020;
|
|
99
|
+
--athena-danger: #a00000;
|
|
100
|
+
--athena-success: #005a1e;
|
|
101
|
+
--athena-warning: #6b4500;
|
|
102
|
+
--athena-info: #00389a;
|
|
103
|
+
--athena-shadow: none;
|
|
104
|
+
--series-1: #00389a; --series-2: #a84300; --series-3: #a00000; --series-4: #006064;
|
|
105
|
+
--series-5: #005a1e; --series-6: #6b4500; --series-7: #6a1b9a; --series-8: #333;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
[data-theme="contrast"][data-mode="dark"] {
|
|
109
|
+
color-scheme: dark;
|
|
110
|
+
--athena-bg: #000;
|
|
111
|
+
--athena-fg: #fff;
|
|
112
|
+
--athena-muted: #d0d0d0;
|
|
113
|
+
--athena-border: #fff;
|
|
114
|
+
--athena-line: #fff;
|
|
115
|
+
--athena-surface: #000;
|
|
116
|
+
--athena-surface-alt: #1a1a1a;
|
|
117
|
+
--athena-hover: #3a3000;
|
|
118
|
+
--athena-overlay: rgb(0 0 0 / 80%);
|
|
119
|
+
--athena-accent: #ffe14d;
|
|
120
|
+
--athena-accent-fg: #000;
|
|
121
|
+
--athena-focus: #ffe14d;
|
|
122
|
+
--athena-danger: #ff8a80;
|
|
123
|
+
--athena-success: #69f0ae;
|
|
124
|
+
--athena-warning: #ffd740;
|
|
125
|
+
--athena-info: #80d8ff;
|
|
126
|
+
--athena-shadow: none;
|
|
127
|
+
--series-1: #80d8ff; --series-2: #ffd740; --series-3: #ff8a80; --series-4: #69f0ae;
|
|
128
|
+
--series-5: #ea80fc; --series-6: #ffab40; --series-7: #b9f6ca; --series-8: #e0e0e0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/* ------------------------------------------------------------ Without the script: follow the system */
|
|
132
|
+
|
|
133
|
+
@media (prefers-color-scheme: dark) {
|
|
134
|
+
:root:not([data-mode]) {
|
|
135
|
+
color-scheme: dark;
|
|
136
|
+
--athena-bg: #16181d;
|
|
137
|
+
--athena-fg: #e6e8eb;
|
|
138
|
+
--athena-muted: #9aa3ad;
|
|
139
|
+
--athena-border: #3a3f47;
|
|
140
|
+
--athena-line: #3a3f47;
|
|
141
|
+
--athena-surface: #23272e;
|
|
142
|
+
--athena-surface-alt: #1c1f25;
|
|
143
|
+
--athena-hover: #2b3340;
|
|
144
|
+
--athena-overlay: rgb(0 0 0 / 65%);
|
|
145
|
+
--athena-accent: #5b9bd5;
|
|
146
|
+
--athena-accent-fg: #0b1118;
|
|
147
|
+
--athena-focus: #8cc0ee;
|
|
148
|
+
--athena-danger: #ef6b5e;
|
|
149
|
+
--athena-success: #4cc38a;
|
|
150
|
+
--athena-warning: #e0b04a;
|
|
151
|
+
--athena-info: #5b9bd5;
|
|
152
|
+
--athena-shadow: 0 4px 14px rgb(0 0 0 / 55%);
|
|
153
|
+
}
|
|
154
|
+
}
|