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.
- package/README.md +352 -11
- package/package.json +84 -60
- package/src/athena-ui.js +68 -1
- package/src/libs/config/config.js +15 -1
- package/src/libs/config/hooks.js +15 -3
- package/src/libs/core/combobox.js +179 -0
- package/src/libs/core/csv.js +37 -0
- package/src/libs/core/dateRange.js +50 -0
- package/src/libs/core/dom.js +57 -0
- package/src/libs/core/focus.js +49 -0
- package/src/libs/core/format.js +31 -0
- package/src/libs/core/i18n.js +160 -0
- package/src/libs/core/pagination.js +24 -0
- package/src/libs/core/sanitize.js +65 -0
- package/src/libs/core/tableMath.js +31 -0
- package/src/libs/core/validation.js +79 -0
- package/src/libs/hephaestus/MenuGenerator/basicMenuGenerator.js +81 -0
- package/src/libs/hephaestus/auth/session.js +68 -0
- package/src/libs/hephaestus/chartGenerator/chartGenerator.js +187 -0
- package/src/libs/hephaestus/componentGenerator/componentGenerator.js +201 -0
- package/src/libs/hephaestus/dialogGenerator/dialogGenerator.js +126 -0
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +138 -0
- package/src/libs/hephaestus/formGenerator/changePasswordFormGenerator.js +50 -0
- package/src/libs/hephaestus/formGenerator/formBuilder.js +379 -0
- package/src/libs/hephaestus/formGenerator/formUtils.js +66 -0
- package/src/libs/hephaestus/formGenerator/loginFormGenerator.js +20 -6
- package/src/libs/hephaestus/formGenerator/lostPasswordFormGenerator.js +30 -0
- package/src/libs/hephaestus/formGenerator/registrationFormGenerator.js +54 -0
- package/src/libs/hephaestus/hephaestus.js +8 -1
- package/src/libs/hephaestus/inputGenerator/inputGenerator.js +299 -0
- package/src/libs/hephaestus/inputGenerator/richTextGenerator.js +222 -0
- package/src/libs/hephaestus/modalGenerator/modalGenerator.js +13 -2
- package/src/libs/hephaestus/navigationGenerator/navigationGenerator.js +388 -0
- package/src/libs/hephaestus/router/matchRoute.js +42 -0
- package/src/libs/hephaestus/router/router.js +102 -0
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +457 -0
- package/src/res/themes/dark/style.css +31 -0
- package/src/res/themes/default/style.css +1388 -7
- package/src/res/themes/themes.js +6 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
//navigationGenerator 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 { cx, el, mount, setContent, uid } from "../../core/dom.js";
|
|
7
|
+
import { translate } from "../../core/i18n.js";
|
|
8
|
+
import { pageRange } from "../../core/pagination.js";
|
|
9
|
+
|
|
10
|
+
/* ------------------TABS------------------- */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generates tabs. Arrow keys, Home and End move between the tabs.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} options
|
|
16
|
+
* @param {string|Element} [options.parent]
|
|
17
|
+
* @param {string} [options.id]
|
|
18
|
+
* @param {{id: string, label: string, content: *, disabled?: boolean}[]} options.tabs content is text, a Node,
|
|
19
|
+
* an array, or a function called once when the tab is first shown (lazy content)
|
|
20
|
+
* @param {string} [options.active] id of the initially active tab (default: the first)
|
|
21
|
+
* @param {(id: string) => void} [options.onChange]
|
|
22
|
+
* @returns {{element: HTMLElement, select: (id: string) => void, getActive: () => string}}
|
|
23
|
+
*/
|
|
24
|
+
export function generateTabs(options = {}) {
|
|
25
|
+
const tabs = options.tabs ?? [];
|
|
26
|
+
const list = el("div", { class: "tab-list", attrs: { role: "tablist" } });
|
|
27
|
+
const element = el("div", { id: options.id, class: cx("tabs", options.class) }, list);
|
|
28
|
+
const base = uid("tabs");
|
|
29
|
+
const buttons = new Map();
|
|
30
|
+
const panels = new Map();
|
|
31
|
+
const rendered = new Set();
|
|
32
|
+
let active;
|
|
33
|
+
|
|
34
|
+
for (const tab of tabs) {
|
|
35
|
+
const button = el("button", {
|
|
36
|
+
class: "tab",
|
|
37
|
+
text: tab.label,
|
|
38
|
+
id: `${base}-${tab.id}-tab`,
|
|
39
|
+
attrs: { type: "button", role: "tab", "aria-controls": `${base}-${tab.id}-panel`, disabled: tab.disabled }
|
|
40
|
+
});
|
|
41
|
+
const panel = el("div", {
|
|
42
|
+
class: "tab-panel",
|
|
43
|
+
id: `${base}-${tab.id}-panel`,
|
|
44
|
+
attrs: { role: "tabpanel", "aria-labelledby": button.id, hidden: true, tabindex: 0 }
|
|
45
|
+
});
|
|
46
|
+
button.addEventListener("click", () => select(tab.id));
|
|
47
|
+
list.append(button);
|
|
48
|
+
element.append(panel);
|
|
49
|
+
buttons.set(tab.id, button);
|
|
50
|
+
panels.set(tab.id, panel);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function select(id) {
|
|
54
|
+
if (!buttons.has(id) || id === active) return;
|
|
55
|
+
active = id;
|
|
56
|
+
for (const [tabId, button] of buttons) {
|
|
57
|
+
const isActive = tabId === id;
|
|
58
|
+
button.setAttribute("aria-selected", String(isActive));
|
|
59
|
+
button.tabIndex = isActive ? 0 : -1;
|
|
60
|
+
button.classList.toggle("tab-active", isActive);
|
|
61
|
+
panels.get(tabId).hidden = !isActive;
|
|
62
|
+
}
|
|
63
|
+
if (!rendered.has(id)) {
|
|
64
|
+
rendered.add(id);
|
|
65
|
+
setContent(panels.get(id), tabs.find(tab => tab.id === id).content);
|
|
66
|
+
}
|
|
67
|
+
options.onChange?.(id);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
list.addEventListener("keydown", event => {
|
|
71
|
+
const enabled = tabs.filter(tab => !tab.disabled).map(tab => tab.id);
|
|
72
|
+
const index = enabled.indexOf(active);
|
|
73
|
+
const target = {
|
|
74
|
+
ArrowRight: enabled[(index + 1) % enabled.length],
|
|
75
|
+
ArrowLeft: enabled[(index - 1 + enabled.length) % enabled.length],
|
|
76
|
+
Home: enabled[0],
|
|
77
|
+
End: enabled[enabled.length - 1]
|
|
78
|
+
}[event.key];
|
|
79
|
+
if (target) {
|
|
80
|
+
event.preventDefault();
|
|
81
|
+
select(target);
|
|
82
|
+
buttons.get(target).focus();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
mount(options.parent, element);
|
|
87
|
+
select(options.active ?? tabs.find(tab => !tab.disabled)?.id);
|
|
88
|
+
return { element, select, getActive: () => active };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* ------------------BREADCRUMBS------------------- */
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Generates breadcrumbs. The last item is the current page.
|
|
95
|
+
*
|
|
96
|
+
* @param {object} options
|
|
97
|
+
* @param {string|Element} [options.parent]
|
|
98
|
+
* @param {{label: string, href?: string, onclick?: Function}[]} options.items
|
|
99
|
+
*/
|
|
100
|
+
export function generateBreadcrumbs(options = {}) {
|
|
101
|
+
const items = options.items ?? [];
|
|
102
|
+
const list = el("ol", { class: "breadcrumb-list" });
|
|
103
|
+
items.forEach((item, index) => {
|
|
104
|
+
const current = index === items.length - 1;
|
|
105
|
+
let content;
|
|
106
|
+
if (current) {
|
|
107
|
+
content = el("span", { text: item.label, attrs: { "aria-current": "page" } });
|
|
108
|
+
} else {
|
|
109
|
+
content = el("a", { text: item.label, attrs: { href: item.href ?? "#" } });
|
|
110
|
+
content.addEventListener("click", event => {
|
|
111
|
+
if (item.onclick) {
|
|
112
|
+
event.preventDefault();
|
|
113
|
+
item.onclick(item);
|
|
114
|
+
} else if (!item.href) {
|
|
115
|
+
event.preventDefault();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
list.append(el("li", { class: "breadcrumb-item" }, content));
|
|
120
|
+
});
|
|
121
|
+
return mount(options.parent, el("nav", { id: options.id, class: cx("breadcrumbs", options.class), attrs: { "aria-label": options.label ?? "Breadcrumb" } }, list));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* ------------------PAGINATION------------------- */
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Generates a page selector.
|
|
128
|
+
*
|
|
129
|
+
* @param {object} options
|
|
130
|
+
* @param {string|Element} [options.parent]
|
|
131
|
+
* @param {number} options.total number of items
|
|
132
|
+
* @param {number} [options.pageSize=10]
|
|
133
|
+
* @param {number} [options.page=1]
|
|
134
|
+
* @param {number} [options.siblings=1] page buttons shown next to the current page
|
|
135
|
+
* @param {(page: number) => void} [options.onChange]
|
|
136
|
+
* @returns {{element: HTMLElement, setPage: Function, setTotal: Function, getPage: Function}}
|
|
137
|
+
*/
|
|
138
|
+
export function generatePagination(options = {}) {
|
|
139
|
+
const { pageSize = 10, siblings = 1, onChange } = options;
|
|
140
|
+
let total = options.total ?? 0;
|
|
141
|
+
let page = options.page ?? 1;
|
|
142
|
+
const element = el("nav", { id: options.id, class: cx("pagination", options.class), attrs: { "aria-label": options.label ?? "Pagination" } });
|
|
143
|
+
|
|
144
|
+
const pages = () => Math.max(1, Math.ceil(total / pageSize));
|
|
145
|
+
function go(target) {
|
|
146
|
+
const next = Math.min(Math.max(target, 1), pages());
|
|
147
|
+
if (next === page) return;
|
|
148
|
+
page = next;
|
|
149
|
+
render();
|
|
150
|
+
onChange?.(page);
|
|
151
|
+
}
|
|
152
|
+
function pageButton(text, target, { disabled, current, label } = {}) {
|
|
153
|
+
return el("button", {
|
|
154
|
+
class: cx("page-button", current && "page-current"),
|
|
155
|
+
text,
|
|
156
|
+
attrs: { type: "button", disabled, "aria-current": current ? "page" : null, "aria-label": label },
|
|
157
|
+
on: { click: () => go(target) }
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function render() {
|
|
161
|
+
page = Math.min(page, pages());
|
|
162
|
+
const parts = [pageButton("‹", page - 1, { disabled: page === 1, label: translate("previous") })];
|
|
163
|
+
for (const item of pageRange(page, pages(), siblings)) {
|
|
164
|
+
parts.push(item === "…" ? el("span", { class: "page-gap", text: "…" }) : pageButton(String(item), item, { current: item === page }));
|
|
165
|
+
}
|
|
166
|
+
parts.push(pageButton("›", page + 1, { disabled: page === pages(), label: translate("next") }));
|
|
167
|
+
element.replaceChildren(...parts);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
render();
|
|
171
|
+
mount(options.parent, element);
|
|
172
|
+
return {
|
|
173
|
+
element,
|
|
174
|
+
getPage: () => page,
|
|
175
|
+
setPage: go,
|
|
176
|
+
setTotal(value) {
|
|
177
|
+
total = value;
|
|
178
|
+
render();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/* ------------------ACCORDION------------------- */
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Generates collapsible panels.
|
|
187
|
+
*
|
|
188
|
+
* @param {object} options
|
|
189
|
+
* @param {string|Element} [options.parent]
|
|
190
|
+
* @param {{id?: string, title: string, content: *, open?: boolean}[]} options.items
|
|
191
|
+
* @param {boolean} [options.multiple=false] allow several panels to be open at once
|
|
192
|
+
* @returns {{element: HTMLElement, toggle: (index: number, open?: boolean) => void}}
|
|
193
|
+
*/
|
|
194
|
+
export function generateAccordion(options = {}) {
|
|
195
|
+
const element = el("div", { id: options.id, class: cx("accordion", options.class) });
|
|
196
|
+
const sections = [];
|
|
197
|
+
|
|
198
|
+
function toggle(index, open) {
|
|
199
|
+
const section = sections[index];
|
|
200
|
+
const willOpen = open ?? section.button.getAttribute("aria-expanded") !== "true";
|
|
201
|
+
if (willOpen && !options.multiple) sections.forEach((other, i) => i !== index && setOpen(other, false));
|
|
202
|
+
setOpen(section, willOpen);
|
|
203
|
+
}
|
|
204
|
+
function setOpen(section, open) {
|
|
205
|
+
section.button.setAttribute("aria-expanded", String(open));
|
|
206
|
+
section.panel.hidden = !open;
|
|
207
|
+
section.item.classList.toggle("accordion-open", open);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
(options.items ?? []).forEach((item, index) => {
|
|
211
|
+
const base = uid("accordion");
|
|
212
|
+
const button = el("button", {
|
|
213
|
+
class: "accordion-header",
|
|
214
|
+
id: `${base}-header`,
|
|
215
|
+
attrs: { type: "button", "aria-controls": `${base}-panel` },
|
|
216
|
+
on: { click: () => toggle(index) }
|
|
217
|
+
}, el("span", { text: item.title }), el("span", { class: "accordion-icon", attrs: { "aria-hidden": "true" } }));
|
|
218
|
+
const panel = el("div", { class: "accordion-panel", id: `${base}-panel`, attrs: { role: "region", "aria-labelledby": button.id } });
|
|
219
|
+
setContent(panel, item.content);
|
|
220
|
+
const wrapper = el("div", { class: "accordion-item" }, el("h3", { class: "accordion-title" }, button), panel);
|
|
221
|
+
const section = { item: wrapper, button, panel };
|
|
222
|
+
sections.push(section);
|
|
223
|
+
element.append(wrapper);
|
|
224
|
+
setOpen(section, false);
|
|
225
|
+
});
|
|
226
|
+
(options.items ?? []).forEach((item, index) => { if (item.open) toggle(index, true); });
|
|
227
|
+
|
|
228
|
+
mount(options.parent, element);
|
|
229
|
+
return { element, toggle };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* ------------------DROPDOWN------------------- */
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Generates a button that opens a menu. Arrow keys move through the items, Esc closes it.
|
|
236
|
+
*
|
|
237
|
+
* @param {object} options
|
|
238
|
+
* @param {string|Element} [options.parent]
|
|
239
|
+
* @param {string} options.label button text
|
|
240
|
+
* @param {{label?: string, onclick?: Function, href?: string, disabled?: boolean, divider?: boolean}[]} options.items
|
|
241
|
+
* @param {"left"|"right"} [options.align="left"]
|
|
242
|
+
* @returns {{element: HTMLElement, open: Function, close: Function}}
|
|
243
|
+
*/
|
|
244
|
+
export function generateDropdown(options = {}) {
|
|
245
|
+
const menuId = uid("dropdown");
|
|
246
|
+
const menu = el("div", { class: cx("dropdown-menu", options.align === "right" && "dropdown-right"), id: menuId, attrs: { role: "menu", hidden: true } });
|
|
247
|
+
const button = el("button", {
|
|
248
|
+
class: "athena-button button-secondary dropdown-toggle",
|
|
249
|
+
text: options.label,
|
|
250
|
+
attrs: { type: "button", "aria-haspopup": "menu", "aria-expanded": "false", "aria-controls": menuId }
|
|
251
|
+
});
|
|
252
|
+
const element = el("div", { id: options.id, class: cx("dropdown", options.class) }, button, menu);
|
|
253
|
+
|
|
254
|
+
const enabledItems = () => [...menu.querySelectorAll("[role=menuitem]:not([disabled])")];
|
|
255
|
+
const onOutside = event => { if (!element.contains(event.target)) close(); };
|
|
256
|
+
function open() {
|
|
257
|
+
menu.hidden = false;
|
|
258
|
+
button.setAttribute("aria-expanded", "true");
|
|
259
|
+
document.addEventListener("pointerdown", onOutside);
|
|
260
|
+
}
|
|
261
|
+
function close(restoreFocus = false) {
|
|
262
|
+
menu.hidden = true;
|
|
263
|
+
button.setAttribute("aria-expanded", "false");
|
|
264
|
+
document.removeEventListener("pointerdown", onOutside);
|
|
265
|
+
if (restoreFocus === true) button.focus();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
for (const item of options.items ?? []) {
|
|
269
|
+
if (item.divider) {
|
|
270
|
+
menu.append(el("div", { class: "dropdown-divider", attrs: { role: "separator" } }));
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const node = item.href
|
|
274
|
+
? el("a", { text: item.label, attrs: { href: item.href } })
|
|
275
|
+
: el("button", { text: item.label, attrs: { type: "button", disabled: item.disabled } });
|
|
276
|
+
node.classList.add("dropdown-item");
|
|
277
|
+
node.setAttribute("role", "menuitem");
|
|
278
|
+
node.tabIndex = -1;
|
|
279
|
+
node.addEventListener("click", event => {
|
|
280
|
+
close(true);
|
|
281
|
+
item.onclick?.(item, event);
|
|
282
|
+
});
|
|
283
|
+
menu.append(node);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
button.addEventListener("click", () => menu.hidden ? open() : close());
|
|
287
|
+
button.addEventListener("keydown", event => {
|
|
288
|
+
if (event.key === "ArrowDown") {
|
|
289
|
+
event.preventDefault();
|
|
290
|
+
if (menu.hidden) open();
|
|
291
|
+
enabledItems()[0]?.focus();
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
menu.addEventListener("keydown", event => {
|
|
295
|
+
const items = enabledItems();
|
|
296
|
+
const index = items.indexOf(document.activeElement);
|
|
297
|
+
if (event.key === "ArrowDown") items[(index + 1) % items.length]?.focus();
|
|
298
|
+
else if (event.key === "ArrowUp") items[(index - 1 + items.length) % items.length]?.focus();
|
|
299
|
+
else if (event.key === "Home") items[0]?.focus();
|
|
300
|
+
else if (event.key === "End") items[items.length - 1]?.focus();
|
|
301
|
+
else if (event.key === "Escape") close(true);
|
|
302
|
+
else if (event.key === "Tab") close();
|
|
303
|
+
else return;
|
|
304
|
+
if (event.key !== "Tab") event.preventDefault();
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
mount(options.parent, element);
|
|
308
|
+
return { element, open, close };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ------------------SIDEBAR------------------- */
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Generates a vertical navigation with nested items. On small screens it collapses behind a toggle button.
|
|
315
|
+
*
|
|
316
|
+
* @param {object} options
|
|
317
|
+
* @param {string|Element} [options.parent]
|
|
318
|
+
* @param {{id: string, label: string, icon?: string, href?: string, children?: object[]}[]} options.items
|
|
319
|
+
* @param {(item: object) => void} [options.onSelect]
|
|
320
|
+
* @param {string} [options.active] id of the initially selected item
|
|
321
|
+
* @returns {{element: HTMLElement, select: (id: string) => void, toggle: Function}}
|
|
322
|
+
*/
|
|
323
|
+
export function generateSidebar(options = {}) {
|
|
324
|
+
const { onSelect } = options;
|
|
325
|
+
const links = new Map();
|
|
326
|
+
const listId = uid("sidebar");
|
|
327
|
+
|
|
328
|
+
function buildList(items, nested) {
|
|
329
|
+
const list = el("ul", { class: nested ? "sidebar-sublist" : "sidebar-list" });
|
|
330
|
+
for (const item of items) {
|
|
331
|
+
const li = el("li", { class: "sidebar-item" });
|
|
332
|
+
const content = [item.icon ? el("span", { class: "sidebar-icon", text: item.icon, attrs: { "aria-hidden": "true" } }) : "", el("span", { text: item.label })];
|
|
333
|
+
if (item.children?.length) {
|
|
334
|
+
const sub = buildList(item.children, true);
|
|
335
|
+
sub.hidden = true;
|
|
336
|
+
const header = el("button", { class: "sidebar-link sidebar-parent", attrs: { type: "button", "aria-expanded": "false" } }, ...content, el("span", { class: "accordion-icon", attrs: { "aria-hidden": "true" } }));
|
|
337
|
+
header.addEventListener("click", () => {
|
|
338
|
+
sub.hidden = !sub.hidden;
|
|
339
|
+
header.setAttribute("aria-expanded", String(!sub.hidden));
|
|
340
|
+
});
|
|
341
|
+
li.append(header, sub);
|
|
342
|
+
} else {
|
|
343
|
+
const link = el(item.href ? "a" : "button", { class: "sidebar-link", attrs: item.href ? { href: item.href } : { type: "button" } }, ...content);
|
|
344
|
+
link.addEventListener("click", () => select(item.id, true));
|
|
345
|
+
links.set(item.id, { link, item });
|
|
346
|
+
li.append(link);
|
|
347
|
+
}
|
|
348
|
+
list.append(li);
|
|
349
|
+
}
|
|
350
|
+
return list;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const toggleButton = el("button", {
|
|
354
|
+
class: "athena-button button-secondary sidebar-toggle",
|
|
355
|
+
text: "☰",
|
|
356
|
+
attrs: { type: "button", "aria-label": "Menu", "aria-expanded": "false", "aria-controls": listId }
|
|
357
|
+
});
|
|
358
|
+
const list = buildList(options.items ?? [], false);
|
|
359
|
+
list.id = listId;
|
|
360
|
+
const element = el("nav", { id: options.id, class: cx("sidebar", options.class), attrs: { "aria-label": options.label ?? "Sidebar" } }, toggleButton, list);
|
|
361
|
+
|
|
362
|
+
function toggle(open) {
|
|
363
|
+
const next = open ?? !element.classList.contains("sidebar-open");
|
|
364
|
+
element.classList.toggle("sidebar-open", next);
|
|
365
|
+
toggleButton.setAttribute("aria-expanded", String(next));
|
|
366
|
+
}
|
|
367
|
+
toggleButton.addEventListener("click", () => toggle());
|
|
368
|
+
|
|
369
|
+
function select(id, fromUser = false) {
|
|
370
|
+
const entry = links.get(id);
|
|
371
|
+
if (!entry) return;
|
|
372
|
+
for (const { link } of links.values()) link.removeAttribute("aria-current");
|
|
373
|
+
entry.link.setAttribute("aria-current", "page");
|
|
374
|
+
// Open the parents of the selected item.
|
|
375
|
+
for (let node = entry.link.closest(".sidebar-sublist"); node; node = node.parentElement?.closest(".sidebar-sublist")) {
|
|
376
|
+
node.hidden = false;
|
|
377
|
+
node.previousElementSibling?.setAttribute("aria-expanded", "true");
|
|
378
|
+
}
|
|
379
|
+
if (fromUser) {
|
|
380
|
+
toggle(false);
|
|
381
|
+
onSelect?.(entry.item);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
mount(options.parent, element);
|
|
386
|
+
if (options.active) select(options.active);
|
|
387
|
+
return { element, select, toggle };
|
|
388
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//matchRoute 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
|
+
export function normalizePath(path) {
|
|
7
|
+
const clean = `/${String(path ?? "").split(/[?#]/)[0]}`.replace(/\/+/g, "/");
|
|
8
|
+
return clean.length > 1 ? clean.replace(/\/$/, "") : clean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parseQuery(search) {
|
|
12
|
+
return Object.fromEntries(new URLSearchParams(search));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Matches a path against a pattern. `:name` segments capture a value, a trailing `*` matches the rest.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Object<string, string>|null} the captured params, or null when the path does not match
|
|
19
|
+
*/
|
|
20
|
+
export function matchRoute(pattern, path) {
|
|
21
|
+
const patternParts = normalizePath(pattern).split("/").slice(1);
|
|
22
|
+
const pathParts = normalizePath(path).split("/").slice(1);
|
|
23
|
+
const params = {};
|
|
24
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
25
|
+
const part = patternParts[i];
|
|
26
|
+
if (part === "*") {
|
|
27
|
+
params.rest = pathParts.slice(i).join("/");
|
|
28
|
+
return params;
|
|
29
|
+
}
|
|
30
|
+
if (pathParts[i] === undefined) return null;
|
|
31
|
+
if (part.startsWith(":")) {
|
|
32
|
+
try {
|
|
33
|
+
params[part.slice(1)] = decodeURIComponent(pathParts[i]);
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
} else if (part !== pathParts[i]) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return patternParts.length === pathParts.length ? params : null;
|
|
42
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
//router 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 { matchRoute, normalizePath, parseQuery } from "./matchRoute.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a router that renders a page function into an outlet element.
|
|
10
|
+
*
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {Object<string, Function|{handler: Function, title?: string}>} options.routes path pattern -> page
|
|
13
|
+
* function. Patterns support `:param` segments and a trailing `*`. A page function is called with
|
|
14
|
+
* `{ path, params, query }` after the outlet was emptied, and may return a promise.
|
|
15
|
+
* @param {string} [options.outlet="wrapper"] id of the element the pages are rendered into
|
|
16
|
+
* @param {"hash"|"history"} [options.mode="hash"] "history" needs the server to answer every path with the page
|
|
17
|
+
* @param {(context) => void} [options.notFound] called when no route matches
|
|
18
|
+
* @param {(context) => (boolean|string|void|Promise)} [options.beforeEach] guard: return false to cancel,
|
|
19
|
+
* or a path to redirect to (login redirect)
|
|
20
|
+
* @param {(context) => void} [options.afterEach]
|
|
21
|
+
* @param {string} [options.titlePrefix] document title is `${titlePrefix}${route.title}`
|
|
22
|
+
* @returns {{start: Function, stop: Function, navigate: Function, current: Function, href: Function}}
|
|
23
|
+
*/
|
|
24
|
+
export function createRouter(options = {}) {
|
|
25
|
+
const { routes = {}, outlet = "wrapper", mode = "hash", notFound, beforeEach, afterEach, titlePrefix = "" } = options;
|
|
26
|
+
const entries = Object.entries(routes).map(([pattern, route]) => [pattern, typeof route === "function" ? { handler: route } : route]);
|
|
27
|
+
let currentContext = null;
|
|
28
|
+
let renderToken = 0;
|
|
29
|
+
let started = false;
|
|
30
|
+
|
|
31
|
+
const readLocation = () => mode === "hash"
|
|
32
|
+
? location.hash.replace(/^#/, "") || "/"
|
|
33
|
+
: location.pathname + location.search;
|
|
34
|
+
|
|
35
|
+
async function render(raw, depth = 0) {
|
|
36
|
+
const token = ++renderToken;
|
|
37
|
+
const path = normalizePath(raw);
|
|
38
|
+
const query = parseQuery(String(raw).split("?")[1]?.split("#")[0] ?? "");
|
|
39
|
+
let matched = null;
|
|
40
|
+
for (const [pattern, route] of entries) {
|
|
41
|
+
const params = matchRoute(pattern, path);
|
|
42
|
+
if (params) {
|
|
43
|
+
matched = { route, params };
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const context = { path, params: matched?.params ?? {}, query };
|
|
48
|
+
|
|
49
|
+
const guard = await beforeEach?.(context);
|
|
50
|
+
if (token !== renderToken || guard === false) return;
|
|
51
|
+
if (typeof guard === "string" && depth < 5) {
|
|
52
|
+
navigate(guard, { replace: true });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const host = document.getElementById(outlet);
|
|
57
|
+
host?.replaceChildren();
|
|
58
|
+
currentContext = context;
|
|
59
|
+
if (!matched) {
|
|
60
|
+
notFound?.(context);
|
|
61
|
+
} else {
|
|
62
|
+
if (matched.route.title) document.title = titlePrefix + matched.route.title;
|
|
63
|
+
await matched.route.handler(context);
|
|
64
|
+
}
|
|
65
|
+
if (token === renderToken) afterEach?.(context);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const onChange = () => render(readLocation());
|
|
69
|
+
|
|
70
|
+
function navigate(path, { replace = false } = {}) {
|
|
71
|
+
if (mode === "hash") {
|
|
72
|
+
const target = `#${path}`;
|
|
73
|
+
if (location.hash === target) {
|
|
74
|
+
render(path);
|
|
75
|
+
} else if (replace) {
|
|
76
|
+
location.replace(target);
|
|
77
|
+
} else {
|
|
78
|
+
location.hash = path;
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
history[replace ? "replaceState" : "pushState"]({}, "", path);
|
|
82
|
+
render(path);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
start() {
|
|
88
|
+
if (started) return;
|
|
89
|
+
started = true;
|
|
90
|
+
window.addEventListener(mode === "hash" ? "hashchange" : "popstate", onChange);
|
|
91
|
+
onChange();
|
|
92
|
+
},
|
|
93
|
+
stop() {
|
|
94
|
+
started = false;
|
|
95
|
+
window.removeEventListener(mode === "hash" ? "hashchange" : "popstate", onChange);
|
|
96
|
+
},
|
|
97
|
+
navigate,
|
|
98
|
+
current: () => currentContext,
|
|
99
|
+
// Value for an anchor's href.
|
|
100
|
+
href: path => mode === "hash" ? `#${path}` : path
|
|
101
|
+
};
|
|
102
|
+
}
|