athena-ui-js 0.4.1 → 1.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.
Files changed (39) hide show
  1. package/README.md +352 -11
  2. package/package.json +84 -60
  3. package/src/athena-ui.js +68 -1
  4. package/src/libs/config/config.js +15 -1
  5. package/src/libs/config/hooks.js +15 -3
  6. package/src/libs/core/combobox.js +179 -0
  7. package/src/libs/core/csv.js +37 -0
  8. package/src/libs/core/dateRange.js +50 -0
  9. package/src/libs/core/dom.js +57 -0
  10. package/src/libs/core/focus.js +49 -0
  11. package/src/libs/core/format.js +31 -0
  12. package/src/libs/core/i18n.js +160 -0
  13. package/src/libs/core/pagination.js +24 -0
  14. package/src/libs/core/sanitize.js +65 -0
  15. package/src/libs/core/tableMath.js +31 -0
  16. package/src/libs/core/validation.js +79 -0
  17. package/src/libs/hephaestus/MenuGenerator/basicMenuGenerator.js +81 -0
  18. package/src/libs/hephaestus/auth/session.js +68 -0
  19. package/src/libs/hephaestus/chartGenerator/chartGenerator.js +187 -0
  20. package/src/libs/hephaestus/componentGenerator/componentGenerator.js +201 -0
  21. package/src/libs/hephaestus/dialogGenerator/dialogGenerator.js +126 -0
  22. package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +138 -0
  23. package/src/libs/hephaestus/formGenerator/changePasswordFormGenerator.js +50 -0
  24. package/src/libs/hephaestus/formGenerator/formBuilder.js +379 -0
  25. package/src/libs/hephaestus/formGenerator/formUtils.js +66 -0
  26. package/src/libs/hephaestus/formGenerator/loginFormGenerator.js +20 -6
  27. package/src/libs/hephaestus/formGenerator/lostPasswordFormGenerator.js +30 -0
  28. package/src/libs/hephaestus/formGenerator/registrationFormGenerator.js +54 -0
  29. package/src/libs/hephaestus/hephaestus.js +8 -1
  30. package/src/libs/hephaestus/inputGenerator/inputGenerator.js +299 -0
  31. package/src/libs/hephaestus/inputGenerator/richTextGenerator.js +222 -0
  32. package/src/libs/hephaestus/modalGenerator/modalGenerator.js +13 -2
  33. package/src/libs/hephaestus/navigationGenerator/navigationGenerator.js +388 -0
  34. package/src/libs/hephaestus/router/matchRoute.js +42 -0
  35. package/src/libs/hephaestus/router/router.js +102 -0
  36. package/src/libs/hephaestus/tableGenerator/tableGenerator.js +457 -0
  37. package/src/res/themes/dark/style.css +31 -0
  38. package/src/res/themes/default/style.css +1388 -7
  39. package/src/res/themes/themes.js +6 -0
