asta-ui 0.7.0 → 0.9.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/runtime/blocks.css +101 -0
- package/css/runtime/blocks.js +511 -0
- package/css/runtime/core.css +120 -0
- package/css/runtime/core.js +441 -0
- package/css/runtime/data.css +156 -0
- package/css/runtime/data.js +1017 -0
- package/css/runtime/forms.css +116 -0
- package/css/runtime/forms.js +1459 -0
- package/css/runtime/overlays.css +124 -0
- package/css/runtime/overlays.js +1310 -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
|
@@ -0,0 +1,1310 @@
|
|
|
1
|
+
/* Overlays: popover, hover card, dropdown and context menus, drawer drag, command palette, navbar, sidebar,
|
|
2
|
+
* workspace switcher, footer newsletter and wordmark, announcement bar, stepper, tool dock.
|
|
3
|
+
* Event delegation on document, so markup swapped in by htmx works. Dialog open/close comes from core (window.asta).
|
|
4
|
+
* Popover [data-asta-popover][popover] opened by [popovertarget]
|
|
5
|
+
* HoverCard [data-asta-hovercard][popover=manual] opened by [data-asta-hovercard-for]
|
|
6
|
+
* Menus [data-asta-dropdown] / [data-asta-context-menu] holding [role=menu][data-asta-mpanel]
|
|
7
|
+
* Drawer dialog.asta-drawer, [data-asta-drawer-handle] drags a bottom sheet
|
|
8
|
+
* Command dialog[data-asta-command], ⌘K / Ctrl+K
|
|
9
|
+
* Navbar [data-asta-navbar], Sidebar [data-asta-sidebar], ToolDock [data-asta-tooldock]
|
|
10
|
+
* Stepper [data-asta-stepper], AnnouncementBar [data-asta-announce], Newsletter [data-asta-newsletter]
|
|
11
|
+
*/
|
|
12
|
+
(() => {
|
|
13
|
+
if (window.__astaOverlays) return;
|
|
14
|
+
window.__astaOverlays = true;
|
|
15
|
+
|
|
16
|
+
const PAD = 8;
|
|
17
|
+
const FOCUSABLE =
|
|
18
|
+
'a[href],button:not([disabled]),input:not([disabled]):not([type="hidden"]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])';
|
|
19
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
20
|
+
const elOf = (t) => (t instanceof Element ? t : null);
|
|
21
|
+
const byId = (id) => (id ? document.getElementById(id) : null);
|
|
22
|
+
const isOpen = (p) => !!p && p.matches(":popover-open");
|
|
23
|
+
const showPop = (p, source) => {
|
|
24
|
+
if (isOpen(p)) return;
|
|
25
|
+
try {
|
|
26
|
+
p.showPopover(source ? { source } : undefined);
|
|
27
|
+
} catch {
|
|
28
|
+
p.showPopover();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const hidePop = (p) => {
|
|
32
|
+
if (isOpen(p)) p.hidePopover();
|
|
33
|
+
};
|
|
34
|
+
const fire = (el, name, detail) => el.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
35
|
+
const typing = (t) => t instanceof HTMLElement && (t.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName));
|
|
36
|
+
const num = (v, d) => {
|
|
37
|
+
const n = parseInt(v ?? "", 10);
|
|
38
|
+
return Number.isFinite(n) && n >= 0 ? n : d;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* ---------- Anchored placement (popover, hover card) ---------- */
|
|
42
|
+
|
|
43
|
+
const OPPOSITE = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
44
|
+
|
|
45
|
+
// Measure, pick the side (flip if needed), clamp to the viewport, and point the growth origin at the trigger.
|
|
46
|
+
function anchor(panel, a) {
|
|
47
|
+
const want = panel.dataset.side || "bottom";
|
|
48
|
+
const align = panel.dataset.align || "center";
|
|
49
|
+
const offset = num(panel.dataset.offset, 8);
|
|
50
|
+
const w = panel.offsetWidth;
|
|
51
|
+
const h = panel.offsetHeight;
|
|
52
|
+
const vw = innerWidth;
|
|
53
|
+
const vh = innerHeight;
|
|
54
|
+
const room = { top: a.top, bottom: vh - a.bottom, left: a.left, right: vw - a.right };
|
|
55
|
+
let s = OPPOSITE[want] ? want : "bottom";
|
|
56
|
+
const need = s === "top" || s === "bottom" ? h : w;
|
|
57
|
+
if (room[s] < need + offset + PAD && room[OPPOSITE[s]] > room[s]) s = OPPOSITE[s];
|
|
58
|
+
const vertical = s === "top" || s === "bottom";
|
|
59
|
+
let x;
|
|
60
|
+
let y;
|
|
61
|
+
if (vertical) {
|
|
62
|
+
x = align === "start" ? a.left : align === "end" ? a.right - w : a.left + a.width / 2 - w / 2;
|
|
63
|
+
y = s === "bottom" ? a.bottom + offset : a.top - offset - h;
|
|
64
|
+
x = Math.min(Math.max(PAD, x), vw - w - PAD);
|
|
65
|
+
} else {
|
|
66
|
+
y = align === "start" ? a.top : align === "end" ? a.bottom - h : a.top + a.height / 2 - h / 2;
|
|
67
|
+
x = s === "right" ? a.right + offset : a.left - offset - w;
|
|
68
|
+
y = Math.min(Math.max(PAD, y), vh - h - PAD);
|
|
69
|
+
}
|
|
70
|
+
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);
|
|
71
|
+
const nudge = { top: ["0px", "4px"], bottom: ["0px", "-4px"], left: ["4px", "0px"], right: ["-4px", "0px"] }[s];
|
|
72
|
+
panel.style.left = `${Math.round(x)}px`;
|
|
73
|
+
panel.style.top = `${Math.round(y)}px`;
|
|
74
|
+
panel.style.transformOrigin = vertical ? `${along}px ${s === "bottom" ? "0" : "100%"}` : `${s === "right" ? "0" : "100%"} ${along}px`;
|
|
75
|
+
panel.style.setProperty("--asta-nx", nudge[0]);
|
|
76
|
+
panel.style.setProperty("--asta-ny", nudge[1]);
|
|
77
|
+
panel.style.setProperty("--asta-gap", `${offset + 2}px`);
|
|
78
|
+
panel.dataset.placed = s;
|
|
79
|
+
const ar = panel.querySelector(":scope > [data-asta-arrow]");
|
|
80
|
+
if (ar) {
|
|
81
|
+
// The arrow box is 14×7. Its base overlaps the panel edge by 1px to hide the ring under it.
|
|
82
|
+
ar.style.transform = `rotate(${{ bottom: 0, top: 180, left: 90, right: -90 }[s]}deg)`;
|
|
83
|
+
ar.style.left = vertical ? `${along - 7}px` : s === "right" ? "-9.5px" : `${w - 4.5}px`;
|
|
84
|
+
ar.style.top = vertical ? (s === "bottom" ? "-6px" : `${h - 1}px`) : `${along - 3.5}px`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Open anchored panels follow their trigger on scroll and resize.
|
|
89
|
+
const anchored = new Set();
|
|
90
|
+
const follow = () => {
|
|
91
|
+
for (const p of anchored) {
|
|
92
|
+
if (!p.isConnected || !isOpen(p) || !p._astaAnchor?.isConnected) {
|
|
93
|
+
anchored.delete(p);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
anchor(p, p._astaAnchor.getBoundingClientRect());
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
addEventListener("scroll", () => anchored.size && follow(), { capture: true, passive: true });
|
|
100
|
+
addEventListener("resize", () => anchored.size && follow());
|
|
101
|
+
|
|
102
|
+
/* ---------- Popover ---------- */
|
|
103
|
+
|
|
104
|
+
// The click is handled here, not by the browser, so the panel is placed before it paints.
|
|
105
|
+
document.addEventListener(
|
|
106
|
+
"pointerdown",
|
|
107
|
+
(e) => {
|
|
108
|
+
const btn = elOf(e.target)?.closest("[popovertarget]");
|
|
109
|
+
const panel = btn && byId(btn.getAttribute("popovertarget"));
|
|
110
|
+
if (panel?.hasAttribute("data-asta-popover")) btn._astaWasOpen = isOpen(panel);
|
|
111
|
+
},
|
|
112
|
+
true,
|
|
113
|
+
);
|
|
114
|
+
document.addEventListener("click", (e) => {
|
|
115
|
+
const btn = elOf(e.target)?.closest("[popovertarget]");
|
|
116
|
+
const panel = btn && byId(btn.getAttribute("popovertarget"));
|
|
117
|
+
if (!panel?.hasAttribute("data-asta-popover") || btn.disabled) return;
|
|
118
|
+
e.preventDefault();
|
|
119
|
+
// A pointer press on the trigger may already have light-dismissed it: don't open it again.
|
|
120
|
+
const was = btn._astaWasOpen ?? isOpen(panel);
|
|
121
|
+
btn._astaWasOpen = undefined;
|
|
122
|
+
if (was || isOpen(panel)) {
|
|
123
|
+
hidePop(panel);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
openPopover(panel, btn);
|
|
127
|
+
});
|
|
128
|
+
function openPopover(panel, btn) {
|
|
129
|
+
panel._astaAnchor = btn;
|
|
130
|
+
showPop(panel, btn);
|
|
131
|
+
anchor(panel, btn.getBoundingClientRect());
|
|
132
|
+
anchored.add(panel);
|
|
133
|
+
btn.setAttribute("aria-expanded", "true");
|
|
134
|
+
const first = panel.querySelector("[autofocus],[data-autofocus]") || panel.querySelector(FOCUSABLE);
|
|
135
|
+
(first || panel).focus({ preventScroll: true });
|
|
136
|
+
}
|
|
137
|
+
document.addEventListener(
|
|
138
|
+
"keydown",
|
|
139
|
+
(e) => {
|
|
140
|
+
if (e.key !== "Escape") return;
|
|
141
|
+
const p = elOf(e.target)?.closest("[data-asta-popover]");
|
|
142
|
+
if (p) p._astaRefocus = true;
|
|
143
|
+
},
|
|
144
|
+
true,
|
|
145
|
+
);
|
|
146
|
+
document.addEventListener(
|
|
147
|
+
"toggle",
|
|
148
|
+
(e) => {
|
|
149
|
+
const p = e.target;
|
|
150
|
+
if (!(p instanceof HTMLElement) || !p.hasAttribute("data-asta-popover") || e.newState !== "closed") return;
|
|
151
|
+
anchored.delete(p);
|
|
152
|
+
p._astaAnchor?.setAttribute("aria-expanded", "false");
|
|
153
|
+
// Focus goes back to the trigger after Escape, or when it was lost with the panel.
|
|
154
|
+
if (p._astaRefocus || document.activeElement === document.body) p._astaAnchor?.focus({ preventScroll: true });
|
|
155
|
+
p._astaRefocus = false;
|
|
156
|
+
},
|
|
157
|
+
true,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
/* ---------- Hover card ---------- */
|
|
161
|
+
|
|
162
|
+
const cardFor = (trigger) => byId(trigger.getAttribute("data-asta-hovercard-for"));
|
|
163
|
+
const cardTrigger = (card) => card._astaAnchor || document.querySelector(`[data-asta-hovercard-for="${CSS.escape(card.id)}"]`);
|
|
164
|
+
function cardLater(card, trigger, open) {
|
|
165
|
+
clearTimeout(card._astaT);
|
|
166
|
+
if (open === isOpen(card) && !(open && trigger !== card._astaAnchor)) return;
|
|
167
|
+
const ms = open ? num(card.dataset.openDelay, 500) : num(card.dataset.closeDelay, 220);
|
|
168
|
+
card._astaT = setTimeout(() => (open ? cardOpen(card, trigger) : cardClose(card)), ms);
|
|
169
|
+
}
|
|
170
|
+
function cardOpen(card, trigger) {
|
|
171
|
+
clearTimeout(card._astaT);
|
|
172
|
+
if (!trigger?.isConnected) return;
|
|
173
|
+
card._astaAnchor = trigger;
|
|
174
|
+
showPop(card);
|
|
175
|
+
anchor(card, trigger.getBoundingClientRect());
|
|
176
|
+
anchored.add(card);
|
|
177
|
+
trigger.setAttribute("aria-describedby", card.id);
|
|
178
|
+
}
|
|
179
|
+
function cardClose(card) {
|
|
180
|
+
clearTimeout(card._astaT);
|
|
181
|
+
if (!isOpen(card)) return;
|
|
182
|
+
hidePop(card);
|
|
183
|
+
anchored.delete(card);
|
|
184
|
+
card._astaAnchor?.removeAttribute("aria-describedby");
|
|
185
|
+
}
|
|
186
|
+
document.addEventListener("pointerover", (e) => {
|
|
187
|
+
if (e.pointerType !== "mouse") return;
|
|
188
|
+
const t = elOf(e.target);
|
|
189
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
190
|
+
if (trigger && !trigger.contains(elOf(e.relatedTarget))) {
|
|
191
|
+
const card = cardFor(trigger);
|
|
192
|
+
if (card) cardLater(card, trigger, true);
|
|
193
|
+
}
|
|
194
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
195
|
+
if (card) clearTimeout(card._astaT);
|
|
196
|
+
});
|
|
197
|
+
document.addEventListener("pointerout", (e) => {
|
|
198
|
+
if (e.pointerType !== "mouse") return;
|
|
199
|
+
const t = elOf(e.target);
|
|
200
|
+
const to = elOf(e.relatedTarget);
|
|
201
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
202
|
+
if (trigger && !trigger.contains(to)) {
|
|
203
|
+
const card = cardFor(trigger);
|
|
204
|
+
if (card && !card.contains(to)) cardLater(card, trigger, false);
|
|
205
|
+
}
|
|
206
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
207
|
+
if (card && !card.contains(to) && !cardTrigger(card)?.contains(to)) cardLater(card, null, false);
|
|
208
|
+
});
|
|
209
|
+
document.addEventListener("focusin", (e) => {
|
|
210
|
+
const t = elOf(e.target);
|
|
211
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
212
|
+
// Keyboard focus only: a mouse click shouldn't pop the card a second time.
|
|
213
|
+
if (trigger?.matches(":focus-visible")) {
|
|
214
|
+
const card = cardFor(trigger);
|
|
215
|
+
if (card) cardLater(card, trigger, true);
|
|
216
|
+
}
|
|
217
|
+
const card = t?.closest("[data-asta-hovercard]");
|
|
218
|
+
if (card) clearTimeout(card._astaT);
|
|
219
|
+
});
|
|
220
|
+
document.addEventListener("focusout", (e) => {
|
|
221
|
+
const t = elOf(e.target);
|
|
222
|
+
const to = elOf(e.relatedTarget);
|
|
223
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
224
|
+
const card = trigger ? cardFor(trigger) : t?.closest("[data-asta-hovercard]");
|
|
225
|
+
if (!card || card.contains(to) || cardTrigger(card)?.contains(to)) return;
|
|
226
|
+
cardLater(card, null, false);
|
|
227
|
+
});
|
|
228
|
+
document.addEventListener("keydown", (e) => {
|
|
229
|
+
if (e.key !== "Escape") return;
|
|
230
|
+
const t = elOf(e.target);
|
|
231
|
+
const trigger = t?.closest("[data-asta-hovercard-for]");
|
|
232
|
+
const card = trigger ? cardFor(trigger) : t?.closest("[data-asta-hovercard]");
|
|
233
|
+
if (!card || !isOpen(card)) return;
|
|
234
|
+
e.stopPropagation();
|
|
235
|
+
const back = !trigger && card.contains(t);
|
|
236
|
+
cardClose(card);
|
|
237
|
+
if (back) card._astaAnchor?.focus({ preventScroll: true });
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
/* ---------- Menus: dropdown and context ---------- */
|
|
241
|
+
|
|
242
|
+
const ROOTS = "[data-asta-dropdown],[data-asta-context-menu]";
|
|
243
|
+
const panelOf = (root) => root.querySelector(":scope > [data-asta-mpanel]");
|
|
244
|
+
const subOf = (item) => item.parentElement?.matches("[data-asta-menu-sub]") ? item.parentElement.querySelector(":scope > [data-asta-mpanel]") : null;
|
|
245
|
+
const enabled = (i) => !i.disabled && i.getAttribute("aria-disabled") !== "true";
|
|
246
|
+
const itemsOf = (panel) => [...panel.querySelectorAll('[role^="menuitem"]')].filter((i) => i.closest('[role="menu"]') === panel && enabled(i));
|
|
247
|
+
const openRoots = () => [...document.querySelectorAll(`[data-asta-dropdown][data-open],[data-asta-context-menu][data-open]`)];
|
|
248
|
+
|
|
249
|
+
// at: { x, y } for a context menu, { rect, align } under a trigger, { rect, sub: true } beside a parent item.
|
|
250
|
+
function placeMenu(panel, at) {
|
|
251
|
+
const w = panel.offsetWidth;
|
|
252
|
+
const h = panel.offsetHeight;
|
|
253
|
+
const vw = innerWidth;
|
|
254
|
+
const vh = innerHeight;
|
|
255
|
+
let x;
|
|
256
|
+
let y;
|
|
257
|
+
let right = false;
|
|
258
|
+
let up = false;
|
|
259
|
+
if (at.sub) {
|
|
260
|
+
x = at.rect.right + 2;
|
|
261
|
+
if (x + w > vw - PAD && at.rect.left - w - 2 >= PAD) {
|
|
262
|
+
x = at.rect.left - w - 2;
|
|
263
|
+
right = true;
|
|
264
|
+
}
|
|
265
|
+
// Line the first item up with the parent item (the panel has 6px of padding).
|
|
266
|
+
y = at.rect.top - 6;
|
|
267
|
+
panel.style.setProperty("--asta-nx", right ? "4px" : "-4px");
|
|
268
|
+
panel.style.setProperty("--asta-ny", "0px");
|
|
269
|
+
} else if (at.rect) {
|
|
270
|
+
x = at.align === "end" ? at.rect.right - w : at.rect.left;
|
|
271
|
+
y = at.rect.bottom + 6;
|
|
272
|
+
if (y + h > vh - PAD && at.rect.top - h - 6 >= PAD) {
|
|
273
|
+
y = at.rect.top - h - 6;
|
|
274
|
+
up = true;
|
|
275
|
+
}
|
|
276
|
+
right = at.align === "end";
|
|
277
|
+
panel.style.setProperty("--asta-nx", "0px");
|
|
278
|
+
panel.style.setProperty("--asta-ny", up ? "4px" : "-4px");
|
|
279
|
+
} else {
|
|
280
|
+
x = at.x;
|
|
281
|
+
y = at.y;
|
|
282
|
+
if (x + w > vw - PAD && x - w >= PAD) {
|
|
283
|
+
x -= w;
|
|
284
|
+
right = true;
|
|
285
|
+
}
|
|
286
|
+
if (y + h > vh - PAD && y - h >= PAD) {
|
|
287
|
+
y -= h;
|
|
288
|
+
up = true;
|
|
289
|
+
}
|
|
290
|
+
panel.style.setProperty("--asta-nx", "0px");
|
|
291
|
+
panel.style.setProperty("--asta-ny", "0px");
|
|
292
|
+
}
|
|
293
|
+
x = Math.max(PAD, Math.min(x, vw - w - PAD));
|
|
294
|
+
y = Math.max(PAD, Math.min(y, vh - h - PAD));
|
|
295
|
+
panel.style.left = `${Math.round(x)}px`;
|
|
296
|
+
panel.style.top = `${Math.round(y)}px`;
|
|
297
|
+
panel.style.transformOrigin = `${right ? "100%" : "0"} ${up ? "100%" : "0"}`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function openPanel(panel, at, focusFirst) {
|
|
301
|
+
showPop(panel);
|
|
302
|
+
placeMenu(panel, at);
|
|
303
|
+
const first = focusFirst && itemsOf(panel)[0];
|
|
304
|
+
(first || panel).focus({ preventScroll: true });
|
|
305
|
+
}
|
|
306
|
+
function closeSubs(panel) {
|
|
307
|
+
for (const sub of panel.querySelectorAll("[data-asta-mpanel]")) {
|
|
308
|
+
if (!isOpen(sub)) continue;
|
|
309
|
+
hidePop(sub);
|
|
310
|
+
sub.parentElement?.querySelector(':scope > [aria-haspopup="menu"]')?.setAttribute("aria-expanded", "false");
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function openSub(item, focusFirst) {
|
|
314
|
+
const panel = item.closest('[role="menu"]');
|
|
315
|
+
const sub = subOf(item);
|
|
316
|
+
if (!sub) return;
|
|
317
|
+
clearTimeout(panel._astaT);
|
|
318
|
+
// One open submenu per level.
|
|
319
|
+
for (const other of panel.querySelectorAll(":scope [data-asta-menu-sub] > [data-asta-mpanel]")) {
|
|
320
|
+
if (other !== sub && other.closest('[role="menu"]:not([data-asta-menu-sub] > *)') !== null && other.parentElement.closest('[role="menu"]') === panel && isOpen(other)) {
|
|
321
|
+
closeSubs(other);
|
|
322
|
+
hidePop(other);
|
|
323
|
+
other.parentElement.querySelector(':scope > [aria-haspopup="menu"]')?.setAttribute("aria-expanded", "false");
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
item.setAttribute("aria-expanded", "true");
|
|
327
|
+
if (isOpen(sub)) {
|
|
328
|
+
if (focusFirst) itemsOf(sub)[0]?.focus({ preventScroll: true });
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
openPanel(sub, { rect: item.getBoundingClientRect(), sub: true }, focusFirst);
|
|
332
|
+
// Keep the highlight on the parent item while the pointer is on its way.
|
|
333
|
+
if (!focusFirst) item.focus({ preventScroll: true });
|
|
334
|
+
}
|
|
335
|
+
function closeSub(sub, refocus) {
|
|
336
|
+
closeSubs(sub);
|
|
337
|
+
hidePop(sub);
|
|
338
|
+
const item = sub.parentElement?.querySelector(':scope > [aria-haspopup="menu"]');
|
|
339
|
+
item?.setAttribute("aria-expanded", "false");
|
|
340
|
+
if (refocus) item?.focus({ preventScroll: true });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function openRoot(root, at, focusFirst) {
|
|
344
|
+
for (const other of openRoots()) if (other !== root) closeRoot(other, false);
|
|
345
|
+
const panel = panelOf(root);
|
|
346
|
+
if (!panel) return;
|
|
347
|
+
if (!root.hasAttribute("data-open")) {
|
|
348
|
+
const a = document.activeElement;
|
|
349
|
+
root._astaRestore = a && root.contains(a) && !panel.contains(a) ? a : root;
|
|
350
|
+
}
|
|
351
|
+
closeSubs(panel);
|
|
352
|
+
root.setAttribute("data-open", "");
|
|
353
|
+
root.querySelector(":scope > [data-asta-dropdown-trigger]")?.setAttribute("aria-expanded", "true");
|
|
354
|
+
root._astaAt = performance.now();
|
|
355
|
+
if (root.hasAttribute("data-match")) {
|
|
356
|
+
const t = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
357
|
+
if (t) panel.style.minWidth = `${t.offsetWidth}px`;
|
|
358
|
+
}
|
|
359
|
+
// Re-opening a context menu at a new point: hide first, so it animates in again.
|
|
360
|
+
if (isOpen(panel)) hidePop(panel);
|
|
361
|
+
openPanel(panel, at, focusFirst);
|
|
362
|
+
}
|
|
363
|
+
function closeRoot(root, refocus) {
|
|
364
|
+
if (!root.hasAttribute("data-open")) return;
|
|
365
|
+
const panel = panelOf(root);
|
|
366
|
+
if (panel) {
|
|
367
|
+
clearTimeout(panel._astaT);
|
|
368
|
+
closeSubs(panel);
|
|
369
|
+
hidePop(panel);
|
|
370
|
+
}
|
|
371
|
+
root.removeAttribute("data-open");
|
|
372
|
+
const trigger = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
373
|
+
trigger?.setAttribute("aria-expanded", "false");
|
|
374
|
+
if (refocus) (trigger || root._astaRestore || root).focus({ preventScroll: true });
|
|
375
|
+
}
|
|
376
|
+
function toggleDropdown(root, focusFirst) {
|
|
377
|
+
if (root.hasAttribute("data-open")) return closeRoot(root, false);
|
|
378
|
+
const t = root.querySelector(":scope > [data-asta-dropdown-trigger]");
|
|
379
|
+
openRoot(root, { rect: t.getBoundingClientRect(), align: root.getAttribute("data-align") === "end" ? "end" : "start" }, focusFirst);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Checkbox and radio items keep their hidden inputs in step and announce the change.
|
|
383
|
+
function check(item) {
|
|
384
|
+
const role = item.getAttribute("role");
|
|
385
|
+
if (role === "menuitemcheckbox") {
|
|
386
|
+
const on = item.getAttribute("aria-checked") !== "true";
|
|
387
|
+
item.setAttribute("aria-checked", String(on));
|
|
388
|
+
const input = item.querySelector(':scope > input[type="hidden"]');
|
|
389
|
+
if (input) input.disabled = !on;
|
|
390
|
+
fire(item, "asta:change", { checked: on, value: item.dataset.value ?? "" });
|
|
391
|
+
} else if (role === "menuitemradio") {
|
|
392
|
+
if (item.getAttribute("aria-checked") === "true") return;
|
|
393
|
+
const panel = item.closest('[role="menu"]');
|
|
394
|
+
const group = item.dataset.group ?? "";
|
|
395
|
+
for (const r of panel.querySelectorAll('[role="menuitemradio"]')) {
|
|
396
|
+
if ((r.dataset.group ?? "") !== group || r.closest('[role="menu"]') !== panel) continue;
|
|
397
|
+
const on = r === item;
|
|
398
|
+
r.setAttribute("aria-checked", String(on));
|
|
399
|
+
const input = r.querySelector('input[type="hidden"]');
|
|
400
|
+
if (input) input.disabled = !on;
|
|
401
|
+
}
|
|
402
|
+
fire(item, "asta:change", { checked: true, value: item.dataset.value ?? "" });
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
document.addEventListener("click", (e) => {
|
|
407
|
+
const t = elOf(e.target);
|
|
408
|
+
if (!t) return;
|
|
409
|
+
const trigger = t.closest("[data-asta-dropdown-trigger]");
|
|
410
|
+
if (trigger) {
|
|
411
|
+
const root = trigger.closest("[data-asta-dropdown]");
|
|
412
|
+
if (root) toggleDropdown(root, e.detail === 0);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const item = t.closest('[data-asta-mpanel] [role^="menuitem"]');
|
|
416
|
+
const root = item?.closest(ROOTS);
|
|
417
|
+
if (!item || !root || !enabled(item)) return;
|
|
418
|
+
if (item.getAttribute("aria-haspopup") === "menu") {
|
|
419
|
+
openSub(item, e.detail === 0);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
check(item);
|
|
423
|
+
if (!item.hasAttribute("data-keep-open")) closeRoot(root, true);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// A press outside every open panel closes its menu. A right-click inside a context area moves it instead.
|
|
427
|
+
document.addEventListener(
|
|
428
|
+
"pointerdown",
|
|
429
|
+
(e) => {
|
|
430
|
+
const t = elOf(e.target);
|
|
431
|
+
for (const root of openRoots()) {
|
|
432
|
+
if (t?.closest("[data-asta-mpanel]") && root.contains(t)) continue;
|
|
433
|
+
if (root.querySelector(":scope > [data-asta-dropdown-trigger]")?.contains(t)) continue;
|
|
434
|
+
if (e.button === 2 && root.matches("[data-asta-context-menu]") && root.contains(t)) continue;
|
|
435
|
+
closeRoot(root, false);
|
|
436
|
+
}
|
|
437
|
+
},
|
|
438
|
+
true,
|
|
439
|
+
);
|
|
440
|
+
addEventListener(
|
|
441
|
+
"scroll",
|
|
442
|
+
(e) => {
|
|
443
|
+
const t = elOf(e.target);
|
|
444
|
+
for (const root of openRoots()) if (!t?.closest?.("[data-asta-mpanel]")) closeRoot(root, false);
|
|
445
|
+
},
|
|
446
|
+
{ capture: true, passive: true },
|
|
447
|
+
);
|
|
448
|
+
addEventListener("resize", () => openRoots().forEach((r) => closeRoot(r, false)));
|
|
449
|
+
addEventListener("blur", () => openRoots().forEach((r) => closeRoot(r, false)));
|
|
450
|
+
|
|
451
|
+
// Context menu: right-click, Shift+F10 / Menu key, long-press on touch.
|
|
452
|
+
const ctxArea = (t) => {
|
|
453
|
+
const root = t?.closest("[data-asta-context-menu]");
|
|
454
|
+
if (!root || root.hasAttribute("data-disabled") || t.closest("[data-asta-mpanel]")) return null;
|
|
455
|
+
return root;
|
|
456
|
+
};
|
|
457
|
+
function ctxFromKeyboard(root) {
|
|
458
|
+
const a = document.activeElement;
|
|
459
|
+
const r = (a instanceof HTMLElement && root.contains(a) ? a : root).getBoundingClientRect();
|
|
460
|
+
openRoot(root, { x: r.left + Math.min(16, r.width / 2), y: r.top + Math.min(r.height / 2, 20) }, true);
|
|
461
|
+
}
|
|
462
|
+
document.addEventListener("contextmenu", (e) => {
|
|
463
|
+
const t = elOf(e.target);
|
|
464
|
+
if (t?.closest("[data-asta-mpanel]")) return e.preventDefault();
|
|
465
|
+
const root = ctxArea(t);
|
|
466
|
+
if (!root) return;
|
|
467
|
+
e.preventDefault();
|
|
468
|
+
clearTimeout(root._astaPress?.t);
|
|
469
|
+
// Already opened by a long-press a moment ago.
|
|
470
|
+
if (root.hasAttribute("data-open") && performance.now() - (root._astaAt || 0) < 700) return;
|
|
471
|
+
if (e.clientX === 0 && e.clientY === 0) return ctxFromKeyboard(root);
|
|
472
|
+
openRoot(root, { x: e.clientX, y: e.clientY }, false);
|
|
473
|
+
});
|
|
474
|
+
document.addEventListener("pointerdown", (e) => {
|
|
475
|
+
if (e.pointerType === "mouse") return;
|
|
476
|
+
const root = ctxArea(elOf(e.target));
|
|
477
|
+
if (!root) return;
|
|
478
|
+
clearTimeout(root._astaPress?.t);
|
|
479
|
+
const { clientX: x, clientY: y } = e;
|
|
480
|
+
root._astaPress = { x, y, t: setTimeout(() => openRoot(root, { x, y }, false), num(root.dataset.longPress, 500)) };
|
|
481
|
+
});
|
|
482
|
+
document.addEventListener("pointermove", (e) => {
|
|
483
|
+
if (e.pointerType === "mouse") return;
|
|
484
|
+
const root = elOf(e.target)?.closest("[data-asta-context-menu]");
|
|
485
|
+
const p = root?._astaPress;
|
|
486
|
+
if (p && Math.hypot(e.clientX - p.x, e.clientY - p.y) > 10) clearTimeout(p.t);
|
|
487
|
+
});
|
|
488
|
+
for (const type of ["pointerup", "pointercancel"]) {
|
|
489
|
+
document.addEventListener(type, (e) => clearTimeout(elOf(e.target)?.closest("[data-asta-context-menu]")?._astaPress?.t));
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Hover: the item under the pointer takes focus; a submenu item opens after a beat, leaving it closes after a grace.
|
|
493
|
+
document.addEventListener("pointerover", (e) => {
|
|
494
|
+
if (e.pointerType !== "mouse") return;
|
|
495
|
+
const t = elOf(e.target);
|
|
496
|
+
const panel = t?.closest("[data-asta-mpanel]");
|
|
497
|
+
if (!panel || !isOpen(panel)) return;
|
|
498
|
+
// Entering a submenu keeps it open.
|
|
499
|
+
const parent = panel.parentElement?.closest("[data-asta-mpanel]");
|
|
500
|
+
if (parent) clearTimeout(parent._astaT);
|
|
501
|
+
const item = t.closest('[role^="menuitem"]');
|
|
502
|
+
if (!item || item.closest('[role="menu"]') !== panel) return;
|
|
503
|
+
clearTimeout(panel._astaT);
|
|
504
|
+
if (!enabled(item)) return panel.focus({ preventScroll: true });
|
|
505
|
+
if (document.activeElement !== item) item.focus({ preventScroll: true });
|
|
506
|
+
const open = [...panel.querySelectorAll("[data-asta-menu-sub] > [data-asta-mpanel]")].find((s) => s.parentElement.closest('[role="menu"]') === panel && isOpen(s));
|
|
507
|
+
if (item.getAttribute("aria-haspopup") === "menu") {
|
|
508
|
+
if (open !== subOf(item)) panel._astaT = setTimeout(() => openSub(item, false), 100);
|
|
509
|
+
} else if (open) {
|
|
510
|
+
panel._astaT = setTimeout(() => closeSub(open, false), 260);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
document.addEventListener("pointerout", (e) => {
|
|
514
|
+
if (e.pointerType !== "mouse") return;
|
|
515
|
+
const panel = elOf(e.target)?.closest("[data-asta-mpanel]");
|
|
516
|
+
if (!panel || !isOpen(panel) || panel.contains(elOf(e.relatedTarget))) return;
|
|
517
|
+
const hasSub = [...panel.querySelectorAll("[data-asta-menu-sub] > [data-asta-mpanel]")].some(isOpen);
|
|
518
|
+
if (!hasSub && panel.contains(document.activeElement)) panel.focus({ preventScroll: true });
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
document.addEventListener("keydown", (e) => {
|
|
522
|
+
const t = elOf(e.target);
|
|
523
|
+
if (!t) return;
|
|
524
|
+
// Openers.
|
|
525
|
+
if (t.matches("[data-asta-dropdown-trigger]") && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
|
|
526
|
+
e.preventDefault();
|
|
527
|
+
const root = t.closest("[data-asta-dropdown]");
|
|
528
|
+
if (root && !root.hasAttribute("data-open")) toggleDropdown(root, true);
|
|
529
|
+
else if (root) itemsOf(panelOf(root))[0]?.focus();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if ((e.key === "ContextMenu" || (e.key === "F10" && e.shiftKey)) && ctxArea(t)) {
|
|
533
|
+
e.preventDefault();
|
|
534
|
+
ctxFromKeyboard(ctxArea(t));
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const panel = t.closest("[data-asta-mpanel]");
|
|
538
|
+
if (!panel || !isOpen(panel)) return;
|
|
539
|
+
const root = panel.closest(ROOTS);
|
|
540
|
+
const list = itemsOf(panel);
|
|
541
|
+
const i = list.indexOf(document.activeElement);
|
|
542
|
+
const parentSub = panel.parentElement?.matches("[data-asta-menu-sub]") ? panel : null;
|
|
543
|
+
const go = (n) => list[(n + list.length) % list.length]?.focus({ preventScroll: true });
|
|
544
|
+
switch (e.key) {
|
|
545
|
+
case "ArrowDown":
|
|
546
|
+
e.preventDefault();
|
|
547
|
+
return go(i < 0 ? 0 : i + 1);
|
|
548
|
+
case "ArrowUp":
|
|
549
|
+
e.preventDefault();
|
|
550
|
+
return go(i < 0 ? list.length - 1 : i - 1);
|
|
551
|
+
case "Home":
|
|
552
|
+
case "PageUp":
|
|
553
|
+
e.preventDefault();
|
|
554
|
+
return go(0);
|
|
555
|
+
case "End":
|
|
556
|
+
case "PageDown":
|
|
557
|
+
e.preventDefault();
|
|
558
|
+
return go(list.length - 1);
|
|
559
|
+
case "ArrowRight": {
|
|
560
|
+
const it = list[i];
|
|
561
|
+
if (it?.getAttribute("aria-haspopup") === "menu") {
|
|
562
|
+
e.preventDefault();
|
|
563
|
+
openSub(it, true);
|
|
564
|
+
}
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
case "ArrowLeft":
|
|
568
|
+
if (parentSub) {
|
|
569
|
+
e.preventDefault();
|
|
570
|
+
closeSub(parentSub, true);
|
|
571
|
+
}
|
|
572
|
+
return;
|
|
573
|
+
case "Escape":
|
|
574
|
+
e.preventDefault();
|
|
575
|
+
e.stopPropagation();
|
|
576
|
+
if (parentSub) return closeSub(parentSub, true);
|
|
577
|
+
return root && closeRoot(root, true);
|
|
578
|
+
case "Tab":
|
|
579
|
+
if (root?.matches("[data-asta-context-menu]")) e.preventDefault();
|
|
580
|
+
return root && closeRoot(root, true);
|
|
581
|
+
}
|
|
582
|
+
// Type-ahead: jump to the next item whose label starts with what was typed.
|
|
583
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && e.key !== " ") {
|
|
584
|
+
const now = e.timeStamp;
|
|
585
|
+
panel._astaTyped = now - (panel._astaTypedAt || 0) > 600 ? e.key.toLowerCase() : (panel._astaTyped || "") + e.key.toLowerCase();
|
|
586
|
+
panel._astaTypedAt = now;
|
|
587
|
+
const q = panel._astaTyped;
|
|
588
|
+
const start = Math.max(0, i + (q.length === 1 ? 1 : 0));
|
|
589
|
+
const order = [...list.slice(start), ...list.slice(0, start)];
|
|
590
|
+
order.find((it) => it.textContent.trim().toLowerCase().startsWith(q))?.focus({ preventScroll: true });
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
/* ---------- Workspace switchers: the trigger follows the picked workspace ---------- */
|
|
595
|
+
|
|
596
|
+
document.addEventListener("asta:change", (e) => {
|
|
597
|
+
const item = elOf(e.target);
|
|
598
|
+
const root = item?.closest("[data-asta-ws]");
|
|
599
|
+
if (!root || !item.hasAttribute("data-ws-name")) return;
|
|
600
|
+
const name = item.dataset.wsName;
|
|
601
|
+
const plan = item.dataset.wsPlan || "";
|
|
602
|
+
for (const n of root.querySelectorAll("[data-ws-name-out]")) if (!n.closest("[data-asta-mpanel]")) n.textContent = name;
|
|
603
|
+
for (const n of root.querySelectorAll("[data-ws-plan-out]")) {
|
|
604
|
+
if (n.closest("[data-asta-mpanel]")) continue;
|
|
605
|
+
n.textContent = plan;
|
|
606
|
+
n.hidden = !plan;
|
|
607
|
+
}
|
|
608
|
+
const box = item.querySelector("[data-ws-logo-box]");
|
|
609
|
+
for (const n of root.querySelectorAll("[data-ws-logo-out]")) {
|
|
610
|
+
if (n.closest("[data-asta-mpanel]")) continue;
|
|
611
|
+
if (box) n.innerHTML = box.innerHTML;
|
|
612
|
+
else n.textContent = [...name][0] || "";
|
|
613
|
+
}
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
/* ---------- Drawer: drag a bottom sheet down to close ---------- */
|
|
617
|
+
|
|
618
|
+
let drag = null;
|
|
619
|
+
document.addEventListener("pointerdown", (e) => {
|
|
620
|
+
const handle = elOf(e.target)?.closest("[data-asta-drawer-handle]");
|
|
621
|
+
const d = handle?.closest("dialog.asta-drawer");
|
|
622
|
+
if (!d || !d.open || e.button !== 0) return;
|
|
623
|
+
handle.setPointerCapture(e.pointerId);
|
|
624
|
+
drag = { d, handle, y: e.clientY, t: e.timeStamp, dy: 0, v: 0 };
|
|
625
|
+
d.style.transition = "none";
|
|
626
|
+
});
|
|
627
|
+
document.addEventListener("pointermove", (e) => {
|
|
628
|
+
if (!drag) return;
|
|
629
|
+
const raw = e.clientY - drag.y;
|
|
630
|
+
// Pulling up resists, pulling down follows 1:1.
|
|
631
|
+
const dy = raw < 0 ? -Math.sqrt(-raw) * 2 : raw;
|
|
632
|
+
drag.v = (dy - drag.dy) / Math.max(1, e.timeStamp - drag.t);
|
|
633
|
+
drag.t = e.timeStamp;
|
|
634
|
+
drag.dy = dy;
|
|
635
|
+
drag.d.style.transform = `translateY(${dy}px)`;
|
|
636
|
+
drag.d.style.setProperty("--asta-drawer-fade", String(Math.max(0, 1 - Math.max(0, dy) / drag.d.offsetHeight)));
|
|
637
|
+
});
|
|
638
|
+
const endDrag = () => {
|
|
639
|
+
if (!drag) return;
|
|
640
|
+
const { d, dy, v } = drag;
|
|
641
|
+
drag = null;
|
|
642
|
+
// Clear the inline styles in the same frame as the close, so the CSS transition runs from where it is.
|
|
643
|
+
d.style.transition = "";
|
|
644
|
+
d.style.transform = "";
|
|
645
|
+
d.style.removeProperty("--asta-drawer-fade");
|
|
646
|
+
if (dy > d.offsetHeight * 0.3 || v > 0.6) window.asta?.close(d.id);
|
|
647
|
+
};
|
|
648
|
+
document.addEventListener("pointerup", endDrag);
|
|
649
|
+
document.addEventListener("pointercancel", endDrag);
|
|
650
|
+
|
|
651
|
+
/* ---------- Command palette ---------- */
|
|
652
|
+
|
|
653
|
+
// Fuzzy match: every query letter appears in order. Prefers consecutive letters and word starts.
|
|
654
|
+
function fuzzy(text, query) {
|
|
655
|
+
const t = text.toLowerCase();
|
|
656
|
+
const q = query.toLowerCase().replace(/\s+/g, "");
|
|
657
|
+
if (!q) return { score: 0, hits: [] };
|
|
658
|
+
const hits = [];
|
|
659
|
+
let score = 0;
|
|
660
|
+
let from = 0;
|
|
661
|
+
for (const ch of q) {
|
|
662
|
+
const at = t.indexOf(ch, from);
|
|
663
|
+
if (at < 0) return null;
|
|
664
|
+
if (hits.length && at === hits[hits.length - 1] + 1) score += 3;
|
|
665
|
+
if (at === 0 || t[at - 1] === " ") score += 2;
|
|
666
|
+
score -= (at - from) * 0.1;
|
|
667
|
+
hits.push(at);
|
|
668
|
+
from = at + 1;
|
|
669
|
+
}
|
|
670
|
+
return { score, hits };
|
|
671
|
+
}
|
|
672
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
673
|
+
const cmdOptions = (root) => [...root.querySelectorAll('[data-asta-command-list] [role="option"]')].filter((o) => !o.hidden);
|
|
674
|
+
let cmdSeq = 0;
|
|
675
|
+
|
|
676
|
+
function cmdFilter(root) {
|
|
677
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
678
|
+
const q = root.hasAttribute("data-server") ? "" : (input?.value ?? "");
|
|
679
|
+
const all = [...root.querySelectorAll('[data-asta-command-list] [role="option"]')];
|
|
680
|
+
all.forEach((o, i) => {
|
|
681
|
+
if (o.dataset.i === undefined) o.dataset.i = String(i);
|
|
682
|
+
const label = o.querySelector("[data-asta-command-label]");
|
|
683
|
+
if (label && label.dataset.text === undefined) label.dataset.text = label.textContent;
|
|
684
|
+
const text = label?.dataset.text ?? o.textContent;
|
|
685
|
+
let m = fuzzy(text, q);
|
|
686
|
+
const kw = (o.dataset.keywords || "").toLowerCase();
|
|
687
|
+
if (!m && kw && q.trim() && kw.includes(q.trim().toLowerCase())) m = { score: -5, hits: [] };
|
|
688
|
+
o.hidden = !m;
|
|
689
|
+
o._astaScore = m ? m.score : -1e9;
|
|
690
|
+
if (label) {
|
|
691
|
+
const set = new Set(m?.hits ?? []);
|
|
692
|
+
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);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
for (const box of root.querySelectorAll("[data-asta-command-items]")) {
|
|
696
|
+
const opts = [...box.querySelectorAll(':scope > [role="option"]')];
|
|
697
|
+
opts.sort((a, b) => (q ? b._astaScore - a._astaScore : 0) || a.dataset.i - b.dataset.i);
|
|
698
|
+
for (const o of opts) box.append(o);
|
|
699
|
+
const group = box.closest("[data-asta-command-group]");
|
|
700
|
+
if (group) group.hidden = !opts.some((o) => !o.hidden);
|
|
701
|
+
}
|
|
702
|
+
cmdRefresh(root, q);
|
|
703
|
+
}
|
|
704
|
+
function cmdRefresh(root, q) {
|
|
705
|
+
const shown = cmdOptions(root);
|
|
706
|
+
const empty = root.querySelector("[data-asta-command-empty]");
|
|
707
|
+
if (empty) {
|
|
708
|
+
empty.hidden = shown.length > 0;
|
|
709
|
+
const qs = root.querySelector("[data-asta-command-q]");
|
|
710
|
+
if (qs) qs.textContent = q ?? root.querySelector("[data-asta-command-input]")?.value ?? "";
|
|
711
|
+
}
|
|
712
|
+
const count = root.querySelector("[data-asta-command-count]");
|
|
713
|
+
if (count) count.textContent = `${shown.length} ${shown.length === 1 ? "result" : "results"}`;
|
|
714
|
+
cmdActive(root, 0);
|
|
715
|
+
}
|
|
716
|
+
function cmdActive(root, i) {
|
|
717
|
+
const list = cmdOptions(root);
|
|
718
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
719
|
+
const at = list.length ? (i + list.length) % list.length : -1;
|
|
720
|
+
root._astaAt = at;
|
|
721
|
+
for (const o of root.querySelectorAll('[data-asta-command-list] [role="option"]')) o.setAttribute("aria-selected", "false");
|
|
722
|
+
const on = list[at];
|
|
723
|
+
if (on) {
|
|
724
|
+
if (!on.id) on.id = `${root.id || "asta-cmd"}-opt-${++cmdSeq}`;
|
|
725
|
+
on.setAttribute("aria-selected", "true");
|
|
726
|
+
on.scrollIntoView({ block: "nearest" });
|
|
727
|
+
input?.setAttribute("aria-activedescendant", on.id);
|
|
728
|
+
} else input?.removeAttribute("aria-activedescendant");
|
|
729
|
+
}
|
|
730
|
+
function cmdRun(root, opt) {
|
|
731
|
+
if (!opt) return;
|
|
732
|
+
if (!opt.hasAttribute("data-keep-open")) window.asta?.close(root.id);
|
|
733
|
+
opt.click();
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
document.addEventListener("keydown", (e) => {
|
|
737
|
+
// ⌘K / Ctrl+K toggles the first palette that listens for that key.
|
|
738
|
+
if ((e.metaKey || e.ctrlKey) && !e.altKey && e.key.length === 1) {
|
|
739
|
+
const root = [...document.querySelectorAll("dialog[data-asta-command]")].find((d) => (d.dataset.hotkey || "k").toLowerCase() === e.key.toLowerCase());
|
|
740
|
+
if (root) {
|
|
741
|
+
e.preventDefault();
|
|
742
|
+
if (root.open) window.asta?.close(root.id);
|
|
743
|
+
else window.asta?.open(root.id);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
const input = elOf(e.target)?.closest("[data-asta-command-input]");
|
|
748
|
+
const root = input?.closest("dialog[data-asta-command]");
|
|
749
|
+
if (!root) return;
|
|
750
|
+
const at = root._astaAt ?? 0;
|
|
751
|
+
const n = cmdOptions(root).length;
|
|
752
|
+
if (e.key === "ArrowDown") cmdActive(root, at + 1);
|
|
753
|
+
else if (e.key === "ArrowUp") cmdActive(root, at - 1);
|
|
754
|
+
else if (e.key === "Home" && !input.value) cmdActive(root, 0);
|
|
755
|
+
else if (e.key === "End" && !input.value) cmdActive(root, n - 1);
|
|
756
|
+
else if (e.key === "Enter") cmdRun(root, cmdOptions(root)[at]);
|
|
757
|
+
else return;
|
|
758
|
+
e.preventDefault();
|
|
759
|
+
});
|
|
760
|
+
document.addEventListener("input", (e) => {
|
|
761
|
+
const input = elOf(e.target)?.closest("[data-asta-command-input]");
|
|
762
|
+
const root = input?.closest("dialog[data-asta-command]");
|
|
763
|
+
if (root && !root.hasAttribute("data-server")) cmdFilter(root);
|
|
764
|
+
});
|
|
765
|
+
document.addEventListener("pointermove", (e) => {
|
|
766
|
+
const opt = elOf(e.target)?.closest('[data-asta-command-list] [role="option"]');
|
|
767
|
+
const root = opt?.closest("dialog[data-asta-command]");
|
|
768
|
+
if (!root || opt.getAttribute("aria-selected") === "true") return;
|
|
769
|
+
cmdActive(root, cmdOptions(root).indexOf(opt));
|
|
770
|
+
});
|
|
771
|
+
document.addEventListener("click", (e) => {
|
|
772
|
+
const opt = elOf(e.target)?.closest('[data-asta-command-list] [role="option"]');
|
|
773
|
+
const root = opt?.closest("dialog[data-asta-command]");
|
|
774
|
+
if (root && !opt.hasAttribute("data-keep-open")) window.asta?.close(root.id);
|
|
775
|
+
});
|
|
776
|
+
// Start fresh each time it closes.
|
|
777
|
+
document.addEventListener(
|
|
778
|
+
"close",
|
|
779
|
+
(e) => {
|
|
780
|
+
const root = e.target;
|
|
781
|
+
if (!(root instanceof HTMLDialogElement) || !root.hasAttribute("data-asta-command")) return;
|
|
782
|
+
const input = root.querySelector("[data-asta-command-input]");
|
|
783
|
+
if (input && input.value) {
|
|
784
|
+
input.value = "";
|
|
785
|
+
if (root.hasAttribute("data-server")) input.dispatchEvent(new Event("search", { bubbles: true }));
|
|
786
|
+
}
|
|
787
|
+
if (!root.hasAttribute("data-server")) cmdFilter(root);
|
|
788
|
+
},
|
|
789
|
+
true,
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
/* ---------- Navbar ---------- */
|
|
793
|
+
|
|
794
|
+
const navTriggers = (root) => [...root.querySelectorAll("[data-asta-nav-trigger]")];
|
|
795
|
+
function navShow(root, i, focusPanel) {
|
|
796
|
+
clearTimeout(root._astaT);
|
|
797
|
+
const cur = root._astaOpen ?? null;
|
|
798
|
+
if (i === null) {
|
|
799
|
+
if (cur === null) return;
|
|
800
|
+
root._astaOpen = null;
|
|
801
|
+
root.removeAttribute("data-open");
|
|
802
|
+
for (const t of navTriggers(root)) t.setAttribute("aria-expanded", "false");
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const box = root.querySelector("[data-asta-nav-surface] > div");
|
|
806
|
+
const bg = root.querySelector("[data-asta-nav-bg]");
|
|
807
|
+
const panels = [...root.querySelectorAll("[data-asta-nav-panel]")];
|
|
808
|
+
const next = root.querySelector(`[data-asta-nav-panel="${i}"]`);
|
|
809
|
+
if (!next || !box) return;
|
|
810
|
+
const was = cur !== null && root.hasAttribute("data-open");
|
|
811
|
+
const old = was ? { w: bg.offsetWidth, h: bg.offsetHeight } : null;
|
|
812
|
+
for (const p of panels) p.hidden = p !== next;
|
|
813
|
+
root._astaOpen = i;
|
|
814
|
+
root.setAttribute("data-open", "");
|
|
815
|
+
for (const t of navTriggers(root)) t.setAttribute("aria-expanded", String(t.dataset.astaNavTrigger === String(i)));
|
|
816
|
+
if (was && cur !== i && !reduce()) {
|
|
817
|
+
// The surface takes the new size at once; its background scales from the old size (FLIP),
|
|
818
|
+
// and the new content slides in from the side you moved toward.
|
|
819
|
+
const w = bg.offsetWidth;
|
|
820
|
+
const h = bg.offsetHeight;
|
|
821
|
+
bg.style.transition = "none";
|
|
822
|
+
bg.style.transform = `scale(${old.w / w}, ${old.h / h})`;
|
|
823
|
+
void bg.offsetWidth;
|
|
824
|
+
bg.style.transition = "";
|
|
825
|
+
bg.style.transform = "";
|
|
826
|
+
next.style.setProperty("--asta-dir", i > cur ? "1" : "-1");
|
|
827
|
+
next.classList.remove("asta-nav-slide");
|
|
828
|
+
void next.offsetWidth;
|
|
829
|
+
next.classList.add("asta-nav-slide");
|
|
830
|
+
} else next.classList.remove("asta-nav-slide");
|
|
831
|
+
if (focusPanel) next.querySelector("[data-asta-nav-link]")?.focus();
|
|
832
|
+
}
|
|
833
|
+
const navZone = (root, el) => !!el && [...root.querySelectorAll("[data-asta-nav-zone]")].some((z) => z.contains(el));
|
|
834
|
+
document.addEventListener("pointerover", (e) => {
|
|
835
|
+
if (e.pointerType !== "mouse") return;
|
|
836
|
+
const t = elOf(e.target);
|
|
837
|
+
const root = t?.closest("[data-asta-navbar]");
|
|
838
|
+
if (!root) return;
|
|
839
|
+
const trig = t.closest("[data-asta-nav-trigger]");
|
|
840
|
+
if (trig) {
|
|
841
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
842
|
+
clearTimeout(root._astaT);
|
|
843
|
+
// Hover intent: a short wait before the first open, instant while moving between triggers.
|
|
844
|
+
const hover = () => {
|
|
845
|
+
root._astaHoverAt = performance.now();
|
|
846
|
+
navShow(root, i);
|
|
847
|
+
};
|
|
848
|
+
if ((root._astaOpen ?? null) === null) root._astaT = setTimeout(hover, 110);
|
|
849
|
+
else if (root._astaOpen !== i) hover();
|
|
850
|
+
} else if (t.closest("[data-asta-nav-link]") && t.closest("nav") && (root._astaOpen ?? null) !== null) navShow(root, null);
|
|
851
|
+
else if (navZone(root, t)) clearTimeout(root._astaT);
|
|
852
|
+
});
|
|
853
|
+
document.addEventListener("pointerout", (e) => {
|
|
854
|
+
if (e.pointerType !== "mouse") return;
|
|
855
|
+
const t = elOf(e.target);
|
|
856
|
+
const root = t?.closest("[data-asta-navbar]");
|
|
857
|
+
if (!root || !navZone(root, t) || navZone(root, elOf(e.relatedTarget))) return;
|
|
858
|
+
clearTimeout(root._astaT);
|
|
859
|
+
root._astaT = setTimeout(() => navShow(root, null), 180);
|
|
860
|
+
});
|
|
861
|
+
document.addEventListener("click", (e) => {
|
|
862
|
+
const t = elOf(e.target);
|
|
863
|
+
const trig = t?.closest("[data-asta-nav-trigger]");
|
|
864
|
+
if (trig) {
|
|
865
|
+
const root = trig.closest("[data-asta-navbar]");
|
|
866
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
867
|
+
// A click right after hover opened it keeps it open instead of toggling it shut.
|
|
868
|
+
if (root._astaOpen === i && performance.now() - (root._astaHoverAt || 0) < 600) return;
|
|
869
|
+
navShow(root, root._astaOpen === i ? null : i);
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const burger = t?.closest("[data-asta-nav-burger]");
|
|
873
|
+
if (burger) {
|
|
874
|
+
const root = burger.closest("[data-asta-navbar]");
|
|
875
|
+
navSheet(root, !root.hasAttribute("data-sheet"));
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
function navSheet(root, on) {
|
|
879
|
+
const sheet = root.querySelector("[data-asta-nav-sheet]");
|
|
880
|
+
const burger = root.querySelector("[data-asta-nav-burger]");
|
|
881
|
+
if (!sheet) return;
|
|
882
|
+
sheet.hidden = !on;
|
|
883
|
+
root.toggleAttribute("data-sheet", on);
|
|
884
|
+
burger?.setAttribute("aria-expanded", String(on));
|
|
885
|
+
burger?.setAttribute("aria-label", on ? "Close menu" : "Open menu");
|
|
886
|
+
}
|
|
887
|
+
document.addEventListener("pointerdown", (e) => {
|
|
888
|
+
const t = elOf(e.target);
|
|
889
|
+
for (const root of document.querySelectorAll("[data-asta-navbar]")) {
|
|
890
|
+
if (root.contains(t)) continue;
|
|
891
|
+
navShow(root, null);
|
|
892
|
+
if (root.hasAttribute("data-sheet")) navSheet(root, false);
|
|
893
|
+
}
|
|
894
|
+
});
|
|
895
|
+
document.addEventListener("keydown", (e) => {
|
|
896
|
+
const t = elOf(e.target);
|
|
897
|
+
if (e.key === "Escape") {
|
|
898
|
+
for (const root of document.querySelectorAll("[data-asta-navbar][data-open],[data-asta-navbar][data-sheet]")) {
|
|
899
|
+
const i = root._astaOpen ?? null;
|
|
900
|
+
if (i !== null) {
|
|
901
|
+
navShow(root, null);
|
|
902
|
+
if (root.contains(t)) root.querySelector(`[data-asta-nav-trigger="${i}"]`)?.focus();
|
|
903
|
+
}
|
|
904
|
+
if (root.hasAttribute("data-sheet")) {
|
|
905
|
+
navSheet(root, false);
|
|
906
|
+
root.querySelector("[data-asta-nav-burger]")?.focus();
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
const trig = t?.closest("[data-asta-nav-trigger]");
|
|
912
|
+
if (!trig) return;
|
|
913
|
+
const root = trig.closest("[data-asta-navbar]");
|
|
914
|
+
const i = Number(trig.dataset.astaNavTrigger);
|
|
915
|
+
if (e.key === "ArrowDown" || ((e.key === "Enter" || e.key === " ") && root._astaOpen !== i)) {
|
|
916
|
+
e.preventDefault();
|
|
917
|
+
navShow(root, i, true);
|
|
918
|
+
} else if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
|
|
919
|
+
e.preventDefault();
|
|
920
|
+
const all = [...root.querySelectorAll('nav[aria-label="Main"] li > *')];
|
|
921
|
+
const at = all.indexOf(trig);
|
|
922
|
+
const next = all[(at + (e.key === "ArrowRight" ? 1 : -1) + all.length) % all.length];
|
|
923
|
+
next?.focus();
|
|
924
|
+
if ((root._astaOpen ?? null) !== null && next?.matches("[data-asta-nav-trigger]")) navShow(root, Number(next.dataset.astaNavTrigger));
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
const navScrolled = () => {
|
|
928
|
+
const on = scrollY > 40;
|
|
929
|
+
for (const root of document.querySelectorAll("[data-asta-navbar]")) root.toggleAttribute("data-scrolled", on);
|
|
930
|
+
};
|
|
931
|
+
addEventListener("scroll", navScrolled, { passive: true });
|
|
932
|
+
|
|
933
|
+
/* ---------- Sidebar ---------- */
|
|
934
|
+
|
|
935
|
+
function sbPlace(root, animate) {
|
|
936
|
+
const ind = root.querySelector("[data-asta-sidebar-indicator]");
|
|
937
|
+
if (!ind) return;
|
|
938
|
+
const el = root.querySelector('[data-asta-sidebar-item][aria-current="page"]');
|
|
939
|
+
if (!el) {
|
|
940
|
+
ind.style.opacity = "0";
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
const box = ind.parentElement.getBoundingClientRect();
|
|
944
|
+
const y = el.getBoundingClientRect().top - box.top;
|
|
945
|
+
if (!animate) ind.style.transition = "none";
|
|
946
|
+
ind.style.transform = `translateY(${y}px)`;
|
|
947
|
+
ind.style.opacity = "1";
|
|
948
|
+
if (!animate) {
|
|
949
|
+
void ind.offsetHeight;
|
|
950
|
+
ind.style.transition = "";
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
function sbCollapse(root, on) {
|
|
954
|
+
root.toggleAttribute("data-collapsed", on);
|
|
955
|
+
const btn = root.querySelector("[data-asta-sidebar-toggle]");
|
|
956
|
+
btn?.setAttribute("aria-pressed", String(on));
|
|
957
|
+
btn?.setAttribute("aria-label", on ? "Expand sidebar" : "Collapse sidebar");
|
|
958
|
+
fire(root, "asta:collapse", { collapsed: on });
|
|
959
|
+
}
|
|
960
|
+
document.addEventListener("click", (e) => {
|
|
961
|
+
const t = elOf(e.target);
|
|
962
|
+
const toggle = t?.closest("[data-asta-sidebar-toggle]");
|
|
963
|
+
if (toggle) {
|
|
964
|
+
const root = toggle.closest("[data-asta-sidebar]");
|
|
965
|
+
return sbCollapse(root, !root.hasAttribute("data-collapsed"));
|
|
966
|
+
}
|
|
967
|
+
const item = t?.closest("[data-asta-sidebar-item]");
|
|
968
|
+
const root = item?.closest("[data-asta-sidebar]");
|
|
969
|
+
if (!root) return;
|
|
970
|
+
for (const it of root.querySelectorAll("[data-asta-sidebar-item]")) {
|
|
971
|
+
if (it === item) it.setAttribute("aria-current", "page");
|
|
972
|
+
else it.removeAttribute("aria-current");
|
|
973
|
+
}
|
|
974
|
+
sbPlace(root, true);
|
|
975
|
+
});
|
|
976
|
+
document.addEventListener("keydown", (e) => {
|
|
977
|
+
const t = elOf(e.target);
|
|
978
|
+
const item = t?.closest("[data-asta-sidebar-nav] [data-asta-sidebar-item]");
|
|
979
|
+
if (!item) return;
|
|
980
|
+
const els = [...item.closest("[data-asta-sidebar-nav]").querySelectorAll("[data-asta-sidebar-item]")];
|
|
981
|
+
const i = els.indexOf(item);
|
|
982
|
+
const next = { ArrowDown: i + 1, ArrowUp: i - 1, Home: 0, End: els.length - 1 }[e.key];
|
|
983
|
+
if (next === undefined) return;
|
|
984
|
+
e.preventDefault();
|
|
985
|
+
els[(next + els.length) % els.length]?.focus();
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
/* ---------- Tool dock ---------- */
|
|
989
|
+
|
|
990
|
+
function dockPlace(root, animate) {
|
|
991
|
+
const pill = root.querySelector("[data-asta-tooldock-pill]");
|
|
992
|
+
const on = root.querySelector('[data-tool][aria-pressed="true"]');
|
|
993
|
+
if (!pill) return;
|
|
994
|
+
if (!on) {
|
|
995
|
+
pill.style.opacity = "0";
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
// The button sits in a positioned wrapper (for its tooltip), so measure the wrapper against the bar.
|
|
999
|
+
const wrap = on.parentElement;
|
|
1000
|
+
if (!animate) pill.style.transition = "none";
|
|
1001
|
+
pill.style.width = `${wrap.offsetWidth}px`;
|
|
1002
|
+
pill.style.transform = `translateX(${wrap.offsetLeft}px)`;
|
|
1003
|
+
pill.style.opacity = "1";
|
|
1004
|
+
if (!animate) {
|
|
1005
|
+
void pill.offsetWidth;
|
|
1006
|
+
pill.style.transition = "";
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function dockPick(root, btn) {
|
|
1010
|
+
if (!btn || btn.disabled) return;
|
|
1011
|
+
for (const b of root.querySelectorAll("[data-tool]")) {
|
|
1012
|
+
const on = b === btn;
|
|
1013
|
+
b.setAttribute("aria-pressed", String(on));
|
|
1014
|
+
b.tabIndex = on ? 0 : -1;
|
|
1015
|
+
}
|
|
1016
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
1017
|
+
if (input) input.value = btn.dataset.tool;
|
|
1018
|
+
dockPlace(root, true);
|
|
1019
|
+
fire(root, "asta:change", { value: btn.dataset.tool });
|
|
1020
|
+
}
|
|
1021
|
+
document.addEventListener("click", (e) => {
|
|
1022
|
+
const btn = elOf(e.target)?.closest("[data-asta-tooldock] [data-tool]");
|
|
1023
|
+
if (btn) dockPick(btn.closest("[data-asta-tooldock]"), btn);
|
|
1024
|
+
});
|
|
1025
|
+
document.addEventListener("keydown", (e) => {
|
|
1026
|
+
const t = elOf(e.target);
|
|
1027
|
+
const btn = t?.closest("[data-asta-tooldock] [data-tool]");
|
|
1028
|
+
if (btn) {
|
|
1029
|
+
const root = btn.closest("[data-asta-tooldock]");
|
|
1030
|
+
const list = [...root.querySelectorAll("[data-tool]:not([disabled])")];
|
|
1031
|
+
const i = list.indexOf(btn);
|
|
1032
|
+
const next = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: list.length - 1 }[e.key];
|
|
1033
|
+
if (next !== undefined) {
|
|
1034
|
+
e.preventDefault();
|
|
1035
|
+
list[(next + list.length) % list.length]?.focus();
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
// Single-key shortcuts from anywhere, unless typing or a modifier is held.
|
|
1040
|
+
if (e.metaKey || e.ctrlKey || e.altKey || typing(e.target) || e.key.length !== 1 || e.defaultPrevented) return;
|
|
1041
|
+
if (t?.closest("[data-asta-mpanel],dialog[open]")) return;
|
|
1042
|
+
const k = e.key.toLowerCase();
|
|
1043
|
+
for (const root of document.querySelectorAll("[data-asta-tooldock][data-hotkeys]")) {
|
|
1044
|
+
const hit = [...root.querySelectorAll("[data-tool][data-shortcut]:not([disabled])")].find((b) => b.dataset.shortcut.toLowerCase() === k);
|
|
1045
|
+
if (hit) dockPick(root, hit);
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
/* ---------- Stepper ---------- */
|
|
1050
|
+
|
|
1051
|
+
function stepGo(root, n) {
|
|
1052
|
+
const count = num(root.dataset.count, 0);
|
|
1053
|
+
const cur = num(root.dataset.step, 0);
|
|
1054
|
+
const next = Math.max(0, Math.min(count, n));
|
|
1055
|
+
if (next === cur && root.hasAttribute("data-ready")) return;
|
|
1056
|
+
root.dataset.step = String(next);
|
|
1057
|
+
root.toggleAttribute("data-first", next === 0);
|
|
1058
|
+
root.toggleAttribute("data-last", next === count - 1);
|
|
1059
|
+
root.toggleAttribute("data-complete", next >= count);
|
|
1060
|
+
const noBack = root.hasAttribute("data-no-back");
|
|
1061
|
+
for (const li of root.querySelectorAll("[data-asta-step]")) {
|
|
1062
|
+
const i = Number(li.dataset.astaStep);
|
|
1063
|
+
const state = i < next ? "done" : i === next ? "current" : "todo";
|
|
1064
|
+
li.dataset.state = state;
|
|
1065
|
+
if (state === "current") li.setAttribute("aria-current", "step");
|
|
1066
|
+
else li.removeAttribute("aria-current");
|
|
1067
|
+
const b = li.querySelector("[data-asta-step-goto]");
|
|
1068
|
+
if (b) b.disabled = noBack || state !== "done" || next >= count;
|
|
1069
|
+
}
|
|
1070
|
+
for (const b of root.querySelectorAll("[data-asta-step-back]")) b.disabled = next === 0;
|
|
1071
|
+
if (next === cur) return;
|
|
1072
|
+
for (const p of root.querySelectorAll("[data-asta-step-panel]")) {
|
|
1073
|
+
const on = Number(p.dataset.astaStepPanel) === next;
|
|
1074
|
+
p.hidden = !on;
|
|
1075
|
+
p.classList.remove("asta-step-next", "asta-step-back");
|
|
1076
|
+
if (on && !reduce()) {
|
|
1077
|
+
void p.offsetWidth;
|
|
1078
|
+
p.classList.add(next > cur ? "asta-step-next" : "asta-step-back");
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
fire(root, "asta:step", { step: next });
|
|
1082
|
+
}
|
|
1083
|
+
document.addEventListener("click", (e) => {
|
|
1084
|
+
const t = elOf(e.target);
|
|
1085
|
+
const b = t?.closest("[data-asta-step-next],[data-asta-step-back],[data-asta-step-goto]");
|
|
1086
|
+
const root = b?.closest("[data-asta-stepper]");
|
|
1087
|
+
if (!root || b.disabled) return;
|
|
1088
|
+
const cur = num(root.dataset.step, 0);
|
|
1089
|
+
if (b.hasAttribute("data-asta-step-next")) stepGo(root, cur + 1);
|
|
1090
|
+
else if (b.hasAttribute("data-asta-step-back")) stepGo(root, cur - 1);
|
|
1091
|
+
else stepGo(root, num(b.getAttribute("data-asta-step-goto"), cur));
|
|
1092
|
+
});
|
|
1093
|
+
|
|
1094
|
+
/* ---------- Announcement bar ---------- */
|
|
1095
|
+
|
|
1096
|
+
const storage = {
|
|
1097
|
+
get: (k) => {
|
|
1098
|
+
try {
|
|
1099
|
+
return localStorage.getItem(k);
|
|
1100
|
+
} catch {
|
|
1101
|
+
return null;
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
set: (k, v) => {
|
|
1105
|
+
try {
|
|
1106
|
+
localStorage.setItem(k, v);
|
|
1107
|
+
} catch {}
|
|
1108
|
+
},
|
|
1109
|
+
};
|
|
1110
|
+
function announceTick(root) {
|
|
1111
|
+
if (!root.isConnected) return clearInterval(root._astaTimer);
|
|
1112
|
+
if (root._astaPaused || document.hidden || reduce() || root.dataset.state === "closed") return;
|
|
1113
|
+
const msgs = [...root.querySelectorAll("[data-asta-announce-msg]")];
|
|
1114
|
+
const cur = msgs.findIndex((m) => m.dataset.state === "cur" || m.dataset.state === "in");
|
|
1115
|
+
const next = (cur + 1) % msgs.length;
|
|
1116
|
+
msgs.forEach((m, i) => {
|
|
1117
|
+
if (i === next) {
|
|
1118
|
+
m.dataset.state = "in";
|
|
1119
|
+
m.removeAttribute("aria-hidden");
|
|
1120
|
+
} else if (i === cur) {
|
|
1121
|
+
m.dataset.state = "out";
|
|
1122
|
+
m.setAttribute("aria-hidden", "true");
|
|
1123
|
+
} else {
|
|
1124
|
+
delete m.dataset.state;
|
|
1125
|
+
m.setAttribute("aria-hidden", "true");
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
function announceInit(root) {
|
|
1130
|
+
if (root._astaInit) return;
|
|
1131
|
+
root._astaInit = true;
|
|
1132
|
+
const key = root.dataset.storageKey;
|
|
1133
|
+
if (key && storage.get(key) === "1") return root.remove();
|
|
1134
|
+
if (root.querySelectorAll("[data-asta-announce-msg]").length > 1) {
|
|
1135
|
+
root._astaTimer = setInterval(() => announceTick(root), num(root.dataset.interval, 5000));
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
const announcePause = (root, on) => {
|
|
1139
|
+
root._astaPaused = on;
|
|
1140
|
+
// Live only while still, so a rotating bar doesn't talk over the page.
|
|
1141
|
+
root.querySelector("[data-asta-announce-track]")?.setAttribute("aria-live", on || reduce() ? "polite" : "off");
|
|
1142
|
+
};
|
|
1143
|
+
document.addEventListener("pointerover", (e) => {
|
|
1144
|
+
const root = e.pointerType === "mouse" && elOf(e.target)?.closest("[data-asta-announce]");
|
|
1145
|
+
if (root) announcePause(root, true);
|
|
1146
|
+
});
|
|
1147
|
+
document.addEventListener("pointerout", (e) => {
|
|
1148
|
+
const root = e.pointerType === "mouse" && elOf(e.target)?.closest("[data-asta-announce]");
|
|
1149
|
+
if (root && !root.contains(elOf(e.relatedTarget)) && !root.contains(document.activeElement)) announcePause(root, false);
|
|
1150
|
+
});
|
|
1151
|
+
document.addEventListener("focusin", (e) => {
|
|
1152
|
+
const root = elOf(e.target)?.closest("[data-asta-announce]");
|
|
1153
|
+
if (root) announcePause(root, true);
|
|
1154
|
+
});
|
|
1155
|
+
document.addEventListener("focusout", (e) => {
|
|
1156
|
+
const root = elOf(e.target)?.closest("[data-asta-announce]");
|
|
1157
|
+
if (root && !root.contains(elOf(e.relatedTarget))) announcePause(root, false);
|
|
1158
|
+
});
|
|
1159
|
+
document.addEventListener("click", (e) => {
|
|
1160
|
+
const btn = elOf(e.target)?.closest("[data-asta-announce-close]");
|
|
1161
|
+
const root = btn?.closest("[data-asta-announce]");
|
|
1162
|
+
if (!root || root.dataset.state === "closed") return;
|
|
1163
|
+
// Focus would be lost with the bar: hand it to the page.
|
|
1164
|
+
if (root.contains(document.activeElement)) document.activeElement.blur();
|
|
1165
|
+
clearInterval(root._astaTimer);
|
|
1166
|
+
root.dataset.state = "closed";
|
|
1167
|
+
const done = () => {
|
|
1168
|
+
// Remember it once the exit has played.
|
|
1169
|
+
if (root.dataset.storageKey) storage.set(root.dataset.storageKey, "1");
|
|
1170
|
+
root.remove();
|
|
1171
|
+
fire(document.body, "asta:dismiss", { id: root.id });
|
|
1172
|
+
};
|
|
1173
|
+
if (reduce()) return done();
|
|
1174
|
+
let finished = false;
|
|
1175
|
+
const finish = () => {
|
|
1176
|
+
if (finished) return;
|
|
1177
|
+
finished = true;
|
|
1178
|
+
done();
|
|
1179
|
+
};
|
|
1180
|
+
root.addEventListener("animationend", (ev) => ev.target === root && finish());
|
|
1181
|
+
setTimeout(finish, 450);
|
|
1182
|
+
});
|
|
1183
|
+
|
|
1184
|
+
/* ---------- Newsletter ---------- */
|
|
1185
|
+
|
|
1186
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
1187
|
+
const nlState = (root, s) => {
|
|
1188
|
+
root.dataset.state = s;
|
|
1189
|
+
root.querySelector("form")?.toggleAttribute("inert", s === "done");
|
|
1190
|
+
root.querySelector("[data-asta-newsletter-done]")?.setAttribute("aria-hidden", String(s !== "done"));
|
|
1191
|
+
};
|
|
1192
|
+
document.addEventListener(
|
|
1193
|
+
"submit",
|
|
1194
|
+
(e) => {
|
|
1195
|
+
const form = elOf(e.target);
|
|
1196
|
+
const root = form?.closest("[data-asta-newsletter]");
|
|
1197
|
+
if (!root) return;
|
|
1198
|
+
const input = form.querySelector('input[type="email"]');
|
|
1199
|
+
if (!EMAIL.test(input?.value.trim() ?? "")) {
|
|
1200
|
+
// Stop htmx from posting a bad address.
|
|
1201
|
+
e.preventDefault();
|
|
1202
|
+
e.stopImmediatePropagation();
|
|
1203
|
+
nlState(root, "error");
|
|
1204
|
+
input?.setAttribute("aria-invalid", "true");
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
input?.removeAttribute("aria-invalid");
|
|
1208
|
+
if (form.hasAttribute("hx-post")) return nlState(root, "busy");
|
|
1209
|
+
// No endpoint: a demo, so fake a short request.
|
|
1210
|
+
e.preventDefault();
|
|
1211
|
+
nlState(root, "busy");
|
|
1212
|
+
const btn = form.querySelector('button[type="submit"]');
|
|
1213
|
+
btn?.classList.add("htmx-request");
|
|
1214
|
+
setTimeout(() => {
|
|
1215
|
+
btn?.classList.remove("htmx-request");
|
|
1216
|
+
nlState(root, "done");
|
|
1217
|
+
}, 900);
|
|
1218
|
+
},
|
|
1219
|
+
true,
|
|
1220
|
+
);
|
|
1221
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
1222
|
+
const root = elOf(e.target)?.closest("[data-asta-newsletter]");
|
|
1223
|
+
if (root) nlState(root, e.detail?.successful ? "done" : "error");
|
|
1224
|
+
});
|
|
1225
|
+
document.addEventListener("input", (e) => {
|
|
1226
|
+
const root = elOf(e.target)?.closest("[data-asta-newsletter]");
|
|
1227
|
+
if (root?.dataset.state === "error") {
|
|
1228
|
+
nlState(root, "idle");
|
|
1229
|
+
e.target.removeAttribute("aria-invalid");
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
|
|
1233
|
+
/* ---------- Wordmark: letters near the pointer lift ---------- */
|
|
1234
|
+
|
|
1235
|
+
document.addEventListener("pointermove", (e) => {
|
|
1236
|
+
const box = elOf(e.target)?.closest("[data-asta-wordmark]");
|
|
1237
|
+
if (box) wordmark(box, e.clientX);
|
|
1238
|
+
});
|
|
1239
|
+
document.addEventListener("pointerout", (e) => {
|
|
1240
|
+
const box = elOf(e.target)?.closest("[data-asta-wordmark]");
|
|
1241
|
+
if (box && !box.contains(elOf(e.relatedTarget))) wordmark(box, null);
|
|
1242
|
+
});
|
|
1243
|
+
function wordmark(box, x) {
|
|
1244
|
+
if (reduce()) return;
|
|
1245
|
+
for (const s of box.querySelectorAll("span[data-l]")) {
|
|
1246
|
+
if (x === null) {
|
|
1247
|
+
s.style.transform = "";
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
// Measure without the current lift, so letters don't chase their own offset.
|
|
1251
|
+
const r = s.offsetLeft + s.offsetWidth / 2 + box.getBoundingClientRect().left;
|
|
1252
|
+
const k = Math.max(0, 1 - Math.abs(x - r) / (s.offsetWidth * 2.5));
|
|
1253
|
+
s.style.transform = `translateY(${-k * 14}%)`;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/* ---------- Boot and htmx ---------- */
|
|
1258
|
+
|
|
1259
|
+
const narrow = () => matchMedia("(max-width: 639px)").matches;
|
|
1260
|
+
function scan(el) {
|
|
1261
|
+
for (const root of el.querySelectorAll("[data-asta-sidebar]")) {
|
|
1262
|
+
if (!root._astaInit) {
|
|
1263
|
+
root._astaInit = true;
|
|
1264
|
+
if (root.hasAttribute("data-auto-rail") && narrow() && !root.hasAttribute("data-collapsed")) sbCollapse(root, true);
|
|
1265
|
+
}
|
|
1266
|
+
sbPlace(root, false);
|
|
1267
|
+
}
|
|
1268
|
+
for (const root of el.querySelectorAll("[data-asta-tooldock]")) dockPlace(root, false);
|
|
1269
|
+
for (const root of el.querySelectorAll("[data-asta-announce]")) announceInit(root);
|
|
1270
|
+
for (const root of el.querySelectorAll("[data-asta-stepper]:not([data-ready])")) {
|
|
1271
|
+
stepGo(root, num(root.dataset.step, 0));
|
|
1272
|
+
root.setAttribute("data-ready", "");
|
|
1273
|
+
}
|
|
1274
|
+
for (const root of el.querySelectorAll("dialog[data-asta-command]")) {
|
|
1275
|
+
if (root.hasAttribute("data-server")) cmdRefresh(root);
|
|
1276
|
+
else cmdFilter(root);
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
const boot = () => {
|
|
1280
|
+
scan(document);
|
|
1281
|
+
navScrolled();
|
|
1282
|
+
};
|
|
1283
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
|
|
1284
|
+
else boot();
|
|
1285
|
+
document.addEventListener("htmx:afterSettle", (e) => {
|
|
1286
|
+
const t = elOf(e.detail?.elt) || document.body;
|
|
1287
|
+
// A server search answered: refresh its palette.
|
|
1288
|
+
const cmd = t.closest?.("dialog[data-asta-command]");
|
|
1289
|
+
if (cmd) return cmdRefresh(cmd);
|
|
1290
|
+
scan(t.parentElement || document);
|
|
1291
|
+
});
|
|
1292
|
+
// Re-measure the sliding highlights when the layout changes (fonts, resize, a sidebar folding).
|
|
1293
|
+
const resized = new ResizeObserver((entries) => {
|
|
1294
|
+
for (const en of entries) {
|
|
1295
|
+
const t = en.target;
|
|
1296
|
+
if (t.matches("[data-asta-sidebar]")) sbPlace(t, false);
|
|
1297
|
+
else if (t.matches("[data-asta-tooldock]")) dockPlace(t, false);
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
const watch = () => {
|
|
1301
|
+
for (const el of document.querySelectorAll("[data-asta-sidebar],[data-asta-tooldock]")) {
|
|
1302
|
+
if (el._astaWatched) continue;
|
|
1303
|
+
el._astaWatched = true;
|
|
1304
|
+
resized.observe(el);
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", watch);
|
|
1308
|
+
else watch();
|
|
1309
|
+
document.addEventListener("htmx:afterSettle", watch);
|
|
1310
|
+
})();
|