athena-ui-js 1.2.0 → 1.3.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 +228 -22
- package/package.json +9 -7
- package/src/athena-ui.js +33 -10
- package/src/libs/config/config.js +8 -1
- package/src/libs/core/controls.js +153 -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 +64 -0
- package/src/libs/core/theme.js +131 -0
- package/src/libs/hephaestus/LayoutGenerator/basicLayoutGenerator.js +4 -6
- 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 +253 -0
- package/src/libs/hephaestus/auth/authState.js +58 -0
- 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 +88 -8
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +21 -0
- package/src/libs/hephaestus/formGenerator/authForms.js +130 -0
- package/src/libs/hephaestus/formGenerator/formUtils.js +10 -9
- package/src/libs/hephaestus/formGenerator/loginFormGenerator.js +9 -9
- package/src/libs/hephaestus/hephaestus.js +7 -2
- package/src/libs/hephaestus/inputGenerator/inputGenerator.js +30 -1
- package/src/libs/hephaestus/modalGenerator/modalGenerator.js +17 -11
- package/src/libs/hephaestus/router/router.js +54 -5
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +290 -16
- package/src/libs/libs.js +3 -3
- package/src/res/themes/appearance.css +154 -0
- package/src/res/themes/default/style.css +609 -0
- package/src/theme-boot.js +25 -0
- 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
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
6
|
import { asHandle, cx, el, mount, resolveElement, setContent, uid } from "../../core/dom.js";
|
|
7
|
+
import { generateBreadcrumbs } from "../navigationGenerator/navigationGenerator.js";
|
|
8
|
+
import { generateSpinner } from "../feedbackGenerator/feedbackGenerator.js";
|
|
9
|
+
import { translate } from "../../core/i18n.js";
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* Generates a link. External links open in a new tab and get `rel="noopener noreferrer"`.
|
|
@@ -143,10 +146,14 @@ export function generateAvatar(options = {}) {
|
|
|
143
146
|
* @param {string|number} options.value
|
|
144
147
|
* @param {number} [options.delta] change in percent, shown with an arrow
|
|
145
148
|
* @param {boolean} [options.invertDelta] a falling value is good news (costs, errors)
|
|
146
|
-
* @
|
|
149
|
+
* @param {"ok"|"warn"|"bad"} [options.tone] colours the tile (the class is-ok / is-warn / is-bad) and adds the word to the screen reader text
|
|
150
|
+
* @param {string} [options.note] small text under the value ("of 120 hosts", "updated 2 min ago")
|
|
151
|
+
* @returns {{element: HTMLElement, setValue: Function, setNote: Function, setTone: Function}}
|
|
147
152
|
*/
|
|
148
153
|
export function generateStat(options = {}) {
|
|
149
|
-
const
|
|
154
|
+
const valueText = document.createTextNode(String(options.value ?? ""));
|
|
155
|
+
const toneWord = el("span", { class: "athena-visually-hidden" });
|
|
156
|
+
const value = el("div", { class: "stat-value" }, valueText, toneWord);
|
|
150
157
|
const element = el("div", { id: options.id, class: cx("stat", options.class) }, el("div", { class: "stat-label", text: options.label }), value);
|
|
151
158
|
if (options.delta != null) {
|
|
152
159
|
const good = options.invertDelta ? options.delta < 0 : options.delta > 0;
|
|
@@ -154,9 +161,25 @@ export function generateStat(options = {}) {
|
|
|
154
161
|
const arrow = options.delta > 0 ? "▲" : options.delta < 0 ? "▼" : "■";
|
|
155
162
|
element.append(el("div", { class: cx("stat-delta", `stat-${kind}`), text: `${arrow} ${Math.abs(options.delta)}%` }));
|
|
156
163
|
}
|
|
164
|
+
const note = el("div", { class: "stat-note" });
|
|
165
|
+
value.after(note);
|
|
166
|
+
const setValue = text => { valueText.data = String(text); };
|
|
167
|
+
const setNote = text => { note.textContent = text ?? ""; note.hidden = !text; };
|
|
168
|
+
const setTone = tone => {
|
|
169
|
+
for (const name of ["ok", "warn", "bad"]) element.classList.toggle(`is-${name}`, name === tone);
|
|
170
|
+
toneWord.textContent = tone ? ` (${translate(`tone.${tone}`)})` : "";
|
|
171
|
+
};
|
|
172
|
+
setNote(options.note);
|
|
173
|
+
setTone(options.tone);
|
|
157
174
|
mount(options.parent, element);
|
|
158
|
-
|
|
159
|
-
|
|
175
|
+
return asHandle({
|
|
176
|
+
element, setValue, setNote, setTone,
|
|
177
|
+
update: ({ value: text, note: noteText, tone } = {}) => {
|
|
178
|
+
if (text != null) setValue(text);
|
|
179
|
+
if (noteText !== undefined) setNote(noteText);
|
|
180
|
+
if (tone !== undefined) setTone(tone);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
160
183
|
}
|
|
161
184
|
|
|
162
185
|
/**
|
|
@@ -200,3 +223,97 @@ export function generateTooltip(target, text) {
|
|
|
200
223
|
host.append(tip);
|
|
201
224
|
return tip;
|
|
202
225
|
}
|
|
226
|
+
|
|
227
|
+
/* ------------------PAGE PARTS------------------- */
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The top of a page: breadcrumbs, the title (a heading), a description and the actions of the page.
|
|
231
|
+
*
|
|
232
|
+
* @param {object} options
|
|
233
|
+
* @param {string|Element} [options.parent]
|
|
234
|
+
* @param {string} options.title
|
|
235
|
+
* @param {string} [options.description]
|
|
236
|
+
* @param {{label: string, href?: string, onclick?: Function}[]} [options.breadcrumbs] see generateBreadcrumbs
|
|
237
|
+
* @param {(Node|string)[]|Node} [options.actions] buttons, shown at the end of the header
|
|
238
|
+
* @param {1|2|3} [options.level=1] heading level
|
|
239
|
+
* @returns {{element: HTMLElement, setTitle: Function, setDescription: Function, actions: HTMLElement}}
|
|
240
|
+
*/
|
|
241
|
+
export function generatePageHeader(options = {}) {
|
|
242
|
+
const title = el(`h${options.level ?? 1}`, { class: "page-title", text: options.title });
|
|
243
|
+
const description = el("p", { class: "page-description", text: options.description ?? "" });
|
|
244
|
+
description.hidden = !options.description;
|
|
245
|
+
const actions = setContent(el("div", { class: "page-actions" }), options.actions);
|
|
246
|
+
const heading = el("div", { class: "page-heading" }, title, description);
|
|
247
|
+
const element = el("header", { id: options.id, class: cx("page-header", options.class) });
|
|
248
|
+
if (options.breadcrumbs?.length) generateBreadcrumbs({ parent: element, items: options.breadcrumbs });
|
|
249
|
+
element.append(el("div", { class: "page-header-row" }, heading, actions));
|
|
250
|
+
mount(options.parent, element);
|
|
251
|
+
return asHandle({
|
|
252
|
+
element, actions,
|
|
253
|
+
setTitle: text => { title.textContent = text; },
|
|
254
|
+
setDescription: text => { description.textContent = text ?? ""; description.hidden = !text; }
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* A list of facts: a term and its description, in pairs (a <dl>).
|
|
260
|
+
*
|
|
261
|
+
* @param {object} options
|
|
262
|
+
* @param {string|Element} [options.parent]
|
|
263
|
+
* @param {{term: string, description: (string|number|Node|Array), title?: string}[]} options.items text is inserted as text
|
|
264
|
+
* @param {boolean} [options.inline=false] terms and descriptions side by side
|
|
265
|
+
* @returns {{element: HTMLElement, setItems: Function}}
|
|
266
|
+
*/
|
|
267
|
+
export function generateFacts(options = {}) {
|
|
268
|
+
const element = el("dl", { id: options.id, class: cx("facts", options.inline && "facts-inline", options.class) });
|
|
269
|
+
const setItems = items => {
|
|
270
|
+
element.replaceChildren();
|
|
271
|
+
for (const item of items ?? []) {
|
|
272
|
+
const group = el("div", { class: "fact" }, el("dt", { text: item.term }), setContent(el("dd", { attrs: { title: item.title } }), item.description));
|
|
273
|
+
element.append(group);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
setItems(options.items);
|
|
277
|
+
mount(options.parent, element);
|
|
278
|
+
return asHandle({ element, setItems, update: ({ items } = {}) => { if (items) setItems(items); } });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* What a list shows when there is nothing in it: a title, a short explanation and what to do about it.
|
|
283
|
+
*
|
|
284
|
+
* @param {object} options
|
|
285
|
+
* @param {string|Element} [options.parent]
|
|
286
|
+
* @param {string} options.title
|
|
287
|
+
* @param {string} [options.message]
|
|
288
|
+
* @param {string|Node} [options.icon] decorative
|
|
289
|
+
* @param {{text: string, onclick: Function, variant?: string}} [options.action] a button
|
|
290
|
+
*/
|
|
291
|
+
export function generateEmptyState(options = {}) {
|
|
292
|
+
const element = el("div", { id: options.id, class: cx("empty-state", options.class) });
|
|
293
|
+
if (options.icon) element.append(setContent(el("div", { class: "empty-icon", attrs: { "aria-hidden": "true" } }), options.icon));
|
|
294
|
+
element.append(el("p", { class: "empty-title", text: options.title }));
|
|
295
|
+
if (options.message) element.append(el("p", { class: "empty-message", text: options.message }));
|
|
296
|
+
if (options.action) {
|
|
297
|
+
const button = el("button", { class: `athena-button button-${options.action.variant ?? "primary"}`, text: options.action.text, attrs: { type: "button" } });
|
|
298
|
+
button.addEventListener("click", event => options.action.onclick?.(event));
|
|
299
|
+
element.append(button);
|
|
300
|
+
}
|
|
301
|
+
mount(options.parent, element);
|
|
302
|
+
return asHandle({ element });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* A placeholder while something loads: a spinner and a text (a live region, so a screen reader says it).
|
|
307
|
+
*
|
|
308
|
+
* @param {object} [options]
|
|
309
|
+
* @param {string|Element} [options.parent]
|
|
310
|
+
* @param {string} [options.text] default: the translated "Loading..."
|
|
311
|
+
*/
|
|
312
|
+
export function generateLoading(options = {}) {
|
|
313
|
+
const text = options.text ?? translate("loading");
|
|
314
|
+
const element = el("div", { id: options.id, class: cx("loading-state", options.class), attrs: { role: "status", "aria-live": "polite" } });
|
|
315
|
+
generateSpinner({ parent: element, label: text });
|
|
316
|
+
element.append(el("span", { class: "loading-text", text }));
|
|
317
|
+
mount(options.parent, element);
|
|
318
|
+
return asHandle({ element });
|
|
319
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//themePicker 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, uid } from "../../core/dom.js";
|
|
7
|
+
import { translate } from "../../core/i18n.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates the appearance picker of an account page: a group of themes with a small preview of each, and a group of modes.
|
|
11
|
+
* The preview is drawn with the colours of the theme itself: the stylesheet of a theme applies to any element that has the
|
|
12
|
+
* attributes data-theme and data-mode, not only to <html>.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} options
|
|
15
|
+
* @param {string|Element} [options.parent]
|
|
16
|
+
* @param {ReturnType<import("../../core/theme.js").createThemeManager>} options.manager
|
|
17
|
+
* @param {string} [options.themeLabel] legend of the theme group (default: translated "Theme")
|
|
18
|
+
* @param {string} [options.modeLabel] legend of the mode group (default: translated "Mode")
|
|
19
|
+
* @param {string} [options.class]
|
|
20
|
+
* @returns {{element: HTMLElement, update: Function, destroy: Function}}
|
|
21
|
+
*/
|
|
22
|
+
export function generateThemePicker(options = {}) {
|
|
23
|
+
const { manager } = options;
|
|
24
|
+
if (!manager) throw new Error("Athena UI: generateThemePicker needs a manager from createThemeManager");
|
|
25
|
+
const name = uid("appearance");
|
|
26
|
+
|
|
27
|
+
const group = (legend, items, kind, preview) => {
|
|
28
|
+
const set = el("fieldset", { class: `theme-picker-group theme-picker-${kind}` }, el("legend", { text: legend }));
|
|
29
|
+
const list = el("div", { class: "theme-picker-options" });
|
|
30
|
+
for (const item of items) {
|
|
31
|
+
const input = el("input", { attrs: { type: "radio", name: `${name}-${kind}`, value: item.id } });
|
|
32
|
+
input.addEventListener("change", () => {
|
|
33
|
+
if (input.checked) (kind === "theme" ? manager.setTheme : manager.setMode)(item.id);
|
|
34
|
+
});
|
|
35
|
+
const label = el("label", { class: "theme-picker-option", attrs: { "data-id": item.id } }, input);
|
|
36
|
+
if (preview) label.append(preview(item));
|
|
37
|
+
label.append(el("span", { class: "theme-picker-name", text: item.name ?? item.id }));
|
|
38
|
+
if (item.description) label.append(el("span", { class: "theme-picker-description", text: item.description }));
|
|
39
|
+
list.append(label);
|
|
40
|
+
}
|
|
41
|
+
set.append(list);
|
|
42
|
+
return set;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// A small page drawn with the colours of the theme, in the mode that is in use (the light one for "system" in a light browser).
|
|
46
|
+
const preview = theme => el("span", {
|
|
47
|
+
class: "theme-preview",
|
|
48
|
+
attrs: { "data-theme": theme.id, "data-mode": manager.appearance().dark ? "dark" : "light", "aria-hidden": "true" }
|
|
49
|
+
},
|
|
50
|
+
el("span", { class: "theme-preview-bar" }),
|
|
51
|
+
el("span", { class: "theme-preview-body" }, el("span", { class: "theme-preview-line" }), el("span", { class: "theme-preview-button" })));
|
|
52
|
+
|
|
53
|
+
const element = el("div", { class: cx("theme-picker", options.class) });
|
|
54
|
+
const render = () => {
|
|
55
|
+
element.replaceChildren(
|
|
56
|
+
group(options.themeLabel ?? translate("theme"), manager.themes, "theme", preview),
|
|
57
|
+
group(options.modeLabel ?? translate("mode"), manager.modes, "mode", null)
|
|
58
|
+
);
|
|
59
|
+
sync(manager.appearance());
|
|
60
|
+
};
|
|
61
|
+
const sync = state => {
|
|
62
|
+
for (const input of element.querySelectorAll("input")) {
|
|
63
|
+
const kind = input.name.endsWith("-theme") ? "theme" : "mode";
|
|
64
|
+
input.checked = input.value === state[kind];
|
|
65
|
+
}
|
|
66
|
+
// The previews follow dark / light
|
|
67
|
+
for (const node of element.querySelectorAll(".theme-preview")) node.dataset.mode = state.dark ? "dark" : "light";
|
|
68
|
+
};
|
|
69
|
+
render();
|
|
70
|
+
const stop = manager.onChange(sync);
|
|
71
|
+
mount(options.parent, element);
|
|
72
|
+
return asHandle({ element, update: () => { render(); }, destroy() { stop(); element.remove(); } });
|
|
73
|
+
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { cx, el, setContent, uid } from "../../core/dom.js";
|
|
7
7
|
import { trapFocus } from "../../core/focus.js";
|
|
8
8
|
import { translate } from "../../core/i18n.js";
|
|
9
|
-
import { generateButton } from "../formGenerator/formBuilder.js";
|
|
9
|
+
import { generateButton, generateForm } from "../formGenerator/formBuilder.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Opens a dialog and resolves with the value of the button that was used,
|
|
@@ -85,17 +85,97 @@ export async function alertDialog(message, { title, okText = translate("ok") } =
|
|
|
85
85
|
await showDialog({ title, content: message, buttons: [{ text: okText, value: true, autofocus: true }] });
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
/** Question with confirm and cancel buttons. Resolves with true / false. */
|
|
89
|
-
export
|
|
90
|
-
|
|
88
|
+
/** Question with confirm and cancel buttons. Resolves with true / false. See showConfirm for `typeToConfirm`. */
|
|
89
|
+
export function confirmDialog(message, options = {}) {
|
|
90
|
+
return showConfirm({ ...options, message });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Asks for a confirmation. Resolves with true when confirmed, false when cancelled or dismissed.
|
|
95
|
+
*
|
|
96
|
+
* @param {object} options
|
|
97
|
+
* @param {string} [options.title]
|
|
98
|
+
* @param {string|Node|Array} options.message
|
|
99
|
+
* @param {string} [options.confirmText]
|
|
100
|
+
* @param {string} [options.cancelText]
|
|
101
|
+
* @param {boolean} [options.danger=false] red confirm button
|
|
102
|
+
* @param {string} [options.typeToConfirm] the person has to type exactly this text before the dialog confirms
|
|
103
|
+
* (for things that cannot be undone: the name of what is deleted)
|
|
104
|
+
* @param {"small"|"medium"|"large"|"full"} [options.size]
|
|
105
|
+
* @returns {Promise<boolean> & {element: HTMLElement, close: Function}}
|
|
106
|
+
*/
|
|
107
|
+
export function showConfirm({ title, message, confirmText = translate("ok"), cancelText = translate("cancel"), danger = false, typeToConfirm, size } = {}) {
|
|
108
|
+
let input = null;
|
|
109
|
+
const error = el("div", { class: "field-error", attrs: { role: "alert" } });
|
|
110
|
+
const content = [message];
|
|
111
|
+
if (typeToConfirm) {
|
|
112
|
+
input = el("input", { class: "confirm-input", attrs: { type: "text", autocomplete: "off", "aria-label": typeToConfirm, "data-autofocus": "true" } });
|
|
113
|
+
content.push(el("p", { class: "confirm-prompt", text: translate("typeToConfirm", { text: typeToConfirm }) }), input, error);
|
|
114
|
+
}
|
|
115
|
+
const dialog = showDialog({
|
|
91
116
|
title,
|
|
92
|
-
content
|
|
117
|
+
content,
|
|
118
|
+
size,
|
|
93
119
|
buttons: [
|
|
94
120
|
{ text: cancelText, value: false, variant: "secondary" },
|
|
95
|
-
{ text: confirmText, value: true, variant: danger ? "danger" : "primary", autofocus:
|
|
96
|
-
]
|
|
121
|
+
{ text: confirmText, value: true, variant: danger ? "danger" : "primary", autofocus: !typeToConfirm }
|
|
122
|
+
],
|
|
123
|
+
beforeClose: value => {
|
|
124
|
+
if (value !== true || !typeToConfirm || input.value === typeToConfirm) return true;
|
|
125
|
+
error.textContent = translate("textMismatch");
|
|
126
|
+
input.focus();
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return Object.assign(dialog.then(result => result === true), { element: dialog.element, backdrop: dialog.backdrop, close: dialog.close });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A dialog that holds a form (see generateForm): validation, Enter submits, the button shows a spinner while it saves.
|
|
135
|
+
* `onSubmit(values, form)` saves. When it throws (or rejects) the message is shown in the dialog and the dialog stays open;
|
|
136
|
+
* `form.setErrors({ field: "message" })` marks single fields, for example with errors of the server. When it returns, the dialog closes.
|
|
137
|
+
*
|
|
138
|
+
* @param {object} options
|
|
139
|
+
* @param {string} [options.title]
|
|
140
|
+
* @param {string} [options.intro] text above the fields
|
|
141
|
+
* @param {string|Node|Array} [options.content] nodes above the fields
|
|
142
|
+
* @param {object[]} options.fields field definitions, see generateField
|
|
143
|
+
* @param {string} [options.submitText]
|
|
144
|
+
* @param {string} [options.cancelText]
|
|
145
|
+
* @param {boolean} [options.danger=false] red submit button
|
|
146
|
+
* @param {"small"|"medium"|"large"|"full"} [options.size]
|
|
147
|
+
* @param {(values: object, form: object) => (void|Promise)} options.onSubmit
|
|
148
|
+
* @param {(form: object) => void} [options.onReady] called with the form once it is built
|
|
149
|
+
* @returns {Promise<boolean> & {element: HTMLElement, form: object, close: Function}} resolves with true when it was saved, false when dismissed
|
|
150
|
+
*/
|
|
151
|
+
export function showFormDialog({ title, intro, content, fields, submitText = translate("save"), cancelText = translate("cancel"), danger = false, size, onSubmit, onReady } = {}) {
|
|
152
|
+
let saved = false;
|
|
153
|
+
let dialog = null;
|
|
154
|
+
const body = el("div", { class: "dialog-form" });
|
|
155
|
+
if (intro) body.append(el("p", { class: "dialog-intro", text: intro }));
|
|
156
|
+
setContent(body, content);
|
|
157
|
+
const form = generateForm({
|
|
158
|
+
parent: body,
|
|
159
|
+
fields,
|
|
160
|
+
submitText,
|
|
161
|
+
onSubmit: async (values, f) => {
|
|
162
|
+
await onSubmit?.(values, f);
|
|
163
|
+
saved = true;
|
|
164
|
+
dialog.close(true);
|
|
165
|
+
}
|
|
97
166
|
});
|
|
98
|
-
|
|
167
|
+
onReady?.(form);
|
|
168
|
+
form.element.querySelector(".form-actions").append(
|
|
169
|
+
generateButton({ text: cancelText, variant: "secondary", onclick: () => dialog.close(null) }).element
|
|
170
|
+
);
|
|
171
|
+
if (danger) {
|
|
172
|
+
form.submitButton.element.classList.remove("button-primary");
|
|
173
|
+
form.submitButton.element.classList.add("button-danger");
|
|
174
|
+
}
|
|
175
|
+
dialog = showDialog({ title, content: body, buttons: [], size });
|
|
176
|
+
const first = form.element.querySelector("input:not([disabled]):not([type=hidden]), select, textarea");
|
|
177
|
+
first?.focus();
|
|
178
|
+
return Object.assign(dialog.then(() => saved), { element: dialog.element, backdrop: dialog.backdrop, form, close: dialog.close });
|
|
99
179
|
}
|
|
100
180
|
|
|
101
181
|
/**
|
|
@@ -80,6 +80,27 @@ export function generateAlert(options = {}) {
|
|
|
80
80
|
return mount(options.parent, alert);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Copies text to the clipboard and says so in a toast. Resolves with true when it worked (it needs a secure page and a click or key
|
|
85
|
+
* press of the person), false otherwise (a toast with the error is shown).
|
|
86
|
+
*
|
|
87
|
+
* @param {string} text
|
|
88
|
+
* @param {object} [options]
|
|
89
|
+
* @param {string} [options.message] text of the toast (default: the translated "Copied")
|
|
90
|
+
* @param {boolean} [options.toast=true] false copies silently
|
|
91
|
+
* @returns {Promise<boolean>}
|
|
92
|
+
*/
|
|
93
|
+
export async function copyToClipboard(text, { message = translate("copied"), toast = true } = {}) {
|
|
94
|
+
try {
|
|
95
|
+
await navigator.clipboard.writeText(String(text));
|
|
96
|
+
if (toast) showToast({ message, type: "success" });
|
|
97
|
+
return true;
|
|
98
|
+
} catch {
|
|
99
|
+
if (toast) showToast({ message: translate("copyFailed"), type: "error" });
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
83
104
|
/**
|
|
84
105
|
* Generates a loading spinner.
|
|
85
106
|
*
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
//authForms 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 { translate } from "../../core/i18n.js";
|
|
7
|
+
import { generateForm } from "./formBuilder.js";
|
|
8
|
+
|
|
9
|
+
// Real <form> elements with the autocomplete tokens that password managers look for (username, current-password, new-password,
|
|
10
|
+
// email), so saving and filling passwords works and Enter submits. The password is sent as it is: hashing it in the browser
|
|
11
|
+
// only turns the hash into the password. Use HTTPS, and let the server hash it (argon2, scrypt, bcrypt).
|
|
12
|
+
//
|
|
13
|
+
// onSubmit is asynchronous. When it throws (or rejects) the message is shown in the form and the person can try again; an error
|
|
14
|
+
// that carries `fields` ({ username: "Already taken" }) marks those fields as well.
|
|
15
|
+
|
|
16
|
+
function build(options, { fields, submitText }) {
|
|
17
|
+
return generateForm({
|
|
18
|
+
parent: options.parent,
|
|
19
|
+
id: options.id,
|
|
20
|
+
class: options.class,
|
|
21
|
+
fields,
|
|
22
|
+
submitText: options.submitText ?? submitText,
|
|
23
|
+
onSubmit: async (values, form) => {
|
|
24
|
+
try {
|
|
25
|
+
await options.onSubmit?.(values, form);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error?.fields) form.setErrors(error.fields);
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A sign in form: username (or email), password, and optionally "Remember me".
|
|
36
|
+
*
|
|
37
|
+
* @param {object} options
|
|
38
|
+
* @param {string|Element} [options.parent]
|
|
39
|
+
* @param {(values: {username: string, password: string, remember?: boolean}, form: object) => (void|Promise)} options.onSubmit
|
|
40
|
+
* @param {boolean} [options.remember=false] shows "Remember me"
|
|
41
|
+
* @param {"username"|"email"} [options.identifier="username"] what the first field asks for
|
|
42
|
+
* @param {string} [options.submitText]
|
|
43
|
+
*/
|
|
44
|
+
export function generateSignInForm(options = {}) {
|
|
45
|
+
const email = options.identifier === "email";
|
|
46
|
+
const fields = [
|
|
47
|
+
{ name: "username", label: translate(email ? "email" : "username"), type: email ? "email" : "text", autocomplete: "username", required: true },
|
|
48
|
+
{ name: "password", label: translate("password"), type: "password", autocomplete: "current-password", required: true }
|
|
49
|
+
];
|
|
50
|
+
if (options.remember) fields.push({ name: "remember", label: translate("rememberMe"), type: "checkbox" });
|
|
51
|
+
return build(options, { fields, submitText: translate("login") });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A registration form: username, email, password and its confirmation.
|
|
56
|
+
*
|
|
57
|
+
* @param {object} options
|
|
58
|
+
* @param {string|Element} [options.parent]
|
|
59
|
+
* @param {(values: {username: string, email: string, password: string}, form: object) => (void|Promise)} options.onSubmit
|
|
60
|
+
* the confirmation is checked here and not sent
|
|
61
|
+
* @param {number} [options.passwordMinLength=8]
|
|
62
|
+
* @param {boolean} [options.username=true] false leaves the username out (sign in with the email)
|
|
63
|
+
* @param {string} [options.submitText]
|
|
64
|
+
*/
|
|
65
|
+
export function generateSignUpForm(options = {}) {
|
|
66
|
+
const fields = [];
|
|
67
|
+
if (options.username !== false) fields.push({ name: "username", label: translate("username"), autocomplete: "username", required: true });
|
|
68
|
+
fields.push(
|
|
69
|
+
{ name: "email", label: translate("email"), type: "email", autocomplete: "email", required: true },
|
|
70
|
+
{ name: "password", label: translate("password"), type: "password", autocomplete: "new-password", required: true, minLength: options.passwordMinLength ?? 8 },
|
|
71
|
+
{ name: "confirmPassword", label: translate("confirmPassword"), type: "password", autocomplete: "new-password", required: true, match: "password" }
|
|
72
|
+
);
|
|
73
|
+
return build({
|
|
74
|
+
...options,
|
|
75
|
+
onSubmit: (values, form) => {
|
|
76
|
+
const { confirmPassword, ...rest } = values;
|
|
77
|
+
void confirmPassword;
|
|
78
|
+
return options.onSubmit?.(rest, form);
|
|
79
|
+
}
|
|
80
|
+
}, { fields, submitText: translate("register") });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Asks for the email address to send a reset link to.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} options
|
|
87
|
+
* @param {string|Element} [options.parent]
|
|
88
|
+
* @param {(values: {email: string}, form: object) => (void|Promise)} options.onSubmit
|
|
89
|
+
*/
|
|
90
|
+
export function generatePasswordResetForm(options = {}) {
|
|
91
|
+
return build(options, {
|
|
92
|
+
fields: [{ name: "email", label: translate("email"), type: "email", autocomplete: "email", required: true }],
|
|
93
|
+
submitText: translate("resetPassword")
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Change of the password of the person who is signed in: the current one, the new one and its confirmation.
|
|
99
|
+
*
|
|
100
|
+
* @param {object} options
|
|
101
|
+
* @param {string|Element} [options.parent]
|
|
102
|
+
* @param {(values: {currentPassword: string, newPassword: string}, form: object) => (void|Promise)} options.onSubmit
|
|
103
|
+
* @param {number} [options.passwordMinLength=8]
|
|
104
|
+
* @param {string} [options.username] when given, a hidden username field helps the password manager to update the right entry
|
|
105
|
+
*/
|
|
106
|
+
export function generatePasswordChangeForm(options = {}) {
|
|
107
|
+
const fields = [
|
|
108
|
+
{ name: "currentPassword", label: translate("currentPassword"), type: "password", autocomplete: "current-password", required: true },
|
|
109
|
+
{ name: "newPassword", label: translate("newPassword"), type: "password", autocomplete: "new-password", required: true, minLength: options.passwordMinLength ?? 8 },
|
|
110
|
+
{ name: "confirmPassword", label: translate("confirmPassword"), type: "password", autocomplete: "new-password", required: true, match: "newPassword" }
|
|
111
|
+
];
|
|
112
|
+
const form = build({
|
|
113
|
+
...options,
|
|
114
|
+
onSubmit: (values, f) => {
|
|
115
|
+
const { confirmPassword, ...rest } = values;
|
|
116
|
+
void confirmPassword;
|
|
117
|
+
return options.onSubmit?.(rest, f);
|
|
118
|
+
}
|
|
119
|
+
}, { fields, submitText: translate("changePassword") });
|
|
120
|
+
if (options.username) {
|
|
121
|
+
const hidden = document.createElement("input");
|
|
122
|
+
hidden.type = "text";
|
|
123
|
+
hidden.name = "username";
|
|
124
|
+
hidden.value = options.username;
|
|
125
|
+
hidden.autocomplete = "username";
|
|
126
|
+
hidden.hidden = true;
|
|
127
|
+
form.element.prepend(hidden);
|
|
128
|
+
}
|
|
129
|
+
return form;
|
|
130
|
+
}
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
//LICENCE: MIT
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
|
-
/* global $, CryptoJS, HTMLControlGenerator */
|
|
7
6
|
|
|
8
|
-
import { HTMLElementGenerator } from "
|
|
7
|
+
import { HTMLElementGenerator } from "../../core/elementGenerator.js";
|
|
9
8
|
import config from "../../config/config.js";
|
|
10
9
|
import { translate } from "../../core/i18n.js";
|
|
10
|
+
import { generateControl } from "../../core/controls.js";
|
|
11
11
|
|
|
12
12
|
export { EMAIL_PATTERN } from "../../core/validation.js";
|
|
13
13
|
|
|
@@ -25,11 +25,11 @@ export function generateRow(formId, name) {
|
|
|
25
25
|
export function generateField(formId, name, label, inputType) {
|
|
26
26
|
const row = generateRow(formId, name);
|
|
27
27
|
HTMLElementGenerator({ elementType: "span", parent: row, id: `${row}-label`, class: "container", text: `${translate(label)}: ` });
|
|
28
|
-
|
|
28
|
+
generateControl({ inputType, parent: row, id: `${row}-control`, class: "text-filed-class" });
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export function generateButton(formId, name, text, onclick) {
|
|
32
|
-
|
|
32
|
+
generateControl({
|
|
33
33
|
inputType: "button",
|
|
34
34
|
parent: generateRow(formId, name),
|
|
35
35
|
text,
|
|
@@ -51,14 +51,15 @@ export function generateMessage(formId) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export function showMessage(formId, text, isError = true) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
const node = document.getElementById(`${formId}-message`);
|
|
55
|
+
if (!node) return;
|
|
56
|
+
node.textContent = text;
|
|
57
|
+
node.classList.toggle("form-message-error", isError);
|
|
58
|
+
node.classList.toggle("form-message-success", !isError);
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
export function readControl(formId, name) {
|
|
61
|
-
return
|
|
62
|
+
return document.getElementById(`${formId}-${name}-control`)?.value ?? "";
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
export function hashPassword(password) {
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
//LICENCE: MIT
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
|
-
/* global $, CryptoJS, HTMLControlGenerator */
|
|
7
6
|
|
|
8
|
-
import { HTMLElementGenerator } from "
|
|
7
|
+
import { HTMLElementGenerator } from "../../core/elementGenerator.js";
|
|
9
8
|
import config from "../../config/config.js";
|
|
10
9
|
import { getLogin } from "../../config/hooks.js";
|
|
11
10
|
import { translate } from "../../core/i18n.js";
|
|
11
|
+
import { generateControl } from "../../core/controls.js";
|
|
12
12
|
|
|
13
13
|
const FORM_ID = "loginForm";
|
|
14
14
|
|
|
@@ -33,7 +33,7 @@ function generateField(name, label, inputType) {
|
|
|
33
33
|
class: "container",
|
|
34
34
|
text: `${translate(label)}: `
|
|
35
35
|
});
|
|
36
|
-
|
|
36
|
+
generateControl({
|
|
37
37
|
inputType,
|
|
38
38
|
parent: row,
|
|
39
39
|
id: `${row}-control`,
|
|
@@ -44,12 +44,12 @@ function generateField(name, label, inputType) {
|
|
|
44
44
|
// Reads the form. The password is SHA-256 hashed when enable_encrypt_password is set.
|
|
45
45
|
// `remember` is included when the login_remember_me setting shows the checkbox.
|
|
46
46
|
export function getLoginFormData() {
|
|
47
|
-
const username =
|
|
48
|
-
let password =
|
|
47
|
+
const username = document.getElementById(`${FORM_ID}-username-control`)?.value;
|
|
48
|
+
let password = document.getElementById(`${FORM_ID}-password-control`)?.value;
|
|
49
49
|
if (config.enable_encrypt_password) password = CryptoJS.SHA256(password).toString();
|
|
50
50
|
const data = { username, password };
|
|
51
|
-
const remember =
|
|
52
|
-
if (remember
|
|
51
|
+
const remember = document.getElementById(`${FORM_ID}-remember-control`);
|
|
52
|
+
if (remember) data.remember = remember.checked;
|
|
53
53
|
return data;
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -85,12 +85,12 @@ export function generatePasswordElements() {
|
|
|
85
85
|
export function generateRememberElement() {
|
|
86
86
|
if (!config.login_remember_me) return;
|
|
87
87
|
const row = generateRow("remember");
|
|
88
|
-
|
|
88
|
+
generateControl({ inputType: "checkbox", parent: row, id: `${row}-control` });
|
|
89
89
|
HTMLElementGenerator({ elementType: "span", parent: row, id: `${row}-label`, text: translate("rememberMe") });
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
export function generateSubmitElement() {
|
|
93
|
-
|
|
93
|
+
generateControl({
|
|
94
94
|
inputType: "button",
|
|
95
95
|
parent: generateRow("submit"),
|
|
96
96
|
text: translate("login"),
|
|
@@ -28,8 +28,13 @@ export function loadScript(src) {
|
|
|
28
28
|
return loadedScripts.get(src);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Loads the classic scripts that are still needed: only crypto-js, and only when the deprecated enable_encrypt_password is on.
|
|
33
|
+
* Athena UI has no other classic script, and does not need jQuery.
|
|
34
|
+
*/
|
|
35
|
+
export function loadLibs(extraScripts = [], { crypto = false } = {}) {
|
|
36
|
+
const wanted = libs.filter(lib => (lib.needs === "crypto" ? crypto : true));
|
|
37
|
+
return Promise.all([...wanted.map(lib => lib.src), ...extraScripts].map(loadScript));
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
// Accepts { id } of a registered theme or a custom { href }. Falls back to the default theme.
|