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,50 @@
|
|
|
1
|
+
//changePasswordFormGenerator 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 config from "../../config/config.js";
|
|
7
|
+
import { getChangePassword } from "../../config/hooks.js";
|
|
8
|
+
import { translate } from "../../core/i18n.js";
|
|
9
|
+
import { validateAll } from "../../core/validation.js";
|
|
10
|
+
import { generateButton, generateField, generateFormDiv, generateMessage, hashPassword, readControl, showMessage } from "./formUtils.js";
|
|
11
|
+
|
|
12
|
+
const FORM_ID = "changePasswordForm";
|
|
13
|
+
|
|
14
|
+
// Reads the form. Both passwords are SHA-256 hashed when enable_encrypt_password is set.
|
|
15
|
+
export function getChangePasswordFormData() {
|
|
16
|
+
return {
|
|
17
|
+
currentPassword: hashPassword(readControl(FORM_ID, "current")),
|
|
18
|
+
newPassword: hashPassword(readControl(FORM_ID, "new"))
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Returns the first error text, or "" when the form is valid. The new password has to differ from the old one.
|
|
23
|
+
export function validateChangePasswordForm() {
|
|
24
|
+
const values = {
|
|
25
|
+
current: readControl(FORM_ID, "current"),
|
|
26
|
+
new: readControl(FORM_ID, "new"),
|
|
27
|
+
new2: readControl(FORM_ID, "new2")
|
|
28
|
+
};
|
|
29
|
+
const errors = validateAll({
|
|
30
|
+
current: { required: true },
|
|
31
|
+
new: { required: true, validate: (value, all) => value !== all.current },
|
|
32
|
+
new2: { required: true, match: "new" }
|
|
33
|
+
}, values);
|
|
34
|
+
return Object.values(errors)[0] ?? "";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function generateChangePasswordForm(parent = "wrapper") {
|
|
38
|
+
generateFormDiv(FORM_ID, config.changePasswordForm_class, parent);
|
|
39
|
+
generateField(FORM_ID, "current", "currentPassword", "password");
|
|
40
|
+
generateField(FORM_ID, "new", "newPassword", "password");
|
|
41
|
+
generateField(FORM_ID, "new2", "confirmPassword", "password");
|
|
42
|
+
generateButton(FORM_ID, "change", translate("changePassword"), submitChangePassword);
|
|
43
|
+
generateMessage(FORM_ID);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function submitChangePassword() {
|
|
47
|
+
const error = validateChangePasswordForm();
|
|
48
|
+
showMessage(FORM_ID, error);
|
|
49
|
+
if (!error && config.change_password_enabled) getChangePassword()?.(getChangePasswordFormData());
|
|
50
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
//formBuilder 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 { createCombobox } from "../../core/combobox.js";
|
|
7
|
+
import { createDateRange } from "../../core/dateRange.js";
|
|
8
|
+
import { cx, el, mount, uid } from "../../core/dom.js";
|
|
9
|
+
import { translate } from "../../core/i18n.js";
|
|
10
|
+
import { validateDateRange, validateValue } from "../../core/validation.js";
|
|
11
|
+
import { createRichText } from "../inputGenerator/richTextGenerator.js";
|
|
12
|
+
|
|
13
|
+
const INPUT_TYPES = new Set([
|
|
14
|
+
"text", "password", "email", "number", "date", "datetime-local", "time", "month", "week",
|
|
15
|
+
"url", "tel", "search", "color", "range", "file", "hidden"
|
|
16
|
+
]);
|
|
17
|
+
const RULE_KEYS = ["required", "email", "minLength", "maxLength", "pattern", "min", "max", "match", "validate"];
|
|
18
|
+
|
|
19
|
+
/* ------------------BUTTON------------------- */
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Generates a button.
|
|
23
|
+
*
|
|
24
|
+
* @param {object} options
|
|
25
|
+
* @param {string|Element} [options.parent] id or element, defaults to the body
|
|
26
|
+
* @param {string} [options.id]
|
|
27
|
+
* @param {string} [options.text]
|
|
28
|
+
* @param {"primary"|"secondary"|"danger"|"link"} [options.variant="primary"]
|
|
29
|
+
* @param {"button"|"submit"|"reset"} [options.type="button"]
|
|
30
|
+
* @param {"small"|"large"} [options.size]
|
|
31
|
+
* @param {boolean} [options.disabled]
|
|
32
|
+
* @param {boolean} [options.loading] shows a spinner and blocks clicks
|
|
33
|
+
* @param {(event: Event) => void} [options.onclick]
|
|
34
|
+
* @returns {{element: HTMLButtonElement, setLoading: Function, setDisabled: Function, setText: Function}}
|
|
35
|
+
*/
|
|
36
|
+
export function generateButton(options = {}) {
|
|
37
|
+
const { variant = "primary", type = "button", size, onclick } = options;
|
|
38
|
+
let disabled = Boolean(options.disabled);
|
|
39
|
+
let loading = false;
|
|
40
|
+
const label = el("span", { class: "button-text", text: options.text ?? "" });
|
|
41
|
+
const element = el("button", {
|
|
42
|
+
id: options.id,
|
|
43
|
+
class: cx("athena-button", `button-${variant}`, size && `button-${size}`, options.class),
|
|
44
|
+
attrs: { type }
|
|
45
|
+
}, label);
|
|
46
|
+
if (onclick) element.addEventListener("click", event => { if (!loading) onclick(event); });
|
|
47
|
+
|
|
48
|
+
const sync = () => {
|
|
49
|
+
element.disabled = disabled || loading;
|
|
50
|
+
element.toggleAttribute("aria-busy", loading);
|
|
51
|
+
element.classList.toggle("button-loading", loading);
|
|
52
|
+
element.querySelector(".spinner")?.remove();
|
|
53
|
+
if (loading) element.prepend(el("span", { class: "spinner spinner-small", attrs: { "aria-hidden": "true" } }));
|
|
54
|
+
};
|
|
55
|
+
sync();
|
|
56
|
+
if (options.loading) {
|
|
57
|
+
loading = true;
|
|
58
|
+
sync();
|
|
59
|
+
}
|
|
60
|
+
mount(options.parent, element);
|
|
61
|
+
return {
|
|
62
|
+
element,
|
|
63
|
+
setLoading(value) {
|
|
64
|
+
loading = Boolean(value);
|
|
65
|
+
sync();
|
|
66
|
+
},
|
|
67
|
+
setDisabled(value) {
|
|
68
|
+
disabled = Boolean(value);
|
|
69
|
+
sync();
|
|
70
|
+
},
|
|
71
|
+
setText(text) {
|
|
72
|
+
label.textContent = text;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/* ------------------FIELD------------------- */
|
|
78
|
+
|
|
79
|
+
function choiceList(wrapper, kind, name, choices, value) {
|
|
80
|
+
const inputs = choices.map(choice => {
|
|
81
|
+
const item = typeof choice === "object" ? choice : { value: choice, text: choice };
|
|
82
|
+
const input = el("input", {
|
|
83
|
+
attrs: { type: kind, name, value: item.value, disabled: item.disabled },
|
|
84
|
+
prop: { checked: kind === "radio" ? item.value === value : Array.isArray(value) && value.includes(item.value) }
|
|
85
|
+
});
|
|
86
|
+
wrapper.append(el("label", { class: "choice" }, input, el("span", { text: item.text ?? item.value })));
|
|
87
|
+
return input;
|
|
88
|
+
});
|
|
89
|
+
return inputs;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Generates one labelled form field with help text and an error line.
|
|
94
|
+
*
|
|
95
|
+
* @param {object} options
|
|
96
|
+
* @param {string} options.name key of the field in the form values
|
|
97
|
+
* @param {"text"|"password"|"email"|"number"|"date"|"datetime-local"|"time"|"url"|"tel"|"search"|"color"|"range"|"file"|"hidden"|"textarea"|"select"|"checkbox"|"switch"|"radio"|"checkboxes"|"autocomplete"|"multiselect"|"daterange"|"richtext"} [options.type="text"]
|
|
98
|
+
* @param {string} [options.label]
|
|
99
|
+
* @param {string} [options.help] help text under the control
|
|
100
|
+
* @param {*} [options.value] initial value (boolean for checkbox / switch, array for checkboxes / multiple select)
|
|
101
|
+
* @param {(string|{value: string, text?: string, disabled?: boolean})[]} [options.options] choices of select / radio / checkboxes
|
|
102
|
+
* @param {boolean} [options.multiple] select / file: allow several values
|
|
103
|
+
* @param {string} [options.placeholder]
|
|
104
|
+
* @param {number} [options.rows] textarea rows
|
|
105
|
+
* @param {object} [options.rules] validation rules, see validateValue. They can also be given directly
|
|
106
|
+
* (required, email, minLength, ...)
|
|
107
|
+
* @param {(value, field) => void} [options.onChange]
|
|
108
|
+
* @param {() => object} [options.getValues] all form values (set by generateForm, used by `match`)
|
|
109
|
+
* @returns {{element: HTMLElement, name: string, getValue: Function, setValue: Function, validate: Function,
|
|
110
|
+
* setError: Function, focus: Function}}
|
|
111
|
+
*/
|
|
112
|
+
export function generateField(options = {}) {
|
|
113
|
+
const { name, type = "text", label, help, placeholder, onChange } = options;
|
|
114
|
+
const rules = { ...Object.fromEntries(RULE_KEYS.filter(key => options[key] != null).map(key => [key, options[key]])), ...options.rules };
|
|
115
|
+
if (type === "email") rules.email = true;
|
|
116
|
+
// min / max of a date range are dates, not the numeric rules
|
|
117
|
+
if (type === "daterange") {
|
|
118
|
+
delete rules.min;
|
|
119
|
+
delete rules.max;
|
|
120
|
+
}
|
|
121
|
+
const id = options.id ?? uid("field");
|
|
122
|
+
const helpId = `${id}-help`;
|
|
123
|
+
const errorId = `${id}-error`;
|
|
124
|
+
const choiceName = uid("choice");
|
|
125
|
+
|
|
126
|
+
const wrapper = el("div", { class: cx("field", `field-${type}`, options.class), attrs: { "data-name": name } });
|
|
127
|
+
const grouped = type === "radio" || type === "checkboxes";
|
|
128
|
+
const inline = type === "checkbox" || type === "switch";
|
|
129
|
+
const caption = label == null ? null : el("span", { class: "field-label-text", text: label });
|
|
130
|
+
if (caption && rules.required) caption.append(el("span", { class: "field-required", text: " *", attrs: { "aria-hidden": "true" } }));
|
|
131
|
+
|
|
132
|
+
let controls;
|
|
133
|
+
let controlNodes;
|
|
134
|
+
let getValue;
|
|
135
|
+
let setValue;
|
|
136
|
+
const events = ["input", "change"];
|
|
137
|
+
const container = grouped ? el("fieldset", { class: "field-group" }, el("legend", {}, caption ?? "")) : wrapper;
|
|
138
|
+
|
|
139
|
+
if (type === "textarea") {
|
|
140
|
+
const control = el("textarea", { id, attrs: { name, placeholder, rows: options.rows ?? 3 }, prop: { value: options.value ?? "" } });
|
|
141
|
+
controls = [control];
|
|
142
|
+
getValue = () => control.value;
|
|
143
|
+
setValue = value => { control.value = value ?? ""; };
|
|
144
|
+
} else if (type === "select") {
|
|
145
|
+
const control = el("select", { id, attrs: { name, multiple: options.multiple } });
|
|
146
|
+
if (placeholder && !options.multiple) control.append(el("option", { text: placeholder, attrs: { value: "" } }));
|
|
147
|
+
for (const choice of options.options ?? []) {
|
|
148
|
+
const item = typeof choice === "object" ? choice : { value: choice, text: choice };
|
|
149
|
+
control.append(el("option", { text: item.text ?? item.value, attrs: { value: item.value, disabled: item.disabled } }));
|
|
150
|
+
}
|
|
151
|
+
controls = [control];
|
|
152
|
+
getValue = () => options.multiple ? [...control.selectedOptions].map(o => o.value) : control.value;
|
|
153
|
+
setValue = value => {
|
|
154
|
+
const wanted = [].concat(value ?? "");
|
|
155
|
+
[...control.options].forEach(o => { o.selected = wanted.includes(o.value); });
|
|
156
|
+
};
|
|
157
|
+
setValue(options.value);
|
|
158
|
+
} else if (inline) {
|
|
159
|
+
const control = el("input", { id, attrs: { type: "checkbox", name, role: type === "switch" ? "switch" : null }, prop: { checked: Boolean(options.value) } });
|
|
160
|
+
controls = [control];
|
|
161
|
+
getValue = () => control.checked;
|
|
162
|
+
setValue = value => { control.checked = Boolean(value); };
|
|
163
|
+
} else if (grouped) {
|
|
164
|
+
const kind = type === "radio" ? "radio" : "checkbox";
|
|
165
|
+
const list = el("div", { class: "choices" });
|
|
166
|
+
controls = choiceList(list, kind, choiceName, options.options ?? [], options.value);
|
|
167
|
+
container.append(list);
|
|
168
|
+
getValue = () => kind === "radio"
|
|
169
|
+
? (controls.find(input => input.checked)?.value ?? "")
|
|
170
|
+
: controls.filter(input => input.checked).map(input => input.value);
|
|
171
|
+
setValue = value => controls.forEach(input => {
|
|
172
|
+
input.checked = kind === "radio" ? input.value === value : [].concat(value ?? []).includes(input.value);
|
|
173
|
+
});
|
|
174
|
+
} else if (type === "autocomplete" || type === "multiselect") {
|
|
175
|
+
const combo = createCombobox({
|
|
176
|
+
id, name, placeholder, options: options.options, multiple: type === "multiselect",
|
|
177
|
+
minChars: options.minChars, strict: options.strict, value: options.value, disabled: options.disabled
|
|
178
|
+
});
|
|
179
|
+
controls = [combo.input];
|
|
180
|
+
controlNodes = [combo.element];
|
|
181
|
+
getValue = combo.getValue;
|
|
182
|
+
setValue = combo.setValue;
|
|
183
|
+
} else if (type === "daterange") {
|
|
184
|
+
const range = createDateRange({ id, name, value: options.value, min: options.min, max: options.max, disabled: options.disabled });
|
|
185
|
+
controls = range.inputs;
|
|
186
|
+
controlNodes = [range.element];
|
|
187
|
+
getValue = range.getValue;
|
|
188
|
+
setValue = range.setValue;
|
|
189
|
+
} else if (type === "richtext") {
|
|
190
|
+
const rich = createRichText({
|
|
191
|
+
id, value: options.value, placeholder, minHeight: options.minHeight, toolbar: options.toolbar, disabled: options.disabled
|
|
192
|
+
});
|
|
193
|
+
rich.editor.setAttribute("aria-labelledby", `${id}-label`);
|
|
194
|
+
controls = [rich.editor];
|
|
195
|
+
controlNodes = [rich.element];
|
|
196
|
+
getValue = rich.getValue;
|
|
197
|
+
setValue = rich.setValue;
|
|
198
|
+
} else if (INPUT_TYPES.has(type)) {
|
|
199
|
+
const control = el("input", { id, attrs: { type, name, placeholder, multiple: options.multiple, accept: options.accept, min: options.min, max: options.max, step: options.step, autocomplete: options.autocomplete } });
|
|
200
|
+
if (type !== "file" && options.value != null) control.value = options.value;
|
|
201
|
+
controls = [control];
|
|
202
|
+
if (type === "file") {
|
|
203
|
+
getValue = () => [...control.files];
|
|
204
|
+
setValue = () => {};
|
|
205
|
+
} else if (type === "number" || type === "range") {
|
|
206
|
+
getValue = () => control.value === "" ? "" : control.valueAsNumber;
|
|
207
|
+
setValue = value => { control.value = value ?? ""; };
|
|
208
|
+
} else {
|
|
209
|
+
getValue = () => control.value;
|
|
210
|
+
setValue = value => { control.value = value ?? ""; };
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
throw new Error(`Athena UI: unknown field type "${type}"`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (const control of controls) {
|
|
217
|
+
control.disabled = Boolean(options.disabled);
|
|
218
|
+
if (!grouped) control.id = control.id || id;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Markup: label above the control, a checkbox / switch label wraps its control.
|
|
222
|
+
const errorEl = el("div", { class: "field-error", id: errorId, attrs: { role: "alert" } });
|
|
223
|
+
const helpEl = help ? el("div", { class: "field-help", id: helpId, text: help }) : null;
|
|
224
|
+
if (inline) {
|
|
225
|
+
const track = type === "switch" ? el("span", { class: "switch-track", attrs: { "aria-hidden": "true" } }) : null;
|
|
226
|
+
wrapper.append(el("label", { class: cx("choice", type === "switch" && "switch"), attrs: { for: id } }, controls[0], track ?? "", caption ?? ""));
|
|
227
|
+
} else if (grouped) {
|
|
228
|
+
wrapper.append(container);
|
|
229
|
+
} else {
|
|
230
|
+
if (caption) wrapper.append(el("label", { class: "field-label", id: `${id}-label`, attrs: { for: id } }, caption));
|
|
231
|
+
wrapper.append(...(controlNodes ?? controls));
|
|
232
|
+
}
|
|
233
|
+
if (helpEl) wrapper.append(helpEl);
|
|
234
|
+
wrapper.append(errorEl);
|
|
235
|
+
for (const control of controls) control.setAttribute("aria-describedby", [helpEl && helpId, errorId].filter(Boolean).join(" "));
|
|
236
|
+
if (rules.required && type !== "checkbox") controls.forEach(control => control.setAttribute("aria-required", "true"));
|
|
237
|
+
mount(options.parent, wrapper);
|
|
238
|
+
|
|
239
|
+
let touched = false;
|
|
240
|
+
const field = {
|
|
241
|
+
element: wrapper,
|
|
242
|
+
name,
|
|
243
|
+
getValue,
|
|
244
|
+
setValue(value) {
|
|
245
|
+
setValue(value);
|
|
246
|
+
field.setError("");
|
|
247
|
+
},
|
|
248
|
+
setError(message) {
|
|
249
|
+
errorEl.textContent = message;
|
|
250
|
+
wrapper.classList.toggle("field-invalid", Boolean(message));
|
|
251
|
+
controls.forEach(control => control.setAttribute("aria-invalid", message ? "true" : "false"));
|
|
252
|
+
},
|
|
253
|
+
validate() {
|
|
254
|
+
const values = options.getValues?.() ?? { [name]: getValue() };
|
|
255
|
+
const value = getValue();
|
|
256
|
+
// A date range counts as filled when both dates are given.
|
|
257
|
+
const filled = type === "daterange" && !(value.from && value.to) ? "" : value;
|
|
258
|
+
let error = validateValue(filled, rules, values);
|
|
259
|
+
if (!error && type === "daterange") error = validateDateRange(value.from, value.to, { min: options.min, max: options.max });
|
|
260
|
+
field.setError(error);
|
|
261
|
+
return error;
|
|
262
|
+
},
|
|
263
|
+
focus() {
|
|
264
|
+
controls[0]?.focus();
|
|
265
|
+
},
|
|
266
|
+
resetState() {
|
|
267
|
+
touched = false;
|
|
268
|
+
field.setError("");
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// Validate when the user leaves the field, then live on every change.
|
|
273
|
+
controls.forEach(control => {
|
|
274
|
+
control.addEventListener("blur", () => { touched = true; field.validate(); });
|
|
275
|
+
events.forEach(event => control.addEventListener(event, () => {
|
|
276
|
+
if (touched) field.validate();
|
|
277
|
+
if (event === "change" || event === "input") onChange?.(getValue(), field);
|
|
278
|
+
}));
|
|
279
|
+
});
|
|
280
|
+
return field;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/* ------------------FORM------------------- */
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Generates a form from a list of field definitions.
|
|
287
|
+
*
|
|
288
|
+
* @param {object} options
|
|
289
|
+
* @param {string|Element} [options.parent]
|
|
290
|
+
* @param {string} [options.id]
|
|
291
|
+
* @param {object[]} options.fields field definitions, see generateField
|
|
292
|
+
* @param {string} [options.submitText]
|
|
293
|
+
* @param {string|false} [options.resetText] adds a reset button when set
|
|
294
|
+
* @param {(values: object, form: object) => (void|Promise)} [options.onSubmit] called with the values when every
|
|
295
|
+
* field is valid. A returned promise keeps the submit button busy; a rejection is shown as form message.
|
|
296
|
+
* @param {(values: object, field: object) => void} [options.onChange]
|
|
297
|
+
* @returns {{element: HTMLFormElement, fields: Object<string, object>, getValues: Function, setValues: Function,
|
|
298
|
+
* setErrors: Function, validate: Function, reset: Function, setMessage: Function, submitButton: object}}
|
|
299
|
+
*/
|
|
300
|
+
export function generateForm(options = {}) {
|
|
301
|
+
const element = el("form", { id: options.id, class: cx("athena-form", options.class), attrs: { novalidate: true } });
|
|
302
|
+
const fields = {};
|
|
303
|
+
const getValues = () => Object.fromEntries(Object.entries(fields).map(([name, field]) => [name, field.getValue()]));
|
|
304
|
+
|
|
305
|
+
for (const definition of options.fields ?? []) {
|
|
306
|
+
const field = generateField({
|
|
307
|
+
...definition,
|
|
308
|
+
parent: element,
|
|
309
|
+
getValues,
|
|
310
|
+
onChange: (value, source) => {
|
|
311
|
+
definition.onChange?.(value, source);
|
|
312
|
+
options.onChange?.(getValues(), source);
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
fields[definition.name] = field;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const message = el("div", { class: "form-message", attrs: { role: "status" } });
|
|
319
|
+
element.append(message);
|
|
320
|
+
const actions = el("div", { class: "form-actions" });
|
|
321
|
+
element.append(actions);
|
|
322
|
+
const submitButton = generateButton({ parent: actions, text: options.submitText ?? translate("ok"), type: "submit" });
|
|
323
|
+
if (options.resetText) generateButton({ parent: actions, text: options.resetText, type: "reset", variant: "secondary" });
|
|
324
|
+
|
|
325
|
+
const form = {
|
|
326
|
+
element,
|
|
327
|
+
fields,
|
|
328
|
+
getValues,
|
|
329
|
+
submitButton,
|
|
330
|
+
setValues(values) {
|
|
331
|
+
for (const [name, value] of Object.entries(values)) fields[name]?.setValue(value);
|
|
332
|
+
},
|
|
333
|
+
// Shows server side errors: { username: "Already taken" }
|
|
334
|
+
setErrors(errors) {
|
|
335
|
+
for (const [name, text] of Object.entries(errors)) fields[name]?.setError(text);
|
|
336
|
+
},
|
|
337
|
+
setMessage(text, isError = true) {
|
|
338
|
+
message.textContent = text;
|
|
339
|
+
message.classList.toggle("form-message-error", isError && Boolean(text));
|
|
340
|
+
message.classList.toggle("form-message-success", !isError && Boolean(text));
|
|
341
|
+
},
|
|
342
|
+
// Validates every field, focuses the first invalid one. Returns the errors by field name.
|
|
343
|
+
validate() {
|
|
344
|
+
const errors = {};
|
|
345
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
346
|
+
const error = field.validate();
|
|
347
|
+
if (error) errors[name] = error;
|
|
348
|
+
}
|
|
349
|
+
fields[Object.keys(errors)[0]]?.focus();
|
|
350
|
+
return errors;
|
|
351
|
+
},
|
|
352
|
+
reset() {
|
|
353
|
+
element.reset();
|
|
354
|
+
Object.values(fields).forEach(field => field.resetState());
|
|
355
|
+
form.setMessage("");
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
element.addEventListener("reset", () => queueMicrotask(() => {
|
|
360
|
+
Object.values(fields).forEach(field => field.resetState());
|
|
361
|
+
form.setMessage("");
|
|
362
|
+
}));
|
|
363
|
+
element.addEventListener("submit", async event => {
|
|
364
|
+
event.preventDefault();
|
|
365
|
+
form.setMessage("");
|
|
366
|
+
if (Object.keys(form.validate()).length) return;
|
|
367
|
+
try {
|
|
368
|
+
submitButton.setLoading(true);
|
|
369
|
+
await options.onSubmit?.(getValues(), form);
|
|
370
|
+
} catch (error) {
|
|
371
|
+
form.setMessage(error?.message ?? String(error));
|
|
372
|
+
} finally {
|
|
373
|
+
submitButton.setLoading(false);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
mount(options.parent, element);
|
|
378
|
+
return form;
|
|
379
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//formUtils 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
|
+
/* global $, CryptoJS, HTMLControlGenerator */
|
|
7
|
+
|
|
8
|
+
import { HTMLElementGenerator } from "../HTMLElementGenerator/src/HTMLElementGenerator.js";
|
|
9
|
+
import config from "../../config/config.js";
|
|
10
|
+
import { translate } from "../../core/i18n.js";
|
|
11
|
+
|
|
12
|
+
export { EMAIL_PATTERN } from "../../core/validation.js";
|
|
13
|
+
|
|
14
|
+
export function generateFormDiv(formId, className, parent = "wrapper") {
|
|
15
|
+
return HTMLElementGenerator({ elementType: "div", parent, id: formId, class: className });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function generateRow(formId, name) {
|
|
19
|
+
const id = `${formId}-${name}`;
|
|
20
|
+
HTMLElementGenerator({ elementType: "div", parent: formId, id, class: "container" });
|
|
21
|
+
return id;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// `label` is a translation key
|
|
25
|
+
export function generateField(formId, name, label, inputType) {
|
|
26
|
+
const row = generateRow(formId, name);
|
|
27
|
+
HTMLElementGenerator({ elementType: "span", parent: row, id: `${row}-label`, class: "container", text: `${translate(label)}: ` });
|
|
28
|
+
HTMLControlGenerator({ inputType, parent: row, id: `${row}-control`, class: "text-filed-class" });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function generateButton(formId, name, text, onclick) {
|
|
32
|
+
HTMLControlGenerator({
|
|
33
|
+
inputType: "button",
|
|
34
|
+
parent: generateRow(formId, name),
|
|
35
|
+
text,
|
|
36
|
+
value: text,
|
|
37
|
+
id: `${formId}-submit-${name}`,
|
|
38
|
+
class: "button-filed-class",
|
|
39
|
+
onclick
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Message line under the form, used for validation errors and status texts.
|
|
44
|
+
export function generateMessage(formId) {
|
|
45
|
+
HTMLElementGenerator({
|
|
46
|
+
elementType: "div",
|
|
47
|
+
parent: generateRow(formId, "message"),
|
|
48
|
+
id: `${formId}-message`,
|
|
49
|
+
class: "form-message"
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function showMessage(formId, text, isError = true) {
|
|
54
|
+
$(`#${formId}-message`)
|
|
55
|
+
.text(text)
|
|
56
|
+
.toggleClass("form-message-error", isError)
|
|
57
|
+
.toggleClass("form-message-success", !isError);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function readControl(formId, name) {
|
|
61
|
+
return $(`#${formId}-${name}-control`).val() ?? "";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function hashPassword(password) {
|
|
65
|
+
return config.enable_encrypt_password ? CryptoJS.SHA256(password).toString() : password;
|
|
66
|
+
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { HTMLElementGenerator } from "../HTMLElementGenerator/src/HTMLElementGenerator.js";
|
|
9
9
|
import config from "../../config/config.js";
|
|
10
10
|
import { getLogin } from "../../config/hooks.js";
|
|
11
|
+
import { translate } from "../../core/i18n.js";
|
|
11
12
|
|
|
12
13
|
const FORM_ID = "loginForm";
|
|
13
14
|
|
|
@@ -22,6 +23,7 @@ function generateRow(name) {
|
|
|
22
23
|
return id;
|
|
23
24
|
}
|
|
24
25
|
|
|
26
|
+
// `label` is a translation key
|
|
25
27
|
function generateField(name, label, inputType) {
|
|
26
28
|
const row = generateRow(name);
|
|
27
29
|
HTMLElementGenerator({
|
|
@@ -29,7 +31,7 @@ function generateField(name, label, inputType) {
|
|
|
29
31
|
parent: row,
|
|
30
32
|
id: `${row}-label`,
|
|
31
33
|
class: "container",
|
|
32
|
-
text: label
|
|
34
|
+
text: `${translate(label)}: `
|
|
33
35
|
});
|
|
34
36
|
HTMLControlGenerator({
|
|
35
37
|
inputType,
|
|
@@ -40,11 +42,15 @@ function generateField(name, label, inputType) {
|
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
// Reads the form. The password is SHA-256 hashed when enable_encrypt_password is set.
|
|
45
|
+
// `remember` is included when the login_remember_me setting shows the checkbox.
|
|
43
46
|
export function getLoginFormData() {
|
|
44
47
|
const username = $(`#${FORM_ID}-username-control`).val();
|
|
45
48
|
let password = $(`#${FORM_ID}-password-control`).val();
|
|
46
49
|
if (config.enable_encrypt_password) password = CryptoJS.SHA256(password).toString();
|
|
47
|
-
|
|
50
|
+
const data = { username, password };
|
|
51
|
+
const remember = $(`#${FORM_ID}-remember-control`);
|
|
52
|
+
if (remember.length) data.remember = remember.prop("checked");
|
|
53
|
+
return data;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
export function generateLoginForm() {
|
|
@@ -64,23 +70,31 @@ export function generateFormDiv() {
|
|
|
64
70
|
export function generateFormElements() {
|
|
65
71
|
generateUsernameElements();
|
|
66
72
|
generatePasswordElements();
|
|
73
|
+
generateRememberElement();
|
|
67
74
|
generateSubmitElement();
|
|
68
75
|
}
|
|
69
76
|
|
|
70
77
|
export function generateUsernameElements() {
|
|
71
|
-
generateField("username", "
|
|
78
|
+
generateField("username", "username", "text");
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
export function generatePasswordElements() {
|
|
75
|
-
generateField("password", "
|
|
82
|
+
generateField("password", "password", "password");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function generateRememberElement() {
|
|
86
|
+
if (!config.login_remember_me) return;
|
|
87
|
+
const row = generateRow("remember");
|
|
88
|
+
HTMLControlGenerator({ inputType: "checkbox", parent: row, id: `${row}-control` });
|
|
89
|
+
HTMLElementGenerator({ elementType: "span", parent: row, id: `${row}-label`, text: translate("rememberMe") });
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
export function generateSubmitElement() {
|
|
79
93
|
HTMLControlGenerator({
|
|
80
94
|
inputType: "button",
|
|
81
95
|
parent: generateRow("submit"),
|
|
82
|
-
text: "
|
|
83
|
-
value: "
|
|
96
|
+
text: translate("login"),
|
|
97
|
+
value: translate("login"),
|
|
84
98
|
id: `${FORM_ID}-submit-login`,
|
|
85
99
|
class: "button-filed-class",
|
|
86
100
|
onclick: () => {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//lostPasswordFormGenerator 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 config from "../../config/config.js";
|
|
7
|
+
import { getLostPassword } from "../../config/hooks.js";
|
|
8
|
+
import { translate } from "../../core/i18n.js";
|
|
9
|
+
import { validateValue } from "../../core/validation.js";
|
|
10
|
+
import { generateButton, generateField, generateFormDiv, generateMessage, readControl, showMessage } from "./formUtils.js";
|
|
11
|
+
|
|
12
|
+
const FORM_ID = "lostPasswordForm";
|
|
13
|
+
|
|
14
|
+
export function getLostPasswordFormData() {
|
|
15
|
+
return { email: readControl(FORM_ID, "email").trim() };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function generateLostPasswordForm(parent = "wrapper") {
|
|
19
|
+
generateFormDiv(FORM_ID, config.lostPasswordForm_class, parent);
|
|
20
|
+
generateField(FORM_ID, "email", "email", "email");
|
|
21
|
+
generateButton(FORM_ID, "reset", translate("resetPassword"), submitLostPassword);
|
|
22
|
+
generateMessage(FORM_ID);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function submitLostPassword() {
|
|
26
|
+
const { email } = getLostPasswordFormData();
|
|
27
|
+
const error = validateValue(email, { required: true, email: true });
|
|
28
|
+
showMessage(FORM_ID, error);
|
|
29
|
+
if (!error && config.lost_password_enabled) getLostPassword()?.({ email });
|
|
30
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//registrationFormGenerator 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 config from "../../config/config.js";
|
|
7
|
+
import { getRegister } from "../../config/hooks.js";
|
|
8
|
+
import { translate } from "../../core/i18n.js";
|
|
9
|
+
import { validateAll } from "../../core/validation.js";
|
|
10
|
+
import { generateButton, generateField, generateFormDiv, generateMessage, hashPassword, readControl, showMessage } from "./formUtils.js";
|
|
11
|
+
|
|
12
|
+
const FORM_ID = "registrationForm";
|
|
13
|
+
|
|
14
|
+
// Reads the form. The password is SHA-256 hashed when enable_encrypt_password is set.
|
|
15
|
+
export function getRegistrationFormData() {
|
|
16
|
+
return {
|
|
17
|
+
username: readControl(FORM_ID, "username"),
|
|
18
|
+
email: readControl(FORM_ID, "email"),
|
|
19
|
+
password: hashPassword(readControl(FORM_ID, "password"))
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Returns the first error text, or "" when the form is valid.
|
|
24
|
+
export function validateRegistrationForm() {
|
|
25
|
+
const values = {
|
|
26
|
+
username: readControl(FORM_ID, "username").trim(),
|
|
27
|
+
email: readControl(FORM_ID, "email"),
|
|
28
|
+
password: readControl(FORM_ID, "password"),
|
|
29
|
+
password2: readControl(FORM_ID, "password2")
|
|
30
|
+
};
|
|
31
|
+
const errors = validateAll({
|
|
32
|
+
username: { required: true },
|
|
33
|
+
email: { required: true, email: true },
|
|
34
|
+
password: { required: true },
|
|
35
|
+
password2: { required: true, match: "password" }
|
|
36
|
+
}, values);
|
|
37
|
+
return Object.values(errors)[0] ?? "";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function generateRegistrationForm(parent = "wrapper") {
|
|
41
|
+
generateFormDiv(FORM_ID, config.registrationForm_class, parent);
|
|
42
|
+
generateField(FORM_ID, "username", "username", "text");
|
|
43
|
+
generateField(FORM_ID, "email", "email", "email");
|
|
44
|
+
generateField(FORM_ID, "password", "password", "password");
|
|
45
|
+
generateField(FORM_ID, "password2", "confirmPassword", "password");
|
|
46
|
+
generateButton(FORM_ID, "register", translate("register"), submitRegistration);
|
|
47
|
+
generateMessage(FORM_ID);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function submitRegistration() {
|
|
51
|
+
const error = validateRegistrationForm();
|
|
52
|
+
showMessage(FORM_ID, error);
|
|
53
|
+
if (!error && config.registration_enabled) getRegister()?.(getRegistrationFormData());
|
|
54
|
+
}
|