@@ -0,0 +1,457 @@
1
+ //tableGenerator for Athena UI
2
+ //Author: Csaba Tóth https://github.com/csabatibortoth/
3
+ //LICENCE: MIT
4
+ //Repository: https://github.com/csabatibortoth/athena-ui
5
+
6
+ import { downloadFile, toCsv } from "../../core/csv.js";
7
+ import { translate } from "../../core/i18n.js";
8
+ import { clampWidth, virtualWindow } from "../../core/tableMath.js";
9
+
10
+ const el = (tag, className, text) => {
11
+ const element = document.createElement(tag);
12
+ if (className) element.className = className;
13
+ if (text != null) element.textContent = text;
14
+ return element;
15
+ };
16
+
17
+ const compare = (a, b) =>
18
+ typeof a === "number" && typeof b === "number"
19
+ ? a - b
20
+ : String(a ?? "").localeCompare(String(b ?? ""), undefined, { numeric: true });
21
+
22
+ /**
23
+ * Generates a table (optional filter box, sortable headers, pagination, row selection, inline
24
+ * editing, resizable columns, virtual scrolling, CSV export) and appends it to its parent.
25
+ *
26
+ * @param {object} options
27
+ * @param {string|Element} [options.parent] id or element, defaults to "wrapper"
28
+ * @param {string} [options.id] id of the table wrapper (default: "table")
29
+ * @param {string} [options.class] extra CSS class of the table
30
+ * @param {string} [options.caption]
31
+ * @param {{key: string, label?: string, width?: number, sortable?: boolean, editable?: boolean, format?: (value, row) => string}[]} options.columns
32
+ * @param {object[]} [options.rows] row objects, read by column key
33
+ * @param {boolean} [options.sortable=true] click on a header sorts by it
34
+ * @param {boolean} [options.filter=false] show a text filter above the table
35
+ * @param {number} [options.pageSize=0] rows per page, 0 disables pagination
36
+ * @param {boolean} [options.selectable=false] checkbox column; the header checkbox selects every filtered row
37
+ * @param {boolean} [options.resizableColumns=false] drag the edge of a header (or use the arrow keys on it) to resize;
38
+ * double click resets the width
39
+ * @param {{height?: number, rowHeight?: number, overscan?: number}|boolean} [options.virtual] render only the rows
40
+ * in view, for lists of thousands of rows. Rows have a fixed height (default 36 px) and the table scrolls
41
+ * inside `height` (default 400 px). Replaces pagination.
42
+ * @param {(key: string, width: number) => void} [options.onColumnResize]
43
+ * @param {(rows: object[]) => void} [options.onSelectionChange]
44
+ * @param {(row: object, key: string, value, oldValue) => (boolean|void)} [options.onEdit] called after an edit
45
+ * of an `editable` column (double click, or Enter / F2 on the focused cell). Return false to reject it.
46
+ * @param {(row: object) => void} [options.onRowClick]
47
+ * @param {string} [options.emptyText]
48
+ * @returns {{element: HTMLElement, setRows: Function, getSelected: () => object[], clearSelection: Function,
49
+ * getColumnWidths: () => Object<string, number>, toCsv: () => string, exportCsv: (filename?: string) => void}}
50
+ */
51
+ export function generateTable(options = {}) {
52
+ const {
53
+ parent = "wrapper", id = "table", columns = [], sortable = true,
54
+ filter = false, selectable = false, resizableColumns = false, onRowClick, onSelectionChange
55
+ } = options;
56
+ const virtual = options.virtual
57
+ ? { height: 400, rowHeight: 36, overscan: 5, ...(options.virtual === true ? {} : options.virtual) }
58
+ : null;
59
+ const pageSize = virtual ? 0 : (options.pageSize ?? 0);
60
+ const host = (typeof parent === "string" ? document.getElementById(parent) : parent) ?? document.body;
61
+ let rows = [...(options.rows ?? [])];
62
+ let current = rows; // filtered and sorted rows
63
+ const selected = new Set();
64
+ const widths = {}; // column key -> px, set by the user (or by options)
65
+ for (const col of columns) if (col.width) widths[col.key] = col.width;
66
+ const state = { sortKey: null, descending: false, page: 0, query: "" };
67
+ let windowKey = "";
68
+
69
+ const root = el("div", "athena-table-wrapper");
70
+ root.id = id;
71
+ const table = el("table", `athena-table ${options.class ?? ""}`.trim());
72
+ if (resizableColumns) table.classList.add("athena-table-resizable");
73
+ let scroller = null;
74
+ if (virtual) {
75
+ table.classList.add("athena-table-virtual");
76
+ scroller = el("div", "athena-table-scroll");
77
+ scroller.style.maxHeight = `${virtual.height}px`;
78
+ scroller.style.setProperty("--row-height", `${virtual.rowHeight}px`);
79
+ scroller.tabIndex = 0;
80
+ scroller.setAttribute("role", "region");
81
+ scroller.setAttribute("aria-label", options.caption ?? id);
82
+ }
83
+ if (filter) {
84
+ const filterInput = el("input", "athena-table-filter");
85
+ filterInput.type = "search";
86
+ filterInput.placeholder = translate("filter");
87
+ filterInput.setAttribute("aria-label", translate("filter"));
88
+ filterInput.addEventListener("input", () => {
89
+ state.query = filterInput.value.toLowerCase();
90
+ state.page = 0;
91
+ if (scroller) scroller.scrollTop = 0;
92
+ render();
93
+ });
94
+ root.append(filterInput);
95
+ }
96
+ if (scroller) {
97
+ scroller.append(table);
98
+ root.append(scroller);
99
+ } else {
100
+ root.append(table);
101
+ }
102
+ const pager = pageSize > 0 ? el("div", "athena-table-pager") : null;
103
+ if (pager) root.append(pager);
104
+ host.append(root);
105
+
106
+ function visibleRows() {
107
+ let result = rows;
108
+ if (state.query) {
109
+ result = result.filter(row =>
110
+ columns.some(col => String(row[col.key] ?? "").toLowerCase().includes(state.query)));
111
+ }
112
+ if (state.sortKey) {
113
+ const dir = state.descending ? -1 : 1;
114
+ result = [...result].sort((a, b) => dir * compare(a[state.sortKey], b[state.sortKey]));
115
+ }
116
+ return result;
117
+ }
118
+
119
+ const selectionChanged = () => onSelectionChange?.([...selected]);
120
+
121
+ /* ------------------COLUMN WIDTHS------------------- */
122
+
123
+ const totalWidth = () => Object.keys(widths).length === columns.length
124
+ ? columns.reduce((sum, col) => sum + widths[col.key], 0) + (selectable ? 40 : 0)
125
+ : 0;
126
+
127
+ function renderColgroup() {
128
+ const group = el("colgroup");
129
+ if (selectable) {
130
+ const col = el("col");
131
+ col.style.width = "40px";
132
+ group.append(col);
133
+ }
134
+ for (const column of columns) {
135
+ const col = el("col");
136
+ if (widths[column.key]) col.style.width = `${widths[column.key]}px`;
137
+ group.append(col);
138
+ }
139
+ return group;
140
+ }
141
+
142
+ // Freezes the current widths, so dragging one edge does not move the others.
143
+ function freezeWidths() {
144
+ if (Object.keys(widths).length === columns.length) return;
145
+ const heads = table.querySelectorAll("thead th:not(.athena-table-select)");
146
+ columns.forEach((col, i) => { widths[col.key] ??= clampWidth(heads[i]?.offsetWidth ?? 120); });
147
+ }
148
+
149
+ function applyWidths() {
150
+ const group = table.querySelector("colgroup");
151
+ if (!group) return;
152
+ const cols = [...group.children].slice(selectable ? 1 : 0);
153
+ columns.forEach((col, i) => { if (widths[col.key]) cols[i].style.width = `${widths[col.key]}px`; });
154
+ // With every width known the table is laid out by them, so dragging one edge moves nothing else.
155
+ const total = totalWidth();
156
+ table.style.tableLayout = total ? "fixed" : "";
157
+ table.style.width = total ? `${total}px` : "";
158
+ }
159
+
160
+ function setWidth(col, width) {
161
+ freezeWidths();
162
+ widths[col.key] = clampWidth(width, col.minWidth ?? 40, col.maxWidth ?? 1200);
163
+ applyWidths();
164
+ options.onColumnResize?.(col.key, widths[col.key]);
165
+ }
166
+
167
+ function makeResizer(th, col) {
168
+ const handle = el("span", "athena-col-resizer");
169
+ handle.setAttribute("role", "separator");
170
+ handle.setAttribute("aria-orientation", "vertical");
171
+ handle.setAttribute("aria-label", `${translate("resizeColumn")}: ${col.label ?? col.key}`);
172
+ handle.tabIndex = 0;
173
+ // The handle sits inside a sortable header, so it must not trigger the sort.
174
+ handle.addEventListener("click", e => e.stopPropagation());
175
+ handle.addEventListener("keydown", e => {
176
+ e.stopPropagation();
177
+ const step = e.shiftKey ? 50 : 10;
178
+ if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
179
+ e.preventDefault();
180
+ freezeWidths();
181
+ setWidth(col, widths[col.key] + (e.key === "ArrowRight" ? step : -step));
182
+ }
183
+ });
184
+ handle.addEventListener("dblclick", e => {
185
+ e.stopPropagation();
186
+ delete widths[col.key];
187
+ if (col.width) widths[col.key] = col.width;
188
+ if (!Object.keys(widths).length) {
189
+ table.style.tableLayout = "";
190
+ table.style.width = "";
191
+ }
192
+ render();
193
+ });
194
+ handle.addEventListener("pointerdown", e => {
195
+ e.preventDefault();
196
+ e.stopPropagation();
197
+ freezeWidths();
198
+ const startX = e.clientX;
199
+ const startWidth = widths[col.key];
200
+ try {
201
+ handle.setPointerCapture(e.pointerId);
202
+ } catch { /* the pointer is gone already */ }
203
+ handle.classList.add("resizing");
204
+ const move = ev => setWidth(col, startWidth + (ev.clientX - startX));
205
+ const stop = () => {
206
+ handle.classList.remove("resizing");
207
+ handle.removeEventListener("pointermove", move);
208
+ handle.removeEventListener("pointerup", stop);
209
+ handle.removeEventListener("pointercancel", stop);
210
+ };
211
+ handle.addEventListener("pointermove", move);
212
+ handle.addEventListener("pointerup", stop);
213
+ handle.addEventListener("pointercancel", stop);
214
+ });
215
+ th.append(handle);
216
+ }
217
+
218
+ /* ------------------RENDER------------------- */
219
+
220
+ function renderHead(all) {
221
+ const tr = el("tr");
222
+ if (selectable) {
223
+ const th = el("th", "athena-table-select");
224
+ const box = el("input");
225
+ box.type = "checkbox";
226
+ box.setAttribute("aria-label", translate("selectAll"));
227
+ const chosen = all.filter(row => selected.has(row)).length;
228
+ box.checked = all.length > 0 && chosen === all.length;
229
+ box.indeterminate = chosen > 0 && chosen < all.length;
230
+ box.addEventListener("change", () => {
231
+ all.forEach(row => (box.checked ? selected.add(row) : selected.delete(row)));
232
+ selectionChanged();
233
+ render();
234
+ });
235
+ th.append(box);
236
+ tr.append(th);
237
+ }
238
+ for (const col of columns) {
239
+ const th = el("th", null, col.label ?? col.key);
240
+ th.scope = "col";
241
+ if (sortable && col.sortable !== false) {
242
+ th.classList.add("sortable");
243
+ th.tabIndex = 0;
244
+ const sort = () => {
245
+ state.descending = state.sortKey === col.key && !state.descending;
246
+ state.sortKey = col.key;
247
+ render();
248
+ };
249
+ th.addEventListener("click", sort);
250
+ th.addEventListener("keydown", e => {
251
+ if (e.target === th && (e.key === "Enter" || e.key === " ")) {
252
+ e.preventDefault();
253
+ sort();
254
+ }
255
+ });
256
+ if (state.sortKey === col.key) {
257
+ th.append(el("span", "sort-indicator", state.descending ? " ▼" : " ▲"));
258
+ th.setAttribute("aria-sort", state.descending ? "descending" : "ascending");
259
+ }
260
+ }
261
+ if (resizableColumns) makeResizer(th, col);
262
+ tr.append(th);
263
+ }
264
+ const thead = el("thead");
265
+ thead.append(tr);
266
+ return thead;
267
+ }
268
+
269
+ // Replaces the cell content with an input until the edit is confirmed (Enter, blur) or cancelled (Esc).
270
+ function startEdit(td, row, col) {
271
+ if (td.querySelector("input")) return;
272
+ const oldValue = row[col.key];
273
+ const input = el("input", "athena-table-edit");
274
+ input.type = typeof oldValue === "number" ? "number" : "text";
275
+ input.value = oldValue ?? "";
276
+ let finished = false;
277
+ const finish = commit => {
278
+ if (finished) return;
279
+ finished = true;
280
+ if (commit && input.value !== String(oldValue ?? "")) {
281
+ const value = typeof oldValue === "number" && input.value !== "" ? Number(input.value) : input.value;
282
+ row[col.key] = value;
283
+ if (options.onEdit?.(row, col.key, value, oldValue) === false) row[col.key] = oldValue;
284
+ }
285
+ render();
286
+ };
287
+ input.addEventListener("keydown", e => {
288
+ if (e.key === "Enter") finish(true);
289
+ else if (e.key === "Escape") {
290
+ e.stopPropagation();
291
+ finish(false);
292
+ }
293
+ });
294
+ input.addEventListener("blur", () => finish(true));
295
+ td.replaceChildren(input);
296
+ input.focus();
297
+ input.select();
298
+ }
299
+
300
+ function spacer(height) {
301
+ const tr = el("tr", "athena-table-spacer");
302
+ tr.setAttribute("aria-hidden", "true");
303
+ tr.style.height = `${height}px`;
304
+ const td = el("td");
305
+ td.colSpan = columns.length + (selectable ? 1 : 0);
306
+ tr.append(td);
307
+ return tr;
308
+ }
309
+
310
+ function renderRow(row, index) {
311
+ const tr = el("tr");
312
+ if (virtual) tr.setAttribute("aria-rowindex", String(index + 2));
313
+ if (onRowClick) {
314
+ tr.classList.add("clickable");
315
+ tr.addEventListener("click", () => onRowClick(row));
316
+ }
317
+ if (selectable) {
318
+ const td = el("td", "athena-table-select");
319
+ const box = el("input");
320
+ box.type = "checkbox";
321
+ box.checked = selected.has(row);
322
+ tr.classList.toggle("selected", box.checked);
323
+ box.addEventListener("click", e => e.stopPropagation());
324
+ box.addEventListener("change", () => {
325
+ if (box.checked) selected.add(row);
326
+ else selected.delete(row);
327
+ selectionChanged();
328
+ render();
329
+ });
330
+ td.append(box);
331
+ tr.append(td);
332
+ }
333
+ for (const col of columns) {
334
+ const value = row[col.key];
335
+ const td = el("td", null, col.format ? col.format(value, row) : (value ?? ""));
336
+ if (col.editable) {
337
+ td.classList.add("editable");
338
+ td.tabIndex = 0;
339
+ td.addEventListener("dblclick", () => startEdit(td, row, col));
340
+ td.addEventListener("keydown", e => {
341
+ if ((e.key === "Enter" || e.key === "F2") && e.target === td) {
342
+ e.preventDefault();
343
+ startEdit(td, row, col);
344
+ }
345
+ });
346
+ }
347
+ tr.append(td);
348
+ }
349
+ return tr;
350
+ }
351
+
352
+ function renderBody() {
353
+ const tbody = el("tbody");
354
+ if (!current.length) {
355
+ const td = el("td", "athena-table-empty", options.emptyText ?? translate("noData"));
356
+ td.colSpan = Math.max(columns.length + (selectable ? 1 : 0), 1);
357
+ const tr = el("tr");
358
+ tr.append(td);
359
+ tbody.append(tr);
360
+ return tbody;
361
+ }
362
+ if (virtual) {
363
+ const win = virtualWindow({
364
+ scrollTop: scroller.scrollTop, viewportHeight: scroller.clientHeight || virtual.height,
365
+ rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
366
+ });
367
+ windowKey = `${win.start}:${win.end}`;
368
+ if (win.offsetTop) tbody.append(spacer(win.offsetTop));
369
+ for (let i = win.start; i < win.end; i++) tbody.append(renderRow(current[i], i));
370
+ if (win.offsetBottom) tbody.append(spacer(win.offsetBottom));
371
+ return tbody;
372
+ }
373
+ const pageRows = pageSize > 0 ? current.slice(state.page * pageSize, (state.page + 1) * pageSize) : current;
374
+ for (const row of pageRows) tbody.append(renderRow(row));
375
+ return tbody;
376
+ }
377
+
378
+ function renderPager(total) {
379
+ if (!pager) return;
380
+ const pages = Math.max(1, Math.ceil(total / pageSize));
381
+ state.page = Math.min(state.page, pages - 1);
382
+ const button = (text, disabled, delta) => {
383
+ const b = el("button", null, text);
384
+ b.type = "button";
385
+ b.disabled = disabled;
386
+ b.addEventListener("click", () => {
387
+ state.page += delta;
388
+ render();
389
+ });
390
+ return b;
391
+ };
392
+ pager.replaceChildren(
393
+ button(`‹ ${translate("previous")}`, state.page === 0, -1),
394
+ el("span", null, ` ${translate("page", { page: state.page + 1, pages })} `),
395
+ button(`${translate("next")} ›`, state.page >= pages - 1, 1)
396
+ );
397
+ }
398
+
399
+ function render() {
400
+ const scrollTop = scroller?.scrollTop ?? 0;
401
+ current = visibleRows();
402
+ renderPager(current.length);
403
+ if (virtual) table.setAttribute("aria-rowcount", String(current.length + 1));
404
+ const parts = [renderHead(current), renderBody()];
405
+ if (resizableColumns || Object.keys(widths).length) parts.unshift(renderColgroup());
406
+ if (options.caption) parts.unshift(el("caption", null, options.caption));
407
+ table.replaceChildren(...parts);
408
+ if (Object.keys(widths).length) applyWidths();
409
+ if (scroller) scroller.scrollTop = scrollTop;
410
+ }
411
+
412
+ // Scrolling only swaps the body, and only when the window of rows really changed.
413
+ if (scroller) {
414
+ let queued = false;
415
+ scroller.addEventListener("scroll", () => {
416
+ if (queued) return;
417
+ queued = true;
418
+ requestAnimationFrame(() => {
419
+ queued = false;
420
+ const win = virtualWindow({
421
+ scrollTop: scroller.scrollTop, viewportHeight: scroller.clientHeight || virtual.height,
422
+ rowHeight: virtual.rowHeight, total: current.length, overscan: virtual.overscan
423
+ });
424
+ if (`${win.start}:${win.end}` === windowKey) return;
425
+ const focused = document.activeElement;
426
+ const keepFocus = focused && table.tBodies[0]?.contains(focused);
427
+ table.tBodies[0].replaceWith(renderBody());
428
+ if (keepFocus) scroller.focus({ preventScroll: true });
429
+ });
430
+ });
431
+ }
432
+
433
+ render();
434
+ return {
435
+ element: root,
436
+ setRows(newRows) {
437
+ rows = [...newRows];
438
+ selected.clear();
439
+ state.page = 0;
440
+ if (scroller) scroller.scrollTop = 0;
441
+ render();
442
+ selectionChanged();
443
+ },
444
+ getSelected: () => [...selected],
445
+ clearSelection() {
446
+ selected.clear();
447
+ render();
448
+ selectionChanged();
449
+ },
450
+ getColumnWidths: () => ({ ...widths }),
451
+ // The filtered and sorted rows as CSV text
452
+ toCsv: () => toCsv(columns, current),
453
+ exportCsv(filename = "table.csv") {
454
+ downloadFile(filename, toCsv(columns, current));
455
+ }
456
+ };
457
+ }
@@ -0,0 +1,31 @@
1
+ /* Dark Athena UI Theme: the default theme with other colors */
2
+
3
+ @import url("../default/style.css");
4
+
5
+ :root {
6
+ color-scheme: dark;
7
+ --athena-bg: #16181d;
8
+ --athena-fg: #e6e8eb;
9
+ --athena-muted: #9aa3ad;
10
+ --athena-border: #3a3f47;
11
+ --athena-line: #3a3f47;
12
+ --athena-surface: #23272e;
13
+ --athena-surface-alt: #1c1f25;
14
+ --athena-hover: #2b3340;
15
+ --athena-overlay: rgb(0 0 0 / 65%);
16
+ --athena-accent: #5b9bd5;
17
+ --athena-accent-fg: #0b1118;
18
+ --athena-danger: #ef6b5e;
19
+ --athena-success: #4cc38a;
20
+ --athena-warning: #e0b04a;
21
+ --athena-info: #5b9bd5;
22
+ --athena-shadow: 0 4px 14px rgb(0 0 0 / 55%);
23
+ }
24
+
25
+ .button-danger,
26
+ .badge-info,
27
+ .badge-success,
28
+ .badge-warning,
29
+ .badge-error {
30
+ color: #0b1118;
31
+ }