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
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
//menuGenerator 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 { asHandle, cx, el, mount, setContent, uid } from "../../core/dom.js";
|
|
7
|
+
import { translate } from "../../core/i18n.js";
|
|
8
|
+
import { isSafeUrl } from "../../core/sanitize.js";
|
|
9
|
+
import { initialsOf } from "../componentGenerator/componentGenerator.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} MenuItem
|
|
13
|
+
* @property {string} id
|
|
14
|
+
* @property {string} label
|
|
15
|
+
* @property {string|Node} [icon]
|
|
16
|
+
* @property {string} [href]
|
|
17
|
+
* @property {boolean} [disabled]
|
|
18
|
+
* @property {"end"} [align]
|
|
19
|
+
* @property {number|string|{text: (number|string), tone?: "info"|"ok"|"warn"|"bad"}} [badge]
|
|
20
|
+
* @property {string} [badgeLabel]
|
|
21
|
+
* @property {{name: string, src?: string}} [avatar]
|
|
22
|
+
* @property {MenuItem[]} [children]
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ENABLED = "[role=menuitem]:not([aria-disabled=true]):not(:disabled)";
|
|
26
|
+
|
|
27
|
+
/** The text of a badge: numbers above 99 become "99+", 0 / null / false / "" mean no badge. */
|
|
28
|
+
export function badgeText(value) {
|
|
29
|
+
if (value == null || value === false || value === 0 || value === "") return "";
|
|
30
|
+
return typeof value === "number" && value > 99 ? "99+" : String(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Generates a navigation menu. Items are real links (`href`) or buttons, so the keyboard and screen readers work without help:
|
|
35
|
+
* Tab reaches every item, Enter and Space use it, the arrow keys move inside a group, Esc closes it.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} options
|
|
38
|
+
* @param {string|Element} [options.parent]
|
|
39
|
+
* @param {MenuItem[]} options.items
|
|
40
|
+
* `icon` is shown before the label (text, or a Node such as an SVG; it is hidden from screen readers).
|
|
41
|
+
* `badge` is a small count or word after the label (0, null and "" show nothing, numbers above 99 show "99+").
|
|
42
|
+
* `badgeLabel` is what a screen reader hears for it ("3 unread"); without it the badge text is read after the label.
|
|
43
|
+
* `avatar` shows the picture (or the initials) of a person: for the user menu, usually with `align: "end"` and children.
|
|
44
|
+
* An item with `children` opens a group; a child can have children too (a group inside a group, any depth).
|
|
45
|
+
* @param {(item: MenuItem, event: Event) => void} [options.onNavigate] called when a PERSON uses an item (click, Enter, Space).
|
|
46
|
+
* It is never called by select() or while the menu is built.
|
|
47
|
+
* @param {string} [options.active] id of the item that is the current page at the start
|
|
48
|
+
* @param {boolean} [options.followHash=false] the item whose `href` equals location.hash is the active one, and follows hashchange
|
|
49
|
+
* @param {"bar"|"bottom-tabs"|"responsive"} [options.layout="bar"] "bottom-tabs": a bar of tabs at the bottom of the screen,
|
|
50
|
+
* groups open as sheets above it. "responsive": a bar on wide screens, bottom tabs on a tablet or phone (max 700px).
|
|
51
|
+
* @param {string} [options.label] accessible name of the navigation
|
|
52
|
+
* @param {string} [options.id]
|
|
53
|
+
* @param {string} [options.class]
|
|
54
|
+
* @returns {{element: HTMLElement, select: (id: string|null) => void, getActive: () => string|null, setBadge: (id: string, badge) => void,
|
|
55
|
+
* close: Function, update: (changes?: {items?: object[]}) => object, destroy: Function}}
|
|
56
|
+
*/
|
|
57
|
+
export function generateMenu(options = {}) {
|
|
58
|
+
const layout = options.layout ?? "bar";
|
|
59
|
+
let items = options.items ?? [];
|
|
60
|
+
let active = null;
|
|
61
|
+
const nodes = new Map(); // id -> { item, control, li, parent (id) }
|
|
62
|
+
let openGroups = []; // from the outermost to the innermost: { toggle, sheet, depth }
|
|
63
|
+
|
|
64
|
+
const list = el("ul", { class: "athena-menu-list" });
|
|
65
|
+
const element = el("nav", {
|
|
66
|
+
id: options.id,
|
|
67
|
+
class: cx("athena-menu", `athena-menu-${layout}`, options.class),
|
|
68
|
+
attrs: { "aria-label": options.label ?? translate("menu") }
|
|
69
|
+
}, list);
|
|
70
|
+
const doc = element.ownerDocument;
|
|
71
|
+
const view = doc.defaultView;
|
|
72
|
+
|
|
73
|
+
const badgeNode = badge => {
|
|
74
|
+
const spec = badge != null && typeof badge === "object" ? badge : { text: badge };
|
|
75
|
+
const text = badgeText(spec.text);
|
|
76
|
+
return text ? el("span", { class: cx("menu-badge", spec.tone && `menu-badge-${spec.tone}`), text }) : null;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const content = item => {
|
|
80
|
+
const parts = [];
|
|
81
|
+
if (item.avatar) {
|
|
82
|
+
const avatar = el("span", { class: "menu-avatar", attrs: { "aria-hidden": "true" } });
|
|
83
|
+
if (item.avatar.src && isSafeUrl(item.avatar.src)) avatar.append(el("img", { attrs: { src: item.avatar.src, alt: "" } }));
|
|
84
|
+
else avatar.textContent = initialsOf(item.avatar.name);
|
|
85
|
+
parts.push(avatar);
|
|
86
|
+
}
|
|
87
|
+
if (item.icon) parts.push(setContent(el("span", { class: "menu-icon", attrs: { "aria-hidden": "true" } }), item.icon));
|
|
88
|
+
parts.push(el("span", { class: "menu-label", text: item.label }));
|
|
89
|
+
const badge = badgeNode(item.badge);
|
|
90
|
+
if (badge) parts.push(badge);
|
|
91
|
+
return parts;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// A link when there is an address, a button otherwise.
|
|
95
|
+
const control = (item, inGroup) => {
|
|
96
|
+
const attrs = { "data-id": item.id, role: inGroup ? "menuitem" : null };
|
|
97
|
+
const node = item.href && !item.children?.length
|
|
98
|
+
? el("a", { class: cx("menu-control", inGroup && "menu-sub-control"), attrs: { ...attrs, href: item.href } }, ...content(item))
|
|
99
|
+
: el("button", { class: cx("menu-control", inGroup && "menu-sub-control"), attrs: { ...attrs, type: "button", disabled: item.disabled } }, ...content(item));
|
|
100
|
+
if (item.disabled && node.tagName === "A") {
|
|
101
|
+
node.setAttribute("aria-disabled", "true");
|
|
102
|
+
node.removeAttribute("href");
|
|
103
|
+
}
|
|
104
|
+
if (item.badgeLabel) node.setAttribute("aria-label", `${item.label}, ${item.badgeLabel}`);
|
|
105
|
+
if (inGroup) node.tabIndex = -1;
|
|
106
|
+
return node;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/* ------------------GROUPS (any depth)------------------- */
|
|
110
|
+
|
|
111
|
+
function closeFrom(depth, restoreFocus = false) {
|
|
112
|
+
const closing = openGroups.filter(group => group.depth >= depth);
|
|
113
|
+
if (!closing.length) return;
|
|
114
|
+
for (const { toggle, sheet } of closing.reverse()) {
|
|
115
|
+
sheet.hidden = true;
|
|
116
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
117
|
+
}
|
|
118
|
+
const outermost = closing.at(-1);
|
|
119
|
+
openGroups = openGroups.filter(group => group.depth < depth);
|
|
120
|
+
if (restoreFocus) outermost.toggle.focus();
|
|
121
|
+
if (!openGroups.length) doc.removeEventListener("pointerdown", onOutside);
|
|
122
|
+
}
|
|
123
|
+
const closeAll = (restoreFocus = false) => closeFrom(0, restoreFocus);
|
|
124
|
+
const onOutside = event => { if (!element.contains(event.target)) closeAll(); };
|
|
125
|
+
|
|
126
|
+
function openSheet(toggle, sheet, depth, focusFirst) {
|
|
127
|
+
closeFrom(depth); // siblings and everything deeper than this group close first
|
|
128
|
+
sheet.hidden = false;
|
|
129
|
+
toggle.setAttribute("aria-expanded", "true");
|
|
130
|
+
if (!openGroups.length) doc.addEventListener("pointerdown", onOutside);
|
|
131
|
+
openGroups.push({ toggle, sheet, depth });
|
|
132
|
+
if (focusFirst) sheet.querySelector(`:scope > li > ${ENABLED}`)?.focus();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Builds a group: the toggle (a button) and its sheet, which holds the children (which may be groups again).
|
|
136
|
+
function buildGroup(item, depth, toggle) {
|
|
137
|
+
const sheetId = uid("menu-sheet");
|
|
138
|
+
const sheet = el("ul", { id: sheetId, class: cx("menu-sheet", depth > 0 && "menu-sheet-nested"), attrs: { role: "menu", hidden: true, "aria-label": item.label } });
|
|
139
|
+
toggle.setAttribute("aria-haspopup", "menu");
|
|
140
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
141
|
+
toggle.setAttribute("aria-controls", sheetId);
|
|
142
|
+
toggle.append(el("span", { class: "menu-caret", text: depth > 0 ? "▸" : "▾", attrs: { "aria-hidden": "true" } }));
|
|
143
|
+
toggle.addEventListener("click", () => (sheet.hidden ? openSheet(toggle, sheet, depth, depth > 0) : closeFrom(depth)));
|
|
144
|
+
toggle.addEventListener("keydown", event => {
|
|
145
|
+
const opens = depth === 0 ? ["ArrowDown", "ArrowUp"] : ["ArrowRight"];
|
|
146
|
+
if (opens.includes(event.key)) {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
openSheet(toggle, sheet, depth, true);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
for (const child of item.children) sheet.append(buildItem(child, depth + 1, item.id, "menuitem"));
|
|
153
|
+
sheet.addEventListener("keydown", event => {
|
|
154
|
+
// Only the innermost sheet reacts: the event comes from an item that is a direct child of this sheet
|
|
155
|
+
if (event.target.closest(".menu-sheet") !== sheet) return;
|
|
156
|
+
const entries = [...sheet.querySelectorAll(`:scope > li > ${ENABLED}`)];
|
|
157
|
+
const index = entries.indexOf(doc.activeElement);
|
|
158
|
+
if (event.key === "ArrowDown") entries[(index + 1) % entries.length]?.focus();
|
|
159
|
+
else if (event.key === "ArrowUp") entries[(index - 1 + entries.length) % entries.length]?.focus();
|
|
160
|
+
else if (event.key === "Home") entries[0]?.focus();
|
|
161
|
+
else if (event.key === "End") entries.at(-1)?.focus();
|
|
162
|
+
else if (event.key === "Escape" || (event.key === "ArrowLeft" && depth > 0)) closeFrom(depth, true);
|
|
163
|
+
else if (event.key === "Tab") closeAll();
|
|
164
|
+
else return;
|
|
165
|
+
if (event.key !== "Tab") {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
event.stopPropagation();
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return sheet;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function buildItem(item, depth, parentId, role) {
|
|
174
|
+
const li = el("li", { class: cx("menu-item", depth === 0 && item.align === "end" && "menu-item-end", item.children?.length && "has-submenu"), attrs: { role: role === "menuitem" ? "none" : null } });
|
|
175
|
+
const node = control(item, depth > 0);
|
|
176
|
+
nodes.set(item.id, { item, control: node, li, parent: parentId });
|
|
177
|
+
if (item.children?.length) {
|
|
178
|
+
li.append(node, buildGroup(item, depth, node));
|
|
179
|
+
} else {
|
|
180
|
+
node.addEventListener("click", event => {
|
|
181
|
+
if (item.disabled) return;
|
|
182
|
+
if (depth > 0) closeAll(true);
|
|
183
|
+
options.onNavigate?.(item, event);
|
|
184
|
+
});
|
|
185
|
+
li.append(node);
|
|
186
|
+
}
|
|
187
|
+
return li;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function build() {
|
|
191
|
+
closeAll();
|
|
192
|
+
list.replaceChildren();
|
|
193
|
+
nodes.clear();
|
|
194
|
+
for (const item of items) list.append(buildItem(item, 0, null, null));
|
|
195
|
+
// Arrow keys move along the top level, like a toolbar (Tab still reaches every item).
|
|
196
|
+
list.onkeydown = event => {
|
|
197
|
+
if (event.key !== "ArrowRight" && event.key !== "ArrowLeft") return;
|
|
198
|
+
if (event.target.closest(".menu-sheet")) return;
|
|
199
|
+
const tops = [...list.querySelectorAll(":scope > li > .menu-control:not(:disabled)")];
|
|
200
|
+
const index = tops.indexOf(event.target);
|
|
201
|
+
if (index < 0) return;
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
tops[(index + (event.key === "ArrowRight" ? 1 : -1) + tops.length) % tops.length].focus();
|
|
204
|
+
};
|
|
205
|
+
select(active);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Marks the current page: class is-active and aria-current="page" on the item (and has-active on every group around it). Does not call onNavigate. */
|
|
209
|
+
function select(id) {
|
|
210
|
+
active = id ?? null;
|
|
211
|
+
for (const { control: node, li } of nodes.values()) {
|
|
212
|
+
node.classList.remove("is-active");
|
|
213
|
+
node.removeAttribute("aria-current");
|
|
214
|
+
li.classList.remove("has-active");
|
|
215
|
+
}
|
|
216
|
+
const found = nodes.get(active);
|
|
217
|
+
if (!found) return;
|
|
218
|
+
found.control.classList.add("is-active");
|
|
219
|
+
found.control.setAttribute("aria-current", "page");
|
|
220
|
+
for (let parent = found.parent; parent; parent = nodes.get(parent)?.parent) nodes.get(parent)?.li.classList.add("has-active");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Changes the badge of an item (a number, text, { text, tone }, or 0 / null to remove it) without building the menu again. */
|
|
224
|
+
function setBadge(id, badge) {
|
|
225
|
+
const found = nodes.get(id);
|
|
226
|
+
if (!found) return;
|
|
227
|
+
found.item.badge = badge;
|
|
228
|
+
found.control.querySelector(".menu-badge")?.remove();
|
|
229
|
+
const node = badgeNode(badge);
|
|
230
|
+
if (node) found.control.querySelector(".menu-caret") ? found.control.querySelector(".menu-caret").before(node) : found.control.append(node);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const fromHash = () => {
|
|
234
|
+
const hash = view.location.hash;
|
|
235
|
+
const match = [...nodes.values()].find(({ item }) => item.href && item.href === hash);
|
|
236
|
+
if (match) select(match.item.id);
|
|
237
|
+
};
|
|
238
|
+
if (options.followHash) view.addEventListener("hashchange", fromHash);
|
|
239
|
+
|
|
240
|
+
active = options.active ?? null;
|
|
241
|
+
build();
|
|
242
|
+
if (options.followHash) fromHash();
|
|
243
|
+
mount(options.parent, element);
|
|
244
|
+
|
|
245
|
+
return asHandle({
|
|
246
|
+
element,
|
|
247
|
+
select,
|
|
248
|
+
setBadge,
|
|
249
|
+
getActive: () => active,
|
|
250
|
+
close: closeAll,
|
|
251
|
+
update(changes = {}) {
|
|
252
|
+
if (changes.items) {
|
|
253
|
+
items = changes.items;
|
|
254
|
+
build();
|
|
255
|
+
if (options.followHash) fromHash();
|
|
256
|
+
}
|
|
257
|
+
return this;
|
|
258
|
+
},
|
|
259
|
+
destroy() {
|
|
260
|
+
closeAll();
|
|
261
|
+
view.removeEventListener("hashchange", fromHash);
|
|
262
|
+
element.remove();
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//authState 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
|
+
/**
|
|
7
|
+
* Who is signed in, kept in memory only. Nothing is written to a cookie or to the storage, so a script (an injected one too) cannot read
|
|
8
|
+
* a credential from here: the real session is a cookie that only the server can read (HttpOnly). This object is the UI's copy of the
|
|
9
|
+
* answer to "who am I", for menus, route guards and the page. The server decides what a person may do.
|
|
10
|
+
*
|
|
11
|
+
* After a reload the state is empty until restore() has asked the server.
|
|
12
|
+
*
|
|
13
|
+
* @param {object} [options]
|
|
14
|
+
* @param {() => Promise<(object|null)>} [options.restore] asks the server who is signed in (for example GET /api/me with the cookie):
|
|
15
|
+
* resolves with the user object, or with null / throws when nobody is
|
|
16
|
+
* @returns {{signIn: (user: object) => void, signOut: () => void, restore: () => Promise<object|null>, isSignedIn: () => boolean,
|
|
17
|
+
* getUser: () => (object|null), hasRole: (role: string) => boolean, onChange: (listener: Function) => Function,
|
|
18
|
+
* routerRules: (paths?: object) => object}}
|
|
19
|
+
*/
|
|
20
|
+
export function createAuthState(options = {}) {
|
|
21
|
+
let user = null;
|
|
22
|
+
const listeners = new Set();
|
|
23
|
+
const notify = () => listeners.forEach(listener => listener(user));
|
|
24
|
+
const set = next => {
|
|
25
|
+
const changed = (next === null) !== (user === null) || JSON.stringify(next) !== JSON.stringify(user);
|
|
26
|
+
user = next;
|
|
27
|
+
if (changed) notify();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const state = {
|
|
31
|
+
/** The server accepted the sign in: remember who it is (the user object comes from the server's answer). */
|
|
32
|
+
signIn(next) { set(next ?? {}); },
|
|
33
|
+
/** Forget the person. (End the session on the server too: this only clears the UI.) */
|
|
34
|
+
signOut() { set(null); },
|
|
35
|
+
/** Asks the server who is signed in. Resolves with the user, or null. Never throws. */
|
|
36
|
+
async restore() {
|
|
37
|
+
try {
|
|
38
|
+
const found = await options.restore?.();
|
|
39
|
+
set(found ?? null);
|
|
40
|
+
} catch {
|
|
41
|
+
set(null);
|
|
42
|
+
}
|
|
43
|
+
return user;
|
|
44
|
+
},
|
|
45
|
+
isSignedIn: () => user !== null,
|
|
46
|
+
getUser: () => user,
|
|
47
|
+
/** True when the user has the role (user.roles is a list, or user.role a single role). */
|
|
48
|
+
hasRole: role => Boolean(user && (user.roles?.includes?.(role) || user.role === role)),
|
|
49
|
+
/** Called with the user (or null) when somebody signs in or out. Returns a function that stops it. */
|
|
50
|
+
onChange(listener) {
|
|
51
|
+
listeners.add(listener);
|
|
52
|
+
return () => listeners.delete(listener);
|
|
53
|
+
},
|
|
54
|
+
/** The \`auth\` option of createRouter: createRouter({ auth: auth.routerRules({ loginPath: "/signin" }) }). */
|
|
55
|
+
routerRules: (paths = {}) => ({ isSignedIn: state.isSignedIn, hasRole: state.hasRole, ...paths })
|
|
56
|
+
};
|
|
57
|
+
return state;
|
|
58
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
6
|
import config from "../../config/config.js";
|
|
7
|
-
import Cookies from "../../
|
|
7
|
+
import Cookies from "../../core/cookies.js";
|
|
8
8
|
|
|
9
9
|
// The session is kept in a cookie, so the page knows after a reload who is logged in.
|
|
10
10
|
// A cookie written from script can be read by any script of the page: keep only what the UI needs
|