asta-ui 0.7.0 → 0.8.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/css/asta.css +3 -0
- package/css/asta.js +4748 -0
- package/css/components.css +5805 -0
- package/css/src/blocks.css +919 -0
- package/css/src/core.css +702 -0
- package/css/src/data.css +965 -0
- package/css/src/display.css +681 -0
- package/css/src/forms.css +861 -0
- package/css/src/overlays.css +1114 -0
- package/css/tokens.css +140 -0
- package/package.json +3 -1
package/css/asta.js
ADDED
|
@@ -0,0 +1,4748 @@
|
|
|
1
|
+
/* Astabyte UI runtime. Generated by scripts/gen-css.ts from go/ui/assets/asta.js. Do not edit. */
|
|
2
|
+
/* ---- src/core.js ---- */
|
|
3
|
+
/* Astabyte UI for Go + htmx. One small script, no dependencies.
|
|
4
|
+
* Everything is wired with event delegation on document, so markup swapped in by htmx works without re-init.
|
|
5
|
+
* Dialog <dialog class="asta-dialog">, opened by [data-asta-open="id"], closed by [data-asta-close]
|
|
6
|
+
* Toast asta.toast({ title, description, tone, duration, action: { label, href | event, detail } }),
|
|
7
|
+
* HX-Trigger "asta:toast", <template data-asta-toast>
|
|
8
|
+
* Tabs [data-asta-tabs] with [role=tab][aria-controls] and [role=tabpanel]
|
|
9
|
+
* Menu [data-asta-menu] with [data-asta-menu-trigger] and [role=menu] holding [role=menuitem]
|
|
10
|
+
*/
|
|
11
|
+
(() => {
|
|
12
|
+
if (window.asta) return;
|
|
13
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
14
|
+
const byId = (id) => (id ? document.getElementById(id) : null);
|
|
15
|
+
|
|
16
|
+
/* ---------- Dialog ---------- */
|
|
17
|
+
|
|
18
|
+
function open(d) {
|
|
19
|
+
if (d && !d.open) d.showModal();
|
|
20
|
+
}
|
|
21
|
+
function close(d) {
|
|
22
|
+
if (!d || !d.open || d.dataset.closing) return;
|
|
23
|
+
// Let the CSS exit transition run before the dialog leaves the top layer.
|
|
24
|
+
if (reduce()) return d.close();
|
|
25
|
+
d.dataset.closing = "";
|
|
26
|
+
const done = () => {
|
|
27
|
+
delete d.dataset.closing;
|
|
28
|
+
d.close();
|
|
29
|
+
};
|
|
30
|
+
const t = setTimeout(done, 320);
|
|
31
|
+
d.addEventListener("transitionend", function end(e) {
|
|
32
|
+
if (e.target !== d || e.propertyName !== "opacity") return;
|
|
33
|
+
d.removeEventListener("transitionend", end);
|
|
34
|
+
clearTimeout(t);
|
|
35
|
+
done();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
document.addEventListener("click", (e) => {
|
|
40
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
41
|
+
if (!t) return;
|
|
42
|
+
const opener = t.closest("[data-asta-open]");
|
|
43
|
+
if (opener) {
|
|
44
|
+
e.preventDefault();
|
|
45
|
+
open(byId(opener.getAttribute("data-asta-open")));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const closer = t.closest("[data-asta-close]");
|
|
49
|
+
if (closer) {
|
|
50
|
+
close(closer.closest("dialog"));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
// A click on the backdrop lands on the <dialog> itself, outside the panel's box.
|
|
54
|
+
if (t.matches("dialog.asta-dialog[open]")) {
|
|
55
|
+
const r = t.getBoundingClientRect();
|
|
56
|
+
if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) close(t);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
document.addEventListener(
|
|
60
|
+
"cancel",
|
|
61
|
+
(e) => {
|
|
62
|
+
if (e.target.matches?.("dialog.asta-dialog")) {
|
|
63
|
+
e.preventDefault();
|
|
64
|
+
close(e.target);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
true,
|
|
68
|
+
);
|
|
69
|
+
document.addEventListener("asta:open", (e) => open(byId(e.detail?.id)));
|
|
70
|
+
document.addEventListener("asta:close", (e) => close(byId(e.detail?.id)));
|
|
71
|
+
|
|
72
|
+
/* ---------- Toast ---------- */
|
|
73
|
+
|
|
74
|
+
const TONE = {
|
|
75
|
+
success: { color: "#30a46c", path: "M4.5 8.5l2.2 2.2L11.5 5.5" },
|
|
76
|
+
error: { color: "#e5484d", path: "M8 4.5v4M8 11h.01" },
|
|
77
|
+
};
|
|
78
|
+
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
79
|
+
|
|
80
|
+
// The stack sits bottom right, toasts tucked behind each other. Hover or focus fans it out and pauses every
|
|
81
|
+
// timer. Swipe a toast sideways to dismiss it. At most three show; the rest wait behind.
|
|
82
|
+
const MAX = 3;
|
|
83
|
+
const GAP = 8;
|
|
84
|
+
let fanned = false;
|
|
85
|
+
let list = null;
|
|
86
|
+
|
|
87
|
+
function stack() {
|
|
88
|
+
if (list?.isConnected) return list;
|
|
89
|
+
const section = document.createElement("section");
|
|
90
|
+
section.id = "asta-toasts";
|
|
91
|
+
section.setAttribute("aria-label", "Notifications");
|
|
92
|
+
section.className = "fixed bottom-4 left-4 right-4 z-40 sm:left-auto sm:w-[340px]";
|
|
93
|
+
list = document.createElement("ol");
|
|
94
|
+
list.setAttribute("aria-live", "polite");
|
|
95
|
+
list.className = "relative";
|
|
96
|
+
section.append(list);
|
|
97
|
+
section.addEventListener("pointerenter", () => fan(true));
|
|
98
|
+
section.addEventListener("pointerleave", () => fan(false));
|
|
99
|
+
section.addEventListener("focusin", () => fan(true));
|
|
100
|
+
section.addEventListener("focusout", (e) => section.contains(e.relatedTarget) || fan(false));
|
|
101
|
+
document.body.append(section);
|
|
102
|
+
return list;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const live = () => (list ? [...list.children].filter((li) => !li.dataset.leaving).reverse() : []); // newest first
|
|
106
|
+
|
|
107
|
+
function fan(on) {
|
|
108
|
+
if (fanned === on) return;
|
|
109
|
+
fanned = on;
|
|
110
|
+
for (const li of live()) on ? pause(li) : run(li);
|
|
111
|
+
layout();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function place(li, dx = 0) {
|
|
115
|
+
const ty = !li._shown ? 24 : li.dataset.leaving === "down" ? 16 : li._y;
|
|
116
|
+
li.style.transform = `translate3d(${dx}px, ${ty}px, 0) scale(${li._scale})`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function layout() {
|
|
120
|
+
const items = live();
|
|
121
|
+
const front = items[0]?.firstElementChild.offsetHeight;
|
|
122
|
+
let above = 0;
|
|
123
|
+
items.forEach((li, i) => {
|
|
124
|
+
const tucked = !fanned && i > 0;
|
|
125
|
+
li._y = i === 0 ? 0 : fanned ? -above : -i * 12;
|
|
126
|
+
li._scale = tucked ? 1 - i * 0.05 : 1;
|
|
127
|
+
above += li.firstElementChild.offsetHeight + GAP;
|
|
128
|
+
li.style.zIndex = String(100 - i);
|
|
129
|
+
li.style.height = tucked ? `${front}px` : "";
|
|
130
|
+
li.firstElementChild.style.opacity = tucked ? "0" : "";
|
|
131
|
+
const off = !li._shown || i >= MAX;
|
|
132
|
+
li.style.opacity = off ? "0" : "";
|
|
133
|
+
li.style.pointerEvents = off ? "none" : "";
|
|
134
|
+
place(li);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function run(li) {
|
|
139
|
+
if (fanned || li.dataset.leaving) return;
|
|
140
|
+
li._start = Date.now();
|
|
141
|
+
li._timer = setTimeout(() => leave(li, "down"), li._left);
|
|
142
|
+
}
|
|
143
|
+
function pause(li) {
|
|
144
|
+
clearTimeout(li._timer);
|
|
145
|
+
li._left = Math.max(800, li._left - (Date.now() - li._start));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function toast(t) {
|
|
149
|
+
if (!t || !t.title) return;
|
|
150
|
+
const tone = TONE[t.tone];
|
|
151
|
+
const li = document.createElement("li");
|
|
152
|
+
li.className =
|
|
153
|
+
"asta-toast absolute bottom-0 left-0 right-0 origin-bottom touch-pan-y select-none overflow-hidden rounded-xl bg-paper shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--ink)_10%,transparent),0_12px_30px_-14px_rgb(0_0_0/0.35)]";
|
|
154
|
+
li.setAttribute("role", t.tone === "error" ? "alert" : "status");
|
|
155
|
+
const a = t.action;
|
|
156
|
+
const safe = (u) => /^(https?:\/\/|\/(?!\/)|\.{0,2}\/|\?|#)/i.test(String(u ?? ""));
|
|
157
|
+
const action = a?.label
|
|
158
|
+
? a.href && safe(a.href)
|
|
159
|
+
? `<a href="${esc(a.href)}" data-asta-toast-action class="flex h-7 shrink-0 items-center rounded-lg bg-ink px-2.5 text-[12px] font-medium text-paper outline-none transition-opacity hover:opacity-85 focus-visible:ring-2 focus-visible:ring-brand/50">${esc(a.label)}</a>`
|
|
160
|
+
: `<button type="button" data-asta-toast-action class="h-7 shrink-0 rounded-lg bg-ink px-2.5 text-[12px] font-medium text-paper outline-none transition-opacity hover:opacity-85 focus-visible:ring-2 focus-visible:ring-brand/50">${esc(a.label)}</button>`
|
|
161
|
+
: `<button type="button" data-asta-toast-close aria-label="Dismiss" class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-mute outline-none transition-colors hover:bg-paper-2 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50"><svg viewBox="0 0 16 16" class="h-3 w-3" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><path d="m4.5 4.5 7 7M11.5 4.5l-7 7"/></svg></button>`;
|
|
162
|
+
li.innerHTML = `<div class="flex items-start gap-3 p-3.5 pr-3 transition-opacity">${
|
|
163
|
+
tone
|
|
164
|
+
? `<span class="mt-px flex h-5 w-5 shrink-0 items-center justify-center rounded-full text-white" style="background:${tone.color}"><svg viewBox="0 0 16 16" class="h-3 w-3" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${tone.path}"/></svg></span>`
|
|
165
|
+
: ""
|
|
166
|
+
}<div class="min-w-0 flex-1"><p class="text-[13.5px] font-medium leading-5 tracking-[-0.01em] text-ink">${esc(t.title)}</p>${
|
|
167
|
+
t.description ? `<p class="mt-0.5 text-[12.5px] leading-snug text-mute">${esc(t.description)}</p>` : ""
|
|
168
|
+
}</div>${action}</div>`;
|
|
169
|
+
|
|
170
|
+
li._left = t.duration || 4000;
|
|
171
|
+
li._scale = 1;
|
|
172
|
+
li._y = 0;
|
|
173
|
+
stack().append(li);
|
|
174
|
+
layout();
|
|
175
|
+
requestAnimationFrame(() =>
|
|
176
|
+
requestAnimationFrame(() => {
|
|
177
|
+
li._shown = true;
|
|
178
|
+
layout();
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
run(li);
|
|
182
|
+
|
|
183
|
+
li.querySelector("[data-asta-toast-close]")?.addEventListener("click", () => leave(li, "down"));
|
|
184
|
+
li.querySelector("[data-asta-toast-action]")?.addEventListener("click", () => {
|
|
185
|
+
// An action names an event (dispatched on document, with the toast's data) or a link.
|
|
186
|
+
if (a.event) document.dispatchEvent(new CustomEvent(a.event, { detail: a.detail ?? null, bubbles: true }));
|
|
187
|
+
leave(li, "down");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Swipe sideways to dismiss.
|
|
191
|
+
let drag = null;
|
|
192
|
+
li.addEventListener("pointerdown", (e) => {
|
|
193
|
+
if (e.target.closest("button, a")) return;
|
|
194
|
+
li.setPointerCapture(e.pointerId);
|
|
195
|
+
drag = { x: e.clientX, t: Date.now() };
|
|
196
|
+
li.style.transition = "none";
|
|
197
|
+
});
|
|
198
|
+
li.addEventListener("pointermove", (e) => {
|
|
199
|
+
if (!drag) return;
|
|
200
|
+
const dx = e.clientX - drag.x;
|
|
201
|
+
place(li, dx);
|
|
202
|
+
li.style.opacity = String(1 - Math.min(0.7, Math.abs(dx) / 260));
|
|
203
|
+
});
|
|
204
|
+
const drop = (e, cancel) => {
|
|
205
|
+
if (!drag) return;
|
|
206
|
+
const dx = cancel ? 0 : e.clientX - drag.x;
|
|
207
|
+
const v = Math.abs(dx) / Math.max(1, Date.now() - drag.t);
|
|
208
|
+
drag = null;
|
|
209
|
+
li.style.transition = "";
|
|
210
|
+
if (Math.abs(dx) > 90 || v > 0.6) leave(li, dx < 0 ? "left" : "right");
|
|
211
|
+
else layout();
|
|
212
|
+
};
|
|
213
|
+
li.addEventListener("pointerup", (e) => drop(e, false));
|
|
214
|
+
li.addEventListener("pointercancel", (e) => drop(e, true));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function leave(li, dir = "down") {
|
|
218
|
+
if (!li || li.dataset.leaving) return;
|
|
219
|
+
clearTimeout(li._timer);
|
|
220
|
+
li.dataset.leaving = dir;
|
|
221
|
+
li.style.opacity = "0";
|
|
222
|
+
li.style.pointerEvents = "none";
|
|
223
|
+
place(li, dir === "left" ? -380 : dir === "right" ? 380 : 0);
|
|
224
|
+
layout();
|
|
225
|
+
setTimeout(() => li.remove(), reduce() ? 0 : 260);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// htmx hands a non-object HX-Trigger value over as { value }, so a list of toasts arrives wrapped.
|
|
229
|
+
const toasts = (d) => {
|
|
230
|
+
if (d && !d.title && d.value !== undefined) d = d.value;
|
|
231
|
+
(Array.isArray(d) ? d : [d]).forEach(toast);
|
|
232
|
+
};
|
|
233
|
+
document.addEventListener("asta:toast", (e) => toasts(e.detail));
|
|
234
|
+
|
|
235
|
+
/* ---------- Tabs ---------- */
|
|
236
|
+
|
|
237
|
+
// A shown panel gets "asta:show", so hx-trigger="asta:show once" loads it lazily.
|
|
238
|
+
function select(tab, focus) {
|
|
239
|
+
const root = tab.closest("[data-asta-tabs]");
|
|
240
|
+
if (!root) return;
|
|
241
|
+
for (const t of root.querySelectorAll('[role="tab"]')) {
|
|
242
|
+
if (t.closest("[data-asta-tabs]") !== root) continue;
|
|
243
|
+
const on = t === tab;
|
|
244
|
+
t.setAttribute("aria-selected", String(on));
|
|
245
|
+
t.tabIndex = on ? 0 : -1;
|
|
246
|
+
const panel = byId(t.getAttribute("aria-controls"));
|
|
247
|
+
if (panel) {
|
|
248
|
+
panel.hidden = !on;
|
|
249
|
+
if (on) panel.dispatchEvent(new CustomEvent("asta:show", { bubbles: true }));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (focus) tab.focus();
|
|
253
|
+
slide(tab.closest('[role="tablist"]'), true);
|
|
254
|
+
root.dispatchEvent(new CustomEvent("asta:tab", { bubbles: true, detail: { tab } }));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// The indicator moves with transform only. Its width is set once per change, and a scaleX from the old width
|
|
258
|
+
// to the new one (FLIP) hides the jump.
|
|
259
|
+
function slide(list, animate) {
|
|
260
|
+
const bar = list?.querySelector(":scope > [data-asta-tab-indicator]");
|
|
261
|
+
const tab = list?.querySelector('[role="tab"][aria-selected="true"]');
|
|
262
|
+
if (!bar || !tab) return;
|
|
263
|
+
const x = tab.offsetLeft;
|
|
264
|
+
const w = tab.offsetWidth;
|
|
265
|
+
const px = parseFloat(bar.dataset.x ?? "");
|
|
266
|
+
const pw = parseFloat(bar.dataset.w ?? "");
|
|
267
|
+
bar.dataset.x = x;
|
|
268
|
+
bar.dataset.w = w;
|
|
269
|
+
bar.style.width = `${w}px`;
|
|
270
|
+
if (!animate || reduce() || !pw || (px === x && pw === w)) {
|
|
271
|
+
bar.style.transition = "none";
|
|
272
|
+
bar.style.transform = `translateX(${x}px)`;
|
|
273
|
+
bar.style.opacity = "1";
|
|
274
|
+
void bar.offsetWidth;
|
|
275
|
+
bar.style.transition = "";
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
bar.style.transition = "none";
|
|
279
|
+
bar.style.transform = `translateX(${px}px) scaleX(${pw / w})`;
|
|
280
|
+
void bar.offsetWidth;
|
|
281
|
+
bar.style.transition = "";
|
|
282
|
+
bar.style.transform = `translateX(${x}px)`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Re-measure when a tab list or a tab changes size (layout, fonts), never per frame.
|
|
286
|
+
const sized = new ResizeObserver((entries) => {
|
|
287
|
+
const lists = new Set(entries.map((e) => e.target.closest('[role="tablist"]')));
|
|
288
|
+
for (const list of lists) slide(list, false);
|
|
289
|
+
});
|
|
290
|
+
function watchTabs(el) {
|
|
291
|
+
for (const bar of el.querySelectorAll("[data-asta-tab-indicator]")) {
|
|
292
|
+
const list = bar.parentElement;
|
|
293
|
+
if (list.dataset.astaWatched !== undefined) continue;
|
|
294
|
+
list.dataset.astaWatched = "";
|
|
295
|
+
sized.observe(list);
|
|
296
|
+
for (const t of list.querySelectorAll('[role="tab"]')) sized.observe(t);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => watchTabs(document));
|
|
300
|
+
else watchTabs(document);
|
|
301
|
+
document.addEventListener("htmx:afterSettle", (e) => watchTabs(e.detail?.elt?.parentElement || document));
|
|
302
|
+
|
|
303
|
+
document.addEventListener("click", (e) => {
|
|
304
|
+
const tab = e.target instanceof Element && e.target.closest('[data-asta-tabs] [role="tab"]');
|
|
305
|
+
if (tab && !tab.disabled) select(tab, false);
|
|
306
|
+
});
|
|
307
|
+
document.addEventListener("keydown", (e) => {
|
|
308
|
+
const tab = e.target instanceof Element && e.target.closest('[data-asta-tabs] [role="tab"]');
|
|
309
|
+
if (!tab) return;
|
|
310
|
+
const list = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]:not([disabled])')];
|
|
311
|
+
const i = list.indexOf(tab);
|
|
312
|
+
const next = { ArrowRight: i + 1, ArrowDown: i + 1, ArrowLeft: i - 1, ArrowUp: i - 1, Home: 0, End: list.length - 1 }[e.key];
|
|
313
|
+
if (next === undefined) return;
|
|
314
|
+
e.preventDefault();
|
|
315
|
+
// A click, so hx-get on a tab fires from the keyboard too. The click handler selects it.
|
|
316
|
+
const to = list[(next + list.length) % list.length];
|
|
317
|
+
to.focus();
|
|
318
|
+
to.click();
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
/* ---------- Menu ---------- */
|
|
322
|
+
|
|
323
|
+
// The nearest ancestor that cuts off overflow, if any.
|
|
324
|
+
function clipper(el) {
|
|
325
|
+
for (let p = el.parentElement; p && p !== document.body; p = p.parentElement) {
|
|
326
|
+
const o = getComputedStyle(p);
|
|
327
|
+
if (/auto|scroll|hidden|clip/.test(o.overflowX + o.overflowY)) return p;
|
|
328
|
+
}
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
// A pinned menu doesn't follow its trigger, so close it when anything scrolls.
|
|
332
|
+
addEventListener(
|
|
333
|
+
"scroll",
|
|
334
|
+
() => {
|
|
335
|
+
for (const m of document.querySelectorAll("[data-asta-menu][data-pinned][data-open]")) setMenu(m, false);
|
|
336
|
+
},
|
|
337
|
+
{ capture: true, passive: true },
|
|
338
|
+
);
|
|
339
|
+
const items = (m) => [...m.querySelectorAll('[role="menuitem"]:not([aria-disabled="true"]):not([disabled])')];
|
|
340
|
+
function setMenu(root, on, focusFirst) {
|
|
341
|
+
const trigger = root.querySelector("[data-asta-menu-trigger]");
|
|
342
|
+
const menu = root.querySelector('[role="menu"]');
|
|
343
|
+
if (!trigger || !menu) return;
|
|
344
|
+
if (on) {
|
|
345
|
+
for (const other of document.querySelectorAll("[data-asta-menu][data-open]")) if (other !== root) setMenu(other, false);
|
|
346
|
+
root.setAttribute("data-open", "");
|
|
347
|
+
menu.hidden = false;
|
|
348
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
349
|
+
menu.style.cssText = "";
|
|
350
|
+
const r = menu.getBoundingClientRect();
|
|
351
|
+
const box = clipper(root)?.getBoundingClientRect();
|
|
352
|
+
if (box && (r.bottom > box.bottom || r.right > box.right || r.left < box.left)) {
|
|
353
|
+
// Inside a scroll box (a table, a card) the menu would be cut off, so pin it to the viewport instead.
|
|
354
|
+
const t = trigger.getBoundingClientRect();
|
|
355
|
+
const up = t.bottom + r.height + 6 > innerHeight - 8 && t.top - r.height - 6 > 8;
|
|
356
|
+
const left = root.getAttribute("data-align") === "end" ? t.right - r.width : t.left;
|
|
357
|
+
Object.assign(menu.style, {
|
|
358
|
+
position: "fixed",
|
|
359
|
+
margin: "0",
|
|
360
|
+
right: "auto",
|
|
361
|
+
bottom: "auto",
|
|
362
|
+
top: `${up ? t.top - r.height - 6 : t.bottom + 6}px`,
|
|
363
|
+
left: `${Math.max(8, Math.min(left, innerWidth - r.width - 8))}px`,
|
|
364
|
+
});
|
|
365
|
+
root.toggleAttribute("data-up", up);
|
|
366
|
+
root.setAttribute("data-pinned", "");
|
|
367
|
+
} else {
|
|
368
|
+
// Flip up when there's no room below.
|
|
369
|
+
root.toggleAttribute("data-up", r.bottom > innerHeight - 8 && r.top - r.height > 8);
|
|
370
|
+
root.removeAttribute("data-pinned");
|
|
371
|
+
}
|
|
372
|
+
if (focusFirst) items(menu)[0]?.focus({ preventScroll: true });
|
|
373
|
+
} else if (root.hasAttribute("data-open")) {
|
|
374
|
+
root.removeAttribute("data-open");
|
|
375
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
376
|
+
setTimeout(() => {
|
|
377
|
+
if (!root.hasAttribute("data-open")) menu.hidden = true;
|
|
378
|
+
}, reduce() ? 0 : 160);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
document.addEventListener("click", (e) => {
|
|
382
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
383
|
+
const trigger = t?.closest("[data-asta-menu-trigger]");
|
|
384
|
+
if (trigger) {
|
|
385
|
+
const root = trigger.closest("[data-asta-menu]");
|
|
386
|
+
setMenu(root, !root.hasAttribute("data-open"), e.detail === 0);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const item = t?.closest('[data-asta-menu] [role="menuitem"]');
|
|
390
|
+
for (const root of document.querySelectorAll("[data-asta-menu][data-open]")) {
|
|
391
|
+
if (item && root.contains(item)) {
|
|
392
|
+
if (!item.hasAttribute("data-keep-open")) setMenu(root, false);
|
|
393
|
+
} else if (!root.contains(t)) setMenu(root, false);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
document.addEventListener("keydown", (e) => {
|
|
397
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
398
|
+
const root = t?.closest("[data-asta-menu]");
|
|
399
|
+
if (!root) return;
|
|
400
|
+
const menu = root.querySelector('[role="menu"]');
|
|
401
|
+
const trigger = root.querySelector("[data-asta-menu-trigger]");
|
|
402
|
+
if (t === trigger && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
403
|
+
e.preventDefault();
|
|
404
|
+
setMenu(root, true, true);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (!root.hasAttribute("data-open")) return;
|
|
408
|
+
const list = items(menu);
|
|
409
|
+
const i = list.indexOf(document.activeElement);
|
|
410
|
+
if (e.key === "Escape") {
|
|
411
|
+
e.preventDefault();
|
|
412
|
+
setMenu(root, false);
|
|
413
|
+
trigger.focus();
|
|
414
|
+
} else if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
415
|
+
e.preventDefault();
|
|
416
|
+
list[(i + (e.key === "ArrowDown" ? 1 : -1) + list.length) % list.length]?.focus();
|
|
417
|
+
} else if (e.key === "Home" || e.key === "End") {
|
|
418
|
+
e.preventDefault();
|
|
419
|
+
list[e.key === "Home" ? 0 : list.length - 1]?.focus();
|
|
420
|
+
} else if (e.key === "Tab") setMenu(root, false);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
/* ---------- Boot and htmx ---------- */
|
|
424
|
+
|
|
425
|
+
function scan(el) {
|
|
426
|
+
for (const d of el.querySelectorAll("dialog.asta-dialog[data-asta-autoopen]")) {
|
|
427
|
+
d.removeAttribute("data-asta-autoopen");
|
|
428
|
+
open(d);
|
|
429
|
+
}
|
|
430
|
+
for (const tpl of el.querySelectorAll("template[data-asta-toast]")) {
|
|
431
|
+
try {
|
|
432
|
+
toasts(JSON.parse(tpl.getAttribute("data-asta-toast")));
|
|
433
|
+
} catch {}
|
|
434
|
+
tpl.remove();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const boot = () => scan(document);
|
|
438
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
439
|
+
else boot();
|
|
440
|
+
document.addEventListener("htmx:afterSettle", (e) => scan(e.detail?.elt?.parentElement || document));
|
|
441
|
+
|
|
442
|
+
window.asta = { toast, open: (id) => open(byId(id)), close: (id) => close(byId(id)), version: "0.8.0" };
|
|
443
|
+
})();
|
|
444
|
+
|
|
445
|
+
/* ---- src/blocks.js ---- */
|
|
446
|
+
/* Blocks: sign-in, onboarding, AI chat, checkout, properties and settings panels, team members.
|
|
447
|
+
* Event delegation on document, so blocks swapped in by htmx work without re-init.
|
|
448
|
+
* Sign-in [data-asta-reveal] shows and hides the password next to it
|
|
449
|
+
* Onboarding [data-asta-onboarding-toggle] opens one step, [data-asta-onboarding-review] shows the list again
|
|
450
|
+
* AI chat [data-asta-chat]: Enter sends, Shift+Enter adds a line, the person's message shows at once,
|
|
451
|
+
* the list follows new messages, [data-asta-chat-copy], [data-asta-chat-stream="url"] (SSE)
|
|
452
|
+
* Checkout [data-asta-checkout-format], [data-asta-checkout-step], "asta:recalc" on the summary, rolling amounts
|
|
453
|
+
* Properties [data-asta-prop-scrub] drag to scrub (Shift ×10, Alt ×0.1), [data-asta-prop-color] swatch + hex
|
|
454
|
+
* Settings [data-asta-settings]: data-dirty while fields differ from the loaded values, data-saved after a save
|
|
455
|
+
* Team Undo rows ([data-asta-team-removed]) fade out when their countdown ends
|
|
456
|
+
*/
|
|
457
|
+
(() => {
|
|
458
|
+
if (window.astaBlocks) return;
|
|
459
|
+
window.astaBlocks = true;
|
|
460
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
461
|
+
const EASE = "cubic-bezier(.16,1,.3,1)";
|
|
462
|
+
const el = (e) => (e.target instanceof Element ? e.target : null);
|
|
463
|
+
const shake = (node) => {
|
|
464
|
+
if (!node || reduce()) return;
|
|
465
|
+
node.animate(
|
|
466
|
+
[{ transform: "none" }, { transform: "translateX(-8px)" }, { transform: "translateX(7px)" }, { transform: "translateX(-4px)" }, { transform: "translateX(2px)" }, { transform: "none" }],
|
|
467
|
+
{ duration: 450, easing: EASE },
|
|
468
|
+
);
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
/* ---------- Sign-in: reveal password ---------- */
|
|
472
|
+
|
|
473
|
+
document.addEventListener("click", (e) => {
|
|
474
|
+
const btn = el(e)?.closest("[data-asta-reveal]");
|
|
475
|
+
if (!btn) return;
|
|
476
|
+
const input = btn.closest("label")?.querySelector("input");
|
|
477
|
+
if (!input) return;
|
|
478
|
+
const show = input.type === "password";
|
|
479
|
+
input.type = show ? "text" : "password";
|
|
480
|
+
btn.setAttribute("aria-pressed", String(show));
|
|
481
|
+
btn.setAttribute("aria-label", show ? "Hide password" : "Show password");
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
/* ---------- Onboarding ---------- */
|
|
485
|
+
|
|
486
|
+
document.addEventListener("click", (e) => {
|
|
487
|
+
const t = el(e);
|
|
488
|
+
const toggle = t?.closest("[data-asta-onboarding-toggle]");
|
|
489
|
+
if (toggle) {
|
|
490
|
+
const root = toggle.closest("[data-asta-onboarding]");
|
|
491
|
+
const open = toggle.getAttribute("aria-expanded") !== "true";
|
|
492
|
+
for (const b of root.querySelectorAll("[data-asta-onboarding-toggle]")) {
|
|
493
|
+
const on = open && b === toggle;
|
|
494
|
+
b.setAttribute("aria-expanded", String(on));
|
|
495
|
+
const panel = document.getElementById(b.getAttribute("aria-controls"));
|
|
496
|
+
if (panel) panel.hidden = !on;
|
|
497
|
+
b.closest("[data-asta-onboarding-step]")?.classList.toggle("bg-paper-2", on);
|
|
498
|
+
}
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const review = t?.closest("[data-asta-onboarding-review]");
|
|
502
|
+
if (review) {
|
|
503
|
+
const root = review.closest("[data-asta-onboarding]");
|
|
504
|
+
const done = root.querySelector("[data-asta-onboarding-done]");
|
|
505
|
+
if (done) done.hidden = true;
|
|
506
|
+
const list = root.querySelector("[data-asta-onboarding-list]");
|
|
507
|
+
if (list) {
|
|
508
|
+
list.hidden = false;
|
|
509
|
+
list.querySelector("[data-asta-onboarding-toggle]")?.focus();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
/* ---------- AI chat ---------- */
|
|
515
|
+
|
|
516
|
+
const chatOf = (n) => n?.closest?.("[data-asta-chat]");
|
|
517
|
+
const chatStatus = (chat, text) => {
|
|
518
|
+
const s = chat?.querySelector("[data-asta-chat-status]");
|
|
519
|
+
if (s) s.textContent = text;
|
|
520
|
+
};
|
|
521
|
+
// The list follows new content unless the reader scrolled up.
|
|
522
|
+
const unpinned = new WeakSet();
|
|
523
|
+
const follow = (chat, force) => {
|
|
524
|
+
const log = chat?.querySelector("[data-asta-chat-log]");
|
|
525
|
+
if (!log) return;
|
|
526
|
+
if (force) unpinned.delete(log);
|
|
527
|
+
if (!unpinned.has(log)) log.scrollTop = log.scrollHeight;
|
|
528
|
+
};
|
|
529
|
+
addEventListener(
|
|
530
|
+
"scroll",
|
|
531
|
+
(e) => {
|
|
532
|
+
const log = e.target instanceof Element && e.target.matches("[data-asta-chat-log]") ? e.target : null;
|
|
533
|
+
if (!log) return;
|
|
534
|
+
if (log.scrollHeight - log.scrollTop - log.clientHeight < 40) unpinned.delete(log);
|
|
535
|
+
else unpinned.add(log);
|
|
536
|
+
},
|
|
537
|
+
{ capture: true, passive: true },
|
|
538
|
+
);
|
|
539
|
+
const sendState = (chat) => {
|
|
540
|
+
const input = chat?.querySelector("[data-asta-chat-input]");
|
|
541
|
+
const send = chat?.querySelector("[data-asta-chat-send]");
|
|
542
|
+
if (input && send) send.disabled = !input.value.trim() || !!chat.querySelector("[data-asta-chat-placeholder]");
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
document.addEventListener("input", (e) => {
|
|
546
|
+
if (el(e)?.matches("[data-asta-chat-input]")) sendState(chatOf(e.target));
|
|
547
|
+
});
|
|
548
|
+
document.addEventListener("keydown", (e) => {
|
|
549
|
+
const input = el(e)?.closest("[data-asta-chat-input]");
|
|
550
|
+
if (!input || e.key !== "Enter" || e.shiftKey || e.isComposing) return;
|
|
551
|
+
e.preventDefault();
|
|
552
|
+
if (input.value.trim()) input.form?.requestSubmit();
|
|
553
|
+
});
|
|
554
|
+
document.addEventListener("click", (e) => {
|
|
555
|
+
const t = el(e);
|
|
556
|
+
const chip = t?.closest("[data-asta-chat-suggest]");
|
|
557
|
+
if (chip) {
|
|
558
|
+
const chat = chatOf(chip);
|
|
559
|
+
const input = chat?.querySelector("[data-asta-chat-input]");
|
|
560
|
+
if (!input) return;
|
|
561
|
+
input.value = chip.getAttribute("data-asta-chat-suggest");
|
|
562
|
+
input.form?.requestSubmit();
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
const copy = t?.closest("[data-asta-chat-copy]");
|
|
566
|
+
if (copy) {
|
|
567
|
+
const text = copy.closest("li")?.querySelector("[data-asta-chat-text]")?.textContent ?? "";
|
|
568
|
+
const chat = chatOf(copy);
|
|
569
|
+
navigator.clipboard?.writeText(text).then(
|
|
570
|
+
() => {
|
|
571
|
+
copy.setAttribute("data-copied", "");
|
|
572
|
+
copy.setAttribute("aria-label", "Copied");
|
|
573
|
+
chatStatus(chat, "Copied.");
|
|
574
|
+
clearTimeout(copy._astaT);
|
|
575
|
+
copy._astaT = setTimeout(() => {
|
|
576
|
+
copy.removeAttribute("data-copied");
|
|
577
|
+
copy.setAttribute("aria-label", "Copy");
|
|
578
|
+
}, 1600);
|
|
579
|
+
},
|
|
580
|
+
() => chatStatus(chat, "Couldn't copy."),
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
// Draw the person's message and a thinking row right away. The server answers with the reply only.
|
|
586
|
+
document.addEventListener("htmx:configRequest", (e) => {
|
|
587
|
+
const form = e.detail?.elt;
|
|
588
|
+
if (!(form instanceof Element) || !form.matches("[data-asta-chat-form]")) return;
|
|
589
|
+
const chat = chatOf(form);
|
|
590
|
+
const input = form.querySelector("[data-asta-chat-input]");
|
|
591
|
+
const text = input?.value.trim() ?? "";
|
|
592
|
+
if (!text || chat.querySelector("[data-asta-chat-placeholder]")) {
|
|
593
|
+
e.preventDefault();
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const list = chat.querySelector("[data-asta-chat-list]");
|
|
597
|
+
const tpl = chat.querySelector("template[data-asta-chat-pending]");
|
|
598
|
+
if (list && tpl) {
|
|
599
|
+
const frag = tpl.content.cloneNode(true);
|
|
600
|
+
const [user, reply] = frag.querySelectorAll("li");
|
|
601
|
+
user.querySelector("p").textContent = text;
|
|
602
|
+
reply.removeAttribute("data-asta-chat-stream");
|
|
603
|
+
reply.setAttribute("data-asta-chat-placeholder", "");
|
|
604
|
+
list.append(frag);
|
|
605
|
+
}
|
|
606
|
+
input.value = "";
|
|
607
|
+
sendState(chat);
|
|
608
|
+
chatStatus(chat, "Assistant is replying.");
|
|
609
|
+
follow(chat, true);
|
|
610
|
+
});
|
|
611
|
+
document.addEventListener("htmx:beforeSwap", (e) => {
|
|
612
|
+
const target = e.detail?.target;
|
|
613
|
+
if (target instanceof Element && target.matches("[data-asta-chat-list]") && e.detail.xhr?.status < 400) {
|
|
614
|
+
target.querySelector("[data-asta-chat-placeholder]")?.remove();
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
618
|
+
const form = e.detail?.elt;
|
|
619
|
+
if (!(form instanceof Element) || !form.matches("[data-asta-chat-form]")) return;
|
|
620
|
+
const chat = chatOf(form);
|
|
621
|
+
const pending = chat.querySelector("[data-asta-chat-placeholder]");
|
|
622
|
+
if (pending) {
|
|
623
|
+
// The request failed: keep the row and say so.
|
|
624
|
+
pending.removeAttribute("data-asta-chat-placeholder");
|
|
625
|
+
pending.removeAttribute("aria-busy");
|
|
626
|
+
pending.querySelector("[data-asta-chat-dots]")?.replaceWith(
|
|
627
|
+
Object.assign(document.createElement("p"), { className: "text-[12.5px] text-mute", textContent: "Something went wrong. Try again." }),
|
|
628
|
+
);
|
|
629
|
+
chatStatus(chat, "The reply failed.");
|
|
630
|
+
} else chatStatus(chat, "Reply received.");
|
|
631
|
+
sendState(chat);
|
|
632
|
+
follow(chat);
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
// Server-Sent Events: each message's data is appended to the reply; an event named "done" ends it.
|
|
636
|
+
function stream(li) {
|
|
637
|
+
const url = li.getAttribute("data-asta-chat-stream");
|
|
638
|
+
li.removeAttribute("data-asta-chat-stream");
|
|
639
|
+
const chat = chatOf(li);
|
|
640
|
+
const text = li.querySelector("[data-asta-chat-text]");
|
|
641
|
+
const es = new EventSource(url);
|
|
642
|
+
const end = (failed) => {
|
|
643
|
+
es.close();
|
|
644
|
+
li.removeAttribute("aria-busy");
|
|
645
|
+
li.querySelector("[data-asta-chat-dots]")?.remove();
|
|
646
|
+
if (failed && !text.textContent) {
|
|
647
|
+
text.classList.remove("hidden");
|
|
648
|
+
text.classList.add("text-mute");
|
|
649
|
+
text.textContent = "Something went wrong. Try again.";
|
|
650
|
+
}
|
|
651
|
+
chatStatus(chat, failed ? "The reply failed." : "Reply finished.");
|
|
652
|
+
follow(chat);
|
|
653
|
+
};
|
|
654
|
+
es.onmessage = (m) => {
|
|
655
|
+
li.querySelector("[data-asta-chat-dots]")?.remove();
|
|
656
|
+
text.classList.remove("hidden");
|
|
657
|
+
const span = document.createElement("span");
|
|
658
|
+
span.textContent = m.data;
|
|
659
|
+
if (!reduce()) span.style.animation = `asta-blk-tok .5s ${EASE}`;
|
|
660
|
+
text.append(span);
|
|
661
|
+
follow(chat);
|
|
662
|
+
};
|
|
663
|
+
es.addEventListener("done", () => end(false));
|
|
664
|
+
es.onerror = () => end(true);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/* ---------- Checkout ---------- */
|
|
668
|
+
|
|
669
|
+
const digits = (s) => s.replace(/\D/g, "");
|
|
670
|
+
const FORMAT = {
|
|
671
|
+
card: (d) => {
|
|
672
|
+
if (/^3[47]/.test(d)) {
|
|
673
|
+
const v = d.slice(0, 15);
|
|
674
|
+
return [v.slice(0, 4), v.slice(4, 10), v.slice(10)].filter(Boolean).join(" ");
|
|
675
|
+
}
|
|
676
|
+
return d.slice(0, 19).replace(/(\d{4})(?=\d)/g, "$1 ");
|
|
677
|
+
},
|
|
678
|
+
expiry: (d) => {
|
|
679
|
+
const v = d.slice(0, 4);
|
|
680
|
+
return v.length > 2 ? `${v.slice(0, 2)} / ${v.slice(2)}` : v;
|
|
681
|
+
},
|
|
682
|
+
cvc: (d) => d.slice(0, 4),
|
|
683
|
+
};
|
|
684
|
+
// Formatted fields keep the caret after the same number of digits.
|
|
685
|
+
document.addEventListener("input", (e) => {
|
|
686
|
+
const input = el(e)?.closest("[data-asta-checkout-format]");
|
|
687
|
+
const fmt = input && FORMAT[input.getAttribute("data-asta-checkout-format")];
|
|
688
|
+
if (!fmt) return;
|
|
689
|
+
const before = digits(input.value.slice(0, input.selectionStart ?? input.value.length)).length;
|
|
690
|
+
const out = fmt(digits(input.value));
|
|
691
|
+
input.value = out;
|
|
692
|
+
let pos = 0;
|
|
693
|
+
for (let seen = 0; pos < out.length && seen < before; pos++) if (/\d/.test(out[pos])) seen++;
|
|
694
|
+
if (document.activeElement === input) input.setSelectionRange(pos, pos);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
const recalc = (from) => {
|
|
698
|
+
const summary = from.closest("[data-asta-checkout]")?.querySelector("[data-asta-checkout-summary]");
|
|
699
|
+
if (!summary) return;
|
|
700
|
+
// Batch quick clicks into one request.
|
|
701
|
+
clearTimeout(summary._astaT);
|
|
702
|
+
summary._astaT = setTimeout(() => summary.dispatchEvent(new CustomEvent("asta:recalc", { bubbles: false })), 220);
|
|
703
|
+
};
|
|
704
|
+
document.addEventListener("click", (e) => {
|
|
705
|
+
const t = el(e);
|
|
706
|
+
const step = t?.closest("[data-asta-checkout-step]");
|
|
707
|
+
if (step) {
|
|
708
|
+
const box = step.closest("[data-asta-checkout-qty]");
|
|
709
|
+
const input = box.querySelector("input");
|
|
710
|
+
const q = Math.min(99, Math.max(1, (parseInt(input.value, 10) || 1) + parseInt(step.getAttribute("data-asta-checkout-step"), 10)));
|
|
711
|
+
input.value = String(q);
|
|
712
|
+
box.querySelector("[data-asta-checkout-qty-text]").textContent = String(q);
|
|
713
|
+
const [less, more] = box.querySelectorAll("[data-asta-checkout-step]");
|
|
714
|
+
less.disabled = q <= 1;
|
|
715
|
+
more.disabled = q >= 99;
|
|
716
|
+
recalc(step);
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
if (t?.closest("[data-asta-checkout-apply]")) return recalc(t);
|
|
720
|
+
const remove = t?.closest("[data-asta-checkout-promo-remove]");
|
|
721
|
+
if (remove) {
|
|
722
|
+
const hidden = remove.closest("[data-asta-checkout-summary]").querySelector("input[data-asta-checkout-promo]");
|
|
723
|
+
if (hidden) hidden.value = "";
|
|
724
|
+
recalc(remove);
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
document.addEventListener("change", (e) => {
|
|
728
|
+
if (el(e)?.matches("[data-asta-checkout-recalc]")) recalc(e.target);
|
|
729
|
+
});
|
|
730
|
+
// Enter in the promo field applies the code instead of paying.
|
|
731
|
+
document.addEventListener("keydown", (e) => {
|
|
732
|
+
if (e.key === "Enter" && el(e)?.matches("input[data-asta-checkout-promo]")) {
|
|
733
|
+
e.preventDefault();
|
|
734
|
+
recalc(e.target);
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
// Amounts whose text changed roll in from the direction the value moved.
|
|
739
|
+
const amounts = new Map();
|
|
740
|
+
const amount = (s) => parseFloat(s.replace(/[^\d.-]/g, "")) || 0;
|
|
741
|
+
document.addEventListener("htmx:beforeSwap", (e) => {
|
|
742
|
+
const t = e.detail?.target;
|
|
743
|
+
if (!(t instanceof Element) || !t.matches("[data-asta-checkout-summary]")) return;
|
|
744
|
+
const m = {};
|
|
745
|
+
for (const a of t.querySelectorAll("[data-asta-money]")) m[a.getAttribute("data-asta-money")] = a.textContent.trim();
|
|
746
|
+
amounts.set(t.id, m);
|
|
747
|
+
});
|
|
748
|
+
function roll(root) {
|
|
749
|
+
for (const [id, old] of amounts) {
|
|
750
|
+
const s = document.getElementById(id);
|
|
751
|
+
if (!s || !root.contains(s)) continue;
|
|
752
|
+
amounts.delete(id);
|
|
753
|
+
if (reduce()) continue;
|
|
754
|
+
for (const a of s.querySelectorAll("[data-asta-money]")) {
|
|
755
|
+
const prev = old[a.getAttribute("data-asta-money")];
|
|
756
|
+
const now = a.textContent.trim();
|
|
757
|
+
if (prev === undefined || prev === now) continue;
|
|
758
|
+
const up = amount(now) >= amount(prev);
|
|
759
|
+
a.firstElementChild?.animate([{ opacity: 0, transform: `translateY(${up ? 70 : -70}%)` }, { opacity: 1, transform: "none" }], { duration: 500, easing: EASE });
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/* ---------- Properties panel ---------- */
|
|
765
|
+
|
|
766
|
+
const num = (input) => {
|
|
767
|
+
const d = input.dataset;
|
|
768
|
+
return { step: parseFloat(d.step) || 1, prec: parseInt(d.precision, 10) || 0, min: d.min === undefined ? -Infinity : +d.min, max: d.max === undefined ? Infinity : +d.max };
|
|
769
|
+
};
|
|
770
|
+
const setNum = (input, v) => {
|
|
771
|
+
const o = num(input);
|
|
772
|
+
const f = 10 ** o.prec;
|
|
773
|
+
input.value = (Math.min(o.max, Math.max(o.min, Math.round(v * f) / f))).toFixed(o.prec);
|
|
774
|
+
};
|
|
775
|
+
const factor = (e) => (e.shiftKey ? 10 : e.altKey ? 0.1 : 1);
|
|
776
|
+
const fire = (input, type) => input.dispatchEvent(new Event(type, { bubbles: true }));
|
|
777
|
+
let drag = null;
|
|
778
|
+
document.addEventListener("pointerdown", (e) => {
|
|
779
|
+
const handle = el(e)?.closest("[data-asta-prop-handle]");
|
|
780
|
+
if (!handle || e.button !== 0) return;
|
|
781
|
+
const box = handle.closest("[data-asta-prop-scrub]");
|
|
782
|
+
const input = box.querySelector("input");
|
|
783
|
+
if (input.disabled) return;
|
|
784
|
+
e.preventDefault();
|
|
785
|
+
handle.setPointerCapture(e.pointerId);
|
|
786
|
+
drag = { handle, box, input, x: e.clientX, v: parseFloat(input.value) || 0, moved: false, start: input.value };
|
|
787
|
+
box.setAttribute("data-scrubbing", "");
|
|
788
|
+
});
|
|
789
|
+
document.addEventListener("pointermove", (e) => {
|
|
790
|
+
if (!drag) return;
|
|
791
|
+
const dx = e.clientX - drag.x;
|
|
792
|
+
if (Math.abs(dx) > 2) drag.moved = true;
|
|
793
|
+
if (!drag.moved) return;
|
|
794
|
+
setNum(drag.input, drag.v + Math.round(dx / 2) * num(drag.input).step * factor(e));
|
|
795
|
+
fire(drag.input, "input");
|
|
796
|
+
});
|
|
797
|
+
const endDrag = () => {
|
|
798
|
+
if (!drag) return;
|
|
799
|
+
const d = drag;
|
|
800
|
+
drag = null;
|
|
801
|
+
d.box.removeAttribute("data-scrubbing");
|
|
802
|
+
if (!d.moved) {
|
|
803
|
+
d.input.focus();
|
|
804
|
+
d.input.select();
|
|
805
|
+
} else if (d.input.value !== d.start) fire(d.input, "change");
|
|
806
|
+
};
|
|
807
|
+
document.addEventListener("pointerup", endDrag);
|
|
808
|
+
document.addEventListener("pointercancel", endDrag);
|
|
809
|
+
|
|
810
|
+
document.addEventListener("focusin", (e) => {
|
|
811
|
+
const input = el(e)?.matches("[data-asta-prop-scrub] input, input[data-asta-prop-hex]") ? e.target : null;
|
|
812
|
+
if (!input) return;
|
|
813
|
+
input.dataset.prev = input.value;
|
|
814
|
+
input.select();
|
|
815
|
+
});
|
|
816
|
+
document.addEventListener("keydown", (e) => {
|
|
817
|
+
const input = el(e)?.matches("[data-asta-prop-scrub] input, input[data-asta-prop-hex]") ? e.target : null;
|
|
818
|
+
if (!input) return;
|
|
819
|
+
if (e.key === "Enter") {
|
|
820
|
+
// Commit with a change event; don't submit the panel's form.
|
|
821
|
+
e.preventDefault();
|
|
822
|
+
input.blur();
|
|
823
|
+
} else if (e.key === "Escape") {
|
|
824
|
+
input.value = input.dataset.prev ?? input.value;
|
|
825
|
+
input.blur();
|
|
826
|
+
} else if ((e.key === "ArrowUp" || e.key === "ArrowDown") && input.closest("[data-asta-prop-scrub]")) {
|
|
827
|
+
e.preventDefault();
|
|
828
|
+
setNum(input, (parseFloat(input.value) || 0) + (e.key === "ArrowUp" ? 1 : -1) * num(input).step * factor(e));
|
|
829
|
+
input.dataset.prev = input.value;
|
|
830
|
+
fire(input, "input");
|
|
831
|
+
fire(input, "change");
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
const HEX = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
836
|
+
const normHex = (v) => {
|
|
837
|
+
const m = v.trim().match(HEX);
|
|
838
|
+
if (!m) return null;
|
|
839
|
+
return `#${(m[1].length === 3 ? m[1].replace(/./g, (c) => c + c) : m[1]).toLowerCase()}`;
|
|
840
|
+
};
|
|
841
|
+
const paint = (box, hex) => {
|
|
842
|
+
const sw = box.querySelector("[data-asta-prop-swatch]");
|
|
843
|
+
if (sw) sw.style.background = hex;
|
|
844
|
+
const picker = box.querySelector('input[type="color"]');
|
|
845
|
+
if (picker) picker.value = hex;
|
|
846
|
+
const text = box.querySelector("input[data-asta-prop-hex]");
|
|
847
|
+
if (text) text.value = hex;
|
|
848
|
+
};
|
|
849
|
+
document.addEventListener("input", (e) => {
|
|
850
|
+
const picker = el(e)?.matches('[data-asta-prop-color] input[type="color"]') ? e.target : null;
|
|
851
|
+
if (picker) paint(picker.closest("[data-asta-prop-color]"), picker.value);
|
|
852
|
+
});
|
|
853
|
+
// Capture runs before htmx's listener on the form, so the server gets the cleaned value.
|
|
854
|
+
document.addEventListener(
|
|
855
|
+
"change",
|
|
856
|
+
(e) => {
|
|
857
|
+
const t = el(e);
|
|
858
|
+
if (t?.matches("[data-asta-prop-scrub] input")) {
|
|
859
|
+
const v = parseFloat(t.value.replace(",", "."));
|
|
860
|
+
if (Number.isFinite(v)) setNum(t, v);
|
|
861
|
+
else t.value = t.dataset.prev ?? "0";
|
|
862
|
+
} else if (t?.matches("input[data-asta-prop-hex]")) {
|
|
863
|
+
const box = t.closest("[data-asta-prop-color]");
|
|
864
|
+
paint(box, normHex(t.value) ?? normHex(t.dataset.prev ?? "") ?? box.querySelector('input[type="color"]').value);
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
true,
|
|
868
|
+
);
|
|
869
|
+
|
|
870
|
+
/* ---------- Settings panel ---------- */
|
|
871
|
+
|
|
872
|
+
const baseline = new WeakMap();
|
|
873
|
+
const serial = (form) => new URLSearchParams(new FormData(form)).toString();
|
|
874
|
+
const barButtons = (form, on) => {
|
|
875
|
+
for (const b of form.querySelectorAll("[data-asta-settings-bar] button")) b.tabIndex = on ? 0 : -1;
|
|
876
|
+
};
|
|
877
|
+
function dirty(form) {
|
|
878
|
+
if (!baseline.has(form)) return;
|
|
879
|
+
const on = serial(form) !== baseline.get(form);
|
|
880
|
+
form.toggleAttribute("data-dirty", on);
|
|
881
|
+
if (on) {
|
|
882
|
+
form.removeAttribute("data-saved");
|
|
883
|
+
clearTimeout(form._astaT);
|
|
884
|
+
}
|
|
885
|
+
barButtons(form, on);
|
|
886
|
+
}
|
|
887
|
+
const settingsForm = (t) => t?.closest("[data-asta-settings]:not([data-autosave])");
|
|
888
|
+
document.addEventListener("input", (e) => {
|
|
889
|
+
const f = settingsForm(el(e));
|
|
890
|
+
if (f) dirty(f);
|
|
891
|
+
});
|
|
892
|
+
document.addEventListener("change", (e) => {
|
|
893
|
+
const f = settingsForm(el(e));
|
|
894
|
+
if (f) dirty(f);
|
|
895
|
+
});
|
|
896
|
+
// "reset" fires before the fields change back.
|
|
897
|
+
document.addEventListener("reset", (e) => {
|
|
898
|
+
const f = settingsForm(el(e));
|
|
899
|
+
if (f) setTimeout(() => dirty(f));
|
|
900
|
+
});
|
|
901
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
902
|
+
const f = e.detail?.elt instanceof Element && e.detail.elt.matches("[data-asta-settings]:not([data-autosave])") ? e.detail.elt : null;
|
|
903
|
+
if (!f || !f.isConnected) return;
|
|
904
|
+
if (!e.detail.successful) {
|
|
905
|
+
shake(f.querySelector("[data-asta-settings-bar] > *"));
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
// The saved values become what Reset goes back to.
|
|
909
|
+
for (const i of f.elements) {
|
|
910
|
+
if (i.type === "checkbox" || i.type === "radio") i.defaultChecked = i.checked;
|
|
911
|
+
else if (i.tagName === "SELECT") for (const o of i.options) o.defaultSelected = o.selected;
|
|
912
|
+
else if ("defaultValue" in i) i.defaultValue = i.value;
|
|
913
|
+
}
|
|
914
|
+
baseline.set(f, serial(f));
|
|
915
|
+
f.removeAttribute("data-dirty");
|
|
916
|
+
f.setAttribute("data-saved", "");
|
|
917
|
+
barButtons(f, false);
|
|
918
|
+
clearTimeout(f._astaT);
|
|
919
|
+
f._astaT = setTimeout(() => f.removeAttribute("data-saved"), 1400);
|
|
920
|
+
});
|
|
921
|
+
addEventListener("beforeunload", (e) => {
|
|
922
|
+
const f = document.querySelector("[data-asta-settings][data-dirty]");
|
|
923
|
+
if (f) {
|
|
924
|
+
shake(f.querySelector("[data-asta-settings-bar] > *"));
|
|
925
|
+
e.preventDefault();
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
|
|
929
|
+
/* ---------- Boot and htmx ---------- */
|
|
930
|
+
|
|
931
|
+
function scan(root) {
|
|
932
|
+
for (const chat of root.querySelectorAll("[data-asta-chat]")) {
|
|
933
|
+
sendState(chat);
|
|
934
|
+
follow(chat, !chat.dataset.astaSeen);
|
|
935
|
+
chat.dataset.astaSeen = "";
|
|
936
|
+
}
|
|
937
|
+
if (root.closest?.("[data-asta-chat]")) follow(chatOf(root));
|
|
938
|
+
for (const li of root.querySelectorAll("li[data-asta-chat-stream]")) stream(li);
|
|
939
|
+
for (const f of root.querySelectorAll("[data-asta-settings]:not([data-autosave])")) {
|
|
940
|
+
if (!baseline.has(f)) baseline.set(f, serial(f));
|
|
941
|
+
}
|
|
942
|
+
for (const row of root.querySelectorAll("[data-asta-team-removed]:not([data-timed])")) {
|
|
943
|
+
row.setAttribute("data-timed", "");
|
|
944
|
+
const secs = parseFloat(row.querySelector("[data-asta-team-countdown]")?.style.animationDuration) || 5;
|
|
945
|
+
setTimeout(() => {
|
|
946
|
+
row.setAttribute("data-gone", "");
|
|
947
|
+
setTimeout(() => row.closest("li")?.remove(), reduce() ? 0 : 300);
|
|
948
|
+
}, secs * 1000);
|
|
949
|
+
}
|
|
950
|
+
roll(root);
|
|
951
|
+
}
|
|
952
|
+
const boot = () => scan(document);
|
|
953
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
954
|
+
else boot();
|
|
955
|
+
document.addEventListener("htmx:afterSettle", (e) => scan(e.detail?.elt?.parentElement || document));
|
|
956
|
+
})();
|
|
957
|
+
|
|
958
|
+
/* ---- src/data.js ---- */
|
|
959
|
+
/* Data components for Astabyte UI for Go. Event delegation on document, so htmx swaps need no re-init.
|
|
960
|
+
* DataTable [data-asta-dt] with [data-asta-dt-all], [data-asta-dt-row], [data-asta-dt-clear]
|
|
961
|
+
* TreeView [data-asta-tree] with [role=treeitem] and [data-asta-tree-row]; fires asta:select, asta:expand
|
|
962
|
+
* Timeline [data-asta-timeline][data-inview]
|
|
963
|
+
* Kanban [data-asta-kanban] with [data-asta-kanban-list] and [data-asta-kanban-card]; fires asta:move
|
|
964
|
+
* EventCalendar [data-asta-cal] with [data-day] cells and [data-asta-cal-panel]; fires asta:select
|
|
965
|
+
* FileCard [data-asta-file-star]; fires asta:star
|
|
966
|
+
* ImageCompare [data-asta-compare] with [data-asta-compare-handle]; fires asta:change
|
|
967
|
+
* Carousel [data-asta-carousel] with [data-asta-carousel-track|dot|step]; fires asta:slide
|
|
968
|
+
* NotificationCenter [data-asta-nc] with [data-asta-nc-trigger|panel|tab|item|dot|all]
|
|
969
|
+
* Comments [data-asta-comments] with [data-asta-comment-form|reply|like|cancel]; fires asta:like
|
|
970
|
+
* MotionButton [data-asta-mb="fill|shine|beam|copy|confirm"]; confirm fires asta:confirm
|
|
971
|
+
*/
|
|
972
|
+
(() => {
|
|
973
|
+
if (window.__astaData) return;
|
|
974
|
+
window.__astaData = true;
|
|
975
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
976
|
+
const CURVE = "cubic-bezier(.16,1,.3,1)";
|
|
977
|
+
const all = (el, s) => [...el.querySelectorAll(s)];
|
|
978
|
+
const el = (e) => (e.target instanceof Element ? e.target : null);
|
|
979
|
+
const fire = (node, name, detail) => node.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
980
|
+
// Restart a CSS animation that hangs off an attribute.
|
|
981
|
+
const replay = (node, attr) => {
|
|
982
|
+
node.removeAttribute(attr);
|
|
983
|
+
void node.offsetWidth;
|
|
984
|
+
node.setAttribute(attr, "");
|
|
985
|
+
};
|
|
986
|
+
// Cards glide from where they were to where they are now (FLIP, transform only).
|
|
987
|
+
function flip(nodes, mutate) {
|
|
988
|
+
const before = new Map(nodes.map((n) => [n, n.getBoundingClientRect()]));
|
|
989
|
+
mutate();
|
|
990
|
+
if (reduce()) return;
|
|
991
|
+
for (const [n, r] of before) {
|
|
992
|
+
if (!n.isConnected) continue;
|
|
993
|
+
const now = n.getBoundingClientRect();
|
|
994
|
+
const dx = r.left - now.left;
|
|
995
|
+
const dy = r.top - now.top;
|
|
996
|
+
if (dx || dy) n.animate([{ transform: `translate(${dx}px, ${dy}px)` }, { transform: "none" }], { duration: 340, easing: CURVE });
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
// The scan runs on load and after every htmx settle, for the bits that need a first look.
|
|
1000
|
+
const scans = [];
|
|
1001
|
+
const scan = (root) => scans.forEach((f) => f(root));
|
|
1002
|
+
document.addEventListener("htmx:afterSettle", (e) => {
|
|
1003
|
+
const t = e.detail?.elt;
|
|
1004
|
+
scan(t && t.isConnected ? t.parentElement || document : document);
|
|
1005
|
+
});
|
|
1006
|
+
const within = (root, sel) => [...(root.matches?.(sel) ? [root] : []), ...all(root, sel)];
|
|
1007
|
+
|
|
1008
|
+
/* ---------- DataTable ---------- */
|
|
1009
|
+
|
|
1010
|
+
function dtSync(root) {
|
|
1011
|
+
const rows = all(root, "[data-asta-dt-row]");
|
|
1012
|
+
const head = root.querySelector("[data-asta-dt-all]");
|
|
1013
|
+
const n = rows.filter((r) => r.checked).length;
|
|
1014
|
+
if (head) {
|
|
1015
|
+
const mixed = n > 0 && n < rows.length;
|
|
1016
|
+
head.checked = n > 0 && n === rows.length;
|
|
1017
|
+
head.indeterminate = mixed;
|
|
1018
|
+
head.toggleAttribute("data-indeterminate", mixed);
|
|
1019
|
+
head.disabled = rows.length === 0;
|
|
1020
|
+
head.setAttribute("aria-label", head.checked ? "Deselect all rows on this page" : "Select all rows on this page");
|
|
1021
|
+
}
|
|
1022
|
+
const picked = root.querySelector("[data-asta-dt-picked]");
|
|
1023
|
+
const range = root.querySelector("[data-asta-dt-range]");
|
|
1024
|
+
if (picked && range) {
|
|
1025
|
+
picked.hidden = n === 0;
|
|
1026
|
+
range.hidden = n > 0;
|
|
1027
|
+
const c = picked.querySelector("[data-asta-dt-count]");
|
|
1028
|
+
if (c) c.textContent = String(n);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
scans.push((root) => within(root, "[data-asta-dt]").forEach(dtSync));
|
|
1032
|
+
document.addEventListener("change", (e) => {
|
|
1033
|
+
const t = el(e);
|
|
1034
|
+
const root = t?.closest("[data-asta-dt]");
|
|
1035
|
+
if (!root) return;
|
|
1036
|
+
if (t.matches("[data-asta-dt-all]")) for (const r of all(root, "[data-asta-dt-row]")) r.checked = t.checked;
|
|
1037
|
+
if (t.matches("[data-asta-dt-all], [data-asta-dt-row]")) dtSync(root);
|
|
1038
|
+
});
|
|
1039
|
+
|
|
1040
|
+
/* ---------- TreeView ---------- */
|
|
1041
|
+
|
|
1042
|
+
const treeOf = (li) => li.closest("[data-asta-tree]");
|
|
1043
|
+
const rowOf = (li) => li.querySelector(":scope > [data-asta-tree-row]");
|
|
1044
|
+
const isFolder = (li) => li.hasAttribute("aria-expanded");
|
|
1045
|
+
const isOpen = (li) => li.getAttribute("aria-expanded") === "true";
|
|
1046
|
+
const off = (li) => li.getAttribute("aria-disabled") === "true";
|
|
1047
|
+
function treeVisible(tree) {
|
|
1048
|
+
return all(tree, '[role="treeitem"]').filter((li) => {
|
|
1049
|
+
for (let g = li.parentElement; g && g !== tree; g = g.parentElement) if (g.hidden) return false;
|
|
1050
|
+
return true;
|
|
1051
|
+
});
|
|
1052
|
+
}
|
|
1053
|
+
function treeFocus(li) {
|
|
1054
|
+
if (!li) return;
|
|
1055
|
+
for (const x of all(treeOf(li), '[role="treeitem"][tabindex="0"]')) x.tabIndex = -1;
|
|
1056
|
+
li.tabIndex = 0;
|
|
1057
|
+
li.focus();
|
|
1058
|
+
}
|
|
1059
|
+
function treeOpen(li, open, animate = true) {
|
|
1060
|
+
if (!isFolder(li) || off(li) || isOpen(li) === open) return;
|
|
1061
|
+
li.setAttribute("aria-expanded", String(open));
|
|
1062
|
+
rowOf(li)?.toggleAttribute("data-open", open);
|
|
1063
|
+
const g = li.querySelector(':scope > [role="group"]');
|
|
1064
|
+
if (g) {
|
|
1065
|
+
g.hidden = !open;
|
|
1066
|
+
if (open) {
|
|
1067
|
+
if (animate && !reduce()) replay(g, "data-opened");
|
|
1068
|
+
fire(g, "asta:expand");
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
// Closing a folder that hides the focused row moves focus up to the folder.
|
|
1072
|
+
if (!open && li.contains(document.activeElement) && document.activeElement !== li) treeFocus(li);
|
|
1073
|
+
}
|
|
1074
|
+
function treeSelect(li) {
|
|
1075
|
+
if (off(li)) return;
|
|
1076
|
+
const tree = treeOf(li);
|
|
1077
|
+
for (const x of all(tree, '[role="treeitem"][aria-selected="true"]')) {
|
|
1078
|
+
x.setAttribute("aria-selected", "false");
|
|
1079
|
+
rowOf(x)?.removeAttribute("data-selected");
|
|
1080
|
+
}
|
|
1081
|
+
li.setAttribute("aria-selected", "true");
|
|
1082
|
+
rowOf(li)?.setAttribute("data-selected", "");
|
|
1083
|
+
const input = tree.querySelector(":scope > [data-asta-tree-value]");
|
|
1084
|
+
if (input) {
|
|
1085
|
+
input.value = li.dataset.id;
|
|
1086
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1087
|
+
}
|
|
1088
|
+
fire(li, "asta:select", { id: li.dataset.id });
|
|
1089
|
+
}
|
|
1090
|
+
scans.push((root) => {
|
|
1091
|
+
for (const g of within(root, "[data-asta-tree-lazy]")) if (!g.hidden) fire(g, "asta:expand");
|
|
1092
|
+
});
|
|
1093
|
+
document.addEventListener("click", (e) => {
|
|
1094
|
+
const row = el(e)?.closest("[data-asta-tree-row]");
|
|
1095
|
+
if (!row) return;
|
|
1096
|
+
const li = row.parentElement;
|
|
1097
|
+
if (off(li)) return;
|
|
1098
|
+
treeFocus(li);
|
|
1099
|
+
// The chevron toggles without selecting.
|
|
1100
|
+
if (el(e).closest("[data-asta-tree-chevron]") && isFolder(li)) return treeOpen(li, !isOpen(li));
|
|
1101
|
+
treeSelect(li);
|
|
1102
|
+
if (isFolder(li)) treeOpen(li, !isOpen(li));
|
|
1103
|
+
});
|
|
1104
|
+
let typed = { text: "", at: 0 };
|
|
1105
|
+
document.addEventListener("keydown", (e) => {
|
|
1106
|
+
const li = el(e);
|
|
1107
|
+
if (!li?.matches('[data-asta-tree] [role="treeitem"]')) return;
|
|
1108
|
+
const tree = treeOf(li);
|
|
1109
|
+
const rows = treeVisible(tree);
|
|
1110
|
+
const i = rows.indexOf(li);
|
|
1111
|
+
const parent = li.parentElement.closest('[role="treeitem"]');
|
|
1112
|
+
let handled = true;
|
|
1113
|
+
switch (e.key) {
|
|
1114
|
+
case "ArrowDown":
|
|
1115
|
+
treeFocus(rows[i + 1]);
|
|
1116
|
+
break;
|
|
1117
|
+
case "ArrowUp":
|
|
1118
|
+
treeFocus(rows[i - 1]);
|
|
1119
|
+
break;
|
|
1120
|
+
case "Home":
|
|
1121
|
+
treeFocus(rows[0]);
|
|
1122
|
+
break;
|
|
1123
|
+
case "End":
|
|
1124
|
+
treeFocus(rows[rows.length - 1]);
|
|
1125
|
+
break;
|
|
1126
|
+
case "ArrowRight":
|
|
1127
|
+
if (isFolder(li) && !off(li)) {
|
|
1128
|
+
if (!isOpen(li)) treeOpen(li, true);
|
|
1129
|
+
else treeFocus(li.querySelector(':scope > [role="group"] > [role="treeitem"]'));
|
|
1130
|
+
}
|
|
1131
|
+
break;
|
|
1132
|
+
case "ArrowLeft":
|
|
1133
|
+
if (isFolder(li) && isOpen(li)) treeOpen(li, false);
|
|
1134
|
+
else if (parent && tree.contains(parent)) treeFocus(parent);
|
|
1135
|
+
break;
|
|
1136
|
+
case "Enter":
|
|
1137
|
+
rowOf(li)?.click();
|
|
1138
|
+
break;
|
|
1139
|
+
case " ":
|
|
1140
|
+
treeSelect(li);
|
|
1141
|
+
break;
|
|
1142
|
+
case "*": {
|
|
1143
|
+
const group = li.parentElement;
|
|
1144
|
+
for (const s of all(group, ':scope > [role="treeitem"]')) treeOpen(s, true);
|
|
1145
|
+
break;
|
|
1146
|
+
}
|
|
1147
|
+
default:
|
|
1148
|
+
handled = false;
|
|
1149
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
1150
|
+
const k = e.key.toLowerCase();
|
|
1151
|
+
typed = { text: e.timeStamp - typed.at > 600 ? k : typed.text + k, at: e.timeStamp };
|
|
1152
|
+
const start = typed.text.length === 1 ? i + 1 : i;
|
|
1153
|
+
const order = [...rows.slice(start), ...rows.slice(0, start)];
|
|
1154
|
+
const hit = order.find((r) => (rowOf(r)?.textContent || "").trim().toLowerCase().startsWith(typed.text));
|
|
1155
|
+
if (hit) {
|
|
1156
|
+
treeFocus(hit);
|
|
1157
|
+
handled = true;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
if (handled) e.preventDefault();
|
|
1162
|
+
});
|
|
1163
|
+
|
|
1164
|
+
/* ---------- Timeline ---------- */
|
|
1165
|
+
|
|
1166
|
+
const seen = new IntersectionObserver(
|
|
1167
|
+
(entries) => {
|
|
1168
|
+
for (const en of entries) {
|
|
1169
|
+
if (!en.isIntersecting) continue;
|
|
1170
|
+
en.target.dataset.inview = "true";
|
|
1171
|
+
seen.unobserve(en.target);
|
|
1172
|
+
}
|
|
1173
|
+
},
|
|
1174
|
+
{ threshold: 0.2 },
|
|
1175
|
+
);
|
|
1176
|
+
scans.push((root) => within(root, '[data-asta-timeline][data-inview="false"]').forEach((t) => seen.observe(t)));
|
|
1177
|
+
|
|
1178
|
+
/* ---------- Kanban ---------- */
|
|
1179
|
+
|
|
1180
|
+
const kbCards = (root) => all(root, "[data-asta-kanban-card]");
|
|
1181
|
+
const kbListOf = (root, col) => root.querySelector(`[data-asta-kanban-col="${CSS.escape(col)}"] [data-asta-kanban-list]`);
|
|
1182
|
+
function kbWhere(card) {
|
|
1183
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
1184
|
+
const list = all(col, "[data-asta-kanban-card]");
|
|
1185
|
+
return `${col.getAttribute("aria-label")}, position ${list.indexOf(card) + 1} of ${list.length}`;
|
|
1186
|
+
}
|
|
1187
|
+
function kbSay(root, msg) {
|
|
1188
|
+
const s = root.querySelector("[data-asta-kanban-status]");
|
|
1189
|
+
if (s) s.textContent = msg;
|
|
1190
|
+
}
|
|
1191
|
+
function kbCounts(root) {
|
|
1192
|
+
for (const col of all(root, "[data-asta-kanban-col]")) {
|
|
1193
|
+
const c = col.querySelector("[data-asta-kanban-count]");
|
|
1194
|
+
const n = String(col.querySelectorAll("[data-asta-kanban-card]").length);
|
|
1195
|
+
if (c && c.textContent !== n) {
|
|
1196
|
+
c.textContent = n;
|
|
1197
|
+
if (!reduce()) replay(c, "data-bump");
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
// Save a move: fill the hidden fields and fire asta:move, which the board's hx-post listens for.
|
|
1202
|
+
function kbCommit(root, card) {
|
|
1203
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
1204
|
+
const detail = {
|
|
1205
|
+
card: card.dataset.astaKanbanCard,
|
|
1206
|
+
column: col.dataset.astaKanbanCol,
|
|
1207
|
+
position: all(col, "[data-asta-kanban-card]").indexOf(card),
|
|
1208
|
+
};
|
|
1209
|
+
for (const [k, v] of Object.entries(detail)) {
|
|
1210
|
+
const f = root.querySelector(`[data-asta-kanban-field="${k}"]`);
|
|
1211
|
+
if (f) f.value = String(v);
|
|
1212
|
+
}
|
|
1213
|
+
kbCounts(root);
|
|
1214
|
+
kbSay(root, `Dropped ${card.dataset.title} in ${kbWhere(card)}.`);
|
|
1215
|
+
fire(root, "asta:move", detail);
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
let drag = null;
|
|
1219
|
+
const gap = document.createElement("li");
|
|
1220
|
+
gap.setAttribute("data-asta-kanban-gap", "");
|
|
1221
|
+
gap.setAttribute("aria-hidden", "true");
|
|
1222
|
+
document.addEventListener("dragstart", (e) => {
|
|
1223
|
+
const card = el(e)?.closest("[data-asta-kanban-card]");
|
|
1224
|
+
if (!card) return;
|
|
1225
|
+
const root = card.closest("[data-asta-kanban]");
|
|
1226
|
+
drag = { card, root, h: card.offsetHeight, dropped: false };
|
|
1227
|
+
e.dataTransfer.effectAllowed = "move";
|
|
1228
|
+
e.dataTransfer.setData("text/plain", card.dataset.astaKanbanCard);
|
|
1229
|
+
gap.style.height = `${drag.h}px`;
|
|
1230
|
+
// Let the browser take its drag picture first, then fade the original.
|
|
1231
|
+
requestAnimationFrame(() => card.setAttribute("data-dragging", ""));
|
|
1232
|
+
});
|
|
1233
|
+
document.addEventListener("dragover", (e) => {
|
|
1234
|
+
if (!drag) return;
|
|
1235
|
+
const col = el(e)?.closest("[data-asta-kanban-col]");
|
|
1236
|
+
if (!col || !drag.root.contains(col)) return;
|
|
1237
|
+
e.preventDefault();
|
|
1238
|
+
e.dataTransfer.dropEffect = "move";
|
|
1239
|
+
const list = col.querySelector("[data-asta-kanban-list]");
|
|
1240
|
+
const cards = all(list, "[data-asta-kanban-card]").filter((c) => c !== drag.card);
|
|
1241
|
+
// The first card whose middle is below the pointer.
|
|
1242
|
+
const before = cards.find((c) => {
|
|
1243
|
+
const r = c.getBoundingClientRect();
|
|
1244
|
+
return e.clientY < r.top + r.height / 2;
|
|
1245
|
+
});
|
|
1246
|
+
if (gap.parentElement === list && gap.nextElementSibling === (before ?? null)) return;
|
|
1247
|
+
if (!before && gap.parentElement === list && !gap.nextElementSibling) return;
|
|
1248
|
+
const moving = [...new Set([...kbCards(drag.root)].filter((c) => c !== drag.card))];
|
|
1249
|
+
flip(moving, () => {
|
|
1250
|
+
if (drag.card.parentElement) drag.card.hidden = true;
|
|
1251
|
+
list.insertBefore(gap, before ?? null);
|
|
1252
|
+
});
|
|
1253
|
+
});
|
|
1254
|
+
document.addEventListener("drop", (e) => {
|
|
1255
|
+
if (!drag || !gap.parentElement) return;
|
|
1256
|
+
e.preventDefault();
|
|
1257
|
+
const { card, root } = drag;
|
|
1258
|
+
gap.replaceWith(card);
|
|
1259
|
+
card.hidden = false;
|
|
1260
|
+
drag.dropped = true;
|
|
1261
|
+
kbCommit(root, card);
|
|
1262
|
+
});
|
|
1263
|
+
document.addEventListener("dragend", () => {
|
|
1264
|
+
if (!drag) return;
|
|
1265
|
+
const { card, root, dropped } = drag;
|
|
1266
|
+
if (!dropped) {
|
|
1267
|
+
const moving = kbCards(root).filter((c) => c !== card);
|
|
1268
|
+
flip(moving, () => {
|
|
1269
|
+
gap.remove();
|
|
1270
|
+
card.hidden = false;
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
gap.remove();
|
|
1274
|
+
card.hidden = false;
|
|
1275
|
+
card.removeAttribute("data-dragging");
|
|
1276
|
+
drag = null;
|
|
1277
|
+
});
|
|
1278
|
+
|
|
1279
|
+
// Keyboard: Space picks up, arrows move, Space drops, Escape cancels.
|
|
1280
|
+
let held = null;
|
|
1281
|
+
let moving = false;
|
|
1282
|
+
function kbDrop(cancel, focus) {
|
|
1283
|
+
if (!held) return;
|
|
1284
|
+
const { card, root, from, next } = held;
|
|
1285
|
+
held = null;
|
|
1286
|
+
card.removeAttribute("data-picked");
|
|
1287
|
+
if (cancel) {
|
|
1288
|
+
moving = true;
|
|
1289
|
+
flip(kbCards(root), () => from.insertBefore(card, next && next.parentElement === from ? next : null));
|
|
1290
|
+
moving = false;
|
|
1291
|
+
kbCounts(root);
|
|
1292
|
+
if (focus) card.focus();
|
|
1293
|
+
kbSay(root, `Cancelled. ${card.dataset.title} is back in ${kbWhere(card)}.`);
|
|
1294
|
+
} else kbCommit(root, card);
|
|
1295
|
+
}
|
|
1296
|
+
document.addEventListener("keydown", (e) => {
|
|
1297
|
+
const card = el(e);
|
|
1298
|
+
if (!card?.matches("[data-asta-kanban-card]")) return;
|
|
1299
|
+
const root = card.closest("[data-asta-kanban]");
|
|
1300
|
+
if (e.key === " " || e.key === "Enter") {
|
|
1301
|
+
e.preventDefault();
|
|
1302
|
+
if (held?.card === card) kbDrop(false);
|
|
1303
|
+
else if (!held) {
|
|
1304
|
+
held = { card, root, from: card.parentElement, next: card.nextElementSibling };
|
|
1305
|
+
card.setAttribute("data-picked", "");
|
|
1306
|
+
kbSay(root, `Picked up ${card.dataset.title}. Use the arrow keys to move it, Space to drop, Escape to cancel.`);
|
|
1307
|
+
}
|
|
1308
|
+
return;
|
|
1309
|
+
}
|
|
1310
|
+
if (held?.card !== card) return;
|
|
1311
|
+
if (e.key === "Escape") {
|
|
1312
|
+
e.preventDefault();
|
|
1313
|
+
return kbDrop(true, true);
|
|
1314
|
+
}
|
|
1315
|
+
const dir = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[e.key];
|
|
1316
|
+
if (!dir) return;
|
|
1317
|
+
e.preventDefault();
|
|
1318
|
+
const cols = all(root, "[data-asta-kanban-col]");
|
|
1319
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
1320
|
+
const ci = cols.indexOf(col);
|
|
1321
|
+
const target = cols[Math.min(cols.length - 1, Math.max(0, ci + dir[0]))];
|
|
1322
|
+
const here = all(col, "[data-asta-kanban-card]").indexOf(card);
|
|
1323
|
+
const list = target.querySelector("[data-asta-kanban-list]");
|
|
1324
|
+
const others = all(list, "[data-asta-kanban-card]").filter((c) => c !== card);
|
|
1325
|
+
const index = dir[0] ? Math.min(here, others.length) : Math.min(others.length, Math.max(0, here + dir[1]));
|
|
1326
|
+
if (target === col && index === here) return;
|
|
1327
|
+
moving = true;
|
|
1328
|
+
flip(kbCards(root), () => list.insertBefore(card, others[index] ?? null));
|
|
1329
|
+
card.focus();
|
|
1330
|
+
moving = false;
|
|
1331
|
+
kbCounts(root);
|
|
1332
|
+
kbSay(root, `${card.dataset.title}: ${kbWhere(card)}.`);
|
|
1333
|
+
});
|
|
1334
|
+
// Focus leaving the held card cancels the pick-up.
|
|
1335
|
+
document.addEventListener("focusout", (e) => {
|
|
1336
|
+
if (!held || moving || el(e) !== held.card) return;
|
|
1337
|
+
const card = held.card;
|
|
1338
|
+
setTimeout(() => {
|
|
1339
|
+
if (held?.card === card && document.activeElement !== card) kbDrop(true, false);
|
|
1340
|
+
});
|
|
1341
|
+
});
|
|
1342
|
+
|
|
1343
|
+
/* ---------- EventCalendar ---------- */
|
|
1344
|
+
|
|
1345
|
+
const parseDay = (k) => new Date(`${k}T00:00:00Z`);
|
|
1346
|
+
const dayKey = (d) => d.toISOString().slice(0, 10);
|
|
1347
|
+
const addDays = (d, n) => new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() + n));
|
|
1348
|
+
const addMonths = (d, n) => {
|
|
1349
|
+
const last = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + n + 1, 0)).getUTCDate();
|
|
1350
|
+
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + n, Math.min(d.getUTCDate(), last)));
|
|
1351
|
+
};
|
|
1352
|
+
let calPending = null;
|
|
1353
|
+
function calSelect(root, cell) {
|
|
1354
|
+
for (const c of all(root, '[role="gridcell"][aria-selected="true"]')) c.setAttribute("aria-selected", "false");
|
|
1355
|
+
cell.setAttribute("aria-selected", "true");
|
|
1356
|
+
calFocus(root, cell, false);
|
|
1357
|
+
const k = cell.dataset.day;
|
|
1358
|
+
for (const p of all(root, "[data-asta-cal-panel]")) p.hidden = p.dataset.astaCalPanel !== k;
|
|
1359
|
+
const input = root.querySelector("[data-asta-cal-value]");
|
|
1360
|
+
if (input) {
|
|
1361
|
+
input.value = k;
|
|
1362
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1363
|
+
}
|
|
1364
|
+
fire(root, "asta:select", { day: k });
|
|
1365
|
+
}
|
|
1366
|
+
function calFocus(root, cell, focus = true) {
|
|
1367
|
+
for (const c of all(root, '[role="gridcell"][tabindex="0"]')) c.tabIndex = -1;
|
|
1368
|
+
cell.tabIndex = 0;
|
|
1369
|
+
if (focus) cell.focus();
|
|
1370
|
+
}
|
|
1371
|
+
// Load another month with htmx, then put focus on the day.
|
|
1372
|
+
function calGo(root, k, focus) {
|
|
1373
|
+
const url = root.dataset.tpl.replace("__M__", k.slice(0, 7)).replace("__D__", k);
|
|
1374
|
+
calPending = { id: root.id, key: k, focus, dir: k.slice(0, 7) > root.dataset.month ? "next" : "prev" };
|
|
1375
|
+
if (!window.htmx) return void (location.href = url);
|
|
1376
|
+
const push = root.getAttribute("hx-push-url") !== "false";
|
|
1377
|
+
htmx.ajax("GET", url, { source: root, target: `#${root.id}`, select: `#${root.id}`, swap: "outerHTML show:none" }).then(() => {
|
|
1378
|
+
if (push) history.pushState({ htmx: true }, "", url);
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
document.addEventListener("click", (e) => {
|
|
1382
|
+
const t = el(e);
|
|
1383
|
+
const nav = t?.closest("[data-asta-cal-nav]");
|
|
1384
|
+
if (nav) {
|
|
1385
|
+
const root = nav.closest("[data-asta-cal]");
|
|
1386
|
+
const kind = nav.dataset.astaCalNav;
|
|
1387
|
+
const dir = kind === "today" ? (root.dataset.today.slice(0, 7) > root.dataset.month ? "next" : root.dataset.today.slice(0, 7) < root.dataset.month ? "prev" : "") : kind;
|
|
1388
|
+
calPending = { id: root.id, dir, focus: false };
|
|
1389
|
+
return;
|
|
1390
|
+
}
|
|
1391
|
+
const cell = t?.closest('[data-asta-cal] [role="gridcell"]');
|
|
1392
|
+
if (!cell) return;
|
|
1393
|
+
const root = cell.closest("[data-asta-cal]");
|
|
1394
|
+
if (cell.hasAttribute("data-out")) calGo(root, cell.dataset.day, true);
|
|
1395
|
+
else calSelect(root, cell);
|
|
1396
|
+
});
|
|
1397
|
+
document.addEventListener("keydown", (e) => {
|
|
1398
|
+
const cell = el(e);
|
|
1399
|
+
if (!cell?.matches('[data-asta-cal] [role="gridcell"]')) return;
|
|
1400
|
+
const root = cell.closest("[data-asta-cal]");
|
|
1401
|
+
const d = parseDay(cell.dataset.day);
|
|
1402
|
+
const week = Number(root.dataset.week || 1);
|
|
1403
|
+
const offset = (d.getUTCDay() - week + 7) % 7;
|
|
1404
|
+
const to = {
|
|
1405
|
+
ArrowLeft: () => addDays(d, -1),
|
|
1406
|
+
ArrowRight: () => addDays(d, 1),
|
|
1407
|
+
ArrowUp: () => addDays(d, -7),
|
|
1408
|
+
ArrowDown: () => addDays(d, 7),
|
|
1409
|
+
Home: () => addDays(d, -offset),
|
|
1410
|
+
End: () => addDays(d, 6 - offset),
|
|
1411
|
+
PageUp: () => addMonths(d, e.shiftKey ? -12 : -1),
|
|
1412
|
+
PageDown: () => addMonths(d, e.shiftKey ? 12 : 1),
|
|
1413
|
+
}[e.key];
|
|
1414
|
+
if (to) {
|
|
1415
|
+
e.preventDefault();
|
|
1416
|
+
const k = dayKey(to());
|
|
1417
|
+
const next = root.querySelector(`[role="gridcell"][data-day="${k}"]:not([data-out])`);
|
|
1418
|
+
if (next) calFocus(root, next);
|
|
1419
|
+
else calGo(root, k, true);
|
|
1420
|
+
} else if (e.key === "Enter" || e.key === " ") {
|
|
1421
|
+
e.preventDefault();
|
|
1422
|
+
cell.click();
|
|
1423
|
+
}
|
|
1424
|
+
});
|
|
1425
|
+
scans.push(() => {
|
|
1426
|
+
if (!calPending) return;
|
|
1427
|
+
const root = document.getElementById(calPending.id);
|
|
1428
|
+
if (!root?.matches("[data-asta-cal]")) return;
|
|
1429
|
+
const { dir, key, focus } = calPending;
|
|
1430
|
+
calPending = null;
|
|
1431
|
+
const grid = root.querySelector("[data-asta-cal-grid]");
|
|
1432
|
+
if (grid && dir && !reduce()) grid.dataset.dir = dir;
|
|
1433
|
+
const cell = key && root.querySelector(`[role="gridcell"][data-day="${key}"]`);
|
|
1434
|
+
if (cell && focus) calFocus(root, cell);
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
/* ---------- FileCard ---------- */
|
|
1438
|
+
|
|
1439
|
+
document.addEventListener("click", (e) => {
|
|
1440
|
+
const b = el(e)?.closest("[data-asta-file-star]");
|
|
1441
|
+
if (!b) return;
|
|
1442
|
+
const on = b.getAttribute("aria-pressed") !== "true";
|
|
1443
|
+
const title = (b.getAttribute("aria-label") || "").replace(/^(Un)?star /, "");
|
|
1444
|
+
b.setAttribute("aria-pressed", String(on));
|
|
1445
|
+
b.value = String(on);
|
|
1446
|
+
b.setAttribute("aria-label", `${on ? "Unstar" : "Star"} ${title}`);
|
|
1447
|
+
if (on) replay(b, "data-popped");
|
|
1448
|
+
fire(b, "asta:star", { starred: on });
|
|
1449
|
+
});
|
|
1450
|
+
scans.push((root) => {
|
|
1451
|
+
for (const b of within(root, "[data-asta-file-star]")) {
|
|
1452
|
+
const on = b.getAttribute("aria-pressed") === "true";
|
|
1453
|
+
if (on && b.getAttribute("aria-label")?.startsWith("Star ")) b.setAttribute("aria-label", `Un${b.getAttribute("aria-label").slice(0, 1).toLowerCase()}${b.getAttribute("aria-label").slice(1)}`);
|
|
1454
|
+
}
|
|
1455
|
+
});
|
|
1456
|
+
|
|
1457
|
+
/* ---------- ImageCompare ---------- */
|
|
1458
|
+
|
|
1459
|
+
const clamp = (n) => Math.min(100, Math.max(0, n));
|
|
1460
|
+
function cmpSet(root, n) {
|
|
1461
|
+
const v = Math.round(clamp(n) * 10) / 10;
|
|
1462
|
+
root.style.setProperty("--pos", String(v));
|
|
1463
|
+
root.toggleAttribute("data-hide-before", v < 18);
|
|
1464
|
+
root.toggleAttribute("data-hide-after", v > 82);
|
|
1465
|
+
const h = root.querySelector("[data-asta-compare-handle]");
|
|
1466
|
+
if (h) {
|
|
1467
|
+
h.setAttribute("aria-valuenow", String(Math.round(v)));
|
|
1468
|
+
h.setAttribute("aria-valuetext", `${Math.round(v)}% ${root.dataset.before}, ${Math.round(100 - v)}% ${root.dataset.after}`);
|
|
1469
|
+
}
|
|
1470
|
+
fire(root, "asta:change", { value: v });
|
|
1471
|
+
}
|
|
1472
|
+
const cmpPos = (root) => parseFloat(root.style.getPropertyValue("--pos")) || 50;
|
|
1473
|
+
const cmpAt = (root, x) => {
|
|
1474
|
+
const r = root.getBoundingClientRect();
|
|
1475
|
+
return r.width ? ((x - r.left) / r.width) * 100 : cmpPos(root);
|
|
1476
|
+
};
|
|
1477
|
+
let press = null;
|
|
1478
|
+
const cmpHandle = (root) => root.querySelector("[data-asta-compare-handle]");
|
|
1479
|
+
document.addEventListener("pointerdown", (e) => {
|
|
1480
|
+
const root = el(e)?.closest("[data-asta-compare]");
|
|
1481
|
+
if (!root || e.button !== 0) return;
|
|
1482
|
+
const touch = e.pointerType === "touch";
|
|
1483
|
+
press = { root, id: e.pointerId, x: e.clientX, y: e.clientY, touch, moved: false };
|
|
1484
|
+
if (touch) return;
|
|
1485
|
+
root.setPointerCapture(e.pointerId);
|
|
1486
|
+
cmpSet(root, cmpAt(root, e.clientX));
|
|
1487
|
+
cmpHandle(root)?.focus({ preventScroll: true });
|
|
1488
|
+
});
|
|
1489
|
+
// Keeps focus on the handle (a press on the frame would blur it) and stops text selection.
|
|
1490
|
+
document.addEventListener("mousedown", (e) => {
|
|
1491
|
+
if (el(e)?.closest("[data-asta-compare]")) e.preventDefault();
|
|
1492
|
+
});
|
|
1493
|
+
document.addEventListener("pointermove", (e) => {
|
|
1494
|
+
const p = press;
|
|
1495
|
+
if (!p || p.id !== e.pointerId) return;
|
|
1496
|
+
if (!p.moved) {
|
|
1497
|
+
const dx = Math.abs(e.clientX - p.x);
|
|
1498
|
+
const dy = Math.abs(e.clientY - p.y);
|
|
1499
|
+
// Touch waits for a horizontal move, so a vertical swipe scrolls the page.
|
|
1500
|
+
if (p.touch ? dx < 6 || dx <= dy : dx < 2) return;
|
|
1501
|
+
p.moved = true;
|
|
1502
|
+
if (p.touch) {
|
|
1503
|
+
p.root.setPointerCapture(e.pointerId);
|
|
1504
|
+
cmpHandle(p.root)?.focus({ preventScroll: true });
|
|
1505
|
+
}
|
|
1506
|
+
p.root.setAttribute("data-dragging", "");
|
|
1507
|
+
}
|
|
1508
|
+
cmpSet(p.root, cmpAt(p.root, e.clientX));
|
|
1509
|
+
});
|
|
1510
|
+
const cmpUp = (e) => {
|
|
1511
|
+
const p = press;
|
|
1512
|
+
if (!p || p.id !== e.pointerId) return;
|
|
1513
|
+
press = null;
|
|
1514
|
+
if (p.touch && !p.moved && e.type === "pointerup") {
|
|
1515
|
+
cmpSet(p.root, cmpAt(p.root, e.clientX));
|
|
1516
|
+
cmpHandle(p.root)?.focus({ preventScroll: true });
|
|
1517
|
+
}
|
|
1518
|
+
p.root.removeAttribute("data-dragging");
|
|
1519
|
+
};
|
|
1520
|
+
document.addEventListener("pointerup", cmpUp);
|
|
1521
|
+
document.addEventListener("pointercancel", cmpUp);
|
|
1522
|
+
document.addEventListener("keydown", (e) => {
|
|
1523
|
+
const h = el(e);
|
|
1524
|
+
if (!h?.matches("[data-asta-compare-handle]")) return;
|
|
1525
|
+
const root = h.closest("[data-asta-compare]");
|
|
1526
|
+
const pos = cmpPos(root);
|
|
1527
|
+
const step = parseFloat(root.dataset.step) || 2;
|
|
1528
|
+
const to = { ArrowLeft: pos - step, ArrowDown: pos - step, ArrowRight: pos + step, ArrowUp: pos + step, PageDown: pos - step * 10, PageUp: pos + step * 10, Home: 0, End: 100 }[e.key];
|
|
1529
|
+
if (to === undefined) return;
|
|
1530
|
+
e.preventDefault();
|
|
1531
|
+
cmpSet(root, to);
|
|
1532
|
+
});
|
|
1533
|
+
|
|
1534
|
+
/* ---------- Carousel ---------- */
|
|
1535
|
+
|
|
1536
|
+
const carTrack = (root) => root.querySelector("[data-asta-carousel-track]");
|
|
1537
|
+
const carSlides = (root) => all(root, "[data-asta-carousel-slide]");
|
|
1538
|
+
const carStep = (root) => {
|
|
1539
|
+
const s = carSlides(root);
|
|
1540
|
+
return s.length > 1 ? s[1].offsetLeft - s[0].offsetLeft : carTrack(root).clientWidth || 1;
|
|
1541
|
+
};
|
|
1542
|
+
function carShow(root, i) {
|
|
1543
|
+
const slides = carSlides(root);
|
|
1544
|
+
const n = slides.length;
|
|
1545
|
+
if (String(i) === root.dataset.index) return;
|
|
1546
|
+
root.dataset.index = String(i);
|
|
1547
|
+
slides.forEach((s, k) => {
|
|
1548
|
+
s.toggleAttribute("inert", k !== i);
|
|
1549
|
+
if (k === i) s.removeAttribute("aria-hidden");
|
|
1550
|
+
else s.setAttribute("aria-hidden", "true");
|
|
1551
|
+
});
|
|
1552
|
+
for (const d of all(root, "[data-asta-carousel-dot]")) {
|
|
1553
|
+
const k = Number(d.dataset.astaCarouselDot);
|
|
1554
|
+
const on = k === i;
|
|
1555
|
+
if (on) d.setAttribute("aria-current", "true");
|
|
1556
|
+
else d.removeAttribute("aria-current");
|
|
1557
|
+
d.style.width = `${on ? 30 : 14}px`;
|
|
1558
|
+
d.style.transform = `translateX(${k * 14 + (k > i ? 16 : 0)}px)`;
|
|
1559
|
+
}
|
|
1560
|
+
const loop = root.hasAttribute("data-loop");
|
|
1561
|
+
const [prev, next] = all(root, "[data-asta-carousel-step]");
|
|
1562
|
+
if (prev) prev.disabled = !loop && i === 0;
|
|
1563
|
+
if (next) next.disabled = !loop && i === n - 1;
|
|
1564
|
+
fire(root, "asta:slide", { index: i });
|
|
1565
|
+
}
|
|
1566
|
+
function carGo(root, i, wrap) {
|
|
1567
|
+
const n = carSlides(root).length;
|
|
1568
|
+
if (!n) return;
|
|
1569
|
+
const to = wrap || root.hasAttribute("data-loop") ? (i + n) % n : Math.max(0, Math.min(n - 1, i));
|
|
1570
|
+
root._target = to;
|
|
1571
|
+
carShow(root, to);
|
|
1572
|
+
carTrack(root).scrollTo({ left: to * carStep(root), behavior: reduce() ? "auto" : "smooth" });
|
|
1573
|
+
}
|
|
1574
|
+
function carPlay(root) {
|
|
1575
|
+
const on = root.hasAttribute("data-autoplay") && !reduce() && root._visible && !root._hover && !root._focus;
|
|
1576
|
+
root.toggleAttribute("data-playing", !!on);
|
|
1577
|
+
const t = carTrack(root);
|
|
1578
|
+
if (t) t.setAttribute("aria-live", on ? "off" : "polite");
|
|
1579
|
+
}
|
|
1580
|
+
const carSeen = new IntersectionObserver((entries) => {
|
|
1581
|
+
for (const en of entries) {
|
|
1582
|
+
en.target._visible = en.isIntersecting;
|
|
1583
|
+
carPlay(en.target);
|
|
1584
|
+
}
|
|
1585
|
+
});
|
|
1586
|
+
scans.push((root) => {
|
|
1587
|
+
for (const c of within(root, "[data-asta-carousel]")) {
|
|
1588
|
+
if (c._init) continue;
|
|
1589
|
+
c._init = true;
|
|
1590
|
+
if (c.dataset.autoplay) c.style.setProperty("--asta-autoplay", `${c.dataset.autoplay}ms`);
|
|
1591
|
+
carSeen.observe(c);
|
|
1592
|
+
}
|
|
1593
|
+
});
|
|
1594
|
+
document.addEventListener(
|
|
1595
|
+
"scroll",
|
|
1596
|
+
(e) => {
|
|
1597
|
+
const t = el(e);
|
|
1598
|
+
if (!t?.matches?.("[data-asta-carousel-track]") || t._raf) return;
|
|
1599
|
+
t._raf = requestAnimationFrame(() => {
|
|
1600
|
+
t._raf = 0;
|
|
1601
|
+
const root = t.closest("[data-asta-carousel]");
|
|
1602
|
+
const i = Math.round(t.scrollLeft / carStep(root));
|
|
1603
|
+
if (root._target !== undefined) {
|
|
1604
|
+
if (i !== root._target) return;
|
|
1605
|
+
root._target = undefined;
|
|
1606
|
+
}
|
|
1607
|
+
carShow(root, i);
|
|
1608
|
+
});
|
|
1609
|
+
},
|
|
1610
|
+
true,
|
|
1611
|
+
);
|
|
1612
|
+
document.addEventListener(
|
|
1613
|
+
"scrollend",
|
|
1614
|
+
(e) => {
|
|
1615
|
+
const t = el(e);
|
|
1616
|
+
if (!t?.matches?.("[data-asta-carousel-track]")) return;
|
|
1617
|
+
const root = t.closest("[data-asta-carousel]");
|
|
1618
|
+
root._target = undefined;
|
|
1619
|
+
carShow(root, Math.round(t.scrollLeft / carStep(root)));
|
|
1620
|
+
},
|
|
1621
|
+
true,
|
|
1622
|
+
);
|
|
1623
|
+
document.addEventListener("click", (e) => {
|
|
1624
|
+
const t = el(e);
|
|
1625
|
+
const dot = t?.closest("[data-asta-carousel-dot]");
|
|
1626
|
+
const step = t?.closest("[data-asta-carousel-step]");
|
|
1627
|
+
if (!dot && !step) return;
|
|
1628
|
+
const root = (dot || step).closest("[data-asta-carousel]");
|
|
1629
|
+
if (dot) carGo(root, Number(dot.dataset.astaCarouselDot));
|
|
1630
|
+
else carGo(root, Number(root.dataset.index) + Number(step.dataset.astaCarouselStep));
|
|
1631
|
+
});
|
|
1632
|
+
document.addEventListener("keydown", (e) => {
|
|
1633
|
+
const root = el(e);
|
|
1634
|
+
if (!root?.matches("[data-asta-carousel]")) return;
|
|
1635
|
+
const i = Number(root.dataset.index);
|
|
1636
|
+
const to = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: carSlides(root).length - 1 }[e.key];
|
|
1637
|
+
if (to === undefined) return;
|
|
1638
|
+
e.preventDefault();
|
|
1639
|
+
carGo(root, to);
|
|
1640
|
+
});
|
|
1641
|
+
document.addEventListener("animationend", (e) => {
|
|
1642
|
+
if (e.animationName !== "asta-carousel-fill") return;
|
|
1643
|
+
const root = el(e).closest("[data-asta-carousel]");
|
|
1644
|
+
if (root) carGo(root, Number(root.dataset.index) + 1, true);
|
|
1645
|
+
});
|
|
1646
|
+
for (const [type, on] of [
|
|
1647
|
+
["pointerover", true],
|
|
1648
|
+
["pointerout", false],
|
|
1649
|
+
])
|
|
1650
|
+
document.addEventListener(type, (e) => {
|
|
1651
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
1652
|
+
if (!root || root.contains(e.relatedTarget)) return;
|
|
1653
|
+
root._hover = on;
|
|
1654
|
+
carPlay(root);
|
|
1655
|
+
});
|
|
1656
|
+
document.addEventListener("focusin", (e) => {
|
|
1657
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
1658
|
+
if (!root) return;
|
|
1659
|
+
// Only keyboard focus pauses autoplay. Clicking an arrow or a dot shouldn't stop it.
|
|
1660
|
+
let visible = true;
|
|
1661
|
+
try {
|
|
1662
|
+
visible = el(e).matches(":focus-visible");
|
|
1663
|
+
} catch {}
|
|
1664
|
+
if (visible) {
|
|
1665
|
+
root._focus = true;
|
|
1666
|
+
carPlay(root);
|
|
1667
|
+
}
|
|
1668
|
+
});
|
|
1669
|
+
document.addEventListener("focusout", (e) => {
|
|
1670
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
1671
|
+
if (!root || root.contains(e.relatedTarget)) return;
|
|
1672
|
+
root._focus = false;
|
|
1673
|
+
carPlay(root);
|
|
1674
|
+
});
|
|
1675
|
+
|
|
1676
|
+
/* ---------- NotificationCenter ---------- */
|
|
1677
|
+
|
|
1678
|
+
const ncParts = (root) => ({
|
|
1679
|
+
trigger: root.querySelector("[data-asta-nc-trigger]"),
|
|
1680
|
+
panel: root.querySelector("[data-asta-nc-panel]"),
|
|
1681
|
+
});
|
|
1682
|
+
function ncOpen(root, on) {
|
|
1683
|
+
const { trigger, panel } = ncParts(root);
|
|
1684
|
+
if (!trigger || !panel) return;
|
|
1685
|
+
if (on) {
|
|
1686
|
+
for (const o of document.querySelectorAll("[data-asta-nc][data-open]")) if (o !== root) ncOpen(o, false);
|
|
1687
|
+
root.setAttribute("data-open", "");
|
|
1688
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
1689
|
+
panel.hidden = false;
|
|
1690
|
+
panel.style.left = panel.style.right = "";
|
|
1691
|
+
// Keep the panel on screen on narrow viewports. Measure the anchor, not the panel: the panel is mid-scale.
|
|
1692
|
+
const box = root.getBoundingClientRect();
|
|
1693
|
+
const w = panel.offsetWidth;
|
|
1694
|
+
if (root.dataset.align === "start") {
|
|
1695
|
+
if (box.left + w > innerWidth - 8) panel.style.left = `${innerWidth - 8 - w - box.left}px`;
|
|
1696
|
+
} else if (box.right - w < 8) panel.style.right = `${box.right - w - 8}px`;
|
|
1697
|
+
replay(root.querySelector("[data-asta-nc-list]"), "data-fresh");
|
|
1698
|
+
} else if (root.hasAttribute("data-open")) {
|
|
1699
|
+
root.removeAttribute("data-open");
|
|
1700
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
1701
|
+
setTimeout(() => {
|
|
1702
|
+
if (!root.hasAttribute("data-open")) panel.hidden = true;
|
|
1703
|
+
}, reduce() ? 0 : 170);
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
function ncSync(root) {
|
|
1707
|
+
const { trigger, panel } = ncParts(root);
|
|
1708
|
+
const entries = all(root, "[data-asta-nc-entry]");
|
|
1709
|
+
const n = entries.filter((x) => !x.hasAttribute("data-read")).length;
|
|
1710
|
+
const label = root.dataset.label;
|
|
1711
|
+
trigger.setAttribute("aria-label", n ? `${label}, ${n} unread` : label);
|
|
1712
|
+
const badge = root.querySelector("[data-asta-nc-badge]");
|
|
1713
|
+
const count = root.querySelector("[data-asta-nc-count]");
|
|
1714
|
+
const text = n > 99 ? "99+" : String(n);
|
|
1715
|
+
if (badge) badge.hidden = n === 0;
|
|
1716
|
+
if (count && count.textContent !== text) {
|
|
1717
|
+
count.textContent = text;
|
|
1718
|
+
if (!reduce()) replay(count, "data-bump");
|
|
1719
|
+
}
|
|
1720
|
+
const tc = root.querySelector("[data-asta-nc-tabcount]");
|
|
1721
|
+
if (tc) {
|
|
1722
|
+
tc.textContent = String(n);
|
|
1723
|
+
tc.hidden = n === 0;
|
|
1724
|
+
}
|
|
1725
|
+
root.querySelector("[data-asta-nc-all]")?.toggleAttribute("aria-disabled", n === 0);
|
|
1726
|
+
// The empty state follows what the current tab shows.
|
|
1727
|
+
const unreadTab = panel.dataset.tab === "unread";
|
|
1728
|
+
const shown = unreadTab ? entries.filter((x) => !x.hasAttribute("data-read") || x.hasAttribute("data-keep")).length : entries.length;
|
|
1729
|
+
const empty = root.querySelector("[data-asta-nc-empty]");
|
|
1730
|
+
if (empty) {
|
|
1731
|
+
empty.hidden = shown > 0;
|
|
1732
|
+
for (const p of all(empty, "[data-unread]")) {
|
|
1733
|
+
p.dataset.all ??= p.textContent;
|
|
1734
|
+
p.textContent = unreadTab ? p.dataset.unread : p.dataset.all;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
function ncRead(root, entries, say) {
|
|
1739
|
+
let changed = false;
|
|
1740
|
+
for (const x of entries) {
|
|
1741
|
+
if (x.hasAttribute("data-read")) continue;
|
|
1742
|
+
changed = true;
|
|
1743
|
+
x.setAttribute("data-read", "");
|
|
1744
|
+
const dot = x.querySelector("[data-asta-nc-dot]");
|
|
1745
|
+
if (dot) {
|
|
1746
|
+
dot.tabIndex = -1;
|
|
1747
|
+
dot.setAttribute("aria-hidden", "true");
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
if (changed) ncSync(root);
|
|
1751
|
+
if (say) {
|
|
1752
|
+
const s = root.querySelector("[data-asta-nc-status]");
|
|
1753
|
+
if (s) s.textContent = say;
|
|
1754
|
+
}
|
|
1755
|
+
return changed;
|
|
1756
|
+
}
|
|
1757
|
+
function ncTab(root, tab) {
|
|
1758
|
+
const { panel } = ncParts(root);
|
|
1759
|
+
for (const t of all(root, "[data-asta-nc-tab]")) {
|
|
1760
|
+
const on = t.dataset.astaNcTab === tab;
|
|
1761
|
+
t.setAttribute("aria-selected", String(on));
|
|
1762
|
+
t.tabIndex = on ? 0 : -1;
|
|
1763
|
+
}
|
|
1764
|
+
// What was unread when the Unread tab opened stays listed, so rows don't vanish as you read them.
|
|
1765
|
+
for (const x of all(root, "[data-asta-nc-entry]")) x.toggleAttribute("data-keep", tab === "unread" && !x.hasAttribute("data-read"));
|
|
1766
|
+
panel.dataset.tab = tab;
|
|
1767
|
+
const list = root.querySelector("[data-asta-nc-list]");
|
|
1768
|
+
list?.setAttribute("aria-label", tab === "unread" ? "Unread notifications" : "All notifications");
|
|
1769
|
+
if (list) replay(list, "data-fresh");
|
|
1770
|
+
ncSync(root);
|
|
1771
|
+
}
|
|
1772
|
+
document.addEventListener("click", (e) => {
|
|
1773
|
+
const t = el(e);
|
|
1774
|
+
const trigger = t?.closest("[data-asta-nc-trigger]");
|
|
1775
|
+
if (trigger) {
|
|
1776
|
+
const root = trigger.closest("[data-asta-nc]");
|
|
1777
|
+
ncOpen(root, !root.hasAttribute("data-open"));
|
|
1778
|
+
}
|
|
1779
|
+
for (const root of document.querySelectorAll("[data-asta-nc][data-open]")) if (!root.contains(t)) ncOpen(root, false);
|
|
1780
|
+
const root = t?.closest("[data-asta-nc]");
|
|
1781
|
+
if (!root || trigger) return;
|
|
1782
|
+
const tab = t.closest("[data-asta-nc-tab]");
|
|
1783
|
+
if (tab) return ncTab(root, tab.dataset.astaNcTab);
|
|
1784
|
+
if (t.closest("[data-asta-nc-all]")) return void ncRead(root, all(root, "[data-asta-nc-entry]"), "All notifications marked as read.");
|
|
1785
|
+
const dot = t.closest("[data-asta-nc-dot]");
|
|
1786
|
+
const entry = t.closest("[data-asta-nc-entry]");
|
|
1787
|
+
if (dot) {
|
|
1788
|
+
ncRead(root, [entry], "Marked as read.");
|
|
1789
|
+
entry.querySelector("[data-asta-nc-item]")?.focus();
|
|
1790
|
+
return;
|
|
1791
|
+
}
|
|
1792
|
+
if (t.closest("[data-asta-nc-item]") && entry && ncRead(root, [entry]) && root.dataset.url) {
|
|
1793
|
+
// The row may be a link that navigates away, so this post must outlive the page.
|
|
1794
|
+
fetch(root.dataset.url, {
|
|
1795
|
+
method: "POST",
|
|
1796
|
+
keepalive: true,
|
|
1797
|
+
headers: { "HX-Request": "true" },
|
|
1798
|
+
body: new URLSearchParams({ id: entry.dataset.astaNcEntry }),
|
|
1799
|
+
}).catch(() => {});
|
|
1800
|
+
}
|
|
1801
|
+
});
|
|
1802
|
+
document.addEventListener("keydown", (e) => {
|
|
1803
|
+
const t = el(e);
|
|
1804
|
+
const root = t?.closest("[data-asta-nc]");
|
|
1805
|
+
if (!root?.hasAttribute("data-open")) return;
|
|
1806
|
+
if (e.key === "Escape") {
|
|
1807
|
+
e.preventDefault();
|
|
1808
|
+
ncOpen(root, false);
|
|
1809
|
+
ncParts(root).trigger.focus();
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
const tab = t.closest("[data-asta-nc-tab]");
|
|
1813
|
+
if (tab && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
|
|
1814
|
+
e.preventDefault();
|
|
1815
|
+
const other = tab.dataset.astaNcTab === "all" ? "unread" : "all";
|
|
1816
|
+
ncTab(root, other);
|
|
1817
|
+
root.querySelector(`[data-asta-nc-tab="${other}"]`)?.focus();
|
|
1818
|
+
return;
|
|
1819
|
+
}
|
|
1820
|
+
if (!t.closest("[data-asta-nc-list]") || !["ArrowDown", "ArrowUp", "Home", "End"].includes(e.key)) return;
|
|
1821
|
+
const rows = all(root, "[data-asta-nc-item]").filter((r) => r.offsetParent);
|
|
1822
|
+
if (!rows.length) return;
|
|
1823
|
+
e.preventDefault();
|
|
1824
|
+
const i = rows.indexOf(document.activeElement);
|
|
1825
|
+
const next = e.key === "Home" ? 0 : e.key === "End" ? rows.length - 1 : Math.min(rows.length - 1, Math.max(0, i + (e.key === "ArrowDown" ? 1 : -1)));
|
|
1826
|
+
rows[next]?.focus();
|
|
1827
|
+
});
|
|
1828
|
+
|
|
1829
|
+
/* ---------- Comments ---------- */
|
|
1830
|
+
|
|
1831
|
+
const cmSubmit = (form) => form.querySelector('button[type="submit"]');
|
|
1832
|
+
function cmCheck(form) {
|
|
1833
|
+
const b = cmSubmit(form);
|
|
1834
|
+
if (b) b.disabled = !form.querySelector("textarea")?.value.trim();
|
|
1835
|
+
}
|
|
1836
|
+
function cmClose(box, focus) {
|
|
1837
|
+
const opener = box._opener;
|
|
1838
|
+
box.remove();
|
|
1839
|
+
if (focus && opener?.isConnected) opener.focus();
|
|
1840
|
+
}
|
|
1841
|
+
function cmTotal(root) {
|
|
1842
|
+
const t = root.querySelector("[data-asta-comments-total]");
|
|
1843
|
+
if (t) t.textContent = String(root.querySelectorAll("[data-asta-comment]").length);
|
|
1844
|
+
}
|
|
1845
|
+
scans.push((root) => {
|
|
1846
|
+
for (const r of within(root, "[data-asta-comments]")) cmTotal(r);
|
|
1847
|
+
if (root !== document) root.closest?.("[data-asta-comments]") && cmTotal(root.closest("[data-asta-comments]"));
|
|
1848
|
+
});
|
|
1849
|
+
document.addEventListener("input", (e) => {
|
|
1850
|
+
const form = el(e)?.closest("[data-asta-comment-form]");
|
|
1851
|
+
if (form) cmCheck(form);
|
|
1852
|
+
});
|
|
1853
|
+
document.addEventListener("keydown", (e) => {
|
|
1854
|
+
const area = el(e);
|
|
1855
|
+
const form = area?.closest("[data-asta-comment-form]");
|
|
1856
|
+
if (!form || area.tagName !== "TEXTAREA") return;
|
|
1857
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
1858
|
+
e.preventDefault();
|
|
1859
|
+
if (area.value.trim()) form.requestSubmit();
|
|
1860
|
+
} else if (e.key === "Escape" && form.hasAttribute("data-reply")) {
|
|
1861
|
+
e.preventDefault();
|
|
1862
|
+
cmClose(form.closest("[data-asta-comment-replybox]"), true);
|
|
1863
|
+
}
|
|
1864
|
+
});
|
|
1865
|
+
document.addEventListener("click", (e) => {
|
|
1866
|
+
const t = el(e);
|
|
1867
|
+
const cancel = t?.closest("[data-asta-comment-cancel]");
|
|
1868
|
+
if (cancel) return cmClose(cancel.closest("[data-asta-comment-replybox]"), true);
|
|
1869
|
+
const reply = t?.closest("[data-asta-comment-reply]");
|
|
1870
|
+
if (reply) {
|
|
1871
|
+
const root = reply.closest("[data-asta-comments]");
|
|
1872
|
+
const thread = root.querySelector(`[data-asta-comment-thread="${CSS.escape(reply.dataset.astaCommentReply)}"]`);
|
|
1873
|
+
const replies = thread?.querySelector("[data-asta-comment-replies]");
|
|
1874
|
+
const tpl = root.querySelector("template[data-asta-comments-replybox]");
|
|
1875
|
+
if (!replies || !tpl) return;
|
|
1876
|
+
let box = replies.querySelector(":scope > [data-asta-comment-replybox]");
|
|
1877
|
+
if (!box) {
|
|
1878
|
+
box = tpl.content.firstElementChild.cloneNode(true);
|
|
1879
|
+
replies.append(box);
|
|
1880
|
+
window.htmx?.process(box);
|
|
1881
|
+
}
|
|
1882
|
+
box._opener = thread.querySelector("[data-asta-comment-reply]");
|
|
1883
|
+
const name = box._opener?.dataset.name || "";
|
|
1884
|
+
box.querySelector('input[name="parent"]').value = reply.dataset.astaCommentReply;
|
|
1885
|
+
const area = box.querySelector("textarea");
|
|
1886
|
+
area.placeholder = `Reply to ${name}…`;
|
|
1887
|
+
if (reply.dataset.mention) area.value = `@${reply.dataset.mention} `;
|
|
1888
|
+
cmCheck(box.querySelector("form"));
|
|
1889
|
+
area.focus();
|
|
1890
|
+
area.setSelectionRange(area.value.length, area.value.length);
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
const like = t?.closest("[data-asta-comment-like]");
|
|
1894
|
+
if (like) {
|
|
1895
|
+
const on = like.getAttribute("aria-pressed") !== "true";
|
|
1896
|
+
const c = like.querySelector("[data-asta-comment-likes]");
|
|
1897
|
+
const n = Math.max(0, Number(c?.textContent || 0) + (on ? 1 : -1));
|
|
1898
|
+
like.setAttribute("aria-pressed", String(on));
|
|
1899
|
+
like.value = String(on);
|
|
1900
|
+
like.setAttribute("aria-label", `Like, ${n}`);
|
|
1901
|
+
if (c) c.textContent = String(n);
|
|
1902
|
+
if (!reduce()) replay(like, "data-popped");
|
|
1903
|
+
fire(like, "asta:like", { liked: on });
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
1907
|
+
const form = e.detail?.elt;
|
|
1908
|
+
if (!form?.matches?.("[data-asta-comment-form]") || !e.detail.successful) return;
|
|
1909
|
+
const area = form.querySelector("textarea");
|
|
1910
|
+
if (area) area.value = "";
|
|
1911
|
+
cmCheck(form);
|
|
1912
|
+
if (form.hasAttribute("data-reply")) cmClose(form.closest("[data-asta-comment-replybox]"), true);
|
|
1913
|
+
});
|
|
1914
|
+
|
|
1915
|
+
/* ---------- MotionButton ---------- */
|
|
1916
|
+
|
|
1917
|
+
const mbSeen = new IntersectionObserver((entries) => {
|
|
1918
|
+
for (const en of entries) en.target.toggleAttribute("data-offscreen", !en.isIntersecting);
|
|
1919
|
+
});
|
|
1920
|
+
scans.push((root) => {
|
|
1921
|
+
for (const b of within(root, '[data-asta-mb="beam"]')) mbSeen.observe(b);
|
|
1922
|
+
});
|
|
1923
|
+
for (const type of ["pointerover", "pointerout"])
|
|
1924
|
+
document.addEventListener(type, (e) => {
|
|
1925
|
+
const b = el(e)?.closest('[data-asta-mb="fill"], [data-asta-mb="shine"]');
|
|
1926
|
+
if (!b || b.contains(e.relatedTarget)) return;
|
|
1927
|
+
if (b.dataset.astaMb === "fill") {
|
|
1928
|
+
// The circle grows from where the pointer came in and shrinks toward where it leaves.
|
|
1929
|
+
const r = b.getBoundingClientRect();
|
|
1930
|
+
b.style.setProperty("--fx", `${e.clientX - r.left}px`);
|
|
1931
|
+
b.style.setProperty("--fy", `${e.clientY - r.top}px`);
|
|
1932
|
+
} else if (type === "pointerover") b.toggleAttribute("data-shine");
|
|
1933
|
+
});
|
|
1934
|
+
document.addEventListener("focusin", (e) => {
|
|
1935
|
+
const b = el(e)?.closest('[data-asta-mb="shine"]');
|
|
1936
|
+
if (b) b.toggleAttribute("data-shine");
|
|
1937
|
+
});
|
|
1938
|
+
document.addEventListener("click", (e) => {
|
|
1939
|
+
const b = el(e)?.closest('[data-asta-mb="copy"], [data-asta-mb="confirm"]');
|
|
1940
|
+
if (!b) return;
|
|
1941
|
+
clearTimeout(b._t);
|
|
1942
|
+
if (b.dataset.astaMb === "copy") {
|
|
1943
|
+
navigator.clipboard?.writeText(b.dataset.value || "").catch(() => {});
|
|
1944
|
+
b.setAttribute("data-done", "");
|
|
1945
|
+
const s = b.querySelector("[data-asta-mb-status]");
|
|
1946
|
+
if (s) s.textContent = "Copied to clipboard";
|
|
1947
|
+
b._t = setTimeout(() => {
|
|
1948
|
+
b.removeAttribute("data-done");
|
|
1949
|
+
if (s) s.textContent = "";
|
|
1950
|
+
}, 1600);
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
const ms = Number(b.dataset.timeout) || 3000;
|
|
1954
|
+
if (b.dataset.stage === "idle") {
|
|
1955
|
+
b.style.setProperty("--asta-timeout", `${ms}ms`);
|
|
1956
|
+
b.dataset.stage = "ask";
|
|
1957
|
+
b._t = setTimeout(() => (b.dataset.stage = "idle"), ms);
|
|
1958
|
+
} else if (b.dataset.stage === "ask") {
|
|
1959
|
+
b.dataset.stage = "done";
|
|
1960
|
+
fire(b, "asta:confirm");
|
|
1961
|
+
b._t = setTimeout(() => (b.dataset.stage = "idle"), 1800);
|
|
1962
|
+
}
|
|
1963
|
+
});
|
|
1964
|
+
document.addEventListener("focusout", (e) => {
|
|
1965
|
+
const b = el(e)?.closest('[data-asta-mb="confirm"][data-stage="ask"]');
|
|
1966
|
+
if (b) {
|
|
1967
|
+
clearTimeout(b._t);
|
|
1968
|
+
b.dataset.stage = "idle";
|
|
1969
|
+
}
|
|
1970
|
+
});
|
|
1971
|
+
|
|
1972
|
+
// First look, once every section above has added its scan.
|
|
1973
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => scan(document));
|
|
1974
|
+
else scan(document);
|
|
1975
|
+
})();
|
|
1976
|
+
|
|
1977
|
+
/* ---- src/forms.js ---- */
|
|
1978
|
+
/* Forms group: the behaviour behind the form controls. Event delegation on document, so markup swapped in by
|
|
1979
|
+
* htmx works without re-init. Every control keeps a real form field (native input or hidden input) up to date.
|
|
1980
|
+
* CopyButton [data-asta-copy]
|
|
1981
|
+
* SegmentedControl [data-asta-segmented] ToggleGroup [data-asta-toggle]
|
|
1982
|
+
* NumberInput [data-asta-number] OtpInput [data-asta-otp]
|
|
1983
|
+
* PasswordInput [data-asta-password] TagInput [data-asta-tags]
|
|
1984
|
+
* RangeSlider [data-asta-range] Rating [data-asta-rating]
|
|
1985
|
+
* ColorPicker [data-asta-color] Combobox [data-asta-combo]
|
|
1986
|
+
* DatePicker [data-asta-date] FileUpload [data-asta-upload]
|
|
1987
|
+
* InlineEdit [data-asta-inline]
|
|
1988
|
+
*/
|
|
1989
|
+
(() => {
|
|
1990
|
+
if (window.__astaForms) return;
|
|
1991
|
+
window.__astaForms = true;
|
|
1992
|
+
const EASE = "cubic-bezier(.16,1,.3,1)";
|
|
1993
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
1994
|
+
const el = (e) => (e.target instanceof Element ? e.target : null);
|
|
1995
|
+
const near = (e, sel) => el(e)?.closest(sel) || null;
|
|
1996
|
+
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
1997
|
+
const emit = (node, type, detail) =>
|
|
1998
|
+
node.dispatchEvent(detail === undefined ? new Event(type, { bubbles: true }) : new CustomEvent(type, { bubbles: true, detail }));
|
|
1999
|
+
// Restart a CSS animation on a class without remounting the element.
|
|
2000
|
+
const replay = (node, cls) => {
|
|
2001
|
+
node.classList.remove(cls);
|
|
2002
|
+
void node.offsetWidth;
|
|
2003
|
+
node.classList.add(cls);
|
|
2004
|
+
};
|
|
2005
|
+
const on = (type, fn, capture) => document.addEventListener(type, fn, capture);
|
|
2006
|
+
|
|
2007
|
+
/* ---------- Slider ---------- */
|
|
2008
|
+
|
|
2009
|
+
// Chrome and Safari can't fill a range track up to the thumb, so the wrapper carries --asta-fill.
|
|
2010
|
+
const sliderFill = (i) => {
|
|
2011
|
+
const lo = +i.min || 0, hi = i.max === "" ? 100 : +i.max;
|
|
2012
|
+
const f = hi > lo ? ((+i.value - lo) / (hi - lo)) * 100 : 0;
|
|
2013
|
+
i.parentElement?.style.setProperty("--asta-fill", `${Math.min(100, Math.max(0, f))}%`);
|
|
2014
|
+
};
|
|
2015
|
+
on("input", (e) => e.target instanceof HTMLInputElement && e.target.matches("[data-asta-slider]") && sliderFill(e.target));
|
|
2016
|
+
on("reset", (e) => {
|
|
2017
|
+
if (!(e.target instanceof HTMLFormElement)) return;
|
|
2018
|
+
const f = e.target;
|
|
2019
|
+
requestAnimationFrame(() => f.querySelectorAll("input[data-asta-slider]").forEach(sliderFill));
|
|
2020
|
+
});
|
|
2021
|
+
|
|
2022
|
+
/* ---------- CopyButton ---------- */
|
|
2023
|
+
|
|
2024
|
+
async function writeText(text) {
|
|
2025
|
+
try {
|
|
2026
|
+
await navigator.clipboard.writeText(text);
|
|
2027
|
+
return true;
|
|
2028
|
+
} catch {
|
|
2029
|
+
// Older browsers and non-secure pages: fall back to a hidden textarea.
|
|
2030
|
+
const ta = document.createElement("textarea");
|
|
2031
|
+
ta.value = text;
|
|
2032
|
+
ta.setAttribute("readonly", "");
|
|
2033
|
+
ta.style.cssText = "position:fixed;opacity:0;pointer-events:none";
|
|
2034
|
+
document.body.append(ta);
|
|
2035
|
+
ta.select();
|
|
2036
|
+
let ok = false;
|
|
2037
|
+
try {
|
|
2038
|
+
ok = document.execCommand("copy");
|
|
2039
|
+
} catch {}
|
|
2040
|
+
ta.remove();
|
|
2041
|
+
return ok;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
|
|
2045
|
+
on("click", async (e) => {
|
|
2046
|
+
const b = near(e, "[data-asta-copy]");
|
|
2047
|
+
if (!b || b.disabled) return;
|
|
2048
|
+
let text = b.getAttribute("data-asta-copy") || "";
|
|
2049
|
+
const from = b.getAttribute("data-asta-copy-from");
|
|
2050
|
+
const src = from ? document.querySelector(from) : null;
|
|
2051
|
+
if (src) text = src instanceof HTMLInputElement || src instanceof HTMLTextAreaElement ? src.value : src.textContent.trim();
|
|
2052
|
+
const ok = await writeText(text);
|
|
2053
|
+
b.dataset.state = ok ? "copied" : "failed";
|
|
2054
|
+
const status = b.nextElementSibling?.matches("[data-asta-copy-status]") ? b.nextElementSibling : null;
|
|
2055
|
+
if (status) status.textContent = ok ? b.dataset.copied || "Copied" : "Couldn't copy";
|
|
2056
|
+
if (ok) emit(b, "asta:copy", { text });
|
|
2057
|
+
clearTimeout(b._astaTimer);
|
|
2058
|
+
b._astaTimer = setTimeout(() => {
|
|
2059
|
+
b.dataset.state = "idle";
|
|
2060
|
+
if (status) status.textContent = "";
|
|
2061
|
+
}, Number(b.dataset.timeout) || 1600);
|
|
2062
|
+
});
|
|
2063
|
+
|
|
2064
|
+
/* ---------- Radio-like groups: SegmentedControl, ToggleGroup (single), Rating ---------- */
|
|
2065
|
+
|
|
2066
|
+
// What was checked just before a pointer or key press, so a change can slide from it (or a click can clear it).
|
|
2067
|
+
const before = new WeakMap();
|
|
2068
|
+
const GROUPS = "[data-asta-segmented],[data-asta-toggle='single'],[data-asta-rating]";
|
|
2069
|
+
const remember = (e) => {
|
|
2070
|
+
const root = near(e, GROUPS);
|
|
2071
|
+
if (root) before.set(root, { input: root.querySelector("input:checked"), pointer: e.type === "pointerdown" });
|
|
2072
|
+
};
|
|
2073
|
+
on("pointerdown", remember, true);
|
|
2074
|
+
on("keydown", remember, true);
|
|
2075
|
+
|
|
2076
|
+
// FLIP the new background from the old one's box, so it slides with transform only.
|
|
2077
|
+
function slidePill(root, from, to) {
|
|
2078
|
+
const ind = to.parentElement.querySelector("[data-asta-ind]");
|
|
2079
|
+
if (!ind || reduce() || from === to) return;
|
|
2080
|
+
const next = to.parentElement;
|
|
2081
|
+
const prev = from?.parentElement;
|
|
2082
|
+
const squash = root.hasAttribute("data-asta-segmented");
|
|
2083
|
+
ind.style.transition = "none";
|
|
2084
|
+
ind.style.transformOrigin = "left";
|
|
2085
|
+
if (prev && prev.offsetWidth) {
|
|
2086
|
+
const dx = prev.offsetLeft - next.offsetLeft;
|
|
2087
|
+
ind.style.transform = `translateX(${dx}px) scaleX(${prev.offsetWidth / next.offsetWidth})${squash ? " scaleY(.86)" : ""}`;
|
|
2088
|
+
} else {
|
|
2089
|
+
ind.style.opacity = "0";
|
|
2090
|
+
ind.style.transform = "scale(.94)";
|
|
2091
|
+
}
|
|
2092
|
+
void ind.offsetWidth;
|
|
2093
|
+
ind.style.transition = `transform ${squash ? 0.5 : 0.44}s ${EASE}, opacity .25s ${EASE}`;
|
|
2094
|
+
ind.style.transform = "";
|
|
2095
|
+
ind.style.opacity = "";
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
on("change", (e) => {
|
|
2099
|
+
const input = e.target;
|
|
2100
|
+
if (!(input instanceof HTMLInputElement)) return;
|
|
2101
|
+
const pill = input.closest("[data-asta-segmented],[data-asta-toggle='single']");
|
|
2102
|
+
if (pill && input.checked) {
|
|
2103
|
+
slidePill(pill, before.get(pill)?.input, input);
|
|
2104
|
+
before.delete(pill);
|
|
2105
|
+
}
|
|
2106
|
+
const rating = input.closest("[data-asta-rating]");
|
|
2107
|
+
if (rating && input.checked) rate(rating, Number(input.value), true);
|
|
2108
|
+
});
|
|
2109
|
+
|
|
2110
|
+
// A click on the pressed option of a ToggleGroup with AllowEmpty, or on the current star, clears it.
|
|
2111
|
+
on("click", (e) => {
|
|
2112
|
+
const input = e.target;
|
|
2113
|
+
if (!(input instanceof HTMLInputElement) || input.type !== "radio") return;
|
|
2114
|
+
const root = input.closest("[data-asta-toggle][data-allow-empty],[data-asta-rating][data-clear]");
|
|
2115
|
+
if (!root) return;
|
|
2116
|
+
const was = before.get(root);
|
|
2117
|
+
before.delete(root);
|
|
2118
|
+
if (!was || was.input !== input) return;
|
|
2119
|
+
if (root.hasAttribute("data-asta-rating")) {
|
|
2120
|
+
if (!was.pointer || Number(input.value) <= 0) return;
|
|
2121
|
+
const zero = root.querySelector('input[value="0"]');
|
|
2122
|
+
if (zero) {
|
|
2123
|
+
zero.checked = true;
|
|
2124
|
+
emit(zero, "change");
|
|
2125
|
+
}
|
|
2126
|
+
} else {
|
|
2127
|
+
input.checked = false;
|
|
2128
|
+
emit(input, "change");
|
|
2129
|
+
}
|
|
2130
|
+
});
|
|
2131
|
+
|
|
2132
|
+
/* ---------- Rating ---------- */
|
|
2133
|
+
|
|
2134
|
+
function paintStars(root, v, preview) {
|
|
2135
|
+
root.querySelectorAll("[data-asta-star]").forEach((s, i) => {
|
|
2136
|
+
s.dataset.fill = v >= i + 1 ? "full" : v >= i + 0.5 ? "half" : "none";
|
|
2137
|
+
s.toggleAttribute("data-preview", preview);
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
function rate(root, v, pop) {
|
|
2141
|
+
root.dataset.value = String(v);
|
|
2142
|
+
paintStars(root, v, false);
|
|
2143
|
+
if (!pop || reduce()) return;
|
|
2144
|
+
root.querySelectorAll("[data-asta-star-pop]").forEach((p, i) => {
|
|
2145
|
+
if (i < Math.ceil(v)) replay(p, "asta-rating-pop");
|
|
2146
|
+
else p.classList.remove("asta-rating-pop");
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
on("pointermove", (e) => {
|
|
2150
|
+
const label = near(e, "[data-asta-rating] label");
|
|
2151
|
+
if (!label) return;
|
|
2152
|
+
const root = label.closest("[data-asta-rating]");
|
|
2153
|
+
const v = Number(label.querySelector("input")?.value);
|
|
2154
|
+
const cur = Number(root.dataset.value) || 0;
|
|
2155
|
+
paintStars(root, v, v !== cur);
|
|
2156
|
+
});
|
|
2157
|
+
on(
|
|
2158
|
+
"pointerleave",
|
|
2159
|
+
(e) => {
|
|
2160
|
+
if (e.target instanceof Element && e.target.matches("[data-asta-rating]")) paintStars(e.target, Number(e.target.dataset.value) || 0, false);
|
|
2161
|
+
},
|
|
2162
|
+
true,
|
|
2163
|
+
);
|
|
2164
|
+
|
|
2165
|
+
/* ---------- NumberInput ---------- */
|
|
2166
|
+
|
|
2167
|
+
const DIGIT =
|
|
2168
|
+
'<span class="relative inline-block h-[1.25em] w-[0.62em] overflow-hidden" data-asta-digit><span class="absolute inset-x-0 top-0 flex flex-col transition-transform duration-500 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none">' +
|
|
2169
|
+
Array.from({ length: 10 }, (_, i) => `<span class="flex h-[1.25em] items-center justify-center">${i}</span>`).join("") +
|
|
2170
|
+
"</span></span>";
|
|
2171
|
+
|
|
2172
|
+
function numberSet(root, v) {
|
|
2173
|
+
const min = Number(root.dataset.min);
|
|
2174
|
+
const max = Number(root.dataset.max);
|
|
2175
|
+
const input = root.querySelector("[data-asta-number-input]");
|
|
2176
|
+
const view = root.querySelector("[data-asta-number-view]");
|
|
2177
|
+
const cur = Number(input.value);
|
|
2178
|
+
const next = Math.min(max, Math.max(min, v));
|
|
2179
|
+
if (next === cur) {
|
|
2180
|
+
// Already at a limit: nudge to say so.
|
|
2181
|
+
if (v !== cur) {
|
|
2182
|
+
view.dataset.bump = v < min ? "min" : "max";
|
|
2183
|
+
clearTimeout(view._astaBump);
|
|
2184
|
+
view._astaBump = setTimeout(() => delete view.dataset.bump, 260);
|
|
2185
|
+
}
|
|
2186
|
+
return false;
|
|
2187
|
+
}
|
|
2188
|
+
input.value = String(next);
|
|
2189
|
+
view.setAttribute("aria-valuenow", String(next));
|
|
2190
|
+
// Reuse strips from the right, so 9 → 10 rolls the last digit and adds one on the left.
|
|
2191
|
+
const wrap = root.querySelector("[data-asta-number-digits]");
|
|
2192
|
+
const digits = String(Math.abs(next)).split("").map(Number);
|
|
2193
|
+
let strips = [...wrap.querySelectorAll("[data-asta-digit]")];
|
|
2194
|
+
while (strips.length < digits.length) {
|
|
2195
|
+
const t = document.createElement("template");
|
|
2196
|
+
t.innerHTML = DIGIT;
|
|
2197
|
+
const s = t.content.firstElementChild;
|
|
2198
|
+
s.firstElementChild.style.transform = `translateY(${-digits[digits.length - strips.length - 1] * 1.25}em)`;
|
|
2199
|
+
wrap.insertBefore(s, strips[0] || null);
|
|
2200
|
+
strips = [s, ...strips];
|
|
2201
|
+
}
|
|
2202
|
+
while (strips.length > digits.length) strips.shift().remove();
|
|
2203
|
+
let minus = wrap.querySelector("[data-minus]");
|
|
2204
|
+
if (next < 0 && !minus) {
|
|
2205
|
+
minus = document.createElement("span");
|
|
2206
|
+
minus.dataset.minus = "";
|
|
2207
|
+
minus.textContent = "−";
|
|
2208
|
+
wrap.prepend(minus);
|
|
2209
|
+
} else if (next >= 0) minus?.remove();
|
|
2210
|
+
strips.forEach((s, i) => (s.firstElementChild.style.transform = `translateY(${-digits[i] * 1.25}em)`));
|
|
2211
|
+
root.querySelector('[data-asta-number-step="-1"]').disabled = next <= min;
|
|
2212
|
+
root.querySelector('[data-asta-number-step="1"]').disabled = next >= max;
|
|
2213
|
+
emit(input, "input");
|
|
2214
|
+
emit(input, "change");
|
|
2215
|
+
return true;
|
|
2216
|
+
}
|
|
2217
|
+
const numberValue = (root) => Number(root.querySelector("[data-asta-number-input]").value);
|
|
2218
|
+
|
|
2219
|
+
let holdTimer = null;
|
|
2220
|
+
const holdStop = () => {
|
|
2221
|
+
clearTimeout(holdTimer);
|
|
2222
|
+
holdTimer = null;
|
|
2223
|
+
};
|
|
2224
|
+
on("pointerdown", (e) => {
|
|
2225
|
+
const b = near(e, "[data-asta-number-step]");
|
|
2226
|
+
if (!b || e.button !== 0 || b.disabled) return;
|
|
2227
|
+
const root = b.closest("[data-asta-number]");
|
|
2228
|
+
const dir = Number(b.dataset.astaNumberStep) * Number(root.dataset.step || 1);
|
|
2229
|
+
try {
|
|
2230
|
+
b.setPointerCapture(e.pointerId);
|
|
2231
|
+
} catch {}
|
|
2232
|
+
holdStop();
|
|
2233
|
+
numberSet(root, numberValue(root) + dir);
|
|
2234
|
+
// Repeat while held, faster the longer it's held.
|
|
2235
|
+
let delay = 380;
|
|
2236
|
+
const tick = () => {
|
|
2237
|
+
if (!numberSet(root, numberValue(root) + dir)) return holdStop();
|
|
2238
|
+
delay = Math.max(40, delay * 0.82);
|
|
2239
|
+
holdTimer = setTimeout(tick, delay);
|
|
2240
|
+
};
|
|
2241
|
+
holdTimer = setTimeout(tick, delay);
|
|
2242
|
+
});
|
|
2243
|
+
// Screen readers click without a pointer press.
|
|
2244
|
+
on("click", (e) => {
|
|
2245
|
+
const b = near(e, "[data-asta-number-step]");
|
|
2246
|
+
if (!b || e.detail !== 0 || b.disabled) return;
|
|
2247
|
+
const root = b.closest("[data-asta-number]");
|
|
2248
|
+
numberSet(root, numberValue(root) + Number(b.dataset.astaNumberStep) * Number(root.dataset.step || 1));
|
|
2249
|
+
});
|
|
2250
|
+
["pointerup", "pointercancel", "lostpointercapture"].forEach((t) => on(t, (e) => near(e, "[data-asta-number-step]") && holdStop()));
|
|
2251
|
+
on("contextmenu", (e) => near(e, "[data-asta-number-step]") && e.preventDefault());
|
|
2252
|
+
on("keydown", (e) => {
|
|
2253
|
+
const view = near(e, "[data-asta-number-view]");
|
|
2254
|
+
if (!view) return;
|
|
2255
|
+
const root = view.closest("[data-asta-number]");
|
|
2256
|
+
const step = Number(root.dataset.step || 1);
|
|
2257
|
+
const cur = numberValue(root);
|
|
2258
|
+
const k = e.key;
|
|
2259
|
+
if (k === "ArrowUp" || k === "ArrowRight") numberSet(root, cur + step);
|
|
2260
|
+
else if (k === "ArrowDown" || k === "ArrowLeft") numberSet(root, cur - step);
|
|
2261
|
+
else if (k === "PageUp") numberSet(root, cur + step * 10);
|
|
2262
|
+
else if (k === "PageDown") numberSet(root, cur - step * 10);
|
|
2263
|
+
else if (k === "Home") numberSet(root, Number(root.dataset.min));
|
|
2264
|
+
else if (k === "End") numberSet(root, Number(root.dataset.max));
|
|
2265
|
+
else if (/^[0-9]$/.test(k)) {
|
|
2266
|
+
const typed = e.timeStamp - (view._astaAt || 0) > 900 ? k : (view._astaTyped || "") + k;
|
|
2267
|
+
view._astaTyped = typed;
|
|
2268
|
+
view._astaAt = e.timeStamp;
|
|
2269
|
+
numberSet(root, Number(typed));
|
|
2270
|
+
} else return;
|
|
2271
|
+
e.preventDefault();
|
|
2272
|
+
});
|
|
2273
|
+
|
|
2274
|
+
/* ---------- OtpInput ---------- */
|
|
2275
|
+
|
|
2276
|
+
function otpPaint(root) {
|
|
2277
|
+
const input = root.querySelector("input");
|
|
2278
|
+
const boxes = [...root.querySelectorAll("[data-asta-otp-box]")];
|
|
2279
|
+
const code = input.value;
|
|
2280
|
+
const n = boxes.length;
|
|
2281
|
+
const err = input.getAttribute("aria-invalid") === "true";
|
|
2282
|
+
const active = document.activeElement === input && !input.disabled ? Math.min(code.length, n - 1) : -1;
|
|
2283
|
+
boxes.forEach((b, i) => {
|
|
2284
|
+
const ch = code[i] || "";
|
|
2285
|
+
b.dataset.state = err ? "error" : i === active ? "active" : code.length === n ? "full" : ch ? "filled" : "";
|
|
2286
|
+
const had = b.dataset.ch ?? b.textContent.trim();
|
|
2287
|
+
if (had !== ch || (!ch && b.querySelector(".asta-otp-caret"))) {
|
|
2288
|
+
b.innerHTML = ch ? `<span class="asta-otp-ch">${esc(ch)}</span>` : "";
|
|
2289
|
+
}
|
|
2290
|
+
b.dataset.ch = ch;
|
|
2291
|
+
if (!ch && i === active && !err && !b.firstChild) b.innerHTML = '<span class="asta-otp-caret"></span>';
|
|
2292
|
+
});
|
|
2293
|
+
}
|
|
2294
|
+
function otpSet(input, raw) {
|
|
2295
|
+
const root = input.closest("[data-asta-otp]");
|
|
2296
|
+
const n = root.querySelectorAll("[data-asta-otp-box]").length;
|
|
2297
|
+
const prev = input._astaPrev ?? input.defaultValue;
|
|
2298
|
+
const next = (input.hasAttribute("data-letters") ? raw.replace(/[^a-z0-9]/gi, "").toUpperCase() : raw.replace(/\D/g, "")).slice(0, n);
|
|
2299
|
+
input.value = next;
|
|
2300
|
+
input._astaPrev = next;
|
|
2301
|
+
if (next !== prev) {
|
|
2302
|
+
// Typing again clears an error the server rendered.
|
|
2303
|
+
input.removeAttribute("aria-invalid");
|
|
2304
|
+
root.querySelector("[data-asta-otp-boxes]").classList.remove("asta-otp-shake");
|
|
2305
|
+
}
|
|
2306
|
+
otpPaint(root);
|
|
2307
|
+
if (next.length === n && prev.length < n) {
|
|
2308
|
+
const boxes = root.querySelector("[data-asta-otp-boxes]");
|
|
2309
|
+
boxes.removeAttribute("data-wave");
|
|
2310
|
+
void boxes.offsetWidth;
|
|
2311
|
+
boxes.setAttribute("data-wave", "");
|
|
2312
|
+
emit(input, "asta:complete", { code: next });
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
const otpEnd = (input) => {
|
|
2316
|
+
const n = input.value.length;
|
|
2317
|
+
if (input.selectionStart !== n || input.selectionEnd !== n) input.setSelectionRange(n, n);
|
|
2318
|
+
};
|
|
2319
|
+
const otpInput = (e) => (e.target instanceof HTMLInputElement && e.target.parentElement?.matches("[data-asta-otp]") ? e.target : null);
|
|
2320
|
+
on("input", (e) => {
|
|
2321
|
+
const i = otpInput(e);
|
|
2322
|
+
if (i) otpSet(i, i.value);
|
|
2323
|
+
});
|
|
2324
|
+
on("paste", (e) => {
|
|
2325
|
+
const i = otpInput(e);
|
|
2326
|
+
if (!i) return;
|
|
2327
|
+
// Paste replaces the whole code, wherever the caret was.
|
|
2328
|
+
e.preventDefault();
|
|
2329
|
+
otpSet(i, e.clipboardData?.getData("text") || "");
|
|
2330
|
+
});
|
|
2331
|
+
["focusin", "focusout"].forEach((t) =>
|
|
2332
|
+
on(t, (e) => {
|
|
2333
|
+
const i = otpInput(e);
|
|
2334
|
+
if (!i) return;
|
|
2335
|
+
if (t === "focusin") otpEnd(i);
|
|
2336
|
+
otpPaint(i.parentElement);
|
|
2337
|
+
}),
|
|
2338
|
+
);
|
|
2339
|
+
["select", "click", "keyup"].forEach((t) =>
|
|
2340
|
+
on(
|
|
2341
|
+
t,
|
|
2342
|
+
(e) => {
|
|
2343
|
+
const i = otpInput(e);
|
|
2344
|
+
if (i) otpEnd(i);
|
|
2345
|
+
},
|
|
2346
|
+
true,
|
|
2347
|
+
),
|
|
2348
|
+
);
|
|
2349
|
+
|
|
2350
|
+
/* ---------- PasswordInput ---------- */
|
|
2351
|
+
|
|
2352
|
+
const WORDS = ["Weak", "Weak", "Fair", "Good", "Strong"];
|
|
2353
|
+
function passwordCheck(root) {
|
|
2354
|
+
const input = root.querySelector("[data-asta-password-input]");
|
|
2355
|
+
const v = input.value;
|
|
2356
|
+
const rules = [...root.querySelectorAll("[data-asta-password-rule]")];
|
|
2357
|
+
let score = 0;
|
|
2358
|
+
rules.forEach((r) => {
|
|
2359
|
+
let ok = false;
|
|
2360
|
+
try {
|
|
2361
|
+
ok = new RegExp(r.getAttribute("data-asta-password-rule")).test(v);
|
|
2362
|
+
} catch {}
|
|
2363
|
+
if (ok) score++;
|
|
2364
|
+
r.toggleAttribute("data-on", ok);
|
|
2365
|
+
const said = r.querySelector("[data-asta-password-said]");
|
|
2366
|
+
if (said) said.textContent = ok ? ", done" : ", not yet";
|
|
2367
|
+
});
|
|
2368
|
+
root.querySelectorAll("[data-asta-password-seg]").forEach((s, i) => s.toggleAttribute("data-on", i < score));
|
|
2369
|
+
root.toggleAttribute("data-strong", rules.length > 0 && score === rules.length);
|
|
2370
|
+
const word = root.querySelector("[data-asta-password-word]");
|
|
2371
|
+
if (word) {
|
|
2372
|
+
const w = !v || !rules.length ? "" : WORDS[Math.round((score / rules.length) * 4)];
|
|
2373
|
+
word.innerHTML = w ? `<span class="sr-only">Strength: </span>${w}` : "";
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
on("input", (e) => {
|
|
2377
|
+
const root = near(e, "[data-asta-password]");
|
|
2378
|
+
if (root && e.target.matches("[data-asta-password-input]")) passwordCheck(root);
|
|
2379
|
+
});
|
|
2380
|
+
on("pointerdown", (e) => near(e, "[data-asta-password-toggle]") && e.preventDefault());
|
|
2381
|
+
on("click", (e) => {
|
|
2382
|
+
const b = near(e, "[data-asta-password-toggle]");
|
|
2383
|
+
if (!b) return;
|
|
2384
|
+
const input = b.closest("[data-asta-password]").querySelector("[data-asta-password-input]");
|
|
2385
|
+
const show = input.type === "password";
|
|
2386
|
+
input.type = show ? "text" : "password";
|
|
2387
|
+
b.setAttribute("aria-pressed", String(show));
|
|
2388
|
+
b.setAttribute("aria-label", show ? "Hide password" : "Show password");
|
|
2389
|
+
});
|
|
2390
|
+
|
|
2391
|
+
/* ---------- TagInput ---------- */
|
|
2392
|
+
|
|
2393
|
+
const tagsOf = (root) => [...root.querySelectorAll("[data-asta-tag]")];
|
|
2394
|
+
function tagsSync(root) {
|
|
2395
|
+
const input = root.querySelector("[data-asta-tags-input]");
|
|
2396
|
+
const max = Number(root.dataset.max) || 8;
|
|
2397
|
+
const n = tagsOf(root).length;
|
|
2398
|
+
const full = n >= max;
|
|
2399
|
+
const count = root.querySelector("[data-asta-tags-count]");
|
|
2400
|
+
count.textContent = `${n}/${max}`;
|
|
2401
|
+
count.classList.toggle("text-[#e5484d]", full);
|
|
2402
|
+
count.classList.toggle("text-mute", !full);
|
|
2403
|
+
input.disabled = full && !input.value;
|
|
2404
|
+
input.placeholder = full ? "Limit reached" : n ? "" : root.dataset.placeholder || "";
|
|
2405
|
+
}
|
|
2406
|
+
function tagsAdd(root, raws) {
|
|
2407
|
+
const input = root.querySelector("[data-asta-tags-input]");
|
|
2408
|
+
const list = root.querySelector("[data-asta-tags-list]");
|
|
2409
|
+
const tpl = root.querySelector("template[data-asta-tags-tpl]");
|
|
2410
|
+
const max = Number(root.dataset.max) || 8;
|
|
2411
|
+
let added = false;
|
|
2412
|
+
for (const r of raws) {
|
|
2413
|
+
const t = r.trim().replace(/\s+/g, " ");
|
|
2414
|
+
if (!t) continue;
|
|
2415
|
+
const tags = tagsOf(root);
|
|
2416
|
+
const dup = tags.find((li) => li.dataset.astaTag.toLowerCase() === t.toLowerCase());
|
|
2417
|
+
if (dup) {
|
|
2418
|
+
if (!reduce()) replay(dup, "asta-tag-shake");
|
|
2419
|
+
continue;
|
|
2420
|
+
}
|
|
2421
|
+
if (tags.length >= max) break;
|
|
2422
|
+
const li = tpl.content.firstElementChild.cloneNode(true);
|
|
2423
|
+
li.dataset.astaTag = t;
|
|
2424
|
+
li.querySelector("[data-asta-tag-text]").textContent = t;
|
|
2425
|
+
li.querySelector("[data-asta-tag-remove]").setAttribute("aria-label", `Remove ${t}`);
|
|
2426
|
+
const hidden = li.querySelector("input[type=hidden]");
|
|
2427
|
+
if (hidden) hidden.value = t;
|
|
2428
|
+
list.append(li);
|
|
2429
|
+
added = true;
|
|
2430
|
+
}
|
|
2431
|
+
input.value = "";
|
|
2432
|
+
tagsSync(root);
|
|
2433
|
+
if (added) emit(root, "change");
|
|
2434
|
+
}
|
|
2435
|
+
function tagsRemove(root, li) {
|
|
2436
|
+
li.remove();
|
|
2437
|
+
tagsSync(root);
|
|
2438
|
+
root.querySelector("[data-asta-tags-input]").focus();
|
|
2439
|
+
emit(root, "change");
|
|
2440
|
+
}
|
|
2441
|
+
const tagsDisarm = (root) => tagsOf(root).forEach((li) => li.removeAttribute("data-armed"));
|
|
2442
|
+
const tagsInput = (e) => (e.target instanceof HTMLInputElement && e.target.matches("[data-asta-tags-input]") ? e.target : null);
|
|
2443
|
+
on("keydown", (e) => {
|
|
2444
|
+
const input = tagsInput(e);
|
|
2445
|
+
if (!input) return;
|
|
2446
|
+
const root = input.closest("[data-asta-tags]");
|
|
2447
|
+
const tags = tagsOf(root);
|
|
2448
|
+
const last = tags[tags.length - 1];
|
|
2449
|
+
if (e.key === "Enter") {
|
|
2450
|
+
e.preventDefault();
|
|
2451
|
+
tagsAdd(root, [input.value]);
|
|
2452
|
+
} else if (e.key === "Backspace" && !input.value && last) {
|
|
2453
|
+
// The first Backspace marks the last tag, the second removes it.
|
|
2454
|
+
if (last.hasAttribute("data-armed")) tagsRemove(root, last);
|
|
2455
|
+
else last.setAttribute("data-armed", "");
|
|
2456
|
+
} else if (e.key === "Escape") tagsDisarm(root);
|
|
2457
|
+
});
|
|
2458
|
+
on("input", (e) => {
|
|
2459
|
+
const input = tagsInput(e);
|
|
2460
|
+
if (!input) return;
|
|
2461
|
+
const root = input.closest("[data-asta-tags]");
|
|
2462
|
+
tagsDisarm(root);
|
|
2463
|
+
if (input.value.includes(",")) tagsAdd(root, input.value.split(","));
|
|
2464
|
+
});
|
|
2465
|
+
on("paste", (e) => {
|
|
2466
|
+
const input = tagsInput(e);
|
|
2467
|
+
const v = e.clipboardData?.getData("text") || "";
|
|
2468
|
+
if (!input || !/[,\n]/.test(v)) return;
|
|
2469
|
+
e.preventDefault();
|
|
2470
|
+
tagsAdd(input.closest("[data-asta-tags]"), v.split(/[,\n]/));
|
|
2471
|
+
});
|
|
2472
|
+
on("focusout", (e) => {
|
|
2473
|
+
const input = tagsInput(e);
|
|
2474
|
+
if (!input) return;
|
|
2475
|
+
const root = input.closest("[data-asta-tags]");
|
|
2476
|
+
tagsDisarm(root);
|
|
2477
|
+
if (input.value.trim()) tagsAdd(root, [input.value]);
|
|
2478
|
+
});
|
|
2479
|
+
on("click", (e) => {
|
|
2480
|
+
const rm = near(e, "[data-asta-tag-remove]");
|
|
2481
|
+
if (rm) {
|
|
2482
|
+
e.stopPropagation();
|
|
2483
|
+
tagsRemove(rm.closest("[data-asta-tags]"), rm.closest("[data-asta-tag]"));
|
|
2484
|
+
return;
|
|
2485
|
+
}
|
|
2486
|
+
const box = near(e, "[data-asta-tags-box]");
|
|
2487
|
+
if (box) box.querySelector("[data-asta-tags-input]")?.focus();
|
|
2488
|
+
});
|
|
2489
|
+
|
|
2490
|
+
/* ---------- RangeSlider ---------- */
|
|
2491
|
+
|
|
2492
|
+
function rangeSync(root, moved) {
|
|
2493
|
+
const [a, b] = root.querySelectorAll("[data-asta-range-input]");
|
|
2494
|
+
const gap = Number(root.dataset.gap) || 0;
|
|
2495
|
+
const min = Number(a.min);
|
|
2496
|
+
const max = Number(a.max);
|
|
2497
|
+
// The thumbs never cross and keep the gap.
|
|
2498
|
+
if (moved === a && Number(a.value) > Number(b.value) - gap) a.value = String(Math.max(min, Number(b.value) - gap));
|
|
2499
|
+
if (moved === b && Number(b.value) < Number(a.value) + gap) b.value = String(Math.min(max, Number(a.value) + gap));
|
|
2500
|
+
const lo = Number(a.value);
|
|
2501
|
+
const hi = Number(b.value);
|
|
2502
|
+
const pct = (v) => (max > min ? ((v - min) / (max - min)) * 100 : 0);
|
|
2503
|
+
root.querySelector("[data-asta-range-fill]").style.transform = `translateX(${pct(lo)}%) scaleX(${(pct(hi) - pct(lo)) / 100})`;
|
|
2504
|
+
const fmt = (v) => `${root.dataset.prefix || ""}${v}${root.dataset.suffix || ""}`;
|
|
2505
|
+
[lo, hi].forEach((v, i) => {
|
|
2506
|
+
const at = root.querySelector(`[data-asta-range-at="${i}"]`);
|
|
2507
|
+
// cqw = % of the track, so the zero-width marker never widens the page.
|
|
2508
|
+
at.style.transform = `translateX(${pct(v)}cqw)`;
|
|
2509
|
+
at.querySelector("[data-asta-range-bubble]").textContent = fmt(v);
|
|
2510
|
+
[a, b][i].setAttribute("aria-valuetext", fmt(v));
|
|
2511
|
+
});
|
|
2512
|
+
// The thumb you moved stays on top, and a low thumb parked at the max must stay grabbable.
|
|
2513
|
+
if (moved) {
|
|
2514
|
+
a.style.zIndex = moved === a || lo >= max ? "2" : "1";
|
|
2515
|
+
b.style.zIndex = moved === a || lo >= max ? "1" : "2";
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
on("input", (e) => {
|
|
2519
|
+
const input = e.target instanceof Element && e.target.closest("[data-asta-range-input]");
|
|
2520
|
+
if (!input) return;
|
|
2521
|
+
const root = input.closest("[data-asta-range]");
|
|
2522
|
+
if (!e.isTrusted) return rangeSync(root, input);
|
|
2523
|
+
root.removeAttribute("data-glide");
|
|
2524
|
+
rangeSync(root, input);
|
|
2525
|
+
});
|
|
2526
|
+
on("pointerdown", (e) => {
|
|
2527
|
+
const track = near(e, "[data-asta-range-track]");
|
|
2528
|
+
if (!track || e.button !== 0) return;
|
|
2529
|
+
const root = track.closest("[data-asta-range]");
|
|
2530
|
+
const [a, b] = root.querySelectorAll("[data-asta-range-input]");
|
|
2531
|
+
if (a.disabled) return;
|
|
2532
|
+
const r = track.getBoundingClientRect();
|
|
2533
|
+
const min = Number(a.min);
|
|
2534
|
+
const max = Number(a.max);
|
|
2535
|
+
const v = min + Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)) * (max - min);
|
|
2536
|
+
const lo = Number(a.value);
|
|
2537
|
+
const hi = Number(b.value);
|
|
2538
|
+
const input = lo === hi ? (v < lo ? a : b) : Math.abs(v - lo) < Math.abs(v - hi) ? a : b;
|
|
2539
|
+
e.preventDefault();
|
|
2540
|
+
root.setAttribute("data-glide", "");
|
|
2541
|
+
input.value = String(v);
|
|
2542
|
+
input.focus({ preventScroll: true });
|
|
2543
|
+
emit(input, "input");
|
|
2544
|
+
emit(input, "change");
|
|
2545
|
+
});
|
|
2546
|
+
|
|
2547
|
+
/* ---------- ColorPicker ---------- */
|
|
2548
|
+
|
|
2549
|
+
const parseHex = (s) => {
|
|
2550
|
+
const h = String(s).trim().replace(/^#/, "").toLowerCase();
|
|
2551
|
+
if (/^[0-9a-f]{3}$/.test(h)) return `#${[...h].map((c) => c + c).join("")}`;
|
|
2552
|
+
if (/^[0-9a-f]{6}$/.test(h)) return `#${h}`;
|
|
2553
|
+
return null;
|
|
2554
|
+
};
|
|
2555
|
+
const colorParts = (root) => ({
|
|
2556
|
+
radios: [...root.querySelectorAll("input[type=radio]:not([data-asta-color-custom])")],
|
|
2557
|
+
custom: root.querySelector("[data-asta-color-custom]"),
|
|
2558
|
+
hex: root.querySelector("[data-asta-color-hex]"),
|
|
2559
|
+
preview: root.querySelector("[data-asta-color-preview]"),
|
|
2560
|
+
hint: root.querySelector("[data-asta-color-hint]"),
|
|
2561
|
+
});
|
|
2562
|
+
const colorCurrent = (root) => root.querySelector("input[type=radio]:checked")?.value || "";
|
|
2563
|
+
function colorPreview(root, hex) {
|
|
2564
|
+
const { preview } = colorParts(root);
|
|
2565
|
+
if (!preview) return;
|
|
2566
|
+
preview.style.background = hex || "transparent";
|
|
2567
|
+
preview.toggleAttribute("data-empty", !hex);
|
|
2568
|
+
}
|
|
2569
|
+
function colorShow(root) {
|
|
2570
|
+
const { hex, hint } = colorParts(root);
|
|
2571
|
+
const cur = colorCurrent(root);
|
|
2572
|
+
if (hex) hex.value = cur.replace(/^#/, "");
|
|
2573
|
+
if (hint) {
|
|
2574
|
+
hint.textContent = "";
|
|
2575
|
+
hint.removeAttribute("data-on");
|
|
2576
|
+
}
|
|
2577
|
+
hex?.removeAttribute("aria-invalid");
|
|
2578
|
+
colorPreview(root, cur);
|
|
2579
|
+
}
|
|
2580
|
+
function colorCommit(root) {
|
|
2581
|
+
const { radios, custom, hex } = colorParts(root);
|
|
2582
|
+
const v = parseHex(hex.value);
|
|
2583
|
+
if (v && v !== colorCurrent(root)) {
|
|
2584
|
+
const match = radios.find((r) => r.value === v);
|
|
2585
|
+
const target = match || custom;
|
|
2586
|
+
target.value = v;
|
|
2587
|
+
target.checked = true;
|
|
2588
|
+
emit(target, "change");
|
|
2589
|
+
}
|
|
2590
|
+
colorShow(root);
|
|
2591
|
+
}
|
|
2592
|
+
on("change", (e) => {
|
|
2593
|
+
const root = e.target instanceof HTMLInputElement && e.target.type === "radio" && e.target.closest("[data-asta-color]");
|
|
2594
|
+
if (root) colorShow(root);
|
|
2595
|
+
});
|
|
2596
|
+
on("keydown", (e) => {
|
|
2597
|
+
const t = e.target;
|
|
2598
|
+
const root = t instanceof HTMLInputElement && t.closest("[data-asta-color]");
|
|
2599
|
+
if (!root) return;
|
|
2600
|
+
if (t.type === "radio" && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
2601
|
+
// Up and down move a whole row, like the grid looks.
|
|
2602
|
+
const { radios } = colorParts(root);
|
|
2603
|
+
const cols = Number(root.dataset.columns) || 5;
|
|
2604
|
+
const i = radios.indexOf(t);
|
|
2605
|
+
const j = e.key === "ArrowDown" ? Math.min(radios.length - 1, i + cols) : Math.max(0, i - cols);
|
|
2606
|
+
e.preventDefault();
|
|
2607
|
+
if (i < 0 || j === i) return;
|
|
2608
|
+
radios[j].checked = true;
|
|
2609
|
+
radios[j].focus();
|
|
2610
|
+
emit(radios[j], "change");
|
|
2611
|
+
} else if (t.matches("[data-asta-color-hex]")) {
|
|
2612
|
+
if (e.key === "Enter") {
|
|
2613
|
+
e.preventDefault();
|
|
2614
|
+
colorCommit(root);
|
|
2615
|
+
} else if (e.key === "Escape") {
|
|
2616
|
+
e.preventDefault();
|
|
2617
|
+
colorShow(root);
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
});
|
|
2621
|
+
on("input", (e) => {
|
|
2622
|
+
const t = e.target;
|
|
2623
|
+
if (!(t instanceof HTMLInputElement) || !t.matches("[data-asta-color-hex]")) return;
|
|
2624
|
+
const root = t.closest("[data-asta-color]");
|
|
2625
|
+
const { hint } = colorParts(root);
|
|
2626
|
+
const v = parseHex(t.value);
|
|
2627
|
+
const bad = t.value.trim() !== "" && !v;
|
|
2628
|
+
colorPreview(root, v);
|
|
2629
|
+
t.toggleAttribute("aria-invalid", bad);
|
|
2630
|
+
if (hint) {
|
|
2631
|
+
hint.textContent = bad ? "Not a hex" : "";
|
|
2632
|
+
hint.toggleAttribute("data-on", bad);
|
|
2633
|
+
}
|
|
2634
|
+
});
|
|
2635
|
+
on("focusout", (e) => {
|
|
2636
|
+
const t = e.target;
|
|
2637
|
+
if (t instanceof HTMLInputElement && t.matches("[data-asta-color-hex]")) colorCommit(t.closest("[data-asta-color]"));
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
/* ---------- Combobox ---------- */
|
|
2641
|
+
|
|
2642
|
+
const combo = (root) => ({
|
|
2643
|
+
input: root.querySelector("[data-asta-combo-input]"),
|
|
2644
|
+
list: root.querySelector('[role="listbox"]'),
|
|
2645
|
+
opts: [...root.querySelectorAll('[role="option"]')],
|
|
2646
|
+
multi: root.dataset.astaCombo === "multiple",
|
|
2647
|
+
value: root.querySelector("[data-asta-combo-value]"),
|
|
2648
|
+
});
|
|
2649
|
+
const comboShown = (root) => combo(root).opts.filter((o) => !o.hidden);
|
|
2650
|
+
const comboEnabled = (root) => comboShown(root).filter((o) => o.getAttribute("aria-disabled") !== "true");
|
|
2651
|
+
function comboActive(root, opt) {
|
|
2652
|
+
const { input, opts } = combo(root);
|
|
2653
|
+
opts.forEach((o) => o.toggleAttribute("data-active", o === opt));
|
|
2654
|
+
if (opt) {
|
|
2655
|
+
input.setAttribute("aria-activedescendant", opt.id);
|
|
2656
|
+
opt.scrollIntoView({ block: "nearest" });
|
|
2657
|
+
} else input.removeAttribute("aria-activedescendant");
|
|
2658
|
+
}
|
|
2659
|
+
function comboFilter(root, q) {
|
|
2660
|
+
const { opts } = combo(root);
|
|
2661
|
+
const query = q.trim().toLowerCase();
|
|
2662
|
+
let n = 0;
|
|
2663
|
+
for (const o of opts) {
|
|
2664
|
+
const label = o.dataset.label || "";
|
|
2665
|
+
const i = query ? label.toLowerCase().indexOf(query) : -1;
|
|
2666
|
+
o.hidden = Boolean(query) && i < 0;
|
|
2667
|
+
if (!o.hidden) n++;
|
|
2668
|
+
const text = o.querySelector("[data-asta-combo-text]");
|
|
2669
|
+
// The typed part in bold.
|
|
2670
|
+
text.innerHTML =
|
|
2671
|
+
i < 0
|
|
2672
|
+
? esc(label)
|
|
2673
|
+
: `${esc(label.slice(0, i))}<span class="font-semibold text-ink">${esc(label.slice(i, i + query.length))}</span>${esc(label.slice(i + query.length))}`;
|
|
2674
|
+
}
|
|
2675
|
+
const empty = root.querySelector("[data-asta-combo-empty]");
|
|
2676
|
+
empty.hidden = n > 0;
|
|
2677
|
+
const said = root.querySelector("[data-asta-combo-query]");
|
|
2678
|
+
said.textContent = query ? `Nothing matches “${q.trim()}”` : "";
|
|
2679
|
+
}
|
|
2680
|
+
function comboOpen(root) {
|
|
2681
|
+
const { input, list, multi } = combo(root);
|
|
2682
|
+
if (input.disabled || root.hasAttribute("data-open")) return;
|
|
2683
|
+
root.setAttribute("data-open", "");
|
|
2684
|
+
list.hidden = false;
|
|
2685
|
+
input.setAttribute("aria-expanded", "true");
|
|
2686
|
+
root.querySelector("[data-asta-combo-toggle]")?.setAttribute("aria-label", "Close list");
|
|
2687
|
+
comboFilter(root, root._astaQuery ?? "");
|
|
2688
|
+
const picked = !multi && comboEnabled(root).find((o) => o.getAttribute("aria-selected") === "true");
|
|
2689
|
+
comboActive(root, picked || comboEnabled(root)[0]);
|
|
2690
|
+
}
|
|
2691
|
+
function comboClose(root, dismiss) {
|
|
2692
|
+
const { input, list, multi, value, opts } = combo(root);
|
|
2693
|
+
if (!root.hasAttribute("data-open") && root._astaQuery == null) return;
|
|
2694
|
+
// Leaving a single combobox with its text cleared clears the value.
|
|
2695
|
+
if (dismiss && !multi && root._astaQuery != null && !root._astaQuery.trim() && opts.some((o) => o.getAttribute("aria-selected") === "true")) {
|
|
2696
|
+
opts.forEach((o) => o.setAttribute("aria-selected", "false"));
|
|
2697
|
+
if (value) {
|
|
2698
|
+
value.value = "";
|
|
2699
|
+
emit(value, "change");
|
|
2700
|
+
} else emit(root, "change");
|
|
2701
|
+
}
|
|
2702
|
+
root._astaQuery = null;
|
|
2703
|
+
root.removeAttribute("data-open");
|
|
2704
|
+
list.hidden = true;
|
|
2705
|
+
input.setAttribute("aria-expanded", "false");
|
|
2706
|
+
input.removeAttribute("aria-activedescendant");
|
|
2707
|
+
root.querySelector("[data-asta-combo-toggle]")?.setAttribute("aria-label", "Open list");
|
|
2708
|
+
if (!multi) input.value = opts.find((o) => o.getAttribute("aria-selected") === "true")?.dataset.label || "";
|
|
2709
|
+
}
|
|
2710
|
+
function comboChipSync(root) {
|
|
2711
|
+
const { input, multi } = combo(root);
|
|
2712
|
+
if (!multi) return;
|
|
2713
|
+
input.placeholder = root.querySelector("[data-asta-combo-chip]") ? "" : input.dataset.placeholder || "";
|
|
2714
|
+
}
|
|
2715
|
+
function comboPick(root, opt) {
|
|
2716
|
+
if (!opt || opt.getAttribute("aria-disabled") === "true") return;
|
|
2717
|
+
const { input, multi, value, opts } = combo(root);
|
|
2718
|
+
if (multi) {
|
|
2719
|
+
const v = opt.dataset.value;
|
|
2720
|
+
const chip = [...root.querySelectorAll("[data-asta-combo-chip]")].find((c) => c.dataset.astaComboChip === v);
|
|
2721
|
+
if (chip) {
|
|
2722
|
+
chip.remove();
|
|
2723
|
+
opt.setAttribute("aria-selected", "false");
|
|
2724
|
+
} else {
|
|
2725
|
+
const c = root.querySelector("template[data-asta-combo-tpl]").content.firstElementChild.cloneNode(true);
|
|
2726
|
+
c.dataset.astaComboChip = v;
|
|
2727
|
+
c.querySelector("[data-asta-combo-chip-text]").textContent = opt.dataset.label;
|
|
2728
|
+
c.querySelector("[data-asta-combo-remove]").setAttribute("aria-label", `Remove ${opt.dataset.label}`);
|
|
2729
|
+
const icon = opt.querySelector("[data-asta-combo-icon]");
|
|
2730
|
+
if (icon) {
|
|
2731
|
+
const s = document.createElement("span");
|
|
2732
|
+
s.className = "inline-flex shrink-0 [&_svg]:h-3.5 [&_svg]:w-3.5";
|
|
2733
|
+
s.innerHTML = icon.innerHTML;
|
|
2734
|
+
c.prepend(s);
|
|
2735
|
+
}
|
|
2736
|
+
const hidden = c.querySelector("input[type=hidden]");
|
|
2737
|
+
if (hidden) hidden.value = v;
|
|
2738
|
+
input.before(c);
|
|
2739
|
+
opt.setAttribute("aria-selected", "true");
|
|
2740
|
+
}
|
|
2741
|
+
root._astaQuery = null;
|
|
2742
|
+
input.value = "";
|
|
2743
|
+
comboFilter(root, "");
|
|
2744
|
+
comboChipSync(root);
|
|
2745
|
+
input.focus();
|
|
2746
|
+
emit(root, "change");
|
|
2747
|
+
return;
|
|
2748
|
+
}
|
|
2749
|
+
opts.forEach((o) => o.setAttribute("aria-selected", String(o === opt)));
|
|
2750
|
+
root._astaQuery = null;
|
|
2751
|
+
comboClose(root, false);
|
|
2752
|
+
if (value) {
|
|
2753
|
+
value.value = opt.dataset.value;
|
|
2754
|
+
emit(value, "change");
|
|
2755
|
+
} else emit(root, "change");
|
|
2756
|
+
}
|
|
2757
|
+
function comboMove(root, step) {
|
|
2758
|
+
const list = comboEnabled(root);
|
|
2759
|
+
if (!list.length) return;
|
|
2760
|
+
const i = list.findIndex((o) => o.hasAttribute("data-active"));
|
|
2761
|
+
comboActive(root, list[(i + step + list.length) % list.length] || list[0]);
|
|
2762
|
+
}
|
|
2763
|
+
const comboInput = (e) => (e.target instanceof HTMLInputElement && e.target.matches("[data-asta-combo-input]") ? e.target : null);
|
|
2764
|
+
on("focusin", (e) => {
|
|
2765
|
+
const input = comboInput(e);
|
|
2766
|
+
if (!input) return;
|
|
2767
|
+
const root = input.closest("[data-asta-combo]");
|
|
2768
|
+
if (root.dataset.astaCombo !== "multiple") input.select();
|
|
2769
|
+
comboOpen(root);
|
|
2770
|
+
});
|
|
2771
|
+
on("focusout", (e) => {
|
|
2772
|
+
const root = near(e, "[data-asta-combo]");
|
|
2773
|
+
if (root && !root.contains(e.relatedTarget)) comboClose(root, true);
|
|
2774
|
+
});
|
|
2775
|
+
on("input", (e) => {
|
|
2776
|
+
const input = comboInput(e);
|
|
2777
|
+
if (!input) return;
|
|
2778
|
+
const root = input.closest("[data-asta-combo]");
|
|
2779
|
+
root._astaQuery = input.value;
|
|
2780
|
+
comboOpen(root);
|
|
2781
|
+
comboFilter(root, input.value);
|
|
2782
|
+
comboActive(root, comboEnabled(root)[0]);
|
|
2783
|
+
});
|
|
2784
|
+
on("keydown", (e) => {
|
|
2785
|
+
const input = comboInput(e);
|
|
2786
|
+
if (!input) return;
|
|
2787
|
+
const root = input.closest("[data-asta-combo]");
|
|
2788
|
+
const open = root.hasAttribute("data-open");
|
|
2789
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
2790
|
+
if (!open) comboOpen(root);
|
|
2791
|
+
else comboMove(root, e.key === "ArrowDown" ? 1 : -1);
|
|
2792
|
+
} else if (e.key === "Enter") {
|
|
2793
|
+
if (!open) return;
|
|
2794
|
+
comboPick(root, root.querySelector('[role="option"][data-active]:not([hidden])'));
|
|
2795
|
+
} else if (e.key === "Escape") {
|
|
2796
|
+
if (!open && root._astaQuery == null) return;
|
|
2797
|
+
comboClose(root, false);
|
|
2798
|
+
} else if (e.key === "Backspace" && root.dataset.astaCombo === "multiple" && !input.value) {
|
|
2799
|
+
const chips = root.querySelectorAll("[data-asta-combo-chip]");
|
|
2800
|
+
const last = chips[chips.length - 1];
|
|
2801
|
+
if (!last) return;
|
|
2802
|
+
const opt = combo(root).opts.find((o) => o.dataset.value === last.dataset.astaComboChip);
|
|
2803
|
+
if (opt) comboPick(root, opt);
|
|
2804
|
+
else last.remove();
|
|
2805
|
+
} else return;
|
|
2806
|
+
e.preventDefault();
|
|
2807
|
+
});
|
|
2808
|
+
on("pointerdown", (e) => {
|
|
2809
|
+
const t = el(e);
|
|
2810
|
+
if (!t) return;
|
|
2811
|
+
if (t.closest('[data-asta-combo] [role="listbox"]') || t.closest("[data-asta-combo-toggle]") || t.closest("[data-asta-combo-remove]")) {
|
|
2812
|
+
// Keep focus in the field.
|
|
2813
|
+
e.preventDefault();
|
|
2814
|
+
return;
|
|
2815
|
+
}
|
|
2816
|
+
const box = t.matches("[data-asta-combo-box]") ? t : null;
|
|
2817
|
+
if (box) {
|
|
2818
|
+
e.preventDefault();
|
|
2819
|
+
const root = box.closest("[data-asta-combo]");
|
|
2820
|
+
combo(root).input.focus();
|
|
2821
|
+
comboOpen(root);
|
|
2822
|
+
}
|
|
2823
|
+
});
|
|
2824
|
+
on("pointermove", (e) => {
|
|
2825
|
+
const opt = near(e, '[data-asta-combo] [role="option"]');
|
|
2826
|
+
if (opt && !opt.hasAttribute("data-active") && opt.getAttribute("aria-disabled") !== "true") comboActive(opt.closest("[data-asta-combo]"), opt);
|
|
2827
|
+
});
|
|
2828
|
+
on("click", (e) => {
|
|
2829
|
+
const opt = near(e, '[data-asta-combo] [role="option"]');
|
|
2830
|
+
if (opt) return comboPick(opt.closest("[data-asta-combo]"), opt);
|
|
2831
|
+
const toggle = near(e, "[data-asta-combo-toggle]");
|
|
2832
|
+
if (toggle) {
|
|
2833
|
+
const root = toggle.closest("[data-asta-combo]");
|
|
2834
|
+
if (root.hasAttribute("data-open")) comboClose(root, false);
|
|
2835
|
+
else {
|
|
2836
|
+
combo(root).input.focus();
|
|
2837
|
+
comboOpen(root);
|
|
2838
|
+
}
|
|
2839
|
+
return;
|
|
2840
|
+
}
|
|
2841
|
+
const rm = near(e, "[data-asta-combo-remove]");
|
|
2842
|
+
if (rm) {
|
|
2843
|
+
const root = rm.closest("[data-asta-combo]");
|
|
2844
|
+
const chip = rm.closest("[data-asta-combo-chip]");
|
|
2845
|
+
const opt = combo(root).opts.find((o) => o.dataset.value === chip.dataset.astaComboChip);
|
|
2846
|
+
if (opt) comboPick(root, opt);
|
|
2847
|
+
else {
|
|
2848
|
+
chip.remove();
|
|
2849
|
+
comboChipSync(root);
|
|
2850
|
+
emit(root, "change");
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
});
|
|
2854
|
+
|
|
2855
|
+
/* ---------- DatePicker ---------- */
|
|
2856
|
+
|
|
2857
|
+
const day0 = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
2858
|
+
const addDays = (d, n) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
|
|
2859
|
+
const month0 = (d) => new Date(d.getFullYear(), d.getMonth(), 1);
|
|
2860
|
+
const addMonths = (d, n) => {
|
|
2861
|
+
const t = new Date(d.getFullYear(), d.getMonth() + n, 1);
|
|
2862
|
+
const last = new Date(t.getFullYear(), t.getMonth() + 1, 0).getDate();
|
|
2863
|
+
return new Date(t.getFullYear(), t.getMonth(), Math.min(d.getDate(), last));
|
|
2864
|
+
};
|
|
2865
|
+
const same = (a, b) => !!a && !!b && a.getTime() === b.getTime();
|
|
2866
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
2867
|
+
const key = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
2868
|
+
const parseDay = (s) => {
|
|
2869
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(String(s || "").trim());
|
|
2870
|
+
return m ? new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])) : null;
|
|
2871
|
+
};
|
|
2872
|
+
|
|
2873
|
+
function dp(root) {
|
|
2874
|
+
if (root._astaDp) return root._astaDp;
|
|
2875
|
+
const locale = root.dataset.locale || "en-US";
|
|
2876
|
+
let fmt, title, full, wd, wdl;
|
|
2877
|
+
try {
|
|
2878
|
+
fmt = new Intl.DateTimeFormat(locale, { month: "short", day: "numeric", year: "numeric" });
|
|
2879
|
+
title = new Intl.DateTimeFormat(locale, { month: "long", year: "numeric" });
|
|
2880
|
+
full = new Intl.DateTimeFormat(locale, { weekday: "long", month: "long", day: "numeric", year: "numeric" });
|
|
2881
|
+
wd = new Intl.DateTimeFormat(locale, { weekday: "short" });
|
|
2882
|
+
wdl = new Intl.DateTimeFormat(locale, { weekday: "long" });
|
|
2883
|
+
} catch {
|
|
2884
|
+
return dp(Object.assign(root, { dataset: Object.assign(root.dataset, { locale: "en-US" }) }));
|
|
2885
|
+
}
|
|
2886
|
+
const s = {
|
|
2887
|
+
range: root.dataset.astaDate === "range",
|
|
2888
|
+
week: Number(root.dataset.week) || 0,
|
|
2889
|
+
min: parseDay(root.dataset.min),
|
|
2890
|
+
max: parseDay(root.dataset.max),
|
|
2891
|
+
fmt,
|
|
2892
|
+
title,
|
|
2893
|
+
full,
|
|
2894
|
+
wd,
|
|
2895
|
+
wdl,
|
|
2896
|
+
start: null,
|
|
2897
|
+
end: null,
|
|
2898
|
+
single: null,
|
|
2899
|
+
view: null,
|
|
2900
|
+
focus: null,
|
|
2901
|
+
hover: null,
|
|
2902
|
+
today: null,
|
|
2903
|
+
};
|
|
2904
|
+
const v = root.querySelector("[data-asta-date-value]").value;
|
|
2905
|
+
if (s.range) {
|
|
2906
|
+
const [a, b] = v.split("/");
|
|
2907
|
+
s.start = parseDay(a);
|
|
2908
|
+
s.end = s.start ? parseDay(b) : null;
|
|
2909
|
+
} else s.single = parseDay(v);
|
|
2910
|
+
root._astaDp = s;
|
|
2911
|
+
return s;
|
|
2912
|
+
}
|
|
2913
|
+
const dpOff = (s, d) => (s.min && d < s.min) || (s.max && d > s.max) || false;
|
|
2914
|
+
const dpClamp = (s, d) => (s.min && d < s.min ? s.min : s.max && d > s.max ? s.max : d);
|
|
2915
|
+
function dpText(root) {
|
|
2916
|
+
const s = dp(root);
|
|
2917
|
+
const t = root.querySelector("[data-asta-date-text]");
|
|
2918
|
+
let shown = null;
|
|
2919
|
+
if (s.range) {
|
|
2920
|
+
if (s.start && s.end) shown = s.fmt.formatRange ? s.fmt.formatRange(s.start, s.end) : `${s.fmt.format(s.start)} – ${s.fmt.format(s.end)}`;
|
|
2921
|
+
else if (s.start) shown = `${s.fmt.format(s.start)} – …`;
|
|
2922
|
+
} else if (s.single) shown = s.fmt.format(s.single);
|
|
2923
|
+
t.textContent = shown ?? root.dataset.placeholder ?? "";
|
|
2924
|
+
t.classList.toggle("text-ink", !!shown);
|
|
2925
|
+
t.classList.toggle("text-mute", !shown);
|
|
2926
|
+
}
|
|
2927
|
+
const DP_NAV =
|
|
2928
|
+
"flex h-8 w-8 items-center justify-center rounded-md text-ink-2 outline-none transition-[background-color,transform] duration-150 hover:bg-paper-2 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50 active:scale-90 disabled:pointer-events-none disabled:opacity-35";
|
|
2929
|
+
const DP_DAY =
|
|
2930
|
+
"relative mx-auto flex h-9 w-9 items-center justify-center rounded-lg text-[13px] tabular-nums outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-brand/50 disabled:cursor-not-allowed disabled:opacity-30";
|
|
2931
|
+
const dpCanFlip = (s, n) => {
|
|
2932
|
+
const to = addMonths(s.view, n);
|
|
2933
|
+
return !(s.min && to < month0(s.min)) && !(s.max && to > month0(s.max));
|
|
2934
|
+
};
|
|
2935
|
+
// Build the month. dir slides the grid in from the side the user paged to.
|
|
2936
|
+
function dpRender(root, dir) {
|
|
2937
|
+
const s = dp(root);
|
|
2938
|
+
const cal = root.querySelector("[data-asta-date-cal]");
|
|
2939
|
+
const lead = (s.view.getDay() - s.week + 7) % 7;
|
|
2940
|
+
const first = addDays(s.view, -lead);
|
|
2941
|
+
const arrow = (d) =>
|
|
2942
|
+
`<button type="button" data-dp-flip="${d}" aria-label="${d === 1 ? "Next month" : "Previous month"}" class="${DP_NAV}"${dpCanFlip(s, d) ? "" : " disabled"}><svg viewBox="0 0 16 16" class="h-4 w-4" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${d === 1 ? "m6 3.5 4.5 4.5L6 12.5" : "m10 3.5L5.5 8l4.5 4.5"}"/></svg></button>`;
|
|
2943
|
+
const weekdays = Array.from({ length: 7 }, (_, i) => {
|
|
2944
|
+
const d = new Date(2026, 10, 1 + ((i + s.week) % 7));
|
|
2945
|
+
return `<span class="py-1 text-center text-[11px] font-medium text-mute" title="${esc(s.wdl.format(d))}">${esc(s.wd.format(d))}</span>`;
|
|
2946
|
+
}).join("");
|
|
2947
|
+
// 6 weeks, always, so the popover never changes height between months.
|
|
2948
|
+
const cells = Array.from({ length: 42 }, (_, i) => {
|
|
2949
|
+
const d = addDays(first, i);
|
|
2950
|
+
const k = key(d);
|
|
2951
|
+
return `<div role="gridcell" class="relative" data-dp-cell="${k}"><span aria-hidden="true" data-dp-band class="absolute inset-y-0 hidden bg-[color-mix(in_srgb,var(--brand)_11%,transparent)]"></span><button type="button" data-dp-day="${k}" tabindex="-1" aria-label="${esc(s.full.format(d))}" class="${DP_DAY} ${d.getMonth() === s.view.getMonth() ? "text-ink" : "text-mute"}"${dpOff(s, d) ? " disabled" : ""}><span class="relative">${d.getDate()}</span></button></div>`;
|
|
2952
|
+
}).join("");
|
|
2953
|
+
const today = s.today && !dpOff(s, s.today);
|
|
2954
|
+
cal.innerHTML =
|
|
2955
|
+
`<div class="mb-2 flex items-center justify-between gap-2 pl-2"><p aria-live="polite" class="text-[14px] font-medium tracking-[-0.01em] text-ink">${esc(s.title.format(s.view))}</p><div class="flex gap-0.5">${arrow(-1)}${arrow(1)}</div></div>` +
|
|
2956
|
+
`<div class="grid grid-cols-7 pb-1" aria-hidden="true">${weekdays}</div>` +
|
|
2957
|
+
`<div class="overflow-hidden"><div role="grid" aria-label="${esc(s.title.format(s.view))}" data-dp-grid class="grid grid-cols-7 gap-y-0.5 ${dir === 1 ? "asta-dp-next" : dir === -1 ? "asta-dp-prev" : ""}">${cells}</div></div>` +
|
|
2958
|
+
`<div class="mt-2 flex items-center justify-between gap-2 pl-2"><p class="text-[12px] tabular-nums text-mute" data-dp-status></p><div class="flex gap-1"><button type="button" data-dp-clear class="h-7 rounded-md px-2.5 text-[12px] font-medium text-ink-2 outline-none transition-colors hover:bg-paper-2 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50">Clear</button>${
|
|
2959
|
+
today
|
|
2960
|
+
? '<button type="button" data-dp-today class="h-7 rounded-md bg-paper-2 px-2.5 text-[12px] font-medium text-ink outline-none transition-colors hover:bg-[color-mix(in_srgb,var(--ink)_8%,var(--paper-2))] focus-visible:ring-2 focus-visible:ring-brand/50">Today</button>'
|
|
2961
|
+
: ""
|
|
2962
|
+
}</div></div>`;
|
|
2963
|
+
dpPaint(root, true);
|
|
2964
|
+
}
|
|
2965
|
+
// Apply picked, band, today and focus to the cells already built, so hovering never rebuilds the month.
|
|
2966
|
+
function dpPaint(root, fresh) {
|
|
2967
|
+
const s = dp(root);
|
|
2968
|
+
const preview = s.range && s.start && !s.end ? s.hover || s.focus : null;
|
|
2969
|
+
const from = s.start;
|
|
2970
|
+
const to = s.end || (preview && s.start && preview > s.start ? preview : null);
|
|
2971
|
+
root.querySelectorAll("[data-dp-cell]").forEach((cell) => {
|
|
2972
|
+
const d = parseDay(cell.dataset.dpCell);
|
|
2973
|
+
const b = cell.lastElementChild;
|
|
2974
|
+
const picked = s.range ? same(d, s.start) || same(d, s.end) : same(d, s.single);
|
|
2975
|
+
const inBand = !!(from && to && d >= from && d <= to && !same(from, to));
|
|
2976
|
+
const band = cell.firstElementChild;
|
|
2977
|
+
band.classList.toggle("hidden", !inBand);
|
|
2978
|
+
band.classList.toggle("left-1/2", inBand && same(d, from));
|
|
2979
|
+
band.classList.toggle("right-1/2", inBand && same(d, to));
|
|
2980
|
+
band.classList.toggle("left-0", inBand && !same(d, from));
|
|
2981
|
+
band.classList.toggle("right-0", inBand && !same(d, to));
|
|
2982
|
+
band.classList.toggle("opacity-70", !s.end);
|
|
2983
|
+
cell.setAttribute("aria-selected", String(picked || inBand));
|
|
2984
|
+
const inMonth = d.getMonth() === s.view.getMonth();
|
|
2985
|
+
b.classList.toggle("font-medium", picked);
|
|
2986
|
+
b.classList.toggle("text-white", picked);
|
|
2987
|
+
b.classList.toggle("text-ink", !picked && inMonth);
|
|
2988
|
+
b.classList.toggle("text-mute", !picked && !inMonth);
|
|
2989
|
+
b.classList.toggle("hover:bg-paper-2", !picked);
|
|
2990
|
+
let fill = b.querySelector("[data-dp-fill]");
|
|
2991
|
+
if (picked && !fill) {
|
|
2992
|
+
fill = document.createElement("span");
|
|
2993
|
+
fill.dataset.dpFill = "";
|
|
2994
|
+
fill.setAttribute("aria-hidden", "true");
|
|
2995
|
+
fill.className = `absolute inset-0 rounded-lg bg-brand shadow-[0_4px_10px_-4px_color-mix(in_srgb,var(--brand)_70%,transparent)]${fresh ? "" : " asta-dp-pop"}`;
|
|
2996
|
+
b.prepend(fill);
|
|
2997
|
+
} else if (!picked) fill?.remove();
|
|
2998
|
+
const isToday = same(d, s.today);
|
|
2999
|
+
let dot = b.querySelector("[data-dp-dot]");
|
|
3000
|
+
if (isToday && !dot) {
|
|
3001
|
+
dot = document.createElement("span");
|
|
3002
|
+
dot.dataset.dpDot = "";
|
|
3003
|
+
dot.setAttribute("aria-hidden", "true");
|
|
3004
|
+
b.append(dot);
|
|
3005
|
+
}
|
|
3006
|
+
if (dot) dot.className = `absolute bottom-1 left-1/2 h-1 w-1 -translate-x-1/2 rounded-full ${picked ? "bg-white" : "bg-brand"}`;
|
|
3007
|
+
if (isToday) b.setAttribute("aria-current", "date");
|
|
3008
|
+
b.tabIndex = same(d, s.focus) ? 0 : -1;
|
|
3009
|
+
});
|
|
3010
|
+
const status = root.querySelector("[data-dp-status]");
|
|
3011
|
+
if (status)
|
|
3012
|
+
status.textContent =
|
|
3013
|
+
s.range && s.start && !s.end ? "Pick an end date" : s.range && s.start && s.end ? `${Math.round((s.end - s.start) / 864e5) + 1} days` : "";
|
|
3014
|
+
}
|
|
3015
|
+
function dpFocusDay(root) {
|
|
3016
|
+
const s = dp(root);
|
|
3017
|
+
root.querySelector(`[data-dp-day="${key(s.focus)}"]`)?.focus({ preventScroll: true });
|
|
3018
|
+
}
|
|
3019
|
+
function dpOpen(root) {
|
|
3020
|
+
const s = dp(root);
|
|
3021
|
+
const trigger = root.querySelector("[data-asta-date-trigger]");
|
|
3022
|
+
if (trigger.disabled) return;
|
|
3023
|
+
s.today = day0(new Date());
|
|
3024
|
+
const anchor = dpClamp(s, (s.range ? s.start : s.single) || s.today);
|
|
3025
|
+
s.view = month0(anchor);
|
|
3026
|
+
s.focus = anchor;
|
|
3027
|
+
s.hover = null;
|
|
3028
|
+
root.querySelector("[data-asta-date-cal]").hidden = false;
|
|
3029
|
+
root.setAttribute("data-open", "");
|
|
3030
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
3031
|
+
dpRender(root, 0);
|
|
3032
|
+
dpFocusDay(root);
|
|
3033
|
+
}
|
|
3034
|
+
function dpClose(root, refocus) {
|
|
3035
|
+
if (!root.hasAttribute("data-open")) return;
|
|
3036
|
+
root.querySelector("[data-asta-date-cal]").hidden = true;
|
|
3037
|
+
root.removeAttribute("data-open");
|
|
3038
|
+
const trigger = root.querySelector("[data-asta-date-trigger]");
|
|
3039
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
3040
|
+
if (refocus) trigger.focus();
|
|
3041
|
+
}
|
|
3042
|
+
function dpMove(root, d, steer) {
|
|
3043
|
+
const s = dp(root);
|
|
3044
|
+
const next = dpClamp(s, d);
|
|
3045
|
+
if (next.getFullYear() !== s.view.getFullYear() || next.getMonth() !== s.view.getMonth()) {
|
|
3046
|
+
const dir = next > s.view ? 1 : -1;
|
|
3047
|
+
s.view = month0(next);
|
|
3048
|
+
s.focus = next;
|
|
3049
|
+
dpRender(root, dir);
|
|
3050
|
+
} else {
|
|
3051
|
+
s.focus = next;
|
|
3052
|
+
dpPaint(root, false);
|
|
3053
|
+
}
|
|
3054
|
+
if (steer) dpFocusDay(root);
|
|
3055
|
+
}
|
|
3056
|
+
function dpFlip(root, n, steer) {
|
|
3057
|
+
const s = dp(root);
|
|
3058
|
+
if (!dpCanFlip(s, n)) return;
|
|
3059
|
+
s.view = addMonths(s.view, n);
|
|
3060
|
+
s.view = month0(s.view);
|
|
3061
|
+
s.focus = dpClamp(s, addMonths(s.focus, n));
|
|
3062
|
+
dpRender(root, n > 0 ? 1 : -1);
|
|
3063
|
+
if (steer) dpFocusDay(root);
|
|
3064
|
+
}
|
|
3065
|
+
function dpSave(root) {
|
|
3066
|
+
const s = dp(root);
|
|
3067
|
+
const input = root.querySelector("[data-asta-date-value]");
|
|
3068
|
+
const v = s.range ? (s.start && s.end ? `${key(s.start)}/${key(s.end)}` : "") : s.single ? key(s.single) : "";
|
|
3069
|
+
dpText(root);
|
|
3070
|
+
if (input.value === v) return;
|
|
3071
|
+
input.value = v;
|
|
3072
|
+
emit(input, "change");
|
|
3073
|
+
}
|
|
3074
|
+
function dpPick(root, d) {
|
|
3075
|
+
const s = dp(root);
|
|
3076
|
+
if (dpOff(s, d)) return;
|
|
3077
|
+
if (!s.range) {
|
|
3078
|
+
s.single = d;
|
|
3079
|
+
dpSave(root);
|
|
3080
|
+
dpClose(root, true);
|
|
3081
|
+
return;
|
|
3082
|
+
}
|
|
3083
|
+
if (!s.start || s.end || d < s.start) {
|
|
3084
|
+
s.start = d;
|
|
3085
|
+
s.end = null;
|
|
3086
|
+
} else s.end = d;
|
|
3087
|
+
dpSave(root);
|
|
3088
|
+
if (s.end) dpClose(root, true);
|
|
3089
|
+
else {
|
|
3090
|
+
s.focus = d;
|
|
3091
|
+
dpPaint(root, false);
|
|
3092
|
+
dpFocusDay(root);
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
on("click", (e) => {
|
|
3096
|
+
const t = el(e);
|
|
3097
|
+
const root = t?.closest("[data-asta-date]");
|
|
3098
|
+
// A click outside closes any open calendar.
|
|
3099
|
+
for (const r of document.querySelectorAll("[data-asta-date][data-open]")) if (r !== root) dpClose(r, false);
|
|
3100
|
+
if (!root) return;
|
|
3101
|
+
if (t.closest("[data-asta-date-trigger]")) return root.hasAttribute("data-open") ? dpClose(root, false) : dpOpen(root);
|
|
3102
|
+
const day = t.closest("[data-dp-day]");
|
|
3103
|
+
if (day) return dpPick(root, parseDay(day.dataset.dpDay));
|
|
3104
|
+
const flip = t.closest("[data-dp-flip]");
|
|
3105
|
+
if (flip) return dpFlip(root, Number(flip.dataset.dpFlip), false);
|
|
3106
|
+
if (t.closest("[data-dp-clear]")) {
|
|
3107
|
+
const s = dp(root);
|
|
3108
|
+
s.single = s.start = s.end = null;
|
|
3109
|
+
dpSave(root);
|
|
3110
|
+
return dpClose(root, true);
|
|
3111
|
+
}
|
|
3112
|
+
if (t.closest("[data-dp-today]")) dpMove(root, dp(root).today, true);
|
|
3113
|
+
});
|
|
3114
|
+
on("pointerover", (e) => {
|
|
3115
|
+
const day = near(e, "[data-asta-date] [data-dp-day]");
|
|
3116
|
+
if (!day) return;
|
|
3117
|
+
const root = day.closest("[data-asta-date]");
|
|
3118
|
+
const s = dp(root);
|
|
3119
|
+
if (!s.range || !s.start || s.end) return;
|
|
3120
|
+
s.hover = parseDay(day.dataset.dpDay);
|
|
3121
|
+
dpPaint(root, false);
|
|
3122
|
+
});
|
|
3123
|
+
on("keydown", (e) => {
|
|
3124
|
+
const t = el(e);
|
|
3125
|
+
const root = t?.closest("[data-asta-date]");
|
|
3126
|
+
if (!root) return;
|
|
3127
|
+
if (t.matches("[data-asta-date-trigger]") && e.key === "ArrowDown" && !root.hasAttribute("data-open")) {
|
|
3128
|
+
e.preventDefault();
|
|
3129
|
+
return dpOpen(root);
|
|
3130
|
+
}
|
|
3131
|
+
if (!t.closest("[data-asta-date-cal]")) return;
|
|
3132
|
+
if (e.key === "Escape") {
|
|
3133
|
+
e.preventDefault();
|
|
3134
|
+
return dpClose(root, true);
|
|
3135
|
+
}
|
|
3136
|
+
if (!t.matches("[data-dp-day]")) return;
|
|
3137
|
+
const s = dp(root);
|
|
3138
|
+
const f = s.focus;
|
|
3139
|
+
const dow = (f.getDay() - s.week + 7) % 7;
|
|
3140
|
+
if (e.key === "ArrowRight") dpMove(root, addDays(f, 1), true);
|
|
3141
|
+
else if (e.key === "ArrowLeft") dpMove(root, addDays(f, -1), true);
|
|
3142
|
+
else if (e.key === "ArrowDown") dpMove(root, addDays(f, 7), true);
|
|
3143
|
+
else if (e.key === "ArrowUp") dpMove(root, addDays(f, -7), true);
|
|
3144
|
+
else if (e.key === "Home") dpMove(root, addDays(f, -dow), true);
|
|
3145
|
+
else if (e.key === "End") dpMove(root, addDays(f, 6 - dow), true);
|
|
3146
|
+
else if (e.key === "PageDown") dpFlip(root, e.shiftKey ? 12 : 1, true);
|
|
3147
|
+
else if (e.key === "PageUp") dpFlip(root, e.shiftKey ? -12 : -1, true);
|
|
3148
|
+
else if (e.key === "Enter" || e.key === " ") dpPick(root, f);
|
|
3149
|
+
else return;
|
|
3150
|
+
e.preventDefault();
|
|
3151
|
+
});
|
|
3152
|
+
on("focusin", (e) => {
|
|
3153
|
+
const day = near(e, "[data-asta-date] [data-dp-day]");
|
|
3154
|
+
if (!day) return;
|
|
3155
|
+
const root = day.closest("[data-asta-date]");
|
|
3156
|
+
const s = dp(root);
|
|
3157
|
+
const d = parseDay(day.dataset.dpDay);
|
|
3158
|
+
if (!same(d, s.focus)) {
|
|
3159
|
+
s.focus = d;
|
|
3160
|
+
dpPaint(root, false);
|
|
3161
|
+
}
|
|
3162
|
+
});
|
|
3163
|
+
|
|
3164
|
+
/* ---------- FileUpload ---------- */
|
|
3165
|
+
|
|
3166
|
+
const fmtBytes = (n) => (n < 1024 ? `${n} B` : n < 1024 ** 2 ? `${(n / 1024).toFixed(n < 10240 ? 1 : 0)} KB` : `${(n / 1024 ** 2).toFixed(1)} MB`);
|
|
3167
|
+
const accepts = (file, accept) => {
|
|
3168
|
+
if (!accept) return true;
|
|
3169
|
+
const name = file.name.toLowerCase();
|
|
3170
|
+
const type = file.type.toLowerCase();
|
|
3171
|
+
return accept
|
|
3172
|
+
.split(",")
|
|
3173
|
+
.map((a) => a.trim().toLowerCase())
|
|
3174
|
+
.filter(Boolean)
|
|
3175
|
+
.some((a) => (a.startsWith(".") ? name.endsWith(a) : a.endsWith("/*") ? type.startsWith(a.slice(0, -1)) : type === a));
|
|
3176
|
+
};
|
|
3177
|
+
const extOf = (name) => {
|
|
3178
|
+
const i = name.lastIndexOf(".");
|
|
3179
|
+
return i > 0 ? name.slice(i + 1, i + 5).toUpperCase() : "FILE";
|
|
3180
|
+
};
|
|
3181
|
+
function uploadSync(root) {
|
|
3182
|
+
const input = root.querySelector("[data-asta-upload-input]");
|
|
3183
|
+
const list = root.querySelector("[data-asta-upload-list]");
|
|
3184
|
+
const files = root._astaFiles || [];
|
|
3185
|
+
try {
|
|
3186
|
+
const dt = new DataTransfer();
|
|
3187
|
+
files.forEach((f) => dt.items.add(f));
|
|
3188
|
+
input.files = dt.files;
|
|
3189
|
+
} catch {}
|
|
3190
|
+
const rows = [...list.children];
|
|
3191
|
+
rows.forEach((li) => !files.includes(li._astaFile) && li.remove());
|
|
3192
|
+
const have = new Set(rows.map((li) => li._astaFile));
|
|
3193
|
+
const tpl = root.querySelector("template[data-asta-upload-tpl]");
|
|
3194
|
+
for (const f of files) {
|
|
3195
|
+
if (have.has(f)) continue;
|
|
3196
|
+
const li = tpl.content.firstElementChild.cloneNode(true);
|
|
3197
|
+
li._astaFile = f;
|
|
3198
|
+
li.querySelector("[data-ext]").textContent = extOf(f.name);
|
|
3199
|
+
li.querySelector("[data-file-name]").textContent = f.name;
|
|
3200
|
+
li.querySelector("[data-file-meta]").textContent = fmtBytes(f.size);
|
|
3201
|
+
li.querySelector("[role=progressbar]").setAttribute("aria-label", `${f.name} upload`);
|
|
3202
|
+
li.querySelector("[data-asta-upload-remove]").setAttribute("aria-label", `Remove ${f.name}`);
|
|
3203
|
+
list.append(li);
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
function uploadError(root, msg) {
|
|
3207
|
+
const p = root.querySelector("[data-asta-upload-error]");
|
|
3208
|
+
p.querySelector("[data-asta-upload-error-text]").textContent = msg || "";
|
|
3209
|
+
if (msg) {
|
|
3210
|
+
p.hidden = true;
|
|
3211
|
+
void p.offsetWidth;
|
|
3212
|
+
}
|
|
3213
|
+
p.hidden = !msg;
|
|
3214
|
+
}
|
|
3215
|
+
function uploadAdd(root, list) {
|
|
3216
|
+
const input = root.querySelector("[data-asta-upload-input]");
|
|
3217
|
+
if (input.disabled) return;
|
|
3218
|
+
const incoming = [...list];
|
|
3219
|
+
if (!incoming.length) return;
|
|
3220
|
+
const multiple = input.multiple;
|
|
3221
|
+
const current = root._astaFiles || [];
|
|
3222
|
+
const maxFiles = Number(root.dataset.maxFiles) || 0;
|
|
3223
|
+
const maxSize = Number(root.dataset.maxSize) || 0;
|
|
3224
|
+
// A single-file input replaces what is there, so only a multiple list counts existing files.
|
|
3225
|
+
const room = maxFiles ? Math.max(0, maxFiles - (multiple ? current.length : 0)) : Infinity;
|
|
3226
|
+
const rejected = [];
|
|
3227
|
+
const ok = [];
|
|
3228
|
+
for (const f of multiple ? incoming : incoming.slice(0, 1)) {
|
|
3229
|
+
if (!accepts(f, root.dataset.accept)) rejected.push(`${f.name} isn't an accepted file type.`);
|
|
3230
|
+
else if (maxSize && f.size > maxSize) rejected.push(`${f.name} is over ${fmtBytes(maxSize)}.`);
|
|
3231
|
+
else if (ok.length >= room) rejected.push(`Only ${maxFiles} file${maxFiles === 1 ? "" : "s"} allowed.`);
|
|
3232
|
+
else ok.push(f);
|
|
3233
|
+
}
|
|
3234
|
+
uploadError(root, rejected.length ? (rejected.length === 1 ? rejected[0] : `${rejected[0]} ${rejected.length - 1} more skipped.`) : "");
|
|
3235
|
+
if (ok.length) root._astaFiles = multiple ? [...current, ...ok] : ok;
|
|
3236
|
+
uploadSync(root);
|
|
3237
|
+
}
|
|
3238
|
+
on("change", (e) => {
|
|
3239
|
+
const input = e.target instanceof HTMLInputElement && e.target.matches("[data-asta-upload-input]") ? e.target : null;
|
|
3240
|
+
if (!input || input._astaSyncing) return;
|
|
3241
|
+
// The picker replaces the input's files. Merge them into the list instead.
|
|
3242
|
+
uploadAdd(input.closest("[data-asta-upload]"), input.files);
|
|
3243
|
+
});
|
|
3244
|
+
on("click", (e) => {
|
|
3245
|
+
const rm = near(e, "[data-asta-upload-remove]");
|
|
3246
|
+
if (!rm) return;
|
|
3247
|
+
const root = rm.closest("[data-asta-upload]");
|
|
3248
|
+
const f = rm.closest("li")._astaFile;
|
|
3249
|
+
root._astaFiles = (root._astaFiles || []).filter((x) => x !== f);
|
|
3250
|
+
uploadError(root, "");
|
|
3251
|
+
uploadSync(root);
|
|
3252
|
+
});
|
|
3253
|
+
["dragenter", "dragleave", "dragover", "drop"].forEach((type) =>
|
|
3254
|
+
on(type, (e) => {
|
|
3255
|
+
const zone = near(e, "[data-asta-upload-zone]");
|
|
3256
|
+
if (!zone) return;
|
|
3257
|
+
e.preventDefault();
|
|
3258
|
+
const root = zone.closest("[data-asta-upload]");
|
|
3259
|
+
if (root.querySelector("[data-asta-upload-input]").disabled) return;
|
|
3260
|
+
if (type === "dragenter") {
|
|
3261
|
+
zone._astaDepth = (zone._astaDepth || 0) + 1;
|
|
3262
|
+
zone.setAttribute("data-over", "");
|
|
3263
|
+
} else if (type === "dragleave") {
|
|
3264
|
+
zone._astaDepth = Math.max(0, (zone._astaDepth || 0) - 1);
|
|
3265
|
+
if (!zone._astaDepth) zone.removeAttribute("data-over");
|
|
3266
|
+
} else if (type === "dragover") {
|
|
3267
|
+
if (e.dataTransfer) e.dataTransfer.dropEffect = "copy";
|
|
3268
|
+
} else {
|
|
3269
|
+
zone._astaDepth = 0;
|
|
3270
|
+
zone.removeAttribute("data-over");
|
|
3271
|
+
if (e.dataTransfer) uploadAdd(root, e.dataTransfer.files);
|
|
3272
|
+
}
|
|
3273
|
+
}),
|
|
3274
|
+
);
|
|
3275
|
+
// htmx reports upload progress on the element that sent the request. Show it on the rows of the uploads inside.
|
|
3276
|
+
const uploadsFor = (e) => {
|
|
3277
|
+
const t = el(e);
|
|
3278
|
+
if (!t) return [];
|
|
3279
|
+
const scope = t.closest("form") || t;
|
|
3280
|
+
return [...scope.querySelectorAll("[data-asta-upload]"), ...(t.closest("[data-asta-upload]") ? [t.closest("[data-asta-upload]")] : [])];
|
|
3281
|
+
};
|
|
3282
|
+
on("htmx:xhr:progress", (e) => {
|
|
3283
|
+
const { loaded, total } = e.detail || {};
|
|
3284
|
+
if (!total) return;
|
|
3285
|
+
const p = Math.min(1, loaded / total);
|
|
3286
|
+
for (const root of uploadsFor(e))
|
|
3287
|
+
for (const li of root.querySelectorAll("[data-asta-upload-list] > li")) {
|
|
3288
|
+
li.dataset.status = "uploading";
|
|
3289
|
+
li.querySelector("[data-file-bar]").style.transform = `scaleX(${p})`;
|
|
3290
|
+
li.querySelector("[data-file-meta]").textContent = `${Math.round(p * 100)}%`;
|
|
3291
|
+
li.querySelector("[role=progressbar]").setAttribute("aria-valuenow", String(Math.round(p * 100)));
|
|
3292
|
+
}
|
|
3293
|
+
});
|
|
3294
|
+
on("htmx:afterRequest", (e) => {
|
|
3295
|
+
for (const root of uploadsFor(e))
|
|
3296
|
+
for (const li of root.querySelectorAll('[data-asta-upload-list] > li[data-status="uploading"]')) {
|
|
3297
|
+
const ok = e.detail?.successful;
|
|
3298
|
+
li.dataset.status = ok ? "done" : "error";
|
|
3299
|
+
li.querySelector("[data-file-meta]").textContent = ok ? fmtBytes(li._astaFile.size) : "Failed";
|
|
3300
|
+
}
|
|
3301
|
+
});
|
|
3302
|
+
|
|
3303
|
+
/* ---------- InlineEdit ---------- */
|
|
3304
|
+
|
|
3305
|
+
const inline = (root) => ({
|
|
3306
|
+
view: root.querySelector("[data-asta-inline-view]"),
|
|
3307
|
+
input: root.querySelector("[data-asta-inline-input]"),
|
|
3308
|
+
sizer: root.querySelector("[data-asta-inline-sizer]"),
|
|
3309
|
+
text: root.querySelector("[data-asta-inline-text]"),
|
|
3310
|
+
tick: root.querySelector("[data-asta-inline-tick]"),
|
|
3311
|
+
status: root.querySelector("[data-asta-inline-status]"),
|
|
3312
|
+
});
|
|
3313
|
+
function inlineOpen(root) {
|
|
3314
|
+
const { view, input, sizer, tick } = inline(root);
|
|
3315
|
+
if (view.disabled || root.hasAttribute("data-editing")) return;
|
|
3316
|
+
input.value = input.dataset.saved || "";
|
|
3317
|
+
sizer.textContent = `${input.value || input.placeholder} `;
|
|
3318
|
+
root.removeAttribute("data-failed");
|
|
3319
|
+
clearTimeout(root._astaTick);
|
|
3320
|
+
tick.classList.add("hidden");
|
|
3321
|
+
tick.classList.remove("asta-inline-tick");
|
|
3322
|
+
root.setAttribute("data-editing", "");
|
|
3323
|
+
input.focus();
|
|
3324
|
+
input.select();
|
|
3325
|
+
}
|
|
3326
|
+
function inlineClose(root, keepFocus) {
|
|
3327
|
+
const { view, input } = inline(root);
|
|
3328
|
+
root._astaSaving = null;
|
|
3329
|
+
input.readOnly = false;
|
|
3330
|
+
root.removeAttribute("data-editing");
|
|
3331
|
+
root.removeAttribute("data-failed");
|
|
3332
|
+
if (keepFocus) view.focus();
|
|
3333
|
+
}
|
|
3334
|
+
function inlineCancel(root, keepFocus) {
|
|
3335
|
+
if (root._astaSaving) return;
|
|
3336
|
+
const { input } = inline(root);
|
|
3337
|
+
input.value = input.dataset.saved || "";
|
|
3338
|
+
inlineClose(root, keepFocus);
|
|
3339
|
+
emit(input, "asta:cancel");
|
|
3340
|
+
}
|
|
3341
|
+
function inlineDone(root, next, keepFocus) {
|
|
3342
|
+
const { view, input, text, tick, status } = inline(root);
|
|
3343
|
+
const label = input.getAttribute("aria-label") || "";
|
|
3344
|
+
input.dataset.saved = next;
|
|
3345
|
+
input.value = next;
|
|
3346
|
+
text.textContent = next || input.placeholder;
|
|
3347
|
+
view.classList.toggle("text-ink", !!next);
|
|
3348
|
+
view.classList.toggle("text-mute", !next);
|
|
3349
|
+
view.setAttribute("aria-label", `${label}: ${next || input.placeholder}. Edit`);
|
|
3350
|
+
inlineClose(root, keepFocus);
|
|
3351
|
+
tick.classList.remove("hidden");
|
|
3352
|
+
replay(tick, "asta-inline-tick");
|
|
3353
|
+
status.textContent = "Saved";
|
|
3354
|
+
clearTimeout(root._astaTick);
|
|
3355
|
+
root._astaTick = setTimeout(() => {
|
|
3356
|
+
tick.classList.add("hidden");
|
|
3357
|
+
tick.classList.remove("asta-inline-tick");
|
|
3358
|
+
status.textContent = "";
|
|
3359
|
+
}, 1700);
|
|
3360
|
+
}
|
|
3361
|
+
function inlineSave(root, keepFocus) {
|
|
3362
|
+
if (root._astaSaving || !root.hasAttribute("data-editing")) return;
|
|
3363
|
+
const { input, status } = inline(root);
|
|
3364
|
+
const next = input.value.trim();
|
|
3365
|
+
const cur = (input.dataset.saved || "").trim();
|
|
3366
|
+
// Nothing changed: just close. Empty but required: drop the edit and keep the old text.
|
|
3367
|
+
if (next === cur) return inlineClose(root, keepFocus);
|
|
3368
|
+
if (root.hasAttribute("data-required") && !next) return inlineCancel(root, keepFocus);
|
|
3369
|
+
input.value = next;
|
|
3370
|
+
// With hx-trigger="asta:save" the server saves it: keep the field open until the request lands.
|
|
3371
|
+
const server = window.htmx && /asta:save/.test(input.getAttribute("hx-trigger") || input.getAttribute("data-hx-trigger") || "");
|
|
3372
|
+
if (!server) {
|
|
3373
|
+
emit(input, "asta:save", { value: next });
|
|
3374
|
+
return inlineDone(root, next, keepFocus);
|
|
3375
|
+
}
|
|
3376
|
+
root._astaSaving = { next, keepFocus };
|
|
3377
|
+
input.readOnly = true;
|
|
3378
|
+
const done = (e) => {
|
|
3379
|
+
if (e.target !== input) return;
|
|
3380
|
+
input.removeEventListener("htmx:afterRequest", done);
|
|
3381
|
+
if (!root.isConnected || !root._astaSaving) return;
|
|
3382
|
+
if (e.detail?.successful) return inlineDone(root, next, root._astaSaving.keepFocus);
|
|
3383
|
+
root._astaSaving = null;
|
|
3384
|
+
input.readOnly = false;
|
|
3385
|
+
root.setAttribute("data-failed", "");
|
|
3386
|
+
status.textContent = "Couldn't save. Try again or press Escape.";
|
|
3387
|
+
requestAnimationFrame(() => input.focus());
|
|
3388
|
+
};
|
|
3389
|
+
input.addEventListener("htmx:afterRequest", done);
|
|
3390
|
+
emit(input, "asta:save", { value: next });
|
|
3391
|
+
}
|
|
3392
|
+
on("click", (e) => {
|
|
3393
|
+
const view = near(e, "[data-asta-inline-view]");
|
|
3394
|
+
if (view) inlineOpen(view.closest("[data-asta-inline]"));
|
|
3395
|
+
});
|
|
3396
|
+
on("keydown", (e) => {
|
|
3397
|
+
const input = e.target instanceof HTMLInputElement && e.target.matches("[data-asta-inline-input]") ? e.target : null;
|
|
3398
|
+
if (!input) return;
|
|
3399
|
+
const root = input.closest("[data-asta-inline]");
|
|
3400
|
+
if (e.key === "Enter") {
|
|
3401
|
+
e.preventDefault();
|
|
3402
|
+
inlineSave(root, true);
|
|
3403
|
+
} else if (e.key === "Escape") {
|
|
3404
|
+
e.preventDefault();
|
|
3405
|
+
e.stopPropagation();
|
|
3406
|
+
inlineCancel(root, true);
|
|
3407
|
+
}
|
|
3408
|
+
});
|
|
3409
|
+
on("input", (e) => {
|
|
3410
|
+
const input = e.target instanceof HTMLInputElement && e.target.matches("[data-asta-inline-input]") ? e.target : null;
|
|
3411
|
+
if (!input) return;
|
|
3412
|
+
const root = input.closest("[data-asta-inline]");
|
|
3413
|
+
inline(root).sizer.textContent = `${input.value || input.placeholder} `;
|
|
3414
|
+
root.removeAttribute("data-failed");
|
|
3415
|
+
});
|
|
3416
|
+
on("focusout", (e) => {
|
|
3417
|
+
const input = e.target instanceof HTMLInputElement && e.target.matches("[data-asta-inline-input]") ? e.target : null;
|
|
3418
|
+
if (!input) return;
|
|
3419
|
+
const root = input.closest("[data-asta-inline]");
|
|
3420
|
+
if (!root.hasAttribute("data-editing") || root._astaSaving) return;
|
|
3421
|
+
if (root.hasAttribute("data-cancel-on-blur")) inlineCancel(root, false);
|
|
3422
|
+
else inlineSave(root, false);
|
|
3423
|
+
});
|
|
3424
|
+
|
|
3425
|
+
/* ---------- Boot and htmx ---------- */
|
|
3426
|
+
|
|
3427
|
+
function scan(scope) {
|
|
3428
|
+
for (const r of scope.querySelectorAll("[data-asta-password]")) if (r.querySelector("[data-asta-password-input]")?.value) passwordCheck(r);
|
|
3429
|
+
for (const r of scope.querySelectorAll("[data-asta-date]")) if (!r._astaDp && r.dataset.locale && r.dataset.locale !== "en-US") dpText(r);
|
|
3430
|
+
for (const r of scope.querySelectorAll("[data-asta-otp]")) otpPaint(r);
|
|
3431
|
+
}
|
|
3432
|
+
const boot = () => scan(document);
|
|
3433
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
3434
|
+
else boot();
|
|
3435
|
+
document.addEventListener("htmx:afterSettle", (e) => scan(e.detail?.elt?.parentElement || document));
|
|
3436
|
+
})();
|
|
3437
|
+
|
|
3438
|
+
/* ---- src/overlays.js ---- */
|
|
3439
|
+
/* Overlays: popover, hover card, dropdown and context menus, drawer drag, command palette, navbar, sidebar,
|
|
3440
|
+
* workspace switcher, footer newsletter and wordmark, announcement bar, stepper, tool dock.
|
|
3441
|
+
* Event delegation on document, so markup swapped in by htmx works. Dialog open/close comes from core (window.asta).
|
|
3442
|
+
* Popover [data-asta-popover][popover] opened by [popovertarget]
|
|
3443
|
+
* HoverCard [data-asta-hovercard][popover=manual] opened by [data-asta-hovercard-for]
|
|
3444
|
+
* Menus [data-asta-dropdown] / [data-asta-context-menu] holding [role=menu][data-asta-mpanel]
|
|
3445
|
+
* Drawer dialog.asta-drawer, [data-asta-drawer-handle] drags a bottom sheet
|
|
3446
|
+
* Command dialog[data-asta-command], ⌘K / Ctrl+K
|
|
3447
|
+
* Navbar [data-asta-navbar], Sidebar [data-asta-sidebar], ToolDock [data-asta-tooldock]
|
|
3448
|
+
* Stepper [data-asta-stepper], AnnouncementBar [data-asta-announce], Newsletter [data-asta-newsletter]
|
|
3449
|
+
*/
|
|
3450
|
+
(() => {
|
|
3451
|
+
if (window.__astaOverlays) return;
|
|
3452
|
+
window.__astaOverlays = true;
|
|
3453
|
+
|
|
3454
|
+
const PAD = 8;
|
|
3455
|
+
const FOCUSABLE =
|
|
3456
|
+
'a[href],button:not([disabled]),input:not([disabled]):not([type="hidden"]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
3457
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
3458
|
+
const elOf = (t) => (t instanceof Element ? t : null);
|
|
3459
|
+
const byId = (id) => (id ? document.getElementById(id) : null);
|
|
3460
|
+
const isOpen = (p) => !!p && p.matches(":popover-open");
|
|
3461
|
+
const showPop = (p, source) => {
|
|
3462
|
+
if (isOpen(p)) return;
|
|
3463
|
+
try {
|
|
3464
|
+
p.showPopover(source ? { source } : undefined);
|
|
3465
|
+
} catch {
|
|
3466
|
+
p.showPopover();
|
|
3467
|
+
}
|
|
3468
|
+
};
|
|
3469
|
+
const hidePop = (p) => {
|
|
3470
|
+
if (isOpen(p)) p.hidePopover();
|
|
3471
|
+
};
|
|
3472
|
+
const fire = (el, name, detail) => el.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
3473
|
+
const typing = (t) => t instanceof HTMLElement && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
|
|
3474
|
+
const num = (v, d) => {
|
|
3475
|
+
const n = parseInt(v ?? "", 10);
|
|
3476
|
+
return Number.isFinite(n) && n >= 0 ? n : d;
|
|
3477
|
+
};
|
|
3478
|
+
|
|
3479
|
+
/* ---------- Anchored placement (popover, hover card) ---------- */
|
|
3480
|
+
|
|
3481
|
+
const OPPOSITE = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
3482
|
+
|
|
3483
|
+
// Measure, pick the side (flip if needed), clamp to the viewport, and point the growth origin at the trigger.
|
|
3484
|
+
function anchor(panel, a) {
|
|
3485
|
+
const want = panel.dataset.side || "bottom";
|
|
3486
|
+
const align = panel.dataset.align || "center";
|
|
3487
|
+
const offset = num(panel.dataset.offset, 8);
|
|
3488
|
+
const w = panel.offsetWidth;
|
|
3489
|
+
const h = panel.offsetHeight;
|
|
3490
|
+
const vw = innerWidth;
|
|
3491
|
+
const vh = innerHeight;
|
|
3492
|
+
const room = { top: a.top, bottom: vh - a.bottom, left: a.left, right: vw - a.right };
|
|
3493
|
+
let s = OPPOSITE[want] ? want : "bottom";
|
|
3494
|
+
const need = s === "top" || s === "bottom" ? h : w;
|
|
3495
|
+
if (room[s] < need + offset + PAD && room[OPPOSITE[s]] > room[s]) s = OPPOSITE[s];
|
|
3496
|
+
const vertical = s === "top" || s === "bottom";
|
|
3497
|
+
let x;
|
|
3498
|
+
let y;
|
|
3499
|
+
if (vertical) {
|
|
3500
|
+
x = align === "start" ? a.left : align === "end" ? a.right - w : a.left + a.width / 2 - w / 2;
|
|
3501
|
+
y = s === "bottom" ? a.bottom + offset : a.top - offset - h;
|
|
3502
|
+
x = Math.min(Math.max(PAD, x), vw - w - PAD);
|
|
3503
|
+
} else {
|
|
3504
|
+
y = align === "start" ? a.top : align === "end" ? a.bottom - h : a.top + a.height / 2 - h / 2;
|
|
3505
|
+
x = s === "right" ? a.right + offset : a.left - offset - w;
|
|
3506
|
+
y = Math.min(Math.max(PAD, y), vh - h - PAD);
|
|
3507
|
+
}
|
|
3508
|
+
const along = vertical ? Math.min(Math.max(14, a.left + a.width / 2 - x), w - 14) : Math.min(Math.max(14, a.top + a.height / 2 - y), h - 14);
|
|
3509
|
+
const nudge = { top: ["0px", "4px"], bottom: ["0px", "-4px"], left: ["4px", "0px"], right: ["-4px", "0px"] }[s];
|
|
3510
|
+
panel.style.left = `${Math.round(x)}px`;
|
|
3511
|
+
panel.style.top = `${Math.round(y)}px`;
|
|
3512
|
+
panel.style.transformOrigin = vertical ? `${along}px ${s === "bottom" ? "0" : "100%"}` : `${s === "right" ? "0" : "100%"} ${along}px`;
|
|
3513
|
+
panel.style.setProperty("--asta-nx", nudge[0]);
|
|
3514
|
+
panel.style.setProperty("--asta-ny", nudge[1]);
|
|
3515
|
+
panel.style.setProperty("--asta-gap", `${offset + 2}px`);
|
|
3516
|
+
panel.dataset.placed = s;
|
|
3517
|
+
const ar = panel.querySelector(":scope > [data-asta-arrow]");
|
|
3518
|
+
if (ar) {
|
|
3519
|
+
// The arrow box is 14×7. Its base overlaps the panel edge by 1px to hide the ring under it.
|
|
3520
|
+
ar.style.transform = `rotate(${{ bottom: 0, top: 180, left: 90, right: -90 }[s]}deg)`;
|
|
3521
|
+
ar.style.left = vertical ? `${along - 7}px` : s === "right" ? "-9.5px" : `${w - 4.5}px`;
|
|
3522
|
+
ar.style.top = vertical ? (s === "bottom" ? "-6px" : `${h - 1}px`) : `${along - 3.5}px`;
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
|
|
3526
|
+
// Open anchored panels follow their trigger on scroll and resize.
|
|
3527
|
+
const anchored = new Set();
|
|
3528
|
+
const follow = () => {
|
|
3529
|
+
for (const p of anchored) {
|
|
3530
|
+
if (!p.isConnected || !isOpen(p) || !p._astaAnchor?.isConnected) {
|
|
3531
|
+
anchored.delete(p);
|
|
3532
|
+
continue;
|
|
3533
|
+
}
|
|
3534
|
+
anchor(p, p._astaAnchor.getBoundingClientRect());
|
|
3535
|
+
}
|
|
3536
|
+
};
|
|
3537
|
+
addEventListener("scroll", () => anchored.size && follow(), { capture: true, passive: true });
|
|
3538
|
+
addEventListener("resize", () => anchored.size && follow());
|
|
3539
|
+
|
|
3540
|
+
/* ---------- Popover ---------- */
|
|
3541
|
+
|
|
3542
|
+
// The click is handled here, not by the browser, so the panel is placed before it paints.
|
|
3543
|
+
document.addEventListener(
|
|
3544
|
+
"pointerdown",
|
|
3545
|
+
(e) => {
|
|
3546
|
+
const btn = elOf(e.target)?.closest("[popovertarget]");
|
|
3547
|
+
const panel = btn && byId(btn.getAttribute("popovertarget"));
|
|
3548
|
+
if (panel?.hasAttribute("data-asta-popover")) btn._astaWasOpen = isOpen(panel);
|
|
3549
|
+
},
|
|
3550
|
+
true,
|
|
3551
|
+
);
|
|
3552
|
+
document.addEventListener("click", (e) => {
|
|
3553
|
+
const btn = elOf(e.target)?.closest("[popovertarget]");
|
|
3554
|
+
const panel = btn && byId(btn.getAttribute("popovertarget"));
|
|
3555
|
+
if (!panel?.hasAttribute("data-asta-popover") || btn.disabled) return;
|
|
3556
|
+
e.preventDefault();
|
|
3557
|
+
// A pointer press on the trigger may already have light-dismissed it: don't open it again.
|
|
3558
|
+
const was = btn._astaWasOpen ?? isOpen(panel);
|
|
3559
|
+
btn._astaWasOpen = undefined;
|
|
3560
|
+
if (was || isOpen(panel)) {
|
|
3561
|
+
hidePop(panel);
|
|
3562
|
+
return;
|
|
3563
|
+
}
|
|
3564
|
+
openPopover(panel, btn);
|
|
3565
|
+
});
|
|
3566
|
+
function openPopover(panel, btn) {
|
|
3567
|
+
panel._astaAnchor = btn;
|
|
3568
|
+
showPop(panel, btn);
|
|
3569
|
+
anchor(panel, btn.getBoundingClientRect());
|
|
3570
|
+
anchored.add(panel);
|
|
3571
|
+
btn.setAttribute("aria-expanded", "true");
|
|
3572
|
+
const first = panel.querySelector("[autofocus],[data-autofocus]") || panel.querySelector(FOCUSABLE);
|
|
3573
|
+
(first || panel).focus({ preventScroll: true });
|
|
3574
|
+
}
|
|
3575
|
+
document.addEventListener(
|
|
3576
|
+
"keydown",
|
|
3577
|
+
(e) => {
|
|
3578
|
+
if (e.key !== "Escape") return;
|
|
3579
|
+
const p = elOf(e.target)?.closest("[data-asta-popover]");
|
|
3580
|
+
if (p) p._astaRefocus = true;
|
|
3581
|
+
},
|
|
3582
|
+
true,
|
|
3583
|
+
);
|
|
3584
|
+
document.addEventListener(
|
|
3585
|
+
"toggle",
|
|
3586
|
+
(e) => {
|
|
3587
|
+
const p = e.target;
|
|
3588
|
+
if (!(p instanceof HTMLElement) || !p.hasAttribute("data-asta-popover") || e.newState !== "closed") return;
|
|
3589
|
+
anchored.delete(p);
|
|
3590
|
+
p._astaAnchor?.setAttribute("aria-expanded", "false");
|
|
3591
|
+
// Focus goes back to the trigger after Escape, or when it was lost with the panel.
|
|
3592
|
+
if (p._astaRefocus || document.activeElement === document.body) p._astaAnchor?.focus({ preventScroll: true });
|
|
3593
|
+
p._astaRefocus = false;
|
|
3594
|
+
},
|
|
3595
|
+
true,
|
|
3596
|
+
);
|
|
3597
|
+
|
|
3598
|
+
/* ---------- Hover card ---------- */
|
|
3599
|
+
|
|
3600
|
+
const cardFor = (trigger) => byId(trigger.getAttribute("data-asta-hovercard-for"));
|
|
3601
|
+
const cardTrigger = (card) => card._astaAnchor || document.querySelector(`[data-asta-hovercard-for="${CSS.escape(card.id)}"]`);
|
|
3602
|
+
function cardLater(card, trigger, open) {
|
|
3603
|
+
clearTimeout(card._astaT);
|
|
3604
|
+
if (open === isOpen(card) && !(open && trigger !== card._astaAnchor)) return;
|
|
3605
|
+
const ms = open ? num(card.dataset.openDelay, 500) : num(card.dataset.closeDelay, 220);
|
|
3606
|
+
card._astaT = setTimeout(() => (open ? cardOpen(card, trigger) : cardClose(card)), ms);
|
|
3607
|
+
}
|
|
3608
|
+
function cardOpen(card, trigger) {
|
|
3609
|
+
clearTimeout(card._astaT);
|
|
3610
|
+
if (!trigger?.isConnected) return;
|
|
3611
|
+
card._astaAnchor = trigger;
|
|
3612
|
+
showPop(card);
|
|
3613
|
+
anchor(card, trigger.getBoundingClientRect());
|
|
3614
|
+
anchored.add(card);
|
|
3615
|
+
trigger.setAttribute("aria-describedby", card.id);
|
|
3616
|
+
}
|
|
3617
|
+
function cardClose(card) {
|
|
3618
|
+
clearTimeout(card._astaT);
|
|
3619
|
+
if (!isOpen(card)) return;
|
|
3620
|
+
hidePop(card);
|
|
3621
|
+
anchored.delete(card);
|
|
3622
|
+
card._astaAnchor?.removeAttribute("aria-describedby");
|
|
3623
|
+
}
|
|
3624
|
+
document.addEventListener("pointerover", (e) => {
|
|
3625
|
+
if (e.pointerType !== "mouse") return;
|
|
3626
|
+
const t = elOf(e.target);
|
|
3627
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
3628
|
+
if (trigger && !trigger.contains(elOf(e.relatedTarget))) {
|
|
3629
|
+
const card = cardFor(trigger);
|
|
3630
|
+
if (card) cardLater(card, trigger, true);
|
|
3631
|
+
}
|
|
3632
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
3633
|
+
if (card) clearTimeout(card._astaT);
|
|
3634
|
+
});
|
|
3635
|
+
document.addEventListener("pointerout", (e) => {
|
|
3636
|
+
if (e.pointerType !== "mouse") return;
|
|
3637
|
+
const t = elOf(e.target);
|
|
3638
|
+
const to = elOf(e.relatedTarget);
|
|
3639
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
3640
|
+
if (trigger && !trigger.contains(to)) {
|
|
3641
|
+
const card = cardFor(trigger);
|
|
3642
|
+
if (card && !card.contains(to)) cardLater(card, trigger, false);
|
|
3643
|
+
}
|
|
3644
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
3645
|
+
if (card && !card.contains(to) && !cardTrigger(card)?.contains(to)) cardLater(card, null, false);
|
|
3646
|
+
});
|
|
3647
|
+
document.addEventListener("focusin", (e) => {
|
|
3648
|
+
const t = elOf(e.target);
|
|
3649
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
3650
|
+
// Keyboard focus only: a mouse click shouldn't pop the card a second time.
|
|
3651
|
+
if (trigger?.matches(":focus-visible")) {
|
|
3652
|
+
const card = cardFor(trigger);
|
|
3653
|
+
if (card) cardLater(card, trigger, true);
|
|
3654
|
+
}
|
|
3655
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
3656
|
+
if (card) clearTimeout(card._astaT);
|
|
3657
|
+
});
|
|
3658
|
+
document.addEventListener("focusout", (e) => {
|
|
3659
|
+
const t = elOf(e.target);
|
|
3660
|
+
const to = elOf(e.relatedTarget);
|
|
3661
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
3662
|
+
const card = trigger ? cardFor(trigger) : t?.closest("[data-asta-hovercard]");
|
|
3663
|
+
if (!card || card.contains(to) || cardTrigger(card)?.contains(to)) return;
|
|
3664
|
+
cardLater(card, null, false);
|
|
3665
|
+
});
|
|
3666
|
+
document.addEventListener("keydown", (e) => {
|
|
3667
|
+
if (e.key !== "Escape") return;
|
|
3668
|
+
const t = elOf(e.target);
|
|
3669
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
3670
|
+
const card = trigger ? cardFor(trigger) : t?.closest("[data-asta-hovercard]");
|
|
3671
|
+
if (!card || !isOpen(card)) return;
|
|
3672
|
+
e.stopPropagation();
|
|
3673
|
+
const back = !trigger && card.contains(t);
|
|
3674
|
+
cardClose(card);
|
|
3675
|
+
if (back) card._astaAnchor?.focus({ preventScroll: true });
|
|
3676
|
+
});
|
|
3677
|
+
|
|
3678
|
+
/* ---------- Menus: dropdown and context ---------- */
|
|
3679
|
+
|
|
3680
|
+
const ROOTS = "[data-asta-dropdown],[data-asta-context-menu]";
|
|
3681
|
+
const panelOf = (root) => root.querySelector(":scope > [data-asta-mpanel]");
|
|
3682
|
+
const subOf = (item) => item.parentElement?.matches("[data-asta-menu-sub]") ? item.parentElement.querySelector(":scope > [data-asta-mpanel]") : null;
|
|
3683
|
+
const enabled = (i) => !i.disabled && i.getAttribute("aria-disabled") !== "true";
|
|
3684
|
+
const itemsOf = (panel) => [...panel.querySelectorAll('[role^="menuitem"]')].filter((i) => i.closest('[role="menu"]') === panel && enabled(i));
|
|
3685
|
+
const openRoots = () => [...document.querySelectorAll(`[data-asta-dropdown][data-open],[data-asta-context-menu][data-open]`)];
|
|
3686
|
+
|
|
3687
|
+
// at: { x, y } for a context menu, { rect, align } under a trigger, { rect, sub: true } beside a parent item.
|
|
3688
|
+
function placeMenu(panel, at) {
|
|
3689
|
+
const w = panel.offsetWidth;
|
|
3690
|
+
const h = panel.offsetHeight;
|
|
3691
|
+
const vw = innerWidth;
|
|
3692
|
+
const vh = innerHeight;
|
|
3693
|
+
let x;
|
|
3694
|
+
let y;
|
|
3695
|
+
let right = false;
|
|
3696
|
+
let up = false;
|
|
3697
|
+
if (at.sub) {
|
|
3698
|
+
x = at.rect.right + 2;
|
|
3699
|
+
if (x + w > vw - PAD && at.rect.left - w - 2 >= PAD) {
|
|
3700
|
+
x = at.rect.left - w - 2;
|
|
3701
|
+
right = true;
|
|
3702
|
+
}
|
|
3703
|
+
// Line the first item up with the parent item (the panel has 6px of padding).
|
|
3704
|
+
y = at.rect.top - 6;
|
|
3705
|
+
panel.style.setProperty("--asta-nx", right ? "4px" : "-4px");
|
|
3706
|
+
panel.style.setProperty("--asta-ny", "0px");
|
|
3707
|
+
} else if (at.rect) {
|
|
3708
|
+
x = at.align === "end" ? at.rect.right - w : at.rect.left;
|
|
3709
|
+
y = at.rect.bottom + 6;
|
|
3710
|
+
if (y + h > vh - PAD && at.rect.top - h - 6 >= PAD) {
|
|
3711
|
+
y = at.rect.top - h - 6;
|
|
3712
|
+
up = true;
|
|
3713
|
+
}
|
|
3714
|
+
right = at.align === "end";
|
|
3715
|
+
panel.style.setProperty("--asta-nx", "0px");
|
|
3716
|
+
panel.style.setProperty("--asta-ny", up ? "4px" : "-4px");
|
|
3717
|
+
} else {
|
|
3718
|
+
x = at.x;
|
|
3719
|
+
y = at.y;
|
|
3720
|
+
if (x + w > vw - PAD && x - w >= PAD) {
|
|
3721
|
+
x -= w;
|
|
3722
|
+
right = true;
|
|
3723
|
+
}
|
|
3724
|
+
if (y + h > vh - PAD && y - h >= PAD) {
|
|
3725
|
+
y -= h;
|
|
3726
|
+
up = true;
|
|
3727
|
+
}
|
|
3728
|
+
panel.style.setProperty("--asta-nx", "0px");
|
|
3729
|
+
panel.style.setProperty("--asta-ny", "0px");
|
|
3730
|
+
}
|
|
3731
|
+
x = Math.max(PAD, Math.min(x, vw - w - PAD));
|
|
3732
|
+
y = Math.max(PAD, Math.min(y, vh - h - PAD));
|
|
3733
|
+
panel.style.left = `${Math.round(x)}px`;
|
|
3734
|
+
panel.style.top = `${Math.round(y)}px`;
|
|
3735
|
+
panel.style.transformOrigin = `${right ? "100%" : "0"} ${up ? "100%" : "0"}`;
|
|
3736
|
+
}
|
|
3737
|
+
|
|
3738
|
+
function openPanel(panel, at, focusFirst) {
|
|
3739
|
+
showPop(panel);
|
|
3740
|
+
placeMenu(panel, at);
|
|
3741
|
+
const first = focusFirst && itemsOf(panel)[0];
|
|
3742
|
+
(first || panel).focus({ preventScroll: true });
|
|
3743
|
+
}
|
|
3744
|
+
function closeSubs(panel) {
|
|
3745
|
+
for (const sub of panel.querySelectorAll("[data-asta-mpanel]")) {
|
|
3746
|
+
if (!isOpen(sub)) continue;
|
|
3747
|
+
hidePop(sub);
|
|
3748
|
+
sub.parentElement?.querySelector(':scope > [aria-haspopup="menu"]')?.setAttribute("aria-expanded", "false");
|
|
3749
|
+
}
|
|
3750
|
+
}
|
|
3751
|
+
function openSub(item, focusFirst) {
|
|
3752
|
+
const panel = item.closest('[role="menu"]');
|
|
3753
|
+
const sub = subOf(item);
|
|
3754
|
+
if (!sub) return;
|
|
3755
|
+
clearTimeout(panel._astaT);
|
|
3756
|
+
// One open submenu per level.
|
|
3757
|
+
for (const other of panel.querySelectorAll(":scope [data-asta-menu-sub] > [data-asta-mpanel]")) {
|
|
3758
|
+
if (other !== sub && other.closest('[role="menu"]:not([data-asta-menu-sub] > *)') !== null && other.parentElement.closest('[role="menu"]') === panel && isOpen(other)) {
|
|
3759
|
+
closeSubs(other);
|
|
3760
|
+
hidePop(other);
|
|
3761
|
+
other.parentElement.querySelector(':scope > [aria-haspopup="menu"]')?.setAttribute("aria-expanded", "false");
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
item.setAttribute("aria-expanded", "true");
|
|
3765
|
+
if (isOpen(sub)) {
|
|
3766
|
+
if (focusFirst) itemsOf(sub)[0]?.focus({ preventScroll: true });
|
|
3767
|
+
return;
|
|
3768
|
+
}
|
|
3769
|
+
openPanel(sub, { rect: item.getBoundingClientRect(), sub: true }, focusFirst);
|
|
3770
|
+
// Keep the highlight on the parent item while the pointer is on its way.
|
|
3771
|
+
if (!focusFirst) item.focus({ preventScroll: true });
|
|
3772
|
+
}
|
|
3773
|
+
function closeSub(sub, refocus) {
|
|
3774
|
+
closeSubs(sub);
|
|
3775
|
+
hidePop(sub);
|
|
3776
|
+
const item = sub.parentElement?.querySelector(':scope > [aria-haspopup="menu"]');
|
|
3777
|
+
item?.setAttribute("aria-expanded", "false");
|
|
3778
|
+
if (refocus) item?.focus({ preventScroll: true });
|
|
3779
|
+
}
|
|
3780
|
+
|
|
3781
|
+
function openRoot(root, at, focusFirst) {
|
|
3782
|
+
for (const other of openRoots()) if (other !== root) closeRoot(other, false);
|
|
3783
|
+
const panel = panelOf(root);
|
|
3784
|
+
if (!panel) return;
|
|
3785
|
+
if (!root.hasAttribute("data-open")) {
|
|
3786
|
+
const a = document.activeElement;
|
|
3787
|
+
root._astaRestore = a && root.contains(a) && !panel.contains(a) ? a : root;
|
|
3788
|
+
}
|
|
3789
|
+
closeSubs(panel);
|
|
3790
|
+
root.setAttribute("data-open", "");
|
|
3791
|
+
root.querySelector(":scope > [data-asta-dropdown-trigger]")?.setAttribute("aria-expanded", "true");
|
|
3792
|
+
root._astaAt = performance.now();
|
|
3793
|
+
if (root.hasAttribute("data-match")) {
|
|
3794
|
+
const t = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
3795
|
+
if (t) panel.style.minWidth = `${t.offsetWidth}px`;
|
|
3796
|
+
}
|
|
3797
|
+
// Re-opening a context menu at a new point: hide first, so it animates in again.
|
|
3798
|
+
if (isOpen(panel)) hidePop(panel);
|
|
3799
|
+
openPanel(panel, at, focusFirst);
|
|
3800
|
+
}
|
|
3801
|
+
function closeRoot(root, refocus) {
|
|
3802
|
+
if (!root.hasAttribute("data-open")) return;
|
|
3803
|
+
const panel = panelOf(root);
|
|
3804
|
+
if (panel) {
|
|
3805
|
+
clearTimeout(panel._astaT);
|
|
3806
|
+
closeSubs(panel);
|
|
3807
|
+
hidePop(panel);
|
|
3808
|
+
}
|
|
3809
|
+
root.removeAttribute("data-open");
|
|
3810
|
+
const trigger = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
3811
|
+
trigger?.setAttribute("aria-expanded", "false");
|
|
3812
|
+
if (refocus) (trigger || root._astaRestore || root).focus({ preventScroll: true });
|
|
3813
|
+
}
|
|
3814
|
+
function toggleDropdown(root, focusFirst) {
|
|
3815
|
+
if (root.hasAttribute("data-open")) return closeRoot(root, false);
|
|
3816
|
+
const t = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
3817
|
+
openRoot(root, { rect: t.getBoundingClientRect(), align: root.getAttribute("data-align") === "end" ? "end" : "start" }, focusFirst);
|
|
3818
|
+
}
|
|
3819
|
+
|
|
3820
|
+
// Checkbox and radio items keep their hidden inputs in step and announce the change.
|
|
3821
|
+
function check(item) {
|
|
3822
|
+
const role = item.getAttribute("role");
|
|
3823
|
+
if (role === "menuitemcheckbox") {
|
|
3824
|
+
const on = item.getAttribute("aria-checked") !== "true";
|
|
3825
|
+
item.setAttribute("aria-checked", String(on));
|
|
3826
|
+
const input = item.querySelector(':scope > input[type="hidden"]');
|
|
3827
|
+
if (input) input.disabled = !on;
|
|
3828
|
+
fire(item, "asta:change", { checked: on, value: item.dataset.value ?? "" });
|
|
3829
|
+
} else if (role === "menuitemradio") {
|
|
3830
|
+
if (item.getAttribute("aria-checked") === "true") return;
|
|
3831
|
+
const panel = item.closest('[role="menu"]');
|
|
3832
|
+
const group = item.dataset.group ?? "";
|
|
3833
|
+
for (const r of panel.querySelectorAll('[role="menuitemradio"]')) {
|
|
3834
|
+
if ((r.dataset.group ?? "") !== group || r.closest('[role="menu"]') !== panel) continue;
|
|
3835
|
+
const on = r === item;
|
|
3836
|
+
r.setAttribute("aria-checked", String(on));
|
|
3837
|
+
const input = r.querySelector('input[type="hidden"]');
|
|
3838
|
+
if (input) input.disabled = !on;
|
|
3839
|
+
}
|
|
3840
|
+
fire(item, "asta:change", { checked: true, value: item.dataset.value ?? "" });
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
|
|
3844
|
+
document.addEventListener("click", (e) => {
|
|
3845
|
+
const t = elOf(e.target);
|
|
3846
|
+
if (!t) return;
|
|
3847
|
+
const trigger = t.closest("[data-asta-dropdown-trigger]");
|
|
3848
|
+
if (trigger) {
|
|
3849
|
+
const root = trigger.closest("[data-asta-dropdown]");
|
|
3850
|
+
if (root) toggleDropdown(root, e.detail === 0);
|
|
3851
|
+
return;
|
|
3852
|
+
}
|
|
3853
|
+
const item = t.closest('[data-asta-mpanel] [role^="menuitem"]');
|
|
3854
|
+
const root = item?.closest(ROOTS);
|
|
3855
|
+
if (!item || !root || !enabled(item)) return;
|
|
3856
|
+
if (item.getAttribute("aria-haspopup") === "menu") {
|
|
3857
|
+
openSub(item, e.detail === 0);
|
|
3858
|
+
return;
|
|
3859
|
+
}
|
|
3860
|
+
check(item);
|
|
3861
|
+
if (!item.hasAttribute("data-keep-open")) closeRoot(root, true);
|
|
3862
|
+
});
|
|
3863
|
+
|
|
3864
|
+
// A press outside every open panel closes its menu. A right-click inside a context area moves it instead.
|
|
3865
|
+
document.addEventListener(
|
|
3866
|
+
"pointerdown",
|
|
3867
|
+
(e) => {
|
|
3868
|
+
const t = elOf(e.target);
|
|
3869
|
+
for (const root of openRoots()) {
|
|
3870
|
+
if (t?.closest("[data-asta-mpanel]") && root.contains(t)) continue;
|
|
3871
|
+
if (root.querySelector(":scope > [data-asta-dropdown-trigger]")?.contains(t)) continue;
|
|
3872
|
+
if (e.button === 2 && root.matches("[data-asta-context-menu]") && root.contains(t)) continue;
|
|
3873
|
+
closeRoot(root, false);
|
|
3874
|
+
}
|
|
3875
|
+
},
|
|
3876
|
+
true,
|
|
3877
|
+
);
|
|
3878
|
+
addEventListener(
|
|
3879
|
+
"scroll",
|
|
3880
|
+
(e) => {
|
|
3881
|
+
const t = elOf(e.target);
|
|
3882
|
+
for (const root of openRoots()) if (!t?.closest?.("[data-asta-mpanel]")) closeRoot(root, false);
|
|
3883
|
+
},
|
|
3884
|
+
{ capture: true, passive: true },
|
|
3885
|
+
);
|
|
3886
|
+
addEventListener("resize", () => openRoots().forEach((r) => closeRoot(r, false)));
|
|
3887
|
+
addEventListener("blur", () => openRoots().forEach((r) => closeRoot(r, false)));
|
|
3888
|
+
|
|
3889
|
+
// Context menu: right-click, Shift+F10 / Menu key, long-press on touch.
|
|
3890
|
+
const ctxArea = (t) => {
|
|
3891
|
+
const root = t?.closest("[data-asta-context-menu]");
|
|
3892
|
+
if (!root || root.hasAttribute("data-disabled") || t.closest("[data-asta-mpanel]")) return null;
|
|
3893
|
+
return root;
|
|
3894
|
+
};
|
|
3895
|
+
function ctxFromKeyboard(root) {
|
|
3896
|
+
const a = document.activeElement;
|
|
3897
|
+
const r = (a instanceof HTMLElement && root.contains(a) ? a : root).getBoundingClientRect();
|
|
3898
|
+
openRoot(root, { x: r.left + Math.min(16, r.width / 2), y: r.top + Math.min(r.height / 2, 20) }, true);
|
|
3899
|
+
}
|
|
3900
|
+
document.addEventListener("contextmenu", (e) => {
|
|
3901
|
+
const t = elOf(e.target);
|
|
3902
|
+
if (t?.closest("[data-asta-mpanel]")) return e.preventDefault();
|
|
3903
|
+
const root = ctxArea(t);
|
|
3904
|
+
if (!root) return;
|
|
3905
|
+
e.preventDefault();
|
|
3906
|
+
clearTimeout(root._astaPress?.t);
|
|
3907
|
+
// Already opened by a long-press a moment ago.
|
|
3908
|
+
if (root.hasAttribute("data-open") && performance.now() - (root._astaAt || 0) < 700) return;
|
|
3909
|
+
if (e.clientX === 0 && e.clientY === 0) return ctxFromKeyboard(root);
|
|
3910
|
+
openRoot(root, { x: e.clientX, y: e.clientY }, false);
|
|
3911
|
+
});
|
|
3912
|
+
document.addEventListener("pointerdown", (e) => {
|
|
3913
|
+
if (e.pointerType === "mouse") return;
|
|
3914
|
+
const root = ctxArea(elOf(e.target));
|
|
3915
|
+
if (!root) return;
|
|
3916
|
+
clearTimeout(root._astaPress?.t);
|
|
3917
|
+
const { clientX: x, clientY: y } = e;
|
|
3918
|
+
root._astaPress = { x, y, t: setTimeout(() => openRoot(root, { x, y }, false), num(root.dataset.longPress, 500)) };
|
|
3919
|
+
});
|
|
3920
|
+
document.addEventListener("pointermove", (e) => {
|
|
3921
|
+
if (e.pointerType === "mouse") return;
|
|
3922
|
+
const root = elOf(e.target)?.closest("[data-asta-context-menu]");
|
|
3923
|
+
const p = root?._astaPress;
|
|
3924
|
+
if (p && Math.hypot(e.clientX - p.x, e.clientY - p.y) > 10) clearTimeout(p.t);
|
|
3925
|
+
});
|
|
3926
|
+
for (const type of ["pointerup", "pointercancel"]) {
|
|
3927
|
+
document.addEventListener(type, (e) => clearTimeout(elOf(e.target)?.closest("[data-asta-context-menu]")?._astaPress?.t));
|
|
3928
|
+
}
|
|
3929
|
+
|
|
3930
|
+
// Hover: the item under the pointer takes focus; a submenu item opens after a beat, leaving it closes after a grace.
|
|
3931
|
+
document.addEventListener("pointerover", (e) => {
|
|
3932
|
+
if (e.pointerType !== "mouse") return;
|
|
3933
|
+
const t = elOf(e.target);
|
|
3934
|
+
const panel = t?.closest("[data-asta-mpanel]");
|
|
3935
|
+
if (!panel || !isOpen(panel)) return;
|
|
3936
|
+
// Entering a submenu keeps it open.
|
|
3937
|
+
const parent = panel.parentElement?.closest("[data-asta-mpanel]");
|
|
3938
|
+
if (parent) clearTimeout(parent._astaT);
|
|
3939
|
+
const item = t.closest('[role^="menuitem"]');
|
|
3940
|
+
if (!item || item.closest('[role="menu"]') !== panel) return;
|
|
3941
|
+
clearTimeout(panel._astaT);
|
|
3942
|
+
if (!enabled(item)) return panel.focus({ preventScroll: true });
|
|
3943
|
+
if (document.activeElement !== item) item.focus({ preventScroll: true });
|
|
3944
|
+
const open = [...panel.querySelectorAll("[data-asta-menu-sub] > [data-asta-mpanel]")].find((s) => s.parentElement.closest('[role="menu"]') === panel && isOpen(s));
|
|
3945
|
+
if (item.getAttribute("aria-haspopup") === "menu") {
|
|
3946
|
+
if (open !== subOf(item)) panel._astaT = setTimeout(() => openSub(item, false), 100);
|
|
3947
|
+
} else if (open) {
|
|
3948
|
+
panel._astaT = setTimeout(() => closeSub(open, false), 260);
|
|
3949
|
+
}
|
|
3950
|
+
});
|
|
3951
|
+
document.addEventListener("pointerout", (e) => {
|
|
3952
|
+
if (e.pointerType !== "mouse") return;
|
|
3953
|
+
const panel = elOf(e.target)?.closest("[data-asta-mpanel]");
|
|
3954
|
+
if (!panel || !isOpen(panel) || panel.contains(elOf(e.relatedTarget))) return;
|
|
3955
|
+
const hasSub = [...panel.querySelectorAll("[data-asta-menu-sub] > [data-asta-mpanel]")].some(isOpen);
|
|
3956
|
+
if (!hasSub && panel.contains(document.activeElement)) panel.focus({ preventScroll: true });
|
|
3957
|
+
});
|
|
3958
|
+
|
|
3959
|
+
document.addEventListener("keydown", (e) => {
|
|
3960
|
+
const t = elOf(e.target);
|
|
3961
|
+
if (!t) return;
|
|
3962
|
+
// Openers.
|
|
3963
|
+
if (t.matches("[data-asta-dropdown-trigger]") && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
3964
|
+
e.preventDefault();
|
|
3965
|
+
const root = t.closest("[data-asta-dropdown]");
|
|
3966
|
+
if (root && !root.hasAttribute("data-open")) toggleDropdown(root, true);
|
|
3967
|
+
else if (root) itemsOf(panelOf(root))[0]?.focus();
|
|
3968
|
+
return;
|
|
3969
|
+
}
|
|
3970
|
+
if ((e.key === "ContextMenu" || (e.key === "F10" && e.shiftKey)) && ctxArea(t)) {
|
|
3971
|
+
e.preventDefault();
|
|
3972
|
+
ctxFromKeyboard(ctxArea(t));
|
|
3973
|
+
return;
|
|
3974
|
+
}
|
|
3975
|
+
const panel = t.closest("[data-asta-mpanel]");
|
|
3976
|
+
if (!panel || !isOpen(panel)) return;
|
|
3977
|
+
const root = panel.closest(ROOTS);
|
|
3978
|
+
const list = itemsOf(panel);
|
|
3979
|
+
const i = list.indexOf(document.activeElement);
|
|
3980
|
+
const parentSub = panel.parentElement?.matches("[data-asta-menu-sub]") ? panel : null;
|
|
3981
|
+
const go = (n) => list[(n + list.length) % list.length]?.focus({ preventScroll: true });
|
|
3982
|
+
switch (e.key) {
|
|
3983
|
+
case "ArrowDown":
|
|
3984
|
+
e.preventDefault();
|
|
3985
|
+
return go(i < 0 ? 0 : i + 1);
|
|
3986
|
+
case "ArrowUp":
|
|
3987
|
+
e.preventDefault();
|
|
3988
|
+
return go(i < 0 ? list.length - 1 : i - 1);
|
|
3989
|
+
case "Home":
|
|
3990
|
+
case "PageUp":
|
|
3991
|
+
e.preventDefault();
|
|
3992
|
+
return go(0);
|
|
3993
|
+
case "End":
|
|
3994
|
+
case "PageDown":
|
|
3995
|
+
e.preventDefault();
|
|
3996
|
+
return go(list.length - 1);
|
|
3997
|
+
case "ArrowRight": {
|
|
3998
|
+
const it = list[i];
|
|
3999
|
+
if (it?.getAttribute("aria-haspopup") === "menu") {
|
|
4000
|
+
e.preventDefault();
|
|
4001
|
+
openSub(it, true);
|
|
4002
|
+
}
|
|
4003
|
+
return;
|
|
4004
|
+
}
|
|
4005
|
+
case "ArrowLeft":
|
|
4006
|
+
if (parentSub) {
|
|
4007
|
+
e.preventDefault();
|
|
4008
|
+
closeSub(parentSub, true);
|
|
4009
|
+
}
|
|
4010
|
+
return;
|
|
4011
|
+
case "Escape":
|
|
4012
|
+
e.preventDefault();
|
|
4013
|
+
e.stopPropagation();
|
|
4014
|
+
if (parentSub) return closeSub(parentSub, true);
|
|
4015
|
+
return root && closeRoot(root, true);
|
|
4016
|
+
case "Tab":
|
|
4017
|
+
if (root?.matches("[data-asta-context-menu]")) e.preventDefault();
|
|
4018
|
+
return root && closeRoot(root, true);
|
|
4019
|
+
}
|
|
4020
|
+
// Type-ahead: jump to the next item whose label starts with what was typed.
|
|
4021
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && e.key !== " ") {
|
|
4022
|
+
const now = e.timeStamp;
|
|
4023
|
+
panel._astaTyped = now - (panel._astaTypedAt || 0) > 600 ? e.key.toLowerCase() : (panel._astaTyped || "") + e.key.toLowerCase();
|
|
4024
|
+
panel._astaTypedAt = now;
|
|
4025
|
+
const q = panel._astaTyped;
|
|
4026
|
+
const start = Math.max(0, i + (q.length === 1 ? 1 : 0));
|
|
4027
|
+
const order = [...list.slice(start), ...list.slice(0, start)];
|
|
4028
|
+
order.find((it) => it.textContent.trim().toLowerCase().startsWith(q))?.focus({ preventScroll: true });
|
|
4029
|
+
}
|
|
4030
|
+
});
|
|
4031
|
+
|
|
4032
|
+
/* ---------- Workspace switchers: the trigger follows the picked workspace ---------- */
|
|
4033
|
+
|
|
4034
|
+
document.addEventListener("asta:change", (e) => {
|
|
4035
|
+
const item = elOf(e.target);
|
|
4036
|
+
const root = item?.closest("[data-asta-ws]");
|
|
4037
|
+
if (!root || !item.hasAttribute("data-ws-name")) return;
|
|
4038
|
+
const name = item.dataset.wsName;
|
|
4039
|
+
const plan = item.dataset.wsPlan || "";
|
|
4040
|
+
for (const n of root.querySelectorAll("[data-ws-name-out]")) if (!n.closest("[data-asta-mpanel]")) n.textContent = name;
|
|
4041
|
+
for (const n of root.querySelectorAll("[data-ws-plan-out]")) {
|
|
4042
|
+
if (n.closest("[data-asta-mpanel]")) continue;
|
|
4043
|
+
n.textContent = plan;
|
|
4044
|
+
n.hidden = !plan;
|
|
4045
|
+
}
|
|
4046
|
+
const box = item.querySelector("[data-ws-logo-box]");
|
|
4047
|
+
for (const n of root.querySelectorAll("[data-ws-logo-out]")) {
|
|
4048
|
+
if (n.closest("[data-asta-mpanel]")) continue;
|
|
4049
|
+
if (box) n.innerHTML = box.innerHTML;
|
|
4050
|
+
else n.textContent = [...name][0] || "";
|
|
4051
|
+
}
|
|
4052
|
+
});
|
|
4053
|
+
|
|
4054
|
+
/* ---------- Drawer: drag a bottom sheet down to close ---------- */
|
|
4055
|
+
|
|
4056
|
+
let drag = null;
|
|
4057
|
+
document.addEventListener("pointerdown", (e) => {
|
|
4058
|
+
const handle = elOf(e.target)?.closest("[data-asta-drawer-handle]");
|
|
4059
|
+
const d = handle?.closest("dialog.asta-drawer");
|
|
4060
|
+
if (!d || !d.open || e.button !== 0) return;
|
|
4061
|
+
handle.setPointerCapture(e.pointerId);
|
|
4062
|
+
drag = { d, handle, y: e.clientY, t: e.timeStamp, dy: 0, v: 0 };
|
|
4063
|
+
d.style.transition = "none";
|
|
4064
|
+
});
|
|
4065
|
+
document.addEventListener("pointermove", (e) => {
|
|
4066
|
+
if (!drag) return;
|
|
4067
|
+
const raw = e.clientY - drag.y;
|
|
4068
|
+
// Pulling up resists, pulling down follows 1:1.
|
|
4069
|
+
const dy = raw < 0 ? -Math.sqrt(-raw) * 2 : raw;
|
|
4070
|
+
drag.v = (dy - drag.dy) / Math.max(1, e.timeStamp - drag.t);
|
|
4071
|
+
drag.t = e.timeStamp;
|
|
4072
|
+
drag.dy = dy;
|
|
4073
|
+
drag.d.style.transform = `translateY(${dy}px)`;
|
|
4074
|
+
drag.d.style.setProperty("--asta-drawer-fade", String(Math.max(0, 1 - Math.max(0, dy) / drag.d.offsetHeight)));
|
|
4075
|
+
});
|
|
4076
|
+
const endDrag = () => {
|
|
4077
|
+
if (!drag) return;
|
|
4078
|
+
const { d, dy, v } = drag;
|
|
4079
|
+
drag = null;
|
|
4080
|
+
// Clear the inline styles in the same frame as the close, so the CSS transition runs from where it is.
|
|
4081
|
+
d.style.transition = "";
|
|
4082
|
+
d.style.transform = "";
|
|
4083
|
+
d.style.removeProperty("--asta-drawer-fade");
|
|
4084
|
+
if (dy > d.offsetHeight * 0.3 || v > 0.6) window.asta?.close(d.id);
|
|
4085
|
+
};
|
|
4086
|
+
document.addEventListener("pointerup", endDrag);
|
|
4087
|
+
document.addEventListener("pointercancel", endDrag);
|
|
4088
|
+
|
|
4089
|
+
/* ---------- Command palette ---------- */
|
|
4090
|
+
|
|
4091
|
+
// Fuzzy match: every query letter appears in order. Prefers consecutive letters and word starts.
|
|
4092
|
+
function fuzzy(text, query) {
|
|
4093
|
+
const t = text.toLowerCase();
|
|
4094
|
+
const q = query.toLowerCase().replace(/\s+/g, "");
|
|
4095
|
+
if (!q) return { score: 0, hits: [] };
|
|
4096
|
+
const hits = [];
|
|
4097
|
+
let score = 0;
|
|
4098
|
+
let from = 0;
|
|
4099
|
+
for (const ch of q) {
|
|
4100
|
+
const at = t.indexOf(ch, from);
|
|
4101
|
+
if (at < 0) return null;
|
|
4102
|
+
if (hits.length && at === hits[hits.length - 1] + 1) score += 3;
|
|
4103
|
+
if (at === 0 || t[at - 1] === " ") score += 2;
|
|
4104
|
+
score -= (at - from) * 0.1;
|
|
4105
|
+
hits.push(at);
|
|
4106
|
+
from = at + 1;
|
|
4107
|
+
}
|
|
4108
|
+
return { score, hits };
|
|
4109
|
+
}
|
|
4110
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
4111
|
+
const cmdOptions = (root) => [...root.querySelectorAll('[data-asta-command-list] [role="option"]')].filter((o) => !o.hidden);
|
|
4112
|
+
let cmdSeq = 0;
|
|
4113
|
+
|
|
4114
|
+
function cmdFilter(root) {
|
|
4115
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
4116
|
+
const q = root.hasAttribute("data-server") ? "" : (input?.value ?? "");
|
|
4117
|
+
const all = [...root.querySelectorAll('[data-asta-command-list] [role="option"]')];
|
|
4118
|
+
all.forEach((o, i) => {
|
|
4119
|
+
if (o.dataset.i === undefined) o.dataset.i = String(i);
|
|
4120
|
+
const label = o.querySelector("[data-asta-command-label]");
|
|
4121
|
+
if (label && label.dataset.text === undefined) label.dataset.text = label.textContent;
|
|
4122
|
+
const text = label?.dataset.text ?? o.textContent;
|
|
4123
|
+
let m = fuzzy(text, q);
|
|
4124
|
+
const kw = (o.dataset.keywords || "").toLowerCase();
|
|
4125
|
+
if (!m && kw && q.trim() && kw.includes(q.trim().toLowerCase())) m = { score: -5, hits: [] };
|
|
4126
|
+
o.hidden = !m;
|
|
4127
|
+
o._astaScore = m ? m.score : -1e9;
|
|
4128
|
+
if (label) {
|
|
4129
|
+
const set = new Set(m?.hits ?? []);
|
|
4130
|
+
label.innerHTML = set.size ? [...text].map((c, k) => (set.has(k) ? `<mark class="bg-transparent font-semibold text-ink">${esc(c)}</mark>` : esc(c))).join("") : esc(text);
|
|
4131
|
+
}
|
|
4132
|
+
});
|
|
4133
|
+
for (const box of root.querySelectorAll("[data-asta-command-items]")) {
|
|
4134
|
+
const opts = [...box.querySelectorAll(':scope > [role="option"]')];
|
|
4135
|
+
opts.sort((a, b) => (q ? b._astaScore - a._astaScore : 0) || a.dataset.i - b.dataset.i);
|
|
4136
|
+
for (const o of opts) box.append(o);
|
|
4137
|
+
const group = box.closest("[data-asta-command-group]");
|
|
4138
|
+
if (group) group.hidden = !opts.some((o) => !o.hidden);
|
|
4139
|
+
}
|
|
4140
|
+
cmdRefresh(root, q);
|
|
4141
|
+
}
|
|
4142
|
+
function cmdRefresh(root, q) {
|
|
4143
|
+
const shown = cmdOptions(root);
|
|
4144
|
+
const empty = root.querySelector("[data-asta-command-empty]");
|
|
4145
|
+
if (empty) {
|
|
4146
|
+
empty.hidden = shown.length > 0;
|
|
4147
|
+
const qs = root.querySelector("[data-asta-command-q]");
|
|
4148
|
+
if (qs) qs.textContent = q ?? root.querySelector("[data-asta-command-input]")?.value ?? "";
|
|
4149
|
+
}
|
|
4150
|
+
const count = root.querySelector("[data-asta-command-count]");
|
|
4151
|
+
if (count) count.textContent = `${shown.length} ${shown.length === 1 ? "result" : "results"}`;
|
|
4152
|
+
cmdActive(root, 0);
|
|
4153
|
+
}
|
|
4154
|
+
function cmdActive(root, i) {
|
|
4155
|
+
const list = cmdOptions(root);
|
|
4156
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
4157
|
+
const at = list.length ? (i + list.length) % list.length : -1;
|
|
4158
|
+
root._astaAt = at;
|
|
4159
|
+
for (const o of root.querySelectorAll('[data-asta-command-list] [role="option"]')) o.setAttribute("aria-selected", "false");
|
|
4160
|
+
const on = list[at];
|
|
4161
|
+
if (on) {
|
|
4162
|
+
if (!on.id) on.id = `${root.id || "asta-cmd"}-opt-${++cmdSeq}`;
|
|
4163
|
+
on.setAttribute("aria-selected", "true");
|
|
4164
|
+
on.scrollIntoView({ block: "nearest" });
|
|
4165
|
+
input?.setAttribute("aria-activedescendant", on.id);
|
|
4166
|
+
} else input?.removeAttribute("aria-activedescendant");
|
|
4167
|
+
}
|
|
4168
|
+
function cmdRun(root, opt) {
|
|
4169
|
+
if (!opt) return;
|
|
4170
|
+
if (!opt.hasAttribute("data-keep-open")) window.asta?.close(root.id);
|
|
4171
|
+
opt.click();
|
|
4172
|
+
}
|
|
4173
|
+
|
|
4174
|
+
document.addEventListener("keydown", (e) => {
|
|
4175
|
+
// ⌘K / Ctrl+K toggles the first palette that listens for that key.
|
|
4176
|
+
if ((e.metaKey || e.ctrlKey) && !e.altKey && e.key.length === 1) {
|
|
4177
|
+
const root = [...document.querySelectorAll("dialog[data-asta-command]")].find((d) => (d.dataset.hotkey || "k").toLowerCase() === e.key.toLowerCase());
|
|
4178
|
+
if (root) {
|
|
4179
|
+
e.preventDefault();
|
|
4180
|
+
if (root.open) window.asta?.close(root.id);
|
|
4181
|
+
else window.asta?.open(root.id);
|
|
4182
|
+
return;
|
|
4183
|
+
}
|
|
4184
|
+
}
|
|
4185
|
+
const input = elOf(e.target)?.closest("[data-asta-command-input]");
|
|
4186
|
+
const root = input?.closest("dialog[data-asta-command]");
|
|
4187
|
+
if (!root) return;
|
|
4188
|
+
const at = root._astaAt ?? 0;
|
|
4189
|
+
const n = cmdOptions(root).length;
|
|
4190
|
+
if (e.key === "ArrowDown") cmdActive(root, at + 1);
|
|
4191
|
+
else if (e.key === "ArrowUp") cmdActive(root, at - 1);
|
|
4192
|
+
else if (e.key === "Home" && !input.value) cmdActive(root, 0);
|
|
4193
|
+
else if (e.key === "End" && !input.value) cmdActive(root, n - 1);
|
|
4194
|
+
else if (e.key === "Enter") cmdRun(root, cmdOptions(root)[at]);
|
|
4195
|
+
else return;
|
|
4196
|
+
e.preventDefault();
|
|
4197
|
+
});
|
|
4198
|
+
document.addEventListener("input", (e) => {
|
|
4199
|
+
const input = elOf(e.target)?.closest("[data-asta-command-input]");
|
|
4200
|
+
const root = input?.closest("dialog[data-asta-command]");
|
|
4201
|
+
if (root && !root.hasAttribute("data-server")) cmdFilter(root);
|
|
4202
|
+
});
|
|
4203
|
+
document.addEventListener("pointermove", (e) => {
|
|
4204
|
+
const opt = elOf(e.target)?.closest('[data-asta-command-list] [role="option"]');
|
|
4205
|
+
const root = opt?.closest("dialog[data-asta-command]");
|
|
4206
|
+
if (!root || opt.getAttribute("aria-selected") === "true") return;
|
|
4207
|
+
cmdActive(root, cmdOptions(root).indexOf(opt));
|
|
4208
|
+
});
|
|
4209
|
+
document.addEventListener("click", (e) => {
|
|
4210
|
+
const opt = elOf(e.target)?.closest('[data-asta-command-list] [role="option"]');
|
|
4211
|
+
const root = opt?.closest("dialog[data-asta-command]");
|
|
4212
|
+
if (root && !opt.hasAttribute("data-keep-open")) window.asta?.close(root.id);
|
|
4213
|
+
});
|
|
4214
|
+
// Start fresh each time it closes.
|
|
4215
|
+
document.addEventListener(
|
|
4216
|
+
"close",
|
|
4217
|
+
(e) => {
|
|
4218
|
+
const root = e.target;
|
|
4219
|
+
if (!(root instanceof HTMLDialogElement) || !root.hasAttribute("data-asta-command")) return;
|
|
4220
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
4221
|
+
if (input && input.value) {
|
|
4222
|
+
input.value = "";
|
|
4223
|
+
if (root.hasAttribute("data-server")) input.dispatchEvent(new Event("search", { bubbles: true }));
|
|
4224
|
+
}
|
|
4225
|
+
if (!root.hasAttribute("data-server")) cmdFilter(root);
|
|
4226
|
+
},
|
|
4227
|
+
true,
|
|
4228
|
+
);
|
|
4229
|
+
|
|
4230
|
+
/* ---------- Navbar ---------- */
|
|
4231
|
+
|
|
4232
|
+
const navTriggers = (root) => [...root.querySelectorAll("[data-asta-nav-trigger]")];
|
|
4233
|
+
function navShow(root, i, focusPanel) {
|
|
4234
|
+
clearTimeout(root._astaT);
|
|
4235
|
+
const cur = root._astaOpen ?? null;
|
|
4236
|
+
if (i === null) {
|
|
4237
|
+
if (cur === null) return;
|
|
4238
|
+
root._astaOpen = null;
|
|
4239
|
+
root.removeAttribute("data-open");
|
|
4240
|
+
for (const t of navTriggers(root)) t.setAttribute("aria-expanded", "false");
|
|
4241
|
+
return;
|
|
4242
|
+
}
|
|
4243
|
+
const box = root.querySelector("[data-asta-nav-surface] > div");
|
|
4244
|
+
const bg = root.querySelector("[data-asta-nav-bg]");
|
|
4245
|
+
const panels = [...root.querySelectorAll("[data-asta-nav-panel]")];
|
|
4246
|
+
const next = root.querySelector(`[data-asta-nav-panel="${i}"]`);
|
|
4247
|
+
if (!next || !box) return;
|
|
4248
|
+
const was = cur !== null && root.hasAttribute("data-open");
|
|
4249
|
+
const old = was ? { w: bg.offsetWidth, h: bg.offsetHeight } : null;
|
|
4250
|
+
for (const p of panels) p.hidden = p !== next;
|
|
4251
|
+
root._astaOpen = i;
|
|
4252
|
+
root.setAttribute("data-open", "");
|
|
4253
|
+
for (const t of navTriggers(root)) t.setAttribute("aria-expanded", String(t.dataset.astaNavTrigger === String(i)));
|
|
4254
|
+
if (was && cur !== i && !reduce()) {
|
|
4255
|
+
// The surface takes the new size at once; its background scales from the old size (FLIP),
|
|
4256
|
+
// and the new content slides in from the side you moved toward.
|
|
4257
|
+
const w = bg.offsetWidth;
|
|
4258
|
+
const h = bg.offsetHeight;
|
|
4259
|
+
bg.style.transition = "none";
|
|
4260
|
+
bg.style.transform = `scale(${old.w / w}, ${old.h / h})`;
|
|
4261
|
+
void bg.offsetWidth;
|
|
4262
|
+
bg.style.transition = "";
|
|
4263
|
+
bg.style.transform = "";
|
|
4264
|
+
next.style.setProperty("--asta-dir", i > cur ? "1" : "-1");
|
|
4265
|
+
next.classList.remove("asta-nav-slide");
|
|
4266
|
+
void next.offsetWidth;
|
|
4267
|
+
next.classList.add("asta-nav-slide");
|
|
4268
|
+
} else next.classList.remove("asta-nav-slide");
|
|
4269
|
+
if (focusPanel) next.querySelector("[data-asta-nav-link]")?.focus();
|
|
4270
|
+
}
|
|
4271
|
+
const navZone = (root, el) => !!el && [...root.querySelectorAll("[data-asta-nav-zone]")].some((z) => z.contains(el));
|
|
4272
|
+
document.addEventListener("pointerover", (e) => {
|
|
4273
|
+
if (e.pointerType !== "mouse") return;
|
|
4274
|
+
const t = elOf(e.target);
|
|
4275
|
+
const root = t?.closest("[data-asta-navbar]");
|
|
4276
|
+
if (!root) return;
|
|
4277
|
+
const trig = t.closest("[data-asta-nav-trigger]");
|
|
4278
|
+
if (trig) {
|
|
4279
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
4280
|
+
clearTimeout(root._astaT);
|
|
4281
|
+
// Hover intent: a short wait before the first open, instant while moving between triggers.
|
|
4282
|
+
const hover = () => {
|
|
4283
|
+
root._astaHoverAt = performance.now();
|
|
4284
|
+
navShow(root, i);
|
|
4285
|
+
};
|
|
4286
|
+
if ((root._astaOpen ?? null) === null) root._astaT = setTimeout(hover, 110);
|
|
4287
|
+
else if (root._astaOpen !== i) hover();
|
|
4288
|
+
} else if (t.closest("[data-asta-nav-link]") && t.closest("nav") && (root._astaOpen ?? null) !== null) navShow(root, null);
|
|
4289
|
+
else if (navZone(root, t)) clearTimeout(root._astaT);
|
|
4290
|
+
});
|
|
4291
|
+
document.addEventListener("pointerout", (e) => {
|
|
4292
|
+
if (e.pointerType !== "mouse") return;
|
|
4293
|
+
const t = elOf(e.target);
|
|
4294
|
+
const root = t?.closest("[data-asta-navbar]");
|
|
4295
|
+
if (!root || !navZone(root, t) || navZone(root, elOf(e.relatedTarget))) return;
|
|
4296
|
+
clearTimeout(root._astaT);
|
|
4297
|
+
root._astaT = setTimeout(() => navShow(root, null), 180);
|
|
4298
|
+
});
|
|
4299
|
+
document.addEventListener("click", (e) => {
|
|
4300
|
+
const t = elOf(e.target);
|
|
4301
|
+
const trig = t?.closest("[data-asta-nav-trigger]");
|
|
4302
|
+
if (trig) {
|
|
4303
|
+
const root = trig.closest("[data-asta-navbar]");
|
|
4304
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
4305
|
+
// A click right after hover opened it keeps it open instead of toggling it shut.
|
|
4306
|
+
if (root._astaOpen === i && performance.now() - (root._astaHoverAt || 0) < 600) return;
|
|
4307
|
+
navShow(root, root._astaOpen === i ? null : i);
|
|
4308
|
+
return;
|
|
4309
|
+
}
|
|
4310
|
+
const burger = t?.closest("[data-asta-nav-burger]");
|
|
4311
|
+
if (burger) {
|
|
4312
|
+
const root = burger.closest("[data-asta-navbar]");
|
|
4313
|
+
navSheet(root, !root.hasAttribute("data-sheet"));
|
|
4314
|
+
}
|
|
4315
|
+
});
|
|
4316
|
+
function navSheet(root, on) {
|
|
4317
|
+
const sheet = root.querySelector("[data-asta-nav-sheet]");
|
|
4318
|
+
const burger = root.querySelector("[data-asta-nav-burger]");
|
|
4319
|
+
if (!sheet) return;
|
|
4320
|
+
sheet.hidden = !on;
|
|
4321
|
+
root.toggleAttribute("data-sheet", on);
|
|
4322
|
+
burger?.setAttribute("aria-expanded", String(on));
|
|
4323
|
+
burger?.setAttribute("aria-label", on ? "Close menu" : "Open menu");
|
|
4324
|
+
}
|
|
4325
|
+
document.addEventListener("pointerdown", (e) => {
|
|
4326
|
+
const t = elOf(e.target);
|
|
4327
|
+
for (const root of document.querySelectorAll("[data-asta-navbar]")) {
|
|
4328
|
+
if (root.contains(t)) continue;
|
|
4329
|
+
navShow(root, null);
|
|
4330
|
+
if (root.hasAttribute("data-sheet")) navSheet(root, false);
|
|
4331
|
+
}
|
|
4332
|
+
});
|
|
4333
|
+
document.addEventListener("keydown", (e) => {
|
|
4334
|
+
const t = elOf(e.target);
|
|
4335
|
+
if (e.key === "Escape") {
|
|
4336
|
+
for (const root of document.querySelectorAll("[data-asta-navbar][data-open],[data-asta-navbar][data-sheet]")) {
|
|
4337
|
+
const i = root._astaOpen ?? null;
|
|
4338
|
+
if (i !== null) {
|
|
4339
|
+
navShow(root, null);
|
|
4340
|
+
if (root.contains(t)) root.querySelector(`[data-asta-nav-trigger="${i}"]`)?.focus();
|
|
4341
|
+
}
|
|
4342
|
+
if (root.hasAttribute("data-sheet")) {
|
|
4343
|
+
navSheet(root, false);
|
|
4344
|
+
root.querySelector("[data-asta-nav-burger]")?.focus();
|
|
4345
|
+
}
|
|
4346
|
+
}
|
|
4347
|
+
return;
|
|
4348
|
+
}
|
|
4349
|
+
const trig = t?.closest("[data-asta-nav-trigger]");
|
|
4350
|
+
if (!trig) return;
|
|
4351
|
+
const root = trig.closest("[data-asta-navbar]");
|
|
4352
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
4353
|
+
if (e.key === "ArrowDown" || ((e.key === "Enter" || e.key === " ") && root._astaOpen !== i)) {
|
|
4354
|
+
e.preventDefault();
|
|
4355
|
+
navShow(root, i, true);
|
|
4356
|
+
} else if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
|
|
4357
|
+
e.preventDefault();
|
|
4358
|
+
const all = [...root.querySelectorAll('nav[aria-label="Main"] li > *')];
|
|
4359
|
+
const at = all.indexOf(trig);
|
|
4360
|
+
const next = all[(at + (e.key === "ArrowRight" ? 1 : -1) + all.length) % all.length];
|
|
4361
|
+
next?.focus();
|
|
4362
|
+
if ((root._astaOpen ?? null) !== null && next?.matches("[data-asta-nav-trigger]")) navShow(root, Number(next.dataset.astaNavTrigger));
|
|
4363
|
+
}
|
|
4364
|
+
});
|
|
4365
|
+
const navScrolled = () => {
|
|
4366
|
+
const on = scrollY > 40;
|
|
4367
|
+
for (const root of document.querySelectorAll("[data-asta-navbar]")) root.toggleAttribute("data-scrolled", on);
|
|
4368
|
+
};
|
|
4369
|
+
addEventListener("scroll", navScrolled, { passive: true });
|
|
4370
|
+
|
|
4371
|
+
/* ---------- Sidebar ---------- */
|
|
4372
|
+
|
|
4373
|
+
function sbPlace(root, animate) {
|
|
4374
|
+
const ind = root.querySelector("[data-asta-sidebar-indicator]");
|
|
4375
|
+
if (!ind) return;
|
|
4376
|
+
const el = root.querySelector('[data-asta-sidebar-item][aria-current="page"]');
|
|
4377
|
+
if (!el) {
|
|
4378
|
+
ind.style.opacity = "0";
|
|
4379
|
+
return;
|
|
4380
|
+
}
|
|
4381
|
+
const box = ind.parentElement.getBoundingClientRect();
|
|
4382
|
+
const y = el.getBoundingClientRect().top - box.top;
|
|
4383
|
+
if (!animate) ind.style.transition = "none";
|
|
4384
|
+
ind.style.transform = `translateY(${y}px)`;
|
|
4385
|
+
ind.style.opacity = "1";
|
|
4386
|
+
if (!animate) {
|
|
4387
|
+
void ind.offsetHeight;
|
|
4388
|
+
ind.style.transition = "";
|
|
4389
|
+
}
|
|
4390
|
+
}
|
|
4391
|
+
function sbCollapse(root, on) {
|
|
4392
|
+
root.toggleAttribute("data-collapsed", on);
|
|
4393
|
+
const btn = root.querySelector("[data-asta-sidebar-toggle]");
|
|
4394
|
+
btn?.setAttribute("aria-pressed", String(on));
|
|
4395
|
+
btn?.setAttribute("aria-label", on ? "Expand sidebar" : "Collapse sidebar");
|
|
4396
|
+
fire(root, "asta:collapse", { collapsed: on });
|
|
4397
|
+
}
|
|
4398
|
+
document.addEventListener("click", (e) => {
|
|
4399
|
+
const t = elOf(e.target);
|
|
4400
|
+
const toggle = t?.closest("[data-asta-sidebar-toggle]");
|
|
4401
|
+
if (toggle) {
|
|
4402
|
+
const root = toggle.closest("[data-asta-sidebar]");
|
|
4403
|
+
return sbCollapse(root, !root.hasAttribute("data-collapsed"));
|
|
4404
|
+
}
|
|
4405
|
+
const item = t?.closest("[data-asta-sidebar-item]");
|
|
4406
|
+
const root = item?.closest("[data-asta-sidebar]");
|
|
4407
|
+
if (!root) return;
|
|
4408
|
+
for (const it of root.querySelectorAll("[data-asta-sidebar-item]")) {
|
|
4409
|
+
if (it === item) it.setAttribute("aria-current", "page");
|
|
4410
|
+
else it.removeAttribute("aria-current");
|
|
4411
|
+
}
|
|
4412
|
+
sbPlace(root, true);
|
|
4413
|
+
});
|
|
4414
|
+
document.addEventListener("keydown", (e) => {
|
|
4415
|
+
const t = elOf(e.target);
|
|
4416
|
+
const item = t?.closest("[data-asta-sidebar-nav] [data-asta-sidebar-item]");
|
|
4417
|
+
if (!item) return;
|
|
4418
|
+
const els = [...item.closest("[data-asta-sidebar-nav]").querySelectorAll("[data-asta-sidebar-item]")];
|
|
4419
|
+
const i = els.indexOf(item);
|
|
4420
|
+
const next = { ArrowDown: i + 1, ArrowUp: i - 1, Home: 0, End: els.length - 1 }[e.key];
|
|
4421
|
+
if (next === undefined) return;
|
|
4422
|
+
e.preventDefault();
|
|
4423
|
+
els[(next + els.length) % els.length]?.focus();
|
|
4424
|
+
});
|
|
4425
|
+
|
|
4426
|
+
/* ---------- Tool dock ---------- */
|
|
4427
|
+
|
|
4428
|
+
function dockPlace(root, animate) {
|
|
4429
|
+
const pill = root.querySelector("[data-asta-tooldock-pill]");
|
|
4430
|
+
const on = root.querySelector('[data-tool][aria-pressed="true"]');
|
|
4431
|
+
if (!pill) return;
|
|
4432
|
+
if (!on) {
|
|
4433
|
+
pill.style.opacity = "0";
|
|
4434
|
+
return;
|
|
4435
|
+
}
|
|
4436
|
+
// The button sits in a positioned wrapper (for its tooltip), so measure the wrapper against the bar.
|
|
4437
|
+
const wrap = on.parentElement;
|
|
4438
|
+
if (!animate) pill.style.transition = "none";
|
|
4439
|
+
pill.style.width = `${wrap.offsetWidth}px`;
|
|
4440
|
+
pill.style.transform = `translateX(${wrap.offsetLeft}px)`;
|
|
4441
|
+
pill.style.opacity = "1";
|
|
4442
|
+
if (!animate) {
|
|
4443
|
+
void pill.offsetWidth;
|
|
4444
|
+
pill.style.transition = "";
|
|
4445
|
+
}
|
|
4446
|
+
}
|
|
4447
|
+
function dockPick(root, btn) {
|
|
4448
|
+
if (!btn || btn.disabled) return;
|
|
4449
|
+
for (const b of root.querySelectorAll("[data-tool]")) {
|
|
4450
|
+
const on = b === btn;
|
|
4451
|
+
b.setAttribute("aria-pressed", String(on));
|
|
4452
|
+
b.tabIndex = on ? 0 : -1;
|
|
4453
|
+
}
|
|
4454
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
4455
|
+
if (input) input.value = btn.dataset.tool;
|
|
4456
|
+
dockPlace(root, true);
|
|
4457
|
+
fire(root, "asta:change", { value: btn.dataset.tool });
|
|
4458
|
+
}
|
|
4459
|
+
document.addEventListener("click", (e) => {
|
|
4460
|
+
const btn = elOf(e.target)?.closest("[data-asta-tooldock] [data-tool]");
|
|
4461
|
+
if (btn) dockPick(btn.closest("[data-asta-tooldock]"), btn);
|
|
4462
|
+
});
|
|
4463
|
+
document.addEventListener("keydown", (e) => {
|
|
4464
|
+
const t = elOf(e.target);
|
|
4465
|
+
const btn = t?.closest("[data-asta-tooldock] [data-tool]");
|
|
4466
|
+
if (btn) {
|
|
4467
|
+
const root = btn.closest("[data-asta-tooldock]");
|
|
4468
|
+
const list = [...root.querySelectorAll("[data-tool]:not([disabled])")];
|
|
4469
|
+
const i = list.indexOf(btn);
|
|
4470
|
+
const next = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: list.length - 1 }[e.key];
|
|
4471
|
+
if (next !== undefined) {
|
|
4472
|
+
e.preventDefault();
|
|
4473
|
+
list[(next + list.length) % list.length]?.focus();
|
|
4474
|
+
return;
|
|
4475
|
+
}
|
|
4476
|
+
}
|
|
4477
|
+
// Single-key shortcuts from anywhere, unless typing or a modifier is held.
|
|
4478
|
+
if (e.metaKey || e.ctrlKey || e.altKey || typing(e.target) || e.key.length !== 1 || e.defaultPrevented) return;
|
|
4479
|
+
if (t?.closest("[data-asta-mpanel],dialog[open]")) return;
|
|
4480
|
+
const k = e.key.toLowerCase();
|
|
4481
|
+
for (const root of document.querySelectorAll("[data-asta-tooldock][data-hotkeys]")) {
|
|
4482
|
+
const hit = [...root.querySelectorAll("[data-tool][data-shortcut]:not([disabled])")].find((b) => b.dataset.shortcut.toLowerCase() === k);
|
|
4483
|
+
if (hit) dockPick(root, hit);
|
|
4484
|
+
}
|
|
4485
|
+
});
|
|
4486
|
+
|
|
4487
|
+
/* ---------- Stepper ---------- */
|
|
4488
|
+
|
|
4489
|
+
function stepGo(root, n) {
|
|
4490
|
+
const count = num(root.dataset.count, 0);
|
|
4491
|
+
const cur = num(root.dataset.step, 0);
|
|
4492
|
+
const next = Math.max(0, Math.min(count, n));
|
|
4493
|
+
if (next === cur && root.hasAttribute("data-ready")) return;
|
|
4494
|
+
root.dataset.step = String(next);
|
|
4495
|
+
root.toggleAttribute("data-first", next === 0);
|
|
4496
|
+
root.toggleAttribute("data-last", next === count - 1);
|
|
4497
|
+
root.toggleAttribute("data-complete", next >= count);
|
|
4498
|
+
const noBack = root.hasAttribute("data-no-back");
|
|
4499
|
+
for (const li of root.querySelectorAll("[data-asta-step]")) {
|
|
4500
|
+
const i = Number(li.dataset.astaStep);
|
|
4501
|
+
const state = i < next ? "done" : i === next ? "current" : "todo";
|
|
4502
|
+
li.dataset.state = state;
|
|
4503
|
+
if (state === "current") li.setAttribute("aria-current", "step");
|
|
4504
|
+
else li.removeAttribute("aria-current");
|
|
4505
|
+
const b = li.querySelector("[data-asta-step-goto]");
|
|
4506
|
+
if (b) b.disabled = noBack || state !== "done" || next >= count;
|
|
4507
|
+
}
|
|
4508
|
+
for (const b of root.querySelectorAll("[data-asta-step-back]")) b.disabled = next === 0;
|
|
4509
|
+
if (next === cur) return;
|
|
4510
|
+
for (const p of root.querySelectorAll("[data-asta-step-panel]")) {
|
|
4511
|
+
const on = Number(p.dataset.astaStepPanel) === next;
|
|
4512
|
+
p.hidden = !on;
|
|
4513
|
+
p.classList.remove("asta-step-next", "asta-step-back");
|
|
4514
|
+
if (on && !reduce()) {
|
|
4515
|
+
void p.offsetWidth;
|
|
4516
|
+
p.classList.add(next > cur ? "asta-step-next" : "asta-step-back");
|
|
4517
|
+
}
|
|
4518
|
+
}
|
|
4519
|
+
fire(root, "asta:step", { step: next });
|
|
4520
|
+
}
|
|
4521
|
+
document.addEventListener("click", (e) => {
|
|
4522
|
+
const t = elOf(e.target);
|
|
4523
|
+
const b = t?.closest("[data-asta-step-next],[data-asta-step-back],[data-asta-step-goto]");
|
|
4524
|
+
const root = b?.closest("[data-asta-stepper]");
|
|
4525
|
+
if (!root || b.disabled) return;
|
|
4526
|
+
const cur = num(root.dataset.step, 0);
|
|
4527
|
+
if (b.hasAttribute("data-asta-step-next")) stepGo(root, cur + 1);
|
|
4528
|
+
else if (b.hasAttribute("data-asta-step-back")) stepGo(root, cur - 1);
|
|
4529
|
+
else stepGo(root, num(b.getAttribute("data-asta-step-goto"), cur));
|
|
4530
|
+
});
|
|
4531
|
+
|
|
4532
|
+
/* ---------- Announcement bar ---------- */
|
|
4533
|
+
|
|
4534
|
+
const storage = {
|
|
4535
|
+
get: (k) => {
|
|
4536
|
+
try {
|
|
4537
|
+
return localStorage.getItem(k);
|
|
4538
|
+
} catch {
|
|
4539
|
+
return null;
|
|
4540
|
+
}
|
|
4541
|
+
},
|
|
4542
|
+
set: (k, v) => {
|
|
4543
|
+
try {
|
|
4544
|
+
localStorage.setItem(k, v);
|
|
4545
|
+
} catch {}
|
|
4546
|
+
},
|
|
4547
|
+
};
|
|
4548
|
+
function announceTick(root) {
|
|
4549
|
+
if (!root.isConnected) return clearInterval(root._astaTimer);
|
|
4550
|
+
if (root._astaPaused || document.hidden || reduce() || root.dataset.state === "closed") return;
|
|
4551
|
+
const msgs = [...root.querySelectorAll("[data-asta-announce-msg]")];
|
|
4552
|
+
const cur = msgs.findIndex((m) => m.dataset.state === "cur" || m.dataset.state === "in");
|
|
4553
|
+
const next = (cur + 1) % msgs.length;
|
|
4554
|
+
msgs.forEach((m, i) => {
|
|
4555
|
+
if (i === next) {
|
|
4556
|
+
m.dataset.state = "in";
|
|
4557
|
+
m.removeAttribute("aria-hidden");
|
|
4558
|
+
} else if (i === cur) {
|
|
4559
|
+
m.dataset.state = "out";
|
|
4560
|
+
m.setAttribute("aria-hidden", "true");
|
|
4561
|
+
} else {
|
|
4562
|
+
delete m.dataset.state;
|
|
4563
|
+
m.setAttribute("aria-hidden", "true");
|
|
4564
|
+
}
|
|
4565
|
+
});
|
|
4566
|
+
}
|
|
4567
|
+
function announceInit(root) {
|
|
4568
|
+
if (root._astaInit) return;
|
|
4569
|
+
root._astaInit = true;
|
|
4570
|
+
const key = root.dataset.storageKey;
|
|
4571
|
+
if (key && storage.get(key) === "1") return root.remove();
|
|
4572
|
+
if (root.querySelectorAll("[data-asta-announce-msg]").length > 1) {
|
|
4573
|
+
root._astaTimer = setInterval(() => announceTick(root), num(root.dataset.interval, 5000));
|
|
4574
|
+
}
|
|
4575
|
+
}
|
|
4576
|
+
const announcePause = (root, on) => {
|
|
4577
|
+
root._astaPaused = on;
|
|
4578
|
+
// Live only while still, so a rotating bar doesn't talk over the page.
|
|
4579
|
+
root.querySelector("[data-asta-announce-track]")?.setAttribute("aria-live", on || reduce() ? "polite" : "off");
|
|
4580
|
+
};
|
|
4581
|
+
document.addEventListener("pointerover", (e) => {
|
|
4582
|
+
const root = e.pointerType === "mouse" && elOf(e.target)?.closest("[data-asta-announce]");
|
|
4583
|
+
if (root) announcePause(root, true);
|
|
4584
|
+
});
|
|
4585
|
+
document.addEventListener("pointerout", (e) => {
|
|
4586
|
+
const root = e.pointerType === "mouse" && elOf(e.target)?.closest("[data-asta-announce]");
|
|
4587
|
+
if (root && !root.contains(elOf(e.relatedTarget)) && !root.contains(document.activeElement)) announcePause(root, false);
|
|
4588
|
+
});
|
|
4589
|
+
document.addEventListener("focusin", (e) => {
|
|
4590
|
+
const root = elOf(e.target)?.closest("[data-asta-announce]");
|
|
4591
|
+
if (root) announcePause(root, true);
|
|
4592
|
+
});
|
|
4593
|
+
document.addEventListener("focusout", (e) => {
|
|
4594
|
+
const root = elOf(e.target)?.closest("[data-asta-announce]");
|
|
4595
|
+
if (root && !root.contains(elOf(e.relatedTarget))) announcePause(root, false);
|
|
4596
|
+
});
|
|
4597
|
+
document.addEventListener("click", (e) => {
|
|
4598
|
+
const btn = elOf(e.target)?.closest("[data-asta-announce-close]");
|
|
4599
|
+
const root = btn?.closest("[data-asta-announce]");
|
|
4600
|
+
if (!root || root.dataset.state === "closed") return;
|
|
4601
|
+
// Focus would be lost with the bar: hand it to the page.
|
|
4602
|
+
if (root.contains(document.activeElement)) document.activeElement.blur();
|
|
4603
|
+
clearInterval(root._astaTimer);
|
|
4604
|
+
root.dataset.state = "closed";
|
|
4605
|
+
const done = () => {
|
|
4606
|
+
// Remember it once the exit has played.
|
|
4607
|
+
if (root.dataset.storageKey) storage.set(root.dataset.storageKey, "1");
|
|
4608
|
+
root.remove();
|
|
4609
|
+
fire(document.body, "asta:dismiss", { id: root.id });
|
|
4610
|
+
};
|
|
4611
|
+
if (reduce()) return done();
|
|
4612
|
+
let finished = false;
|
|
4613
|
+
const finish = () => {
|
|
4614
|
+
if (finished) return;
|
|
4615
|
+
finished = true;
|
|
4616
|
+
done();
|
|
4617
|
+
};
|
|
4618
|
+
root.addEventListener("animationend", (ev) => ev.target === root && finish());
|
|
4619
|
+
setTimeout(finish, 450);
|
|
4620
|
+
});
|
|
4621
|
+
|
|
4622
|
+
/* ---------- Newsletter ---------- */
|
|
4623
|
+
|
|
4624
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
4625
|
+
const nlState = (root, s) => {
|
|
4626
|
+
root.dataset.state = s;
|
|
4627
|
+
root.querySelector("form")?.toggleAttribute("inert", s === "done");
|
|
4628
|
+
root.querySelector("[data-asta-newsletter-done]")?.setAttribute("aria-hidden", String(s !== "done"));
|
|
4629
|
+
};
|
|
4630
|
+
document.addEventListener(
|
|
4631
|
+
"submit",
|
|
4632
|
+
(e) => {
|
|
4633
|
+
const form = elOf(e.target);
|
|
4634
|
+
const root = form?.closest("[data-asta-newsletter]");
|
|
4635
|
+
if (!root) return;
|
|
4636
|
+
const input = form.querySelector('input[type="email"]');
|
|
4637
|
+
if (!EMAIL.test(input?.value.trim() ?? "")) {
|
|
4638
|
+
// Stop htmx from posting a bad address.
|
|
4639
|
+
e.preventDefault();
|
|
4640
|
+
e.stopImmediatePropagation();
|
|
4641
|
+
nlState(root, "error");
|
|
4642
|
+
input?.setAttribute("aria-invalid", "true");
|
|
4643
|
+
return;
|
|
4644
|
+
}
|
|
4645
|
+
input?.removeAttribute("aria-invalid");
|
|
4646
|
+
if (form.hasAttribute("hx-post")) return nlState(root, "busy");
|
|
4647
|
+
// No endpoint: a demo, so fake a short request.
|
|
4648
|
+
e.preventDefault();
|
|
4649
|
+
nlState(root, "busy");
|
|
4650
|
+
const btn = form.querySelector('button[type="submit"]');
|
|
4651
|
+
btn?.classList.add("htmx-request");
|
|
4652
|
+
setTimeout(() => {
|
|
4653
|
+
btn?.classList.remove("htmx-request");
|
|
4654
|
+
nlState(root, "done");
|
|
4655
|
+
}, 900);
|
|
4656
|
+
},
|
|
4657
|
+
true,
|
|
4658
|
+
);
|
|
4659
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
4660
|
+
const root = elOf(e.target)?.closest("[data-asta-newsletter]");
|
|
4661
|
+
if (root) nlState(root, e.detail?.successful ? "done" : "error");
|
|
4662
|
+
});
|
|
4663
|
+
document.addEventListener("input", (e) => {
|
|
4664
|
+
const root = elOf(e.target)?.closest("[data-asta-newsletter]");
|
|
4665
|
+
if (root?.dataset.state === "error") {
|
|
4666
|
+
nlState(root, "idle");
|
|
4667
|
+
e.target.removeAttribute("aria-invalid");
|
|
4668
|
+
}
|
|
4669
|
+
});
|
|
4670
|
+
|
|
4671
|
+
/* ---------- Wordmark: letters near the pointer lift ---------- */
|
|
4672
|
+
|
|
4673
|
+
document.addEventListener("pointermove", (e) => {
|
|
4674
|
+
const box = elOf(e.target)?.closest("[data-asta-wordmark]");
|
|
4675
|
+
if (box) wordmark(box, e.clientX);
|
|
4676
|
+
});
|
|
4677
|
+
document.addEventListener("pointerout", (e) => {
|
|
4678
|
+
const box = elOf(e.target)?.closest("[data-asta-wordmark]");
|
|
4679
|
+
if (box && !box.contains(elOf(e.relatedTarget))) wordmark(box, null);
|
|
4680
|
+
});
|
|
4681
|
+
function wordmark(box, x) {
|
|
4682
|
+
if (reduce()) return;
|
|
4683
|
+
for (const s of box.querySelectorAll("span[data-l]")) {
|
|
4684
|
+
if (x === null) {
|
|
4685
|
+
s.style.transform = "";
|
|
4686
|
+
continue;
|
|
4687
|
+
}
|
|
4688
|
+
// Measure without the current lift, so letters don't chase their own offset.
|
|
4689
|
+
const r = s.offsetLeft + s.offsetWidth / 2 + box.getBoundingClientRect().left;
|
|
4690
|
+
const k = Math.max(0, 1 - Math.abs(x - r) / (s.offsetWidth * 2.5));
|
|
4691
|
+
s.style.transform = `translateY(${-k * 14}%)`;
|
|
4692
|
+
}
|
|
4693
|
+
}
|
|
4694
|
+
|
|
4695
|
+
/* ---------- Boot and htmx ---------- */
|
|
4696
|
+
|
|
4697
|
+
const narrow = () => matchMedia("(max-width: 639px)").matches;
|
|
4698
|
+
function scan(el) {
|
|
4699
|
+
for (const root of el.querySelectorAll("[data-asta-sidebar]")) {
|
|
4700
|
+
if (!root._astaInit) {
|
|
4701
|
+
root._astaInit = true;
|
|
4702
|
+
if (root.hasAttribute("data-auto-rail") && narrow() && !root.hasAttribute("data-collapsed")) sbCollapse(root, true);
|
|
4703
|
+
}
|
|
4704
|
+
sbPlace(root, false);
|
|
4705
|
+
}
|
|
4706
|
+
for (const root of el.querySelectorAll("[data-asta-tooldock]")) dockPlace(root, false);
|
|
4707
|
+
for (const root of el.querySelectorAll("[data-asta-announce]")) announceInit(root);
|
|
4708
|
+
for (const root of el.querySelectorAll("[data-asta-stepper]:not([data-ready])")) {
|
|
4709
|
+
stepGo(root, num(root.dataset.step, 0));
|
|
4710
|
+
root.setAttribute("data-ready", "");
|
|
4711
|
+
}
|
|
4712
|
+
for (const root of el.querySelectorAll("dialog[data-asta-command]")) {
|
|
4713
|
+
if (root.hasAttribute("data-server")) cmdRefresh(root);
|
|
4714
|
+
else cmdFilter(root);
|
|
4715
|
+
}
|
|
4716
|
+
}
|
|
4717
|
+
const boot = () => {
|
|
4718
|
+
scan(document);
|
|
4719
|
+
navScrolled();
|
|
4720
|
+
};
|
|
4721
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
4722
|
+
else boot();
|
|
4723
|
+
document.addEventListener("htmx:afterSettle", (e) => {
|
|
4724
|
+
const t = elOf(e.detail?.elt) || document.body;
|
|
4725
|
+
// A server search answered: refresh its palette.
|
|
4726
|
+
const cmd = t.closest?.("dialog[data-asta-command]");
|
|
4727
|
+
if (cmd) return cmdRefresh(cmd);
|
|
4728
|
+
scan(t.parentElement || document);
|
|
4729
|
+
});
|
|
4730
|
+
// Re-measure the sliding highlights when the layout changes (fonts, resize, a sidebar folding).
|
|
4731
|
+
const resized = new ResizeObserver((entries) => {
|
|
4732
|
+
for (const en of entries) {
|
|
4733
|
+
const t = en.target;
|
|
4734
|
+
if (t.matches("[data-asta-sidebar]")) sbPlace(t, false);
|
|
4735
|
+
else if (t.matches("[data-asta-tooldock]")) dockPlace(t, false);
|
|
4736
|
+
}
|
|
4737
|
+
});
|
|
4738
|
+
const watch = () => {
|
|
4739
|
+
for (const el of document.querySelectorAll("[data-asta-sidebar],[data-asta-tooldock]")) {
|
|
4740
|
+
if (el._astaWatched) continue;
|
|
4741
|
+
el._astaWatched = true;
|
|
4742
|
+
resized.observe(el);
|
|
4743
|
+
}
|
|
4744
|
+
};
|
|
4745
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", watch);
|
|
4746
|
+
else watch();
|
|
4747
|
+
document.addEventListener("htmx:afterSettle", watch);
|
|
4748
|
+
})();
|