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
package/src/athena-ui.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
3
|
//LICENCE: MIT
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
-
//Version: 0.
|
|
5
|
+
//Version: 1.0.0
|
|
6
6
|
|
|
7
7
|
/* global $ */
|
|
8
8
|
|
|
@@ -15,6 +15,24 @@ import * as wrapper from "./libs/hephaestus/LayoutGenerator/wrapperContentGenera
|
|
|
15
15
|
import * as footer from "./libs/hephaestus/LayoutGenerator/footerContentGenerator.js";
|
|
16
16
|
import * as menu from "./libs/hephaestus/MenuGenerator/basicMenuGenerator.js";
|
|
17
17
|
import * as loginForm from "./libs/hephaestus/formGenerator/loginFormGenerator.js";
|
|
18
|
+
import * as registrationForm from "./libs/hephaestus/formGenerator/registrationFormGenerator.js";
|
|
19
|
+
import * as lostPasswordForm from "./libs/hephaestus/formGenerator/lostPasswordFormGenerator.js";
|
|
20
|
+
import * as chart from "./libs/hephaestus/chartGenerator/chartGenerator.js";
|
|
21
|
+
import * as table from "./libs/hephaestus/tableGenerator/tableGenerator.js";
|
|
22
|
+
import * as changePasswordForm from "./libs/hephaestus/formGenerator/changePasswordFormGenerator.js";
|
|
23
|
+
import * as formBuilder from "./libs/hephaestus/formGenerator/formBuilder.js";
|
|
24
|
+
import * as feedback from "./libs/hephaestus/feedbackGenerator/feedbackGenerator.js";
|
|
25
|
+
import * as dialog from "./libs/hephaestus/dialogGenerator/dialogGenerator.js";
|
|
26
|
+
import * as navigation from "./libs/hephaestus/navigationGenerator/navigationGenerator.js";
|
|
27
|
+
import * as components from "./libs/hephaestus/componentGenerator/componentGenerator.js";
|
|
28
|
+
import * as inputs from "./libs/hephaestus/inputGenerator/inputGenerator.js";
|
|
29
|
+
import * as richText from "./libs/hephaestus/inputGenerator/richTextGenerator.js";
|
|
30
|
+
import * as sanitize from "./libs/core/sanitize.js";
|
|
31
|
+
import * as router from "./libs/hephaestus/router/router.js";
|
|
32
|
+
import * as session from "./libs/hephaestus/auth/session.js";
|
|
33
|
+
import * as i18n from "./libs/core/i18n.js";
|
|
34
|
+
import * as validation from "./libs/core/validation.js";
|
|
35
|
+
import * as csv from "./libs/core/csv.js";
|
|
18
36
|
import * as modal from "./libs/hephaestus/modalGenerator/modalGenerator.js";
|
|
19
37
|
import Cookies from "./libs/js-cookie/src/api.mjs";
|
|
20
38
|
import { HTMLElementGenerator } from "./libs/hephaestus/HTMLElementGenerator/src/HTMLElementGenerator.js";
|
|
@@ -31,11 +49,15 @@ if (typeof $ !== "function") {
|
|
|
31
49
|
* @param {object} [options.config] settings overriding the defaults (and the global appConfig)
|
|
32
50
|
* @param {Function} [options.nav] navigation callback (falls back to the global nav)
|
|
33
51
|
* @param {Function} [options.login] login callback (falls back to the global login)
|
|
52
|
+
* @param {Function} [options.register] registration callback (falls back to the global register)
|
|
53
|
+
* @param {Function} [options.lostPassword] lost password callback (falls back to the global lostPassword)
|
|
54
|
+
* @param {Function} [options.changePassword] change password callback (falls back to the global changePassword)
|
|
34
55
|
* @param {object} [options.menuItems] { headerMenu, footerMenu } (falls back to the global menuItems)
|
|
35
56
|
* @returns {Promise<object>} the effective configuration
|
|
36
57
|
*/
|
|
37
58
|
export async function init(options = {}) {
|
|
38
59
|
applyConfig(getAppConfig(), options.config);
|
|
60
|
+
i18n.setLocale(config.locale);
|
|
39
61
|
setHooks(options);
|
|
40
62
|
|
|
41
63
|
await Promise.all([
|
|
@@ -48,6 +70,14 @@ export async function init(options = {}) {
|
|
|
48
70
|
return config;
|
|
49
71
|
}
|
|
50
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Switches the theme at runtime: { id } of a registered theme or { href } of a stylesheet.
|
|
75
|
+
* @returns {Promise<object>} the applied theme
|
|
76
|
+
*/
|
|
77
|
+
export function setTheme(selected) {
|
|
78
|
+
return loadTheme(selected);
|
|
79
|
+
}
|
|
80
|
+
|
|
51
81
|
const AthenaUI = {
|
|
52
82
|
init,
|
|
53
83
|
config,
|
|
@@ -60,6 +90,25 @@ const AthenaUI = {
|
|
|
60
90
|
...footer,
|
|
61
91
|
...menu,
|
|
62
92
|
...loginForm,
|
|
93
|
+
...registrationForm,
|
|
94
|
+
...lostPasswordForm,
|
|
95
|
+
...changePasswordForm,
|
|
96
|
+
...formBuilder,
|
|
97
|
+
...feedback,
|
|
98
|
+
...dialog,
|
|
99
|
+
...navigation,
|
|
100
|
+
...components,
|
|
101
|
+
...inputs,
|
|
102
|
+
...richText,
|
|
103
|
+
...sanitize,
|
|
104
|
+
...router,
|
|
105
|
+
...session,
|
|
106
|
+
...i18n,
|
|
107
|
+
...validation,
|
|
108
|
+
...csv,
|
|
109
|
+
setTheme,
|
|
110
|
+
...chart,
|
|
111
|
+
...table,
|
|
63
112
|
...modal
|
|
64
113
|
};
|
|
65
114
|
|
|
@@ -74,5 +123,23 @@ export * from "./libs/hephaestus/LayoutGenerator/wrapperContentGenerator.js";
|
|
|
74
123
|
export * from "./libs/hephaestus/LayoutGenerator/footerContentGenerator.js";
|
|
75
124
|
export * from "./libs/hephaestus/MenuGenerator/basicMenuGenerator.js";
|
|
76
125
|
export * from "./libs/hephaestus/formGenerator/loginFormGenerator.js";
|
|
126
|
+
export * from "./libs/hephaestus/formGenerator/registrationFormGenerator.js";
|
|
127
|
+
export * from "./libs/hephaestus/formGenerator/lostPasswordFormGenerator.js";
|
|
128
|
+
export * from "./libs/hephaestus/chartGenerator/chartGenerator.js";
|
|
129
|
+
export * from "./libs/hephaestus/tableGenerator/tableGenerator.js";
|
|
130
|
+
export * from "./libs/hephaestus/formGenerator/changePasswordFormGenerator.js";
|
|
131
|
+
export * from "./libs/hephaestus/formGenerator/formBuilder.js";
|
|
132
|
+
export * from "./libs/hephaestus/feedbackGenerator/feedbackGenerator.js";
|
|
133
|
+
export * from "./libs/hephaestus/dialogGenerator/dialogGenerator.js";
|
|
134
|
+
export * from "./libs/hephaestus/navigationGenerator/navigationGenerator.js";
|
|
135
|
+
export * from "./libs/hephaestus/componentGenerator/componentGenerator.js";
|
|
136
|
+
export * from "./libs/hephaestus/inputGenerator/inputGenerator.js";
|
|
137
|
+
export * from "./libs/hephaestus/inputGenerator/richTextGenerator.js";
|
|
138
|
+
export * from "./libs/core/sanitize.js";
|
|
139
|
+
export * from "./libs/hephaestus/router/router.js";
|
|
140
|
+
export * from "./libs/hephaestus/auth/session.js";
|
|
141
|
+
export * from "./libs/core/i18n.js";
|
|
142
|
+
export * from "./libs/core/validation.js";
|
|
143
|
+
export * from "./libs/core/csv.js";
|
|
77
144
|
export * from "./libs/hephaestus/modalGenerator/modalGenerator.js";
|
|
78
145
|
export default AthenaUI;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
export const BORDERED_CLASS = "default-cointainer-bordered";
|
|
7
7
|
|
|
8
8
|
export const defaults = Object.freeze({
|
|
9
|
-
version: "0.
|
|
9
|
+
version: "1.0.0",
|
|
10
10
|
container_class: "container",
|
|
11
11
|
header_class: "header-container",
|
|
12
12
|
header_menu_class: "header-menu menu",
|
|
@@ -14,6 +14,9 @@ export const defaults = Object.freeze({
|
|
|
14
14
|
footer_class: "footer-container",
|
|
15
15
|
footer_menu_class: "footer-menu menu",
|
|
16
16
|
loginForm_class: "login-form form",
|
|
17
|
+
registrationForm_class: "registration-form form",
|
|
18
|
+
lostPasswordForm_class: "lost-password-form form",
|
|
19
|
+
changePasswordForm_class: "change-password-form form",
|
|
17
20
|
|
|
18
21
|
bordered: true,
|
|
19
22
|
generateHeader: true,
|
|
@@ -22,12 +25,23 @@ export const defaults = Object.freeze({
|
|
|
22
25
|
generateFooterContent: true,
|
|
23
26
|
generateWrappercontent: true,
|
|
24
27
|
login_enabled: false,
|
|
28
|
+
registration_enabled: false,
|
|
29
|
+
lost_password_enabled: false,
|
|
30
|
+
change_password_enabled: false,
|
|
31
|
+
login_remember_me: false,
|
|
25
32
|
enable_encrypt_password: false,
|
|
26
33
|
|
|
27
34
|
header_title_text: "Athena UI",
|
|
28
35
|
header_img_url: undefined,
|
|
29
36
|
copyright: "Here is the bottom Copyright Text for website",
|
|
30
37
|
|
|
38
|
+
// Language of the generated texts, see core/i18n.js
|
|
39
|
+
locale: "en",
|
|
40
|
+
|
|
41
|
+
// Cookie that keeps the session, and the lifetime (days) of a remembered session
|
|
42
|
+
session_cookie: "athena_session",
|
|
43
|
+
session_days: 7,
|
|
44
|
+
|
|
31
45
|
// URL (relative to the page) of an extra classic script, loaded before the layout is generated
|
|
32
46
|
customLoader: undefined
|
|
33
47
|
});
|
package/src/libs/config/hooks.js
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
//LICENCE: MIT
|
|
4
4
|
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
5
|
|
|
6
|
-
/* global appConfig, menuItems, nav, login */
|
|
6
|
+
/* global appConfig, menuItems, nav, login, register, lostPassword, changePassword */
|
|
7
7
|
|
|
8
8
|
// Hooks can be passed to init({ nav, login, menuItems }). When they are not, the
|
|
9
|
-
// page-level globals of the same name (appConfig, menuItems, nav, login) are used.
|
|
9
|
+
// page-level globals of the same name (appConfig, menuItems, nav, login, register, lostPassword, changePassword) are used.
|
|
10
10
|
// Globals are looked up on every call, so they may be defined after init().
|
|
11
11
|
|
|
12
12
|
const hooks = {};
|
|
13
13
|
|
|
14
14
|
export function setHooks(options = {}) {
|
|
15
|
-
for (const name of ["nav", "login", "menuItems"]) {
|
|
15
|
+
for (const name of ["nav", "login", "register", "lostPassword", "changePassword", "menuItems"]) {
|
|
16
16
|
if (options[name] !== undefined) hooks[name] = options[name];
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -32,3 +32,15 @@ export function getNav() {
|
|
|
32
32
|
export function getLogin() {
|
|
33
33
|
return hooks.login ?? (typeof login === "function" ? login : undefined);
|
|
34
34
|
}
|
|
35
|
+
|
|
36
|
+
export function getRegister() {
|
|
37
|
+
return hooks.register ?? (typeof register === "function" ? register : undefined);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getLostPassword() {
|
|
41
|
+
return hooks.lostPassword ?? (typeof lostPassword === "function" ? lostPassword : undefined);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getChangePassword() {
|
|
45
|
+
return hooks.changePassword ?? (typeof changePassword === "function" ? changePassword : undefined);
|
|
46
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
//combobox for Athena UI
|
|
2
|
+
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
import { cx, el, uid } from "./dom.js";
|
|
7
|
+
import { translate } from "./i18n.js";
|
|
8
|
+
|
|
9
|
+
const asItem = option => typeof option === "object" ? { value: option.value, text: option.text ?? String(option.value) } : { value: option, text: String(option) };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Internal: input with a suggestion list. Single mode is an autocomplete, multiple mode a tag selector.
|
|
13
|
+
* `source` is an array of options, or a function (query) => options | Promise<options>.
|
|
14
|
+
*/
|
|
15
|
+
export function createCombobox(options = {}) {
|
|
16
|
+
const { multiple = false, minChars = multiple ? 0 : 1, strict = multiple, onChange, onSelect } = options;
|
|
17
|
+
const source = options.options ?? [];
|
|
18
|
+
const listId = uid("listbox");
|
|
19
|
+
const known = new Map(); // value -> text, for the chips and for the single value
|
|
20
|
+
let selected = []; // values
|
|
21
|
+
let items = []; // currently listed items
|
|
22
|
+
let activeIndex = -1;
|
|
23
|
+
let token = 0;
|
|
24
|
+
|
|
25
|
+
const input = el("input", {
|
|
26
|
+
id: options.id,
|
|
27
|
+
class: "combobox-input",
|
|
28
|
+
attrs: {
|
|
29
|
+
type: "text", role: "combobox", autocomplete: "off", placeholder: options.placeholder, name: options.name,
|
|
30
|
+
"aria-autocomplete": "list", "aria-expanded": "false", "aria-controls": listId
|
|
31
|
+
},
|
|
32
|
+
prop: { disabled: Boolean(options.disabled) }
|
|
33
|
+
});
|
|
34
|
+
const list = el("ul", { class: "combobox-list", id: listId, attrs: { role: "listbox", hidden: true } });
|
|
35
|
+
const chips = el("span", { class: "combobox-chips" });
|
|
36
|
+
const element = el("div", { class: cx("combobox", multiple && "combobox-multiple", options.class) }, chips, input, list);
|
|
37
|
+
|
|
38
|
+
const notify = () => {
|
|
39
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
40
|
+
onChange?.(getValue());
|
|
41
|
+
};
|
|
42
|
+
const isOpen = () => !list.hidden;
|
|
43
|
+
function open() {
|
|
44
|
+
list.hidden = false;
|
|
45
|
+
input.setAttribute("aria-expanded", "true");
|
|
46
|
+
}
|
|
47
|
+
function close() {
|
|
48
|
+
list.hidden = true;
|
|
49
|
+
activeIndex = -1;
|
|
50
|
+
input.setAttribute("aria-expanded", "false");
|
|
51
|
+
input.removeAttribute("aria-activedescendant");
|
|
52
|
+
}
|
|
53
|
+
function setActive(index) {
|
|
54
|
+
activeIndex = index;
|
|
55
|
+
[...list.children].forEach((li, i) => {
|
|
56
|
+
li.classList.toggle("combobox-active", i === index);
|
|
57
|
+
if (i === index) {
|
|
58
|
+
input.setAttribute("aria-activedescendant", li.id);
|
|
59
|
+
li.scrollIntoView?.({ block: "nearest" });
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function renderChips() {
|
|
64
|
+
chips.replaceChildren(...selected.map(value => el("span", { class: "chip" },
|
|
65
|
+
el("span", { text: known.get(value) ?? value }),
|
|
66
|
+
el("button", {
|
|
67
|
+
class: "chip-remove",
|
|
68
|
+
text: "×",
|
|
69
|
+
attrs: { type: "button", "aria-label": `${translate("close")} ${known.get(value) ?? value}` },
|
|
70
|
+
on: { click: () => { selected = selected.filter(v => v !== value); renderChips(); notify(); input.focus(); } }
|
|
71
|
+
}))));
|
|
72
|
+
}
|
|
73
|
+
function renderList() {
|
|
74
|
+
list.replaceChildren();
|
|
75
|
+
if (!items.length) {
|
|
76
|
+
list.append(el("li", { class: "combobox-empty", text: translate("noResults"), attrs: { role: "presentation" } }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
items.forEach((item, index) => {
|
|
80
|
+
const isSelected = multiple && selected.includes(item.value);
|
|
81
|
+
const li = el("li", {
|
|
82
|
+
id: `${listId}-${index}`,
|
|
83
|
+
class: cx("combobox-option", isSelected && "combobox-selected"),
|
|
84
|
+
text: (isSelected ? "✓ " : "") + item.text,
|
|
85
|
+
attrs: { role: "option", "aria-selected": String(isSelected) }
|
|
86
|
+
});
|
|
87
|
+
// mousedown, so the choice is made before the input loses focus
|
|
88
|
+
li.addEventListener("mousedown", event => { event.preventDefault(); choose(item); });
|
|
89
|
+
list.append(li);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function choose(item) {
|
|
93
|
+
known.set(item.value, item.text);
|
|
94
|
+
if (multiple) {
|
|
95
|
+
selected = selected.includes(item.value) ? selected.filter(v => v !== item.value) : [...selected, item.value];
|
|
96
|
+
input.value = "";
|
|
97
|
+
renderChips();
|
|
98
|
+
renderList();
|
|
99
|
+
} else {
|
|
100
|
+
selected = [item.value];
|
|
101
|
+
input.value = item.text;
|
|
102
|
+
close();
|
|
103
|
+
}
|
|
104
|
+
onSelect?.(item);
|
|
105
|
+
notify();
|
|
106
|
+
}
|
|
107
|
+
async function search() {
|
|
108
|
+
const query = input.value.trim();
|
|
109
|
+
if (query.length < minChars) {
|
|
110
|
+
close();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const mine = ++token;
|
|
114
|
+
const found = typeof source === "function"
|
|
115
|
+
? await source(query)
|
|
116
|
+
: source.filter(option => asItem(option).text.toLowerCase().includes(query.toLowerCase()));
|
|
117
|
+
if (mine !== token) return;
|
|
118
|
+
items = found.map(asItem);
|
|
119
|
+
items.forEach(item => known.set(item.value, item.text));
|
|
120
|
+
renderList();
|
|
121
|
+
open();
|
|
122
|
+
setActive(items.length ? 0 : -1);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
input.addEventListener("input", () => {
|
|
126
|
+
if (!multiple) {
|
|
127
|
+
selected = [];
|
|
128
|
+
notify();
|
|
129
|
+
}
|
|
130
|
+
search();
|
|
131
|
+
});
|
|
132
|
+
input.addEventListener("focus", () => { if (multiple && minChars === 0) search(); });
|
|
133
|
+
input.addEventListener("blur", () => {
|
|
134
|
+
close();
|
|
135
|
+
// A strict single combobox only keeps a text that was chosen from the list.
|
|
136
|
+
if (!multiple && strict && !selected.length) input.value = "";
|
|
137
|
+
});
|
|
138
|
+
input.addEventListener("keydown", event => {
|
|
139
|
+
if (event.key === "ArrowDown") {
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
if (!isOpen()) search();
|
|
142
|
+
else setActive(Math.min(activeIndex + 1, items.length - 1));
|
|
143
|
+
} else if (event.key === "ArrowUp") {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
setActive(Math.max(activeIndex - 1, 0));
|
|
146
|
+
} else if (event.key === "Enter" && isOpen() && items[activeIndex]) {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
choose(items[activeIndex]);
|
|
149
|
+
} else if (event.key === "Escape" && isOpen()) {
|
|
150
|
+
event.stopPropagation();
|
|
151
|
+
close();
|
|
152
|
+
} else if (event.key === "Backspace" && multiple && !input.value && selected.length) {
|
|
153
|
+
selected = selected.slice(0, -1);
|
|
154
|
+
renderChips();
|
|
155
|
+
notify();
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
function getValue() {
|
|
160
|
+
if (multiple) return [...selected];
|
|
161
|
+
return selected.length ? selected[0] : (strict ? "" : input.value);
|
|
162
|
+
}
|
|
163
|
+
function setValue(value) {
|
|
164
|
+
if (multiple) {
|
|
165
|
+
selected = [].concat(value ?? []);
|
|
166
|
+
(Array.isArray(source) ? source : []).forEach(option => known.set(asItem(option).value, asItem(option).text));
|
|
167
|
+
renderChips();
|
|
168
|
+
} else if (value == null || value === "") {
|
|
169
|
+
selected = [];
|
|
170
|
+
input.value = "";
|
|
171
|
+
} else {
|
|
172
|
+
const match = (Array.isArray(source) ? source : []).map(asItem).find(item => item.value === value);
|
|
173
|
+
selected = match ? [match.value] : [];
|
|
174
|
+
input.value = match?.text ?? String(value);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
setValue(options.value);
|
|
178
|
+
return { element, input, getValue, setValue };
|
|
179
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//csv 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
|
+
// Quotes a cell when needed. Cells starting with = + - @ are prefixed with ' so a spreadsheet
|
|
7
|
+
// does not run user supplied data as a formula.
|
|
8
|
+
function cell(value, separator) {
|
|
9
|
+
let text = value == null ? "" : String(value);
|
|
10
|
+
if (/^[=+\-@\t\r]/.test(text) && Number.isNaN(Number(text))) text = `'${text}`;
|
|
11
|
+
return text.includes(separator) || /["\r\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Converts rows to CSV text (RFC 4180 quoting, CRLF line ends).
|
|
16
|
+
*
|
|
17
|
+
* @param {{key: string, label?: string}[]} columns
|
|
18
|
+
* @param {object[]} rows
|
|
19
|
+
* @param {string} [separator=","]
|
|
20
|
+
*/
|
|
21
|
+
export function toCsv(columns, rows, separator = ",") {
|
|
22
|
+
const lines = [columns.map(col => cell(col.label ?? col.key, separator))];
|
|
23
|
+
for (const row of rows) lines.push(columns.map(col => cell(row[col.key], separator)));
|
|
24
|
+
return lines.map(line => line.join(separator)).join("\r\n");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Offers the text as a file download in the browser.
|
|
28
|
+
export function downloadFile(filename, text, type = "text/csv;charset=utf-8") {
|
|
29
|
+
const url = URL.createObjectURL(new Blob(["", text], { type }));
|
|
30
|
+
const link = document.createElement("a");
|
|
31
|
+
link.href = url;
|
|
32
|
+
link.download = filename;
|
|
33
|
+
document.body.append(link);
|
|
34
|
+
link.click();
|
|
35
|
+
link.remove();
|
|
36
|
+
URL.revokeObjectURL(url);
|
|
37
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//dateRange for Athena UI
|
|
2
|
+
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
import { cx, el, uid } from "./dom.js";
|
|
7
|
+
import { translate } from "./i18n.js";
|
|
8
|
+
import { validateDateRange } from "./validation.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal: two native date inputs (from / to). The "to" picker starts at the chosen start date.
|
|
12
|
+
* Values are ISO dates ("2026-01-31"), an empty date is "".
|
|
13
|
+
*/
|
|
14
|
+
export function createDateRange(options = {}) {
|
|
15
|
+
const id = options.id ?? uid("daterange");
|
|
16
|
+
const make = (suffix, labelKey) => {
|
|
17
|
+
const input = el("input", { id: `${id}-${suffix}`, attrs: { type: "date", name: options.name ? `${options.name}-${suffix}` : null, min: options.min, max: options.max }, prop: { disabled: Boolean(options.disabled) } });
|
|
18
|
+
const label = el("label", { class: "daterange-part" }, el("span", { text: translate(labelKey) }), input);
|
|
19
|
+
return { input, label };
|
|
20
|
+
};
|
|
21
|
+
const from = make("from", "dateFrom");
|
|
22
|
+
const to = make("to", "dateTo");
|
|
23
|
+
const element = el("div", { class: cx("daterange", options.class), attrs: { role: "group" } }, from.label, to.label);
|
|
24
|
+
|
|
25
|
+
const syncLimits = () => {
|
|
26
|
+
// The end cannot be picked before the start (when no minimum is set, the start is the minimum).
|
|
27
|
+
to.input.min = from.input.value || options.min || "";
|
|
28
|
+
from.input.max = to.input.value || options.max || "";
|
|
29
|
+
};
|
|
30
|
+
const getValue = () => ({ from: from.input.value, to: to.input.value });
|
|
31
|
+
const setValue = value => {
|
|
32
|
+
from.input.value = value?.from ?? "";
|
|
33
|
+
to.input.value = value?.to ?? "";
|
|
34
|
+
syncLimits();
|
|
35
|
+
};
|
|
36
|
+
for (const part of [from, to]) {
|
|
37
|
+
part.input.addEventListener("change", () => {
|
|
38
|
+
syncLimits();
|
|
39
|
+
options.onChange?.(getValue());
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
setValue({ from: options.from ?? options.value?.from, to: options.to ?? options.value?.to });
|
|
43
|
+
return {
|
|
44
|
+
element,
|
|
45
|
+
inputs: [from.input, to.input],
|
|
46
|
+
getValue,
|
|
47
|
+
setValue,
|
|
48
|
+
validate: () => validateDateRange(from.input.value, to.input.value, { min: options.min, max: options.max })
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//dom helpers 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
|
+
let counter = 0;
|
|
7
|
+
|
|
8
|
+
// Unique element id, for label / aria wiring.
|
|
9
|
+
export const uid = (prefix = "athena") => `${prefix}-${++counter}`;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates an element.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} tag
|
|
15
|
+
* @param {object} [props]
|
|
16
|
+
* @param {string} [props.class]
|
|
17
|
+
* @param {string} [props.id]
|
|
18
|
+
* @param {string} [props.text] set as textContent (never parsed as HTML)
|
|
19
|
+
* @param {Object<string, string|boolean|number>} [props.attrs] attributes, false / null / undefined are skipped
|
|
20
|
+
* @param {Object<string, Function>} [props.on] event listeners
|
|
21
|
+
* @param {Object<string, *>} [props.prop] DOM properties (value, checked, disabled, ...)
|
|
22
|
+
* @param {...(Node|string)} children
|
|
23
|
+
*/
|
|
24
|
+
export function el(tag, props = {}, ...children) {
|
|
25
|
+
const node = document.createElement(tag);
|
|
26
|
+
if (props.class) node.className = props.class;
|
|
27
|
+
if (props.id) node.id = props.id;
|
|
28
|
+
if (props.text != null) node.textContent = props.text;
|
|
29
|
+
for (const [name, value] of Object.entries(props.attrs ?? {})) {
|
|
30
|
+
if (value !== false && value != null) node.setAttribute(name, value === true ? "" : value);
|
|
31
|
+
}
|
|
32
|
+
for (const [name, handler] of Object.entries(props.on ?? {})) node.addEventListener(name, handler);
|
|
33
|
+
Object.assign(node, props.prop);
|
|
34
|
+
node.append(...children);
|
|
35
|
+
return node;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Fills a node with content. Strings are inserted as text, never as HTML.
|
|
40
|
+
* Accepts a string / number, a Node, an array of those, or a function returning one.
|
|
41
|
+
*/
|
|
42
|
+
export function setContent(node, content) {
|
|
43
|
+
if (typeof content === "function") content = content();
|
|
44
|
+
if (content == null || content === false) return node;
|
|
45
|
+
if (Array.isArray(content)) content.forEach(item => setContent(node, item));
|
|
46
|
+
else node.append(content instanceof Node ? content : document.createTextNode(String(content)));
|
|
47
|
+
return node;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Appends a node to a parent given as element or id. Falls back to the body.
|
|
51
|
+
export function mount(parent, node) {
|
|
52
|
+
const host = typeof parent === "string" ? document.getElementById(parent) : parent;
|
|
53
|
+
(host ?? document.body).append(node);
|
|
54
|
+
return node;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const cx = (...classes) => classes.filter(Boolean).join(" ");
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//focus helpers 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
|
+
const FOCUSABLE = [
|
|
7
|
+
"a[href]", "button:not([disabled])", "input:not([disabled]):not([type=hidden])",
|
|
8
|
+
"select:not([disabled])", "textarea:not([disabled])", "[tabindex]:not([tabindex='-1'])"
|
|
9
|
+
].join(",");
|
|
10
|
+
|
|
11
|
+
export function focusableIn(container) {
|
|
12
|
+
return [...container.querySelectorAll(FOCUSABLE)].filter(node => node.getClientRects().length > 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Keeps Tab inside the container, calls onEscape on Esc and moves the focus into the container.
|
|
17
|
+
* Returns a release function that restores the previous focus.
|
|
18
|
+
*/
|
|
19
|
+
export function trapFocus(container, { onEscape } = {}) {
|
|
20
|
+
const previous = document.activeElement;
|
|
21
|
+
const onKeydown = event => {
|
|
22
|
+
if (event.key === "Escape" && onEscape) {
|
|
23
|
+
event.stopPropagation();
|
|
24
|
+
onEscape();
|
|
25
|
+
} else if (event.key === "Tab") {
|
|
26
|
+
const nodes = focusableIn(container);
|
|
27
|
+
if (!nodes.length) {
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const first = nodes[0];
|
|
32
|
+
const last = nodes[nodes.length - 1];
|
|
33
|
+
if (event.shiftKey && (document.activeElement === first || !container.contains(document.activeElement))) {
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
last.focus();
|
|
36
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
first.focus();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
container.addEventListener("keydown", onKeydown);
|
|
43
|
+
if (!container.hasAttribute("tabindex")) container.setAttribute("tabindex", "-1");
|
|
44
|
+
(focusableIn(container)[0] ?? container).focus();
|
|
45
|
+
return () => {
|
|
46
|
+
container.removeEventListener("keydown", onKeydown);
|
|
47
|
+
if (previous?.isConnected) previous.focus();
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//format helpers 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
|
+
// 1536 -> "1.5 KB"
|
|
7
|
+
export function formatBytes(bytes) {
|
|
8
|
+
if (!Number.isFinite(bytes) || bytes < 0) return "";
|
|
9
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
10
|
+
let value = bytes;
|
|
11
|
+
let unit = 0;
|
|
12
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
13
|
+
value /= 1024;
|
|
14
|
+
unit++;
|
|
15
|
+
}
|
|
16
|
+
return `${unit === 0 ? value : +value.toFixed(1)} ${units[unit]}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Checks a file against an `accept` list like ".png,image/*,application/pdf". An empty list accepts everything.
|
|
21
|
+
*/
|
|
22
|
+
export function matchesAccept(file, accept) {
|
|
23
|
+
const rules = String(accept ?? "").split(",").map(rule => rule.trim().toLowerCase()).filter(Boolean);
|
|
24
|
+
if (!rules.length) return true;
|
|
25
|
+
const name = file.name.toLowerCase();
|
|
26
|
+
const type = (file.type ?? "").toLowerCase();
|
|
27
|
+
return rules.some(rule =>
|
|
28
|
+
rule.startsWith(".") ? name.endsWith(rule)
|
|
29
|
+
: rule.endsWith("/*") ? type.startsWith(rule.slice(0, -1))
|
|
30
|
+
: type === rule);
|
|
31
|
+
}
|