arethesis 0.2.0 → 0.2.1
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/CHANGELOG.md +34 -0
- package/README.md +3 -2
- package/dist/arethesis.js +2 -222
- package/dist/binding/data-binder.d.ts +8 -0
- package/dist/binding/data-binder.d.ts.map +1 -1
- package/dist/binding/data-binder.js +605 -0
- package/dist/binding/data-binder.js.map +1 -0
- package/dist/binding/types.d.ts +20 -0
- package/dist/binding/types.d.ts.map +1 -1
- package/dist/binding/validation-engine.js +160 -0
- package/dist/binding/validation-engine.js.map +1 -0
- package/dist/cdn/arethesis.min.js +2 -0
- package/dist/cdn/arethesis.min.js.map +1 -0
- package/dist/cdn/chunks/CdTL-MEv.min.js +3 -0
- package/dist/cdn/chunks/CdTL-MEv.min.js.map +1 -0
- package/dist/cdn/components.min.js +81 -0
- package/dist/cdn/components.min.js.map +1 -0
- package/dist/collab/index.d.ts +22 -0
- package/dist/collab/index.d.ts.map +1 -0
- package/dist/collab/index.js +23 -0
- package/dist/collab/index.js.map +1 -0
- package/dist/components/component.d.ts +17 -0
- package/dist/components/component.d.ts.map +1 -1
- package/dist/components/component.js +765 -0
- package/dist/components/component.js.map +1 -0
- package/dist/components/decorators.js +27 -0
- package/dist/components/decorators.js.map +1 -0
- package/dist/components/form-associated.d.ts +55 -0
- package/dist/components/form-associated.d.ts.map +1 -0
- package/dist/components/form-associated.js +180 -0
- package/dist/components/form-associated.js.map +1 -0
- package/dist/components/runtime.d.ts +2 -0
- package/dist/components/runtime.d.ts.map +1 -1
- package/dist/components/runtime.js +18 -0
- package/dist/components/runtime.js.map +1 -0
- package/dist/components/template-loader.js +90 -0
- package/dist/components/template-loader.js.map +1 -0
- package/dist/components.js +2 -2810
- package/dist/core/arethesis.d.ts +13 -9
- package/dist/core/arethesis.d.ts.map +1 -1
- package/dist/core/arethesis.js +249 -0
- package/dist/core/arethesis.js.map +1 -0
- package/dist/core/hooks.d.ts +19 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +23 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/index.d.ts +57 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +25 -0
- package/dist/data/list-view-registry.js +300 -0
- package/dist/data/list-view-registry.js.map +1 -0
- package/dist/data/object-store.js +280 -0
- package/dist/data/object-store.js.map +1 -0
- package/dist/data/optimistic-updates.js +168 -0
- package/dist/data/optimistic-updates.js.map +1 -0
- package/dist/data/persistence-manager.js +233 -0
- package/dist/data/persistence-manager.js.map +1 -0
- package/dist/defaults.d.ts +1 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +17 -0
- package/dist/defaults.js.map +1 -0
- package/dist/http/auth-manager.js +260 -0
- package/dist/http/auth-manager.js.map +1 -0
- package/dist/http/http-client.d.ts +7 -0
- package/dist/http/http-client.d.ts.map +1 -1
- package/dist/http/http-client.js +501 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.js +53 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/mapping.js +64 -0
- package/dist/http/mapping.js.map +1 -0
- package/dist/index.d.ts +3 -58
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -0
- package/dist/kit/calendar.d.ts +43 -0
- package/dist/kit/calendar.d.ts.map +1 -0
- package/dist/kit/calendar.js +423 -0
- package/dist/kit/calendar.js.map +1 -0
- package/dist/kit/cell-editor.js +191 -0
- package/dist/kit/cell-editor.js.map +1 -0
- package/dist/kit/chart.d.ts +2 -0
- package/dist/kit/chart.d.ts.map +1 -1
- package/dist/kit/chart.js +541 -0
- package/dist/kit/chart.js.map +1 -0
- package/dist/kit/combobox.d.ts +57 -0
- package/dist/kit/combobox.d.ts.map +1 -0
- package/dist/kit/combobox.js +379 -0
- package/dist/kit/combobox.js.map +1 -0
- package/dist/kit/command-palette.d.ts +2 -0
- package/dist/kit/command-palette.d.ts.map +1 -1
- package/dist/kit/command-palette.js +315 -0
- package/dist/kit/command-palette.js.map +1 -0
- package/dist/kit/data-table.d.ts +11 -0
- package/dist/kit/data-table.d.ts.map +1 -1
- package/dist/kit/data-table.js +373 -0
- package/dist/kit/data-table.js.map +1 -0
- package/dist/kit/dialog.d.ts +31 -0
- package/dist/kit/dialog.d.ts.map +1 -0
- package/dist/kit/dialog.js +157 -0
- package/dist/kit/dialog.js.map +1 -0
- package/dist/kit/elements/calendar.d.ts +3 -0
- package/dist/kit/elements/calendar.d.ts.map +1 -0
- package/dist/kit/elements/calendar.js +11 -0
- package/dist/kit/elements/calendar.js.map +1 -0
- package/dist/kit/elements/chart.d.ts +3 -0
- package/dist/kit/elements/chart.d.ts.map +1 -0
- package/dist/kit/elements/chart.js +11 -0
- package/dist/kit/elements/chart.js.map +1 -0
- package/dist/kit/elements/combobox.d.ts +3 -0
- package/dist/kit/elements/combobox.d.ts.map +1 -0
- package/dist/kit/elements/combobox.js +11 -0
- package/dist/kit/elements/combobox.js.map +1 -0
- package/dist/kit/elements/command-palette.d.ts +3 -0
- package/dist/kit/elements/command-palette.d.ts.map +1 -0
- package/dist/kit/elements/command-palette.js +11 -0
- package/dist/kit/elements/command-palette.js.map +1 -0
- package/dist/kit/elements/data-table.d.ts +3 -0
- package/dist/kit/elements/data-table.d.ts.map +1 -0
- package/dist/kit/elements/data-table.js +11 -0
- package/dist/kit/elements/data-table.js.map +1 -0
- package/dist/kit/elements/dialog.d.ts +4 -0
- package/dist/kit/elements/dialog.d.ts.map +1 -0
- package/dist/kit/elements/dialog.js +11 -0
- package/dist/kit/elements/dialog.js.map +1 -0
- package/dist/kit/elements/flex-view.d.ts +3 -0
- package/dist/kit/elements/flex-view.d.ts.map +1 -0
- package/dist/kit/elements/flex-view.js +11 -0
- package/dist/kit/elements/flex-view.js.map +1 -0
- package/dist/kit/elements/form.d.ts +3 -0
- package/dist/kit/elements/form.d.ts.map +1 -0
- package/dist/kit/elements/form.js +11 -0
- package/dist/kit/elements/form.js.map +1 -0
- package/dist/kit/elements/kanban.d.ts +3 -0
- package/dist/kit/elements/kanban.d.ts.map +1 -0
- package/dist/kit/elements/kanban.js +11 -0
- package/dist/kit/elements/kanban.js.map +1 -0
- package/dist/kit/elements/list.d.ts +3 -0
- package/dist/kit/elements/list.d.ts.map +1 -0
- package/dist/kit/elements/list.js +11 -0
- package/dist/kit/elements/list.js.map +1 -0
- package/dist/kit/elements/markdown-editor.d.ts +3 -0
- package/dist/kit/elements/markdown-editor.d.ts.map +1 -0
- package/dist/kit/elements/markdown-editor.js +11 -0
- package/dist/kit/elements/markdown-editor.js.map +1 -0
- package/dist/kit/elements/presence.d.ts +3 -0
- package/dist/kit/elements/presence.d.ts.map +1 -0
- package/dist/kit/elements/presence.js +11 -0
- package/dist/kit/elements/presence.js.map +1 -0
- package/dist/kit/elements/sheet.d.ts +3 -0
- package/dist/kit/elements/sheet.d.ts.map +1 -0
- package/dist/kit/elements/sheet.js +11 -0
- package/dist/kit/elements/sheet.js.map +1 -0
- package/dist/kit/elements/tabs.d.ts +3 -0
- package/dist/kit/elements/tabs.d.ts.map +1 -0
- package/dist/kit/elements/tabs.js +11 -0
- package/dist/kit/elements/tabs.js.map +1 -0
- package/dist/kit/elements/toast.d.ts +4 -0
- package/dist/kit/elements/toast.d.ts.map +1 -0
- package/dist/kit/elements/toast.js +11 -0
- package/dist/kit/elements/toast.js.map +1 -0
- package/dist/kit/elements/tree.d.ts +3 -0
- package/dist/kit/elements/tree.d.ts.map +1 -0
- package/dist/kit/elements/tree.js +11 -0
- package/dist/kit/elements/tree.js.map +1 -0
- package/dist/kit/flex-view.d.ts +2 -0
- package/dist/kit/flex-view.d.ts.map +1 -1
- package/dist/kit/flex-view.js +346 -0
- package/dist/kit/flex-view.js.map +1 -0
- package/dist/kit/form.d.ts +2 -0
- package/dist/kit/form.d.ts.map +1 -1
- package/dist/kit/form.js +211 -0
- package/dist/kit/form.js.map +1 -0
- package/dist/kit/index.d.ts +15 -15
- package/dist/kit/index.d.ts.map +1 -1
- package/dist/kit/index.js +40 -0
- package/dist/kit/internal/modal.d.ts +26 -0
- package/dist/kit/internal/modal.d.ts.map +1 -0
- package/dist/kit/internal/modal.js +59 -0
- package/dist/kit/internal/modal.js.map +1 -0
- package/dist/kit/kanban.d.ts +73 -0
- package/dist/kit/kanban.d.ts.map +1 -0
- package/dist/kit/kanban.js +404 -0
- package/dist/kit/kanban.js.map +1 -0
- package/dist/kit/list.d.ts +4 -0
- package/dist/kit/list.d.ts.map +1 -1
- package/dist/kit/list.js +106 -0
- package/dist/kit/list.js.map +1 -0
- package/dist/kit/markdown/highlight.js +106 -0
- package/dist/kit/markdown/highlight.js.map +1 -0
- package/dist/kit/markdown/render.js +260 -0
- package/dist/kit/markdown/render.js.map +1 -0
- package/dist/kit/markdown-editor.d.ts +2 -0
- package/dist/kit/markdown-editor.d.ts.map +1 -1
- package/dist/kit/markdown-editor.js +797 -0
- package/dist/kit/markdown-editor.js.map +1 -0
- package/dist/kit/presence.d.ts +36 -0
- package/dist/kit/presence.d.ts.map +1 -0
- package/dist/kit/presence.js +262 -0
- package/dist/kit/presence.js.map +1 -0
- package/dist/kit/sheet/formula.js +364 -0
- package/dist/kit/sheet/formula.js.map +1 -0
- package/dist/kit/sheet/model.js +136 -0
- package/dist/kit/sheet/model.js.map +1 -0
- package/dist/kit/sheet/sheet.d.ts +2 -0
- package/dist/kit/sheet/sheet.d.ts.map +1 -1
- package/dist/kit/sheet/sheet.js +676 -0
- package/dist/kit/sheet/sheet.js.map +1 -0
- package/dist/kit/tabs.d.ts +17 -0
- package/dist/kit/tabs.d.ts.map +1 -0
- package/dist/kit/tabs.js +192 -0
- package/dist/kit/tabs.js.map +1 -0
- package/dist/kit/theme-boot.generated.js +8 -0
- package/dist/kit/theme-boot.generated.js.map +1 -0
- package/dist/kit/theme.js +89 -0
- package/dist/kit/theme.js.map +1 -0
- package/dist/kit/toast.d.ts +46 -0
- package/dist/kit/toast.d.ts.map +1 -0
- package/dist/kit/toast.js +154 -0
- package/dist/kit/toast.js.map +1 -0
- package/dist/kit/tree.d.ts +41 -0
- package/dist/kit/tree.d.ts.map +1 -0
- package/dist/kit/tree.js +422 -0
- package/dist/kit/tree.js.map +1 -0
- package/dist/mixins/context-menu.d.ts.map +1 -1
- package/dist/mixins/context-menu.js +183 -0
- package/dist/mixins/context-menu.js.map +1 -0
- package/dist/mixins/draggable.d.ts.map +1 -1
- package/dist/mixins/draggable.js +140 -0
- package/dist/mixins/draggable.js.map +1 -0
- package/dist/mixins/dropdown.d.ts.map +1 -1
- package/dist/mixins/dropdown.js +216 -0
- package/dist/mixins/dropdown.js.map +1 -0
- package/dist/mixins/editable.d.ts.map +1 -1
- package/dist/mixins/editable.js +175 -0
- package/dist/mixins/editable.js.map +1 -0
- package/dist/mixins/grid-navigation.d.ts.map +1 -1
- package/dist/mixins/grid-navigation.js +194 -0
- package/dist/mixins/grid-navigation.js.map +1 -0
- package/dist/mixins/index.d.ts +6 -2
- package/dist/mixins/index.d.ts.map +1 -1
- package/dist/mixins/index.js +38 -0
- package/dist/mixins/index.js.map +1 -0
- package/dist/mixins/infinite-scroll.d.ts.map +1 -1
- package/dist/mixins/infinite-scroll.js +83 -0
- package/dist/mixins/infinite-scroll.js.map +1 -0
- package/dist/mixins/listbox.d.ts.map +1 -1
- package/dist/mixins/listbox.js +208 -0
- package/dist/mixins/listbox.js.map +1 -0
- package/dist/mixins/popup.d.ts +36 -0
- package/dist/mixins/popup.d.ts.map +1 -0
- package/dist/mixins/popup.js +113 -0
- package/dist/mixins/popup.js.map +1 -0
- package/dist/mixins/registry.d.ts +6 -0
- package/dist/mixins/registry.d.ts.map +1 -1
- package/dist/mixins/registry.js +167 -0
- package/dist/mixins/registry.js.map +1 -0
- package/dist/mixins/resizable.d.ts.map +1 -1
- package/dist/mixins/resizable.js +128 -0
- package/dist/mixins/resizable.js.map +1 -0
- package/dist/mixins/sortable.d.ts.map +1 -1
- package/dist/mixins/sortable.js +511 -0
- package/dist/mixins/sortable.js.map +1 -0
- package/dist/mixins/tooltip.d.ts.map +1 -1
- package/dist/mixins/tooltip.js +80 -0
- package/dist/mixins/tooltip.js.map +1 -0
- package/dist/mixins/utils.js +105 -0
- package/dist/mixins/utils.js.map +1 -0
- package/dist/plugins/async-validators.js +118 -0
- package/dist/plugins/async-validators.js.map +1 -0
- package/dist/plugins/builtins.d.ts +1 -1
- package/dist/plugins/builtins.d.ts.map +1 -1
- package/dist/plugins/builtins.js +20 -0
- package/dist/plugins/builtins.js.map +1 -0
- package/dist/plugins/date-utils.js +128 -0
- package/dist/plugins/date-utils.js.map +1 -0
- package/dist/plugins/plugin-manager.d.ts.map +1 -1
- package/dist/plugins/plugin-manager.js +99 -0
- package/dist/plugins/plugin-manager.js.map +1 -0
- package/dist/plugins/rich-text.js +365 -0
- package/dist/plugins/rich-text.js.map +1 -0
- package/dist/realtime/azure-pubsub-manager.js +273 -0
- package/dist/realtime/azure-pubsub-manager.js.map +1 -0
- package/dist/realtime/collab/anchors.js +19 -0
- package/dist/realtime/collab/anchors.js.map +1 -0
- package/dist/realtime/collab/collab-session.js +396 -0
- package/dist/realtime/collab/collab-session.js.map +1 -0
- package/dist/realtime/collab/cursor-overlay.js +187 -0
- package/dist/realtime/collab/cursor-overlay.js.map +1 -0
- package/dist/realtime/collab/textarea-adapter.js +125 -0
- package/dist/realtime/collab/textarea-adapter.js.map +1 -0
- package/dist/realtime/collab/undo-manager.js +148 -0
- package/dist/realtime/collab/undo-manager.js.map +1 -0
- package/dist/realtime/index.d.ts +11 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +22 -0
- package/dist/realtime/index.js.map +1 -0
- package/dist/realtime/ot/client.js +74 -0
- package/dist/realtime/ot/client.js.map +1 -0
- package/dist/realtime/ot/server.js +292 -0
- package/dist/realtime/ot/server.js.map +1 -0
- package/dist/realtime/ot/text-operation.js +282 -0
- package/dist/realtime/ot/text-operation.js.map +1 -0
- package/dist/realtime/reconnect.js +58 -0
- package/dist/realtime/reconnect.js.map +1 -0
- package/dist/realtime/sse-manager.js +172 -0
- package/dist/realtime/sse-manager.js.map +1 -0
- package/dist/realtime/websocket-manager.js +180 -0
- package/dist/realtime/websocket-manager.js.map +1 -0
- package/dist/theme.css +178 -0
- package/dist/utils/backoff.js +30 -0
- package/dist/utils/backoff.js.map +1 -0
- package/dist/utils/dates.js +67 -0
- package/dist/utils/dates.js.map +1 -0
- package/dist/utils/dom.js +30 -0
- package/dist/utils/dom.js.map +1 -0
- package/dist/utils/emitter.js +40 -0
- package/dist/utils/emitter.js.map +1 -0
- package/dist/utils/fuzzy.d.ts +2 -0
- package/dist/utils/fuzzy.d.ts.map +1 -1
- package/dist/utils/fuzzy.js +95 -0
- package/dist/utils/fuzzy.js.map +1 -0
- package/dist/utils/path.js +99 -0
- package/dist/utils/path.js.map +1 -0
- package/dist/utils/sanitize.js +161 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/dist/utils/url.js +44 -0
- package/dist/utils/url.js.map +1 -0
- package/dist/utils/virtual-window.d.ts +25 -0
- package/dist/utils/virtual-window.d.ts.map +1 -0
- package/dist/utils/virtual-window.js +19 -0
- package/dist/utils/virtual-window.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/package.json +61 -11
- package/dist/arethesis.js.map +0 -1
- package/dist/components.js.map +0 -1
- package/dist/fuzzy-Dnlbxdby.js +0 -5752
- package/dist/fuzzy-Dnlbxdby.js.map +0 -1
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Component } from "../components/component.js";
|
|
2
|
+
import { defineComponent } from "../components/decorators.js";
|
|
3
|
+
import { supportsPopover } from "../mixins/popup.js";
|
|
4
|
+
import { announce } from "../mixins/utils.js";
|
|
5
|
+
// src/kit/toast.ts
|
|
6
|
+
/**
|
|
7
|
+
* `<a-toast>`: short notifications in a corner of the screen.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { toast, connectToasts } from 'arethesis/components/toast';
|
|
11
|
+
*
|
|
12
|
+
* toast('Saved');
|
|
13
|
+
* toast('Couldn’t save', { kind: 'error', action: { label: 'Retry', run: save } });
|
|
14
|
+
* connectToasts(getArethesis()); // failed requests and rolled-back edits show up by themselves
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Messages are read by screen readers: info and success politely
|
|
18
|
+
* (`role="status"`), errors at once (`role="alert"`). Both live regions are in
|
|
19
|
+
* the page from the start, so the first message is announced too. A toast
|
|
20
|
+
* stays while the pointer is over it or it has focus, and one with an action
|
|
21
|
+
* stays until it's dismissed, so there's time to use it.
|
|
22
|
+
*
|
|
23
|
+
* `toast()` uses the first `<a-toast>` on the page, adding one if there's none.
|
|
24
|
+
* Place your own to choose its position: `<a-toast position="top-end">`.
|
|
25
|
+
*
|
|
26
|
+
* @module components/toast
|
|
27
|
+
*/
|
|
28
|
+
var Toaster = class extends Component {
|
|
29
|
+
#status = null;
|
|
30
|
+
#alert = null;
|
|
31
|
+
onConnected() {
|
|
32
|
+
if (this.#status) return;
|
|
33
|
+
this.setAttribute("aria-label", this.getAttribute("aria-label") ?? "Notifications");
|
|
34
|
+
const region = (role) => {
|
|
35
|
+
const el = document.createElement("div");
|
|
36
|
+
el.className = "ar-toast__region";
|
|
37
|
+
el.setAttribute("role", role);
|
|
38
|
+
el.setAttribute("aria-live", role === "alert" ? "assertive" : "polite");
|
|
39
|
+
return el;
|
|
40
|
+
};
|
|
41
|
+
this.#status = region("status");
|
|
42
|
+
this.#alert = region("alert");
|
|
43
|
+
this.replaceChildren(this.#status, this.#alert);
|
|
44
|
+
if (supportsPopover()) {
|
|
45
|
+
this.popover = "manual";
|
|
46
|
+
try {
|
|
47
|
+
this.showPopover();
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Show a message. */
|
|
52
|
+
show(message, options = {}) {
|
|
53
|
+
if (!this.#status) this.onConnected();
|
|
54
|
+
const kind = options.kind ?? "info";
|
|
55
|
+
const el = document.createElement("div");
|
|
56
|
+
el.className = "ar-toast";
|
|
57
|
+
el.dataset.kind = kind;
|
|
58
|
+
const text = document.createElement("span");
|
|
59
|
+
text.className = "ar-toast__message";
|
|
60
|
+
text.textContent = message;
|
|
61
|
+
el.append(text);
|
|
62
|
+
let timer = null;
|
|
63
|
+
const dismiss = () => {
|
|
64
|
+
if (timer) clearTimeout(timer);
|
|
65
|
+
if (el.contains(document.activeElement)) document.activeElement.blur();
|
|
66
|
+
el.remove();
|
|
67
|
+
};
|
|
68
|
+
if (options.action) {
|
|
69
|
+
const button = document.createElement("button");
|
|
70
|
+
button.type = "button";
|
|
71
|
+
button.className = "ar-toast__action";
|
|
72
|
+
button.textContent = options.action.label;
|
|
73
|
+
button.addEventListener("click", () => {
|
|
74
|
+
dismiss();
|
|
75
|
+
options.action.run();
|
|
76
|
+
});
|
|
77
|
+
el.append(button);
|
|
78
|
+
}
|
|
79
|
+
const close = document.createElement("button");
|
|
80
|
+
close.type = "button";
|
|
81
|
+
close.className = "ar-toast__close";
|
|
82
|
+
close.setAttribute("aria-label", "Dismiss");
|
|
83
|
+
close.textContent = "×";
|
|
84
|
+
close.addEventListener("click", dismiss);
|
|
85
|
+
el.append(close);
|
|
86
|
+
const timeout = options.action ? 0 : options.timeout ?? (kind === "error" ? 8e3 : 5e3);
|
|
87
|
+
const start = () => {
|
|
88
|
+
if (timeout > 0) timer = setTimeout(dismiss, timeout);
|
|
89
|
+
};
|
|
90
|
+
const pause = () => {
|
|
91
|
+
if (timer) clearTimeout(timer);
|
|
92
|
+
timer = null;
|
|
93
|
+
};
|
|
94
|
+
el.addEventListener("pointerenter", pause);
|
|
95
|
+
el.addEventListener("pointerleave", () => {
|
|
96
|
+
if (!el.contains(document.activeElement)) start();
|
|
97
|
+
});
|
|
98
|
+
el.addEventListener("focusin", pause);
|
|
99
|
+
el.addEventListener("focusout", (e) => {
|
|
100
|
+
if (!el.contains(e.relatedTarget)) start();
|
|
101
|
+
});
|
|
102
|
+
(kind === "error" ? this.#alert : this.#status).append(el);
|
|
103
|
+
if (document.querySelector("dialog[open]:modal")) announce(message);
|
|
104
|
+
start();
|
|
105
|
+
return {
|
|
106
|
+
element: el,
|
|
107
|
+
dismiss
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** Remove every toast. */
|
|
111
|
+
clear() {
|
|
112
|
+
this.#status?.replaceChildren();
|
|
113
|
+
this.#alert?.replaceChildren();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/** Show a toast in the page's `<a-toast>` (added if there's none). */
|
|
117
|
+
function toast(message, options = {}) {
|
|
118
|
+
defineToaster();
|
|
119
|
+
let host = document.querySelector("a-toast");
|
|
120
|
+
if (!host) {
|
|
121
|
+
host = document.createElement("a-toast");
|
|
122
|
+
document.body.append(host);
|
|
123
|
+
}
|
|
124
|
+
return host.show(message, typeof options === "string" ? { kind: options } : options);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Show toasts for rolled-back changes and failed requests. Returns a function
|
|
128
|
+
* that stops. Pass `false` for either to leave it out.
|
|
129
|
+
*/
|
|
130
|
+
function connectToasts(app, options = {}) {
|
|
131
|
+
const offs = [];
|
|
132
|
+
if (options.rollback !== false) {
|
|
133
|
+
const message = options.rollback ?? ((e) => `Couldn’t ${e.kind === "create" ? "add" : e.kind === "delete" ? "delete" : "save"} that. It’s been undone.`);
|
|
134
|
+
offs.push(app.optimistic.on("rollback", (e) => {
|
|
135
|
+
const text = message(e);
|
|
136
|
+
if (text) toast(text, { kind: "error" });
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
if (options.httpError) {
|
|
140
|
+
const message = options.httpError;
|
|
141
|
+
offs.push(app.http.onError((error) => {
|
|
142
|
+
const text = message(error);
|
|
143
|
+
if (text) toast(text, { kind: "error" });
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
return () => offs.forEach((off) => off());
|
|
147
|
+
}
|
|
148
|
+
/** Define `<a-toast>` (idempotent). `import 'arethesis/components/toast'` calls this. */
|
|
149
|
+
function defineToaster() {
|
|
150
|
+
defineComponent({ tag: "a-toast" })(Toaster);
|
|
151
|
+
}
|
|
152
|
+
export { Toaster, connectToasts, defineToaster, toast };
|
|
153
|
+
|
|
154
|
+
//# sourceMappingURL=toast.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toast.js","names":[],"sources":["../../src/kit/toast.ts"],"sourcesContent":["/**\n * `<a-toast>`: short notifications in a corner of the screen.\n *\n * ```ts\n * import { toast, connectToasts } from 'arethesis/components/toast';\n *\n * toast('Saved');\n * toast('Couldn’t save', { kind: 'error', action: { label: 'Retry', run: save } });\n * connectToasts(getArethesis()); // failed requests and rolled-back edits show up by themselves\n * ```\n *\n * Messages are read by screen readers: info and success politely\n * (`role=\"status\"`), errors at once (`role=\"alert\"`). Both live regions are in\n * the page from the start, so the first message is announced too. A toast\n * stays while the pointer is over it or it has focus, and one with an action\n * stays until it's dismissed, so there's time to use it.\n *\n * `toast()` uses the first `<a-toast>` on the page, adding one if there's none.\n * Place your own to choose its position: `<a-toast position=\"top-end\">`.\n *\n * @module components/toast\n */\n\nimport { Component } from '../components/component';\nimport { defineComponent } from '../components/decorators';\nimport type { HttpError } from '../http/http-error';\nimport type { HttpClient } from '../http/http-client';\nimport type { OptimisticEvent, OptimisticUpdateManager } from '../data/optimistic-updates';\nimport { supportsPopover } from '../mixins/popup';\nimport { announce } from '../mixins/utils';\n\nexport type ToastKind = 'info' | 'success' | 'error';\n\nexport interface ToastOptions {\n kind?: ToastKind;\n /** A button on the toast. A toast with an action stays until it's dismissed. */\n action?: { label: string; run: () => void };\n /** How long it stays, in ms. `0` keeps it until dismissed. @default 5000 (8000 for errors) */\n timeout?: number;\n}\n\nexport interface ToastHandle {\n element: HTMLElement;\n dismiss(): void;\n}\n\nexport class Toaster extends Component {\n #status: HTMLElement | null = null;\n #alert: HTMLElement | null = null;\n\n protected override onConnected(): void {\n if (this.#status) return;\n this.setAttribute('aria-label', this.getAttribute('aria-label') ?? 'Notifications');\n const region = (role: 'status' | 'alert'): HTMLElement => {\n const el = document.createElement('div');\n el.className = 'ar-toast__region';\n el.setAttribute('role', role);\n el.setAttribute('aria-live', role === 'alert' ? 'assertive' : 'polite');\n return el;\n };\n this.#status = region('status');\n this.#alert = region('alert');\n this.replaceChildren(this.#status, this.#alert);\n // In the top layer, above sticky headers and non-modal popups.\n if (supportsPopover()) {\n this.popover = 'manual';\n try {\n this.showPopover();\n } catch {\n /* not supported here */\n }\n }\n }\n\n /** Show a message. */\n show(message: string, options: ToastOptions = {}): ToastHandle {\n if (!this.#status) this.onConnected();\n const kind = options.kind ?? 'info';\n const el = document.createElement('div');\n el.className = 'ar-toast';\n el.dataset.kind = kind;\n const text = document.createElement('span');\n text.className = 'ar-toast__message';\n text.textContent = message;\n el.append(text);\n\n let timer: ReturnType<typeof setTimeout> | null = null;\n const dismiss = (): void => {\n if (timer) clearTimeout(timer);\n // Keep focus on the page if it was on the toast.\n if (el.contains(document.activeElement)) (document.activeElement as HTMLElement).blur();\n el.remove();\n };\n if (options.action) {\n const button = document.createElement('button');\n button.type = 'button';\n button.className = 'ar-toast__action';\n button.textContent = options.action.label;\n button.addEventListener('click', () => {\n dismiss();\n options.action!.run();\n });\n el.append(button);\n }\n const close = document.createElement('button');\n close.type = 'button';\n close.className = 'ar-toast__close';\n close.setAttribute('aria-label', 'Dismiss');\n close.textContent = '×';\n close.addEventListener('click', dismiss);\n el.append(close);\n\n const timeout = options.action ? 0 : options.timeout ?? (kind === 'error' ? 8000 : 5000);\n const start = (): void => {\n if (timeout > 0) timer = setTimeout(dismiss, timeout);\n };\n const pause = (): void => {\n if (timer) clearTimeout(timer);\n timer = null;\n };\n el.addEventListener('pointerenter', pause);\n el.addEventListener('pointerleave', () => {\n if (!el.contains(document.activeElement)) start();\n });\n el.addEventListener('focusin', pause);\n el.addEventListener('focusout', e => {\n if (!el.contains(e.relatedTarget as Node)) start();\n });\n\n (kind === 'error' ? this.#alert! : this.#status!).append(el);\n // A modal dialog makes the rest of the page inert, and it may not be announced.\n if (document.querySelector('dialog[open]:modal')) announce(message);\n start();\n return { element: el, dismiss };\n }\n\n /** Remove every toast. */\n clear(): void {\n this.#status?.replaceChildren();\n this.#alert?.replaceChildren();\n }\n}\n\n/** Show a toast in the page's `<a-toast>` (added if there's none). */\nexport function toast(message: string, options: ToastOptions | ToastKind = {}): ToastHandle {\n defineToaster();\n let host = document.querySelector<Toaster>('a-toast');\n if (!host) {\n host = document.createElement('a-toast') as Toaster;\n document.body.append(host);\n }\n return host.show(message, typeof options === 'string' ? { kind: options } : options);\n}\n\nexport interface ConnectToastsOptions {\n /** Message for a rolled-back optimistic change, or `false` for none. */\n rollback?: ((event: OptimisticEvent) => string | false) | false;\n /** Message for a failed request, or `false` for none. Requests that were cancelled are skipped. */\n httpError?: ((error: HttpError) => string | false) | false;\n}\n\n/**\n * Show toasts for rolled-back changes and failed requests. Returns a function\n * that stops. Pass `false` for either to leave it out.\n */\nexport function connectToasts(app: { optimistic: OptimisticUpdateManager; http: HttpClient }, options: ConnectToastsOptions = {}): () => void {\n const offs: Array<() => void> = [];\n if (options.rollback !== false) {\n const message = options.rollback ?? ((e: OptimisticEvent) => `Couldn’t ${e.kind === 'create' ? 'add' : e.kind === 'delete' ? 'delete' : 'save'} that. It’s been undone.`);\n offs.push(app.optimistic.on('rollback', e => {\n const text = message(e);\n if (text) toast(text, { kind: 'error' });\n }));\n }\n if (options.httpError) {\n const message = options.httpError;\n offs.push(app.http.onError(error => {\n const text = message(error);\n if (text) toast(text, { kind: 'error' });\n }));\n }\n return () => offs.forEach(off => off());\n}\n\n/** Define `<a-toast>` (idempotent). `import 'arethesis/components/toast'` calls this. */\nexport function defineToaster(): void {\n defineComponent({ tag: 'a-toast' })(Toaster);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,UAAb,cAA6B,UAAU;CACrC,UAA8B;CAC9B,SAA6B;CAE7B,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,cAAc,MAAuB,YAAY,EAAE,SAAS,WAAW,QAAQ,EAAE,SAAS,WAAW,WAAW,OAAO;EAC/I,KAAK,KAAK,IAAI,WAAW,GAAG,aAAY,MAAK;GAC3C,MAAM,OAAO,QAAQ,CAAC;GACtB,IAAI,MAAM,MAAM,MAAM,EAAE,MAAM,QAAQ,CAAC;EACzC,CAAC,CAAC;CACJ;CACA,IAAI,QAAQ,WAAW;EACrB,MAAM,UAAU,QAAQ;EACxB,KAAK,KAAK,IAAI,KAAK,SAAQ,UAAS;GAClC,MAAM,OAAO,QAAQ,KAAK;GAC1B,IAAI,MAAM,MAAM,MAAM,EAAE,MAAM,QAAQ,CAAC;EACzC,CAAC,CAAC;CACJ;CACA,aAAa,KAAK,SAAQ,QAAO,IAAI,CAAC;AACxC;;AAGA,SAAgB,gBAAsB;CACpC,gBAAgB,EAAE,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO;AAC7C"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { FormAssociatedComponent } from '../components/form-associated';
|
|
2
|
+
import { ListView, ListViewConfig } from '../data/types';
|
|
3
|
+
export interface TreeConfig<T = Record<string, unknown>> {
|
|
4
|
+
/** The top-level nodes. */
|
|
5
|
+
root: ListView | ListViewConfig;
|
|
6
|
+
/** The list of a node's children, or null for a leaf. */
|
|
7
|
+
children: (node: T) => ListViewConfig | null;
|
|
8
|
+
/** Whether a node can be opened, before its children are loaded. @default children(node) !== null */
|
|
9
|
+
hasChildren?: (node: T) => boolean;
|
|
10
|
+
/** A field name, or a function, for each node's text. @default 'name' */
|
|
11
|
+
label?: string | ((node: T) => string);
|
|
12
|
+
/** Add details to a node's element (icons, badges). */
|
|
13
|
+
render?: (element: HTMLElement, node: T) => void;
|
|
14
|
+
/** @default 'none' */
|
|
15
|
+
selection?: 'none' | 'single' | 'multi';
|
|
16
|
+
/** Keys of nodes to open at first (`id`s joined by `/` from the top, like `"3/14"`). */
|
|
17
|
+
expanded?: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare class Tree extends FormAssociatedComponent<string[]> {
|
|
20
|
+
#private;
|
|
21
|
+
/** Set up the tree. */
|
|
22
|
+
configure<T>(config: TreeConfig<T>): void;
|
|
23
|
+
/** Keys of the open nodes. */
|
|
24
|
+
get expandedKeys(): string[];
|
|
25
|
+
protected get focusTarget(): HTMLElement;
|
|
26
|
+
protected serialize(value: string[]): string | FormData | null;
|
|
27
|
+
protected parse(state: string): string[];
|
|
28
|
+
protected emptyValue(): string[];
|
|
29
|
+
/** Focus the current item (the one in the tab order). */
|
|
30
|
+
focus(options?: FocusOptions): void;
|
|
31
|
+
protected isEmpty(value: string[]): boolean;
|
|
32
|
+
protected onValueChange(): void;
|
|
33
|
+
protected onConnected(): void;
|
|
34
|
+
protected onDisconnected(): void;
|
|
35
|
+
connectedCallback(): Promise<void>;
|
|
36
|
+
/** Open or close the node with this key. */
|
|
37
|
+
toggle(key: string, expanded?: boolean): void;
|
|
38
|
+
}
|
|
39
|
+
/** Define `<a-tree>` (idempotent). `import 'arethesis/components/tree'` calls this. */
|
|
40
|
+
export declare function defineTree(): void;
|
|
41
|
+
//# sourceMappingURL=tree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/kit/tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAGxE,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAI9D,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACrD,2BAA2B;IAC3B,IAAI,EAAE,QAAQ,GAAG,cAAc,CAAC;IAChC,yDAAyD;IACzD,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,cAAc,GAAG,IAAI,CAAC;IAC7C,qGAA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ACvF,wBAAgB,UAAU,IAAI,IAAI,CAEjC"}
|
package/dist/kit/tree.js
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { FormAssociatedComponent } from "../components/form-associated.js";
|
|
2
|
+
import { defineComponent } from "../components/decorators.js";
|
|
3
|
+
import { getRuntime } from "../components/runtime.js";
|
|
4
|
+
import { uniqueId } from "../utils/dom.js";
|
|
5
|
+
import { getPath } from "../utils/path.js";
|
|
6
|
+
// src/kit/tree.ts
|
|
7
|
+
/**
|
|
8
|
+
* `<a-tree>`: a tree whose branches load as they're opened (the WAI-ARIA
|
|
9
|
+
* tree pattern). The top level is a ListView, and so is each node's children,
|
|
10
|
+
* so they page, cache and update like any list.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* tree.configure({
|
|
14
|
+
* root: { type: 'Folder', endpoint: 'listFolders', filters: { parent: null } },
|
|
15
|
+
* children: folder => ({ type: 'Folder', endpoint: 'listFolders', filters: { parent: folder.id } }),
|
|
16
|
+
* hasChildren: folder => folder.childCount > 0,
|
|
17
|
+
* label: 'name',
|
|
18
|
+
* selection: 'multi',
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* With `selection` and a `name`, it's a form control that submits the
|
|
23
|
+
* selected ids.
|
|
24
|
+
*
|
|
25
|
+
* Keys: Up/Down move, Right opens a node (or moves to its first child), Left
|
|
26
|
+
* closes it (or moves to its parent), Home/End, `*` opens all siblings,
|
|
27
|
+
* typing jumps to a match, Enter activates (and selects), Space toggles
|
|
28
|
+
* selection in a multi-select tree.
|
|
29
|
+
*
|
|
30
|
+
* Events: `tree:select` (`{ ids }`), `tree:toggle` (`{ key, id, expanded }`),
|
|
31
|
+
* `tree:activate` (`{ key, id, data }`).
|
|
32
|
+
*
|
|
33
|
+
* @module components/tree
|
|
34
|
+
*/
|
|
35
|
+
var Tree = class extends FormAssociatedComponent {
|
|
36
|
+
#config = null;
|
|
37
|
+
#list = null;
|
|
38
|
+
#root = null;
|
|
39
|
+
#views = /* @__PURE__ */ new Map();
|
|
40
|
+
#expanded = /* @__PURE__ */ new Set();
|
|
41
|
+
#rows = [];
|
|
42
|
+
#elements = /* @__PURE__ */ new Map();
|
|
43
|
+
#focusKey = null;
|
|
44
|
+
#offs = [];
|
|
45
|
+
#scheduled = false;
|
|
46
|
+
#typed = "";
|
|
47
|
+
#typedAt = 0;
|
|
48
|
+
/** Set up the tree. */
|
|
49
|
+
configure(config) {
|
|
50
|
+
this.#config = config;
|
|
51
|
+
for (const off of this.#offs.splice(0)) off();
|
|
52
|
+
this.#views.clear();
|
|
53
|
+
this.#expanded = new Set(config.expanded ?? []);
|
|
54
|
+
const { listViews, store } = getRuntime();
|
|
55
|
+
this.#root = "ids" in config.root ? config.root : listViews.getOrCreate(config.root);
|
|
56
|
+
this.#watch(this.#root);
|
|
57
|
+
this.#offs.push(store.subscribeToType(this.#root.type, () => this.#schedule()));
|
|
58
|
+
for (const key of this.#expanded) this.#childView(key);
|
|
59
|
+
this.#schedule();
|
|
60
|
+
}
|
|
61
|
+
/** Keys of the open nodes. */
|
|
62
|
+
get expandedKeys() {
|
|
63
|
+
return [...this.#expanded];
|
|
64
|
+
}
|
|
65
|
+
get focusTarget() {
|
|
66
|
+
return this.#list;
|
|
67
|
+
}
|
|
68
|
+
serialize(value) {
|
|
69
|
+
if (!value.length || !this.name) return null;
|
|
70
|
+
if (value.length === 1) return value[0];
|
|
71
|
+
const data = new FormData();
|
|
72
|
+
for (const id of value) data.append(this.name, id);
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
parse(state) {
|
|
76
|
+
return state ? [state] : [];
|
|
77
|
+
}
|
|
78
|
+
emptyValue() {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
/** Focus the current item (the one in the tab order). */
|
|
82
|
+
focus(options) {
|
|
83
|
+
(this.#elements.get(this.#focusKey ?? "") ?? this.#list)?.focus(options);
|
|
84
|
+
}
|
|
85
|
+
isEmpty(value) {
|
|
86
|
+
return value.length === 0;
|
|
87
|
+
}
|
|
88
|
+
onValueChange() {
|
|
89
|
+
this.#paintSelection();
|
|
90
|
+
}
|
|
91
|
+
onConnected() {
|
|
92
|
+
if (this.#list) return;
|
|
93
|
+
const list = document.createElement("ul");
|
|
94
|
+
list.className = "ar-tree";
|
|
95
|
+
list.setAttribute("role", "tree");
|
|
96
|
+
list.id = `${this.id || uniqueId("ar-tree")}-tree`;
|
|
97
|
+
const label = this.getAttribute("aria-label");
|
|
98
|
+
if (label) list.setAttribute("aria-label", label);
|
|
99
|
+
list.addEventListener("keydown", (e) => this.#onKey(e));
|
|
100
|
+
list.addEventListener("click", (e) => this.#onClick(e));
|
|
101
|
+
list.addEventListener("dblclick", (e) => {
|
|
102
|
+
const row = this.#rowFor(e.target);
|
|
103
|
+
if (row && !row.more) this.#activate(row);
|
|
104
|
+
});
|
|
105
|
+
list.addEventListener("focusin", (e) => {
|
|
106
|
+
const row = this.#rowFor(e.target);
|
|
107
|
+
if (row) this.#focusKey = row.key;
|
|
108
|
+
});
|
|
109
|
+
list.addEventListener("focusout", () => this.markTouched());
|
|
110
|
+
this.replaceChildren(list);
|
|
111
|
+
this.#list = list;
|
|
112
|
+
if (this.#config) this.#render();
|
|
113
|
+
}
|
|
114
|
+
onDisconnected() {
|
|
115
|
+
for (const off of this.#offs.splice(0)) off();
|
|
116
|
+
}
|
|
117
|
+
connectedCallback() {
|
|
118
|
+
if (this.#config && !this.#offs.length && this.#root) {
|
|
119
|
+
this.#watch(this.#root);
|
|
120
|
+
for (const view of this.#views.values()) this.#watch(view);
|
|
121
|
+
this.#offs.push(getRuntime().store.subscribeToType(this.#root.type, () => this.#schedule()));
|
|
122
|
+
}
|
|
123
|
+
return super.connectedCallback();
|
|
124
|
+
}
|
|
125
|
+
#watch(view) {
|
|
126
|
+
const { listViews } = getRuntime();
|
|
127
|
+
this.#offs.push(listViews.subscribe(view.id, () => this.#schedule()));
|
|
128
|
+
if (view.status === "idle" || view.status === "stale") listViews.load(view).catch((error) => this.emit("tree:error", { error }));
|
|
129
|
+
}
|
|
130
|
+
#data(row) {
|
|
131
|
+
return getRuntime().store.getData(row.type, row.id);
|
|
132
|
+
}
|
|
133
|
+
#label(data, id) {
|
|
134
|
+
const label = this.#config?.label ?? "name";
|
|
135
|
+
if (!data) return id;
|
|
136
|
+
return typeof label === "function" ? label(data) : String(getPath(data, label) ?? id);
|
|
137
|
+
}
|
|
138
|
+
#hasChildren(data) {
|
|
139
|
+
if (!data || !this.#config) return false;
|
|
140
|
+
return this.#config.hasChildren ? this.#config.hasChildren(data) : this.#config.children(data) !== null;
|
|
141
|
+
}
|
|
142
|
+
/** The children of the node at `key`, created (and loaded) on first use. */
|
|
143
|
+
#childView(key, data) {
|
|
144
|
+
const existing = this.#views.get(key);
|
|
145
|
+
if (existing) return existing;
|
|
146
|
+
const node = data ?? this.#findData(key);
|
|
147
|
+
const config = node ? this.#config.children(node) : null;
|
|
148
|
+
if (!config) return null;
|
|
149
|
+
const view = getRuntime().listViews.getOrCreate(config);
|
|
150
|
+
this.#views.set(key, view);
|
|
151
|
+
this.#watch(view);
|
|
152
|
+
return view;
|
|
153
|
+
}
|
|
154
|
+
#findData(key) {
|
|
155
|
+
const row = this.#rows.find((r) => r.key === key);
|
|
156
|
+
if (row) return this.#data(row);
|
|
157
|
+
const id = key.split("/").pop();
|
|
158
|
+
return this.#root ? getRuntime().store.getData(this.#root.type, id) : void 0;
|
|
159
|
+
}
|
|
160
|
+
#schedule() {
|
|
161
|
+
if (this.#scheduled) return;
|
|
162
|
+
this.#scheduled = true;
|
|
163
|
+
queueMicrotask(() => {
|
|
164
|
+
this.#scheduled = false;
|
|
165
|
+
this.#render();
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
#flatten() {
|
|
169
|
+
const rows = [];
|
|
170
|
+
const walk = (view, depth, parentKey) => {
|
|
171
|
+
const setsize = view.totalCount ?? view.ids.length;
|
|
172
|
+
view.ids.forEach((id, i) => {
|
|
173
|
+
const key = parentKey ? `${parentKey}/${id}` : id;
|
|
174
|
+
rows.push({
|
|
175
|
+
key,
|
|
176
|
+
id,
|
|
177
|
+
type: view.type,
|
|
178
|
+
depth,
|
|
179
|
+
parentKey,
|
|
180
|
+
posinset: i + 1,
|
|
181
|
+
setsize
|
|
182
|
+
});
|
|
183
|
+
if (this.#expanded.has(key)) {
|
|
184
|
+
const data = getRuntime().store.getData(view.type, id);
|
|
185
|
+
const children = this.#childView(key, data);
|
|
186
|
+
if (children) walk(children, depth + 1, key);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (view.hasMore && view.ids.length) rows.push({
|
|
190
|
+
key: `${parentKey ?? ""}/…more`,
|
|
191
|
+
id: "",
|
|
192
|
+
type: view.type,
|
|
193
|
+
depth,
|
|
194
|
+
parentKey,
|
|
195
|
+
posinset: 0,
|
|
196
|
+
setsize,
|
|
197
|
+
more: view
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
if (this.#root) walk(this.#root, 1, null);
|
|
201
|
+
return rows;
|
|
202
|
+
}
|
|
203
|
+
#render() {
|
|
204
|
+
const list = this.#list;
|
|
205
|
+
if (!list || !this.#config) return;
|
|
206
|
+
const focused = list.contains(document.activeElement) ? this.#focusKey : null;
|
|
207
|
+
this.#rows = this.#flatten();
|
|
208
|
+
const seen = /* @__PURE__ */ new Set();
|
|
209
|
+
let previous = null;
|
|
210
|
+
for (const row of this.#rows) {
|
|
211
|
+
seen.add(row.key);
|
|
212
|
+
const li = this.#element(row);
|
|
213
|
+
if (li !== (previous ? previous.nextSibling : list.firstChild)) {
|
|
214
|
+
if (previous) previous.after(li);
|
|
215
|
+
else list.prepend(li);
|
|
216
|
+
}
|
|
217
|
+
previous = li;
|
|
218
|
+
}
|
|
219
|
+
for (const [key, li] of this.#elements) if (!seen.has(key)) {
|
|
220
|
+
li.remove();
|
|
221
|
+
this.#elements.delete(key);
|
|
222
|
+
}
|
|
223
|
+
list.setAttribute("aria-busy", String(this.#root?.status === "loading"));
|
|
224
|
+
if (this.#config.selection === "multi") list.setAttribute("aria-multiselectable", "true");
|
|
225
|
+
else list.removeAttribute("aria-multiselectable");
|
|
226
|
+
if (!this.#focusKey || !this.#elements.has(this.#focusKey)) this.#focusKey = (this.#rows.find((r) => !r.more) ?? this.#rows[0])?.key ?? null;
|
|
227
|
+
for (const [key, li] of this.#elements) li.tabIndex = key === this.#focusKey ? 0 : -1;
|
|
228
|
+
if (focused) {
|
|
229
|
+
const target = this.#elements.get(focused) ?? this.#elements.get(this.#focusKey ?? "");
|
|
230
|
+
if (target && document.activeElement !== target) target.focus({ preventScroll: true });
|
|
231
|
+
}
|
|
232
|
+
this.#paintSelection();
|
|
233
|
+
}
|
|
234
|
+
#element(row) {
|
|
235
|
+
let li = this.#elements.get(row.key);
|
|
236
|
+
if (!li) {
|
|
237
|
+
li = document.createElement("li");
|
|
238
|
+
li.setAttribute("role", "treeitem");
|
|
239
|
+
li.dataset.key = row.key;
|
|
240
|
+
this.#elements.set(row.key, li);
|
|
241
|
+
}
|
|
242
|
+
li.setAttribute("aria-level", String(row.depth));
|
|
243
|
+
li.style.setProperty("--ar-tree-depth", String(row.depth - 1));
|
|
244
|
+
if (row.more) {
|
|
245
|
+
li.className = "ar-tree__item ar-tree__more";
|
|
246
|
+
li.textContent = row.more.status === "loading-more" ? "Loading…" : "Show more";
|
|
247
|
+
li.removeAttribute("aria-posinset");
|
|
248
|
+
li.removeAttribute("aria-setsize");
|
|
249
|
+
li.toggleAttribute("aria-busy", row.more.status === "loading-more");
|
|
250
|
+
return li;
|
|
251
|
+
}
|
|
252
|
+
const data = this.#data(row);
|
|
253
|
+
li.className = "ar-tree__item";
|
|
254
|
+
li.dataset.id = row.id;
|
|
255
|
+
li.setAttribute("aria-posinset", String(row.posinset));
|
|
256
|
+
li.setAttribute("aria-setsize", String(row.setsize));
|
|
257
|
+
if (this.#hasChildren(data)) {
|
|
258
|
+
const open = this.#expanded.has(row.key);
|
|
259
|
+
li.setAttribute("aria-expanded", String(open));
|
|
260
|
+
const children = this.#views.get(row.key);
|
|
261
|
+
li.toggleAttribute("aria-busy", open && !!children && children.status === "loading");
|
|
262
|
+
} else {
|
|
263
|
+
li.removeAttribute("aria-expanded");
|
|
264
|
+
li.removeAttribute("aria-busy");
|
|
265
|
+
}
|
|
266
|
+
let label = li.querySelector(":scope > .ar-tree__label");
|
|
267
|
+
if (!label) {
|
|
268
|
+
li.replaceChildren();
|
|
269
|
+
const toggle = document.createElement("span");
|
|
270
|
+
toggle.className = "ar-tree__toggle";
|
|
271
|
+
toggle.setAttribute("aria-hidden", "true");
|
|
272
|
+
label = document.createElement("span");
|
|
273
|
+
label.className = "ar-tree__label";
|
|
274
|
+
li.append(toggle, label);
|
|
275
|
+
}
|
|
276
|
+
label.textContent = this.#label(data, row.id);
|
|
277
|
+
if (data) this.#config.render?.(li, data);
|
|
278
|
+
return li;
|
|
279
|
+
}
|
|
280
|
+
#paintSelection() {
|
|
281
|
+
const mode = this.#config?.selection ?? "none";
|
|
282
|
+
const selected = new Set(this.value);
|
|
283
|
+
for (const [key, li] of this.#elements) if (mode === "none" || key.endsWith("…more")) li.removeAttribute("aria-selected");
|
|
284
|
+
else li.setAttribute("aria-selected", String(selected.has(li.dataset.id ?? "")));
|
|
285
|
+
}
|
|
286
|
+
#rowFor(target) {
|
|
287
|
+
const li = target.closest("[role=\"treeitem\"]");
|
|
288
|
+
return li ? this.#rows.find((r) => r.key === li.dataset.key) : void 0;
|
|
289
|
+
}
|
|
290
|
+
/** Open or close the node with this key. */
|
|
291
|
+
toggle(key, expanded = !this.#expanded.has(key)) {
|
|
292
|
+
const row = this.#rows.find((r) => r.key === key);
|
|
293
|
+
if (!row || row.more || !this.#hasChildren(this.#data(row))) return;
|
|
294
|
+
if (expanded === this.#expanded.has(key)) return;
|
|
295
|
+
if (expanded) {
|
|
296
|
+
this.#expanded.add(key);
|
|
297
|
+
this.#childView(key, this.#data(row));
|
|
298
|
+
} else {
|
|
299
|
+
this.#expanded.delete(key);
|
|
300
|
+
if (this.#focusKey?.startsWith(`${key}/`)) this.#focusKey = key;
|
|
301
|
+
}
|
|
302
|
+
this.emit("tree:toggle", {
|
|
303
|
+
key,
|
|
304
|
+
id: row.id,
|
|
305
|
+
expanded
|
|
306
|
+
});
|
|
307
|
+
this.#render();
|
|
308
|
+
}
|
|
309
|
+
#focus(row) {
|
|
310
|
+
if (!row) return;
|
|
311
|
+
this.#focusKey = row.key;
|
|
312
|
+
for (const [key, li] of this.#elements) li.tabIndex = key === row.key ? 0 : -1;
|
|
313
|
+
this.#elements.get(row.key)?.focus();
|
|
314
|
+
}
|
|
315
|
+
#select(row, { toggle = false } = {}) {
|
|
316
|
+
const mode = this.#config?.selection ?? "none";
|
|
317
|
+
if (mode === "none" || row.more) return;
|
|
318
|
+
let next;
|
|
319
|
+
if (mode === "single") next = [row.id];
|
|
320
|
+
else if (toggle) next = this.value.includes(row.id) ? this.value.filter((id) => id !== row.id) : [...this.value, row.id];
|
|
321
|
+
else next = [row.id];
|
|
322
|
+
this.commitValue(next);
|
|
323
|
+
this.emit("tree:select", { ids: next });
|
|
324
|
+
}
|
|
325
|
+
#activate(row) {
|
|
326
|
+
this.#select(row, { toggle: false });
|
|
327
|
+
this.emit("tree:activate", {
|
|
328
|
+
key: row.key,
|
|
329
|
+
id: row.id,
|
|
330
|
+
data: this.#data(row)
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
#onClick(e) {
|
|
334
|
+
const row = this.#rowFor(e.target);
|
|
335
|
+
if (!row) return;
|
|
336
|
+
this.#focus(row);
|
|
337
|
+
if (row.more) {
|
|
338
|
+
getRuntime().listViews.loadMore(row.more);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (e.target.closest(".ar-tree__toggle")) {
|
|
342
|
+
this.toggle(row.key);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.#select(row, { toggle: this.#config?.selection === "multi" && (e.ctrlKey || e.metaKey) });
|
|
346
|
+
}
|
|
347
|
+
#onKey(e) {
|
|
348
|
+
const index = this.#rows.findIndex((r) => r.key === this.#focusKey);
|
|
349
|
+
const row = this.#rows[index];
|
|
350
|
+
if (!row) return;
|
|
351
|
+
const handled = () => e.preventDefault();
|
|
352
|
+
switch (e.key) {
|
|
353
|
+
case "ArrowDown":
|
|
354
|
+
handled();
|
|
355
|
+
this.#focus(this.#rows[index + 1]);
|
|
356
|
+
break;
|
|
357
|
+
case "ArrowUp":
|
|
358
|
+
handled();
|
|
359
|
+
this.#focus(this.#rows[index - 1]);
|
|
360
|
+
break;
|
|
361
|
+
case "Home":
|
|
362
|
+
handled();
|
|
363
|
+
this.#focus(this.#rows[0]);
|
|
364
|
+
break;
|
|
365
|
+
case "End":
|
|
366
|
+
handled();
|
|
367
|
+
this.#focus(this.#rows[this.#rows.length - 1]);
|
|
368
|
+
break;
|
|
369
|
+
case "ArrowRight":
|
|
370
|
+
handled();
|
|
371
|
+
if (row.more || !this.#hasChildren(this.#data(row))) break;
|
|
372
|
+
if (!this.#expanded.has(row.key)) this.toggle(row.key, true);
|
|
373
|
+
else {
|
|
374
|
+
const next = this.#rows[index + 1];
|
|
375
|
+
if (next?.parentKey === row.key) this.#focus(next);
|
|
376
|
+
}
|
|
377
|
+
break;
|
|
378
|
+
case "ArrowLeft":
|
|
379
|
+
handled();
|
|
380
|
+
if (this.#expanded.has(row.key)) this.toggle(row.key, false);
|
|
381
|
+
else this.#focus(this.#rows.find((r) => r.key === row.parentKey));
|
|
382
|
+
break;
|
|
383
|
+
case "*":
|
|
384
|
+
handled();
|
|
385
|
+
for (const sibling of this.#rows.filter((r) => r.parentKey === row.parentKey && !r.more)) if (this.#hasChildren(this.#data(sibling))) this.#expanded.add(sibling.key);
|
|
386
|
+
this.#render();
|
|
387
|
+
break;
|
|
388
|
+
case "Enter":
|
|
389
|
+
handled();
|
|
390
|
+
if (row.more) getRuntime().listViews.loadMore(row.more);
|
|
391
|
+
else this.#activate(row);
|
|
392
|
+
break;
|
|
393
|
+
case " ":
|
|
394
|
+
handled();
|
|
395
|
+
if (row.more) getRuntime().listViews.loadMore(row.more);
|
|
396
|
+
else this.#select(row, { toggle: this.#config?.selection === "multi" });
|
|
397
|
+
break;
|
|
398
|
+
default: if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) this.#typeahead(e.key, index);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
#typeahead(char, from) {
|
|
402
|
+
const now = Date.now();
|
|
403
|
+
this.#typed = now - this.#typedAt > 500 ? char.toLowerCase() : this.#typed + char.toLowerCase();
|
|
404
|
+
this.#typedAt = now;
|
|
405
|
+
const n = this.#rows.length;
|
|
406
|
+
const start = this.#typed.length === 1 ? from + 1 : from;
|
|
407
|
+
for (let k = 0; k < n; k++) {
|
|
408
|
+
const row = this.#rows[(start + k) % n];
|
|
409
|
+
if (!row.more && this.#label(this.#data(row), row.id).toLowerCase().startsWith(this.#typed)) {
|
|
410
|
+
this.#focus(row);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
/** Define `<a-tree>` (idempotent). `import 'arethesis/components/tree'` calls this. */
|
|
417
|
+
function defineTree() {
|
|
418
|
+
defineComponent({ tag: "a-tree" })(Tree);
|
|
419
|
+
}
|
|
420
|
+
export { Tree, defineTree };
|
|
421
|
+
|
|
422
|
+
//# sourceMappingURL=tree.js.map
|