asta-ui 0.8.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 +1 -1
- package/css/asta.js +9 -9
- package/css/components.css +88 -88
- 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 +9 -9
- package/css/src/core.css +15 -15
- package/css/src/data.css +12 -12
- package/css/src/display.css +16 -16
- package/css/src/forms.css +16 -16
- package/css/src/overlays.css +15 -15
- package/package.json +1 -1
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
/* Data components for Astabyte UI. Event delegation on document, so htmx swaps need no re-init.
|
|
2
|
+
* DataTable [data-asta-dt] with [data-asta-dt-all], [data-asta-dt-row], [data-asta-dt-clear]
|
|
3
|
+
* TreeView [data-asta-tree] with [role=treeitem] and [data-asta-tree-row]; fires asta:select, asta:expand
|
|
4
|
+
* Timeline [data-asta-timeline][data-inview]
|
|
5
|
+
* Kanban [data-asta-kanban] with [data-asta-kanban-list] and [data-asta-kanban-card]; fires asta:move
|
|
6
|
+
* EventCalendar [data-asta-cal] with [data-day] cells and [data-asta-cal-panel]; fires asta:select
|
|
7
|
+
* FileCard [data-asta-file-star]; fires asta:star
|
|
8
|
+
* ImageCompare [data-asta-compare] with [data-asta-compare-handle]; fires asta:change
|
|
9
|
+
* Carousel [data-asta-carousel] with [data-asta-carousel-track|dot|step]; fires asta:slide
|
|
10
|
+
* NotificationCenter [data-asta-nc] with [data-asta-nc-trigger|panel|tab|item|dot|all]
|
|
11
|
+
* Comments [data-asta-comments] with [data-asta-comment-form|reply|like|cancel]; fires asta:like
|
|
12
|
+
* MotionButton [data-asta-mb="fill|shine|beam|copy|confirm"]; confirm fires asta:confirm
|
|
13
|
+
*/
|
|
14
|
+
(() => {
|
|
15
|
+
if (window.__astaData) return;
|
|
16
|
+
window.__astaData = true;
|
|
17
|
+
const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
18
|
+
const CURVE = "cubic-bezier(.16,1,.3,1)";
|
|
19
|
+
const all = (el, s) => [...el.querySelectorAll(s)];
|
|
20
|
+
const el = (e) => (e.target instanceof Element ? e.target : null);
|
|
21
|
+
const fire = (node, name, detail) => node.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
|
|
22
|
+
// Restart a CSS animation that hangs off an attribute.
|
|
23
|
+
const replay = (node, attr) => {
|
|
24
|
+
node.removeAttribute(attr);
|
|
25
|
+
void node.offsetWidth;
|
|
26
|
+
node.setAttribute(attr, "");
|
|
27
|
+
};
|
|
28
|
+
// Cards glide from where they were to where they are now (FLIP, transform only).
|
|
29
|
+
function flip(nodes, mutate) {
|
|
30
|
+
const before = new Map(nodes.map((n) => [n, n.getBoundingClientRect()]));
|
|
31
|
+
mutate();
|
|
32
|
+
if (reduce()) return;
|
|
33
|
+
for (const [n, r] of before) {
|
|
34
|
+
if (!n.isConnected) continue;
|
|
35
|
+
const now = n.getBoundingClientRect();
|
|
36
|
+
const dx = r.left - now.left;
|
|
37
|
+
const dy = r.top - now.top;
|
|
38
|
+
if (dx || dy) n.animate([{ transform: `translate(${dx}px, ${dy}px)` }, { transform: "none" }], { duration: 340, easing: CURVE });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// The scan runs on load and after every htmx settle, for the bits that need a first look.
|
|
42
|
+
const scans = [];
|
|
43
|
+
const scan = (root) => scans.forEach((f) => f(root));
|
|
44
|
+
document.addEventListener("htmx:afterSettle", (e) => {
|
|
45
|
+
const t = e.detail?.elt;
|
|
46
|
+
scan(t && t.isConnected ? t.parentElement || document : document);
|
|
47
|
+
});
|
|
48
|
+
const within = (root, sel) => [...(root.matches?.(sel) ? [root] : []), ...all(root, sel)];
|
|
49
|
+
|
|
50
|
+
/* ---------- DataTable ---------- */
|
|
51
|
+
|
|
52
|
+
function dtSync(root) {
|
|
53
|
+
const rows = all(root, "[data-asta-dt-row]");
|
|
54
|
+
const head = root.querySelector("[data-asta-dt-all]");
|
|
55
|
+
const n = rows.filter((r) => r.checked).length;
|
|
56
|
+
if (head) {
|
|
57
|
+
const mixed = n > 0 && n < rows.length;
|
|
58
|
+
head.checked = n > 0 && n === rows.length;
|
|
59
|
+
head.indeterminate = mixed;
|
|
60
|
+
head.toggleAttribute("data-indeterminate", mixed);
|
|
61
|
+
head.disabled = rows.length === 0;
|
|
62
|
+
head.setAttribute("aria-label", head.checked ? "Deselect all rows on this page" : "Select all rows on this page");
|
|
63
|
+
}
|
|
64
|
+
const picked = root.querySelector("[data-asta-dt-picked]");
|
|
65
|
+
const range = root.querySelector("[data-asta-dt-range]");
|
|
66
|
+
if (picked && range) {
|
|
67
|
+
picked.hidden = n === 0;
|
|
68
|
+
range.hidden = n > 0;
|
|
69
|
+
const c = picked.querySelector("[data-asta-dt-count]");
|
|
70
|
+
if (c) c.textContent = String(n);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
scans.push((root) => within(root, "[data-asta-dt]").forEach(dtSync));
|
|
74
|
+
document.addEventListener("change", (e) => {
|
|
75
|
+
const t = el(e);
|
|
76
|
+
const root = t?.closest("[data-asta-dt]");
|
|
77
|
+
if (!root) return;
|
|
78
|
+
if (t.matches("[data-asta-dt-all]")) for (const r of all(root, "[data-asta-dt-row]")) r.checked = t.checked;
|
|
79
|
+
if (t.matches("[data-asta-dt-all], [data-asta-dt-row]")) dtSync(root);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/* ---------- TreeView ---------- */
|
|
83
|
+
|
|
84
|
+
const treeOf = (li) => li.closest("[data-asta-tree]");
|
|
85
|
+
const rowOf = (li) => li.querySelector(":scope > [data-asta-tree-row]");
|
|
86
|
+
const isFolder = (li) => li.hasAttribute("aria-expanded");
|
|
87
|
+
const isOpen = (li) => li.getAttribute("aria-expanded") === "true";
|
|
88
|
+
const off = (li) => li.getAttribute("aria-disabled") === "true";
|
|
89
|
+
function treeVisible(tree) {
|
|
90
|
+
return all(tree, '[role="treeitem"]').filter((li) => {
|
|
91
|
+
for (let g = li.parentElement; g && g !== tree; g = g.parentElement) if (g.hidden) return false;
|
|
92
|
+
return true;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function treeFocus(li) {
|
|
96
|
+
if (!li) return;
|
|
97
|
+
for (const x of all(treeOf(li), '[role="treeitem"][tabindex="0"]')) x.tabIndex = -1;
|
|
98
|
+
li.tabIndex = 0;
|
|
99
|
+
li.focus();
|
|
100
|
+
}
|
|
101
|
+
function treeOpen(li, open, animate = true) {
|
|
102
|
+
if (!isFolder(li) || off(li) || isOpen(li) === open) return;
|
|
103
|
+
li.setAttribute("aria-expanded", String(open));
|
|
104
|
+
rowOf(li)?.toggleAttribute("data-open", open);
|
|
105
|
+
const g = li.querySelector(':scope > [role="group"]');
|
|
106
|
+
if (g) {
|
|
107
|
+
g.hidden = !open;
|
|
108
|
+
if (open) {
|
|
109
|
+
if (animate && !reduce()) replay(g, "data-opened");
|
|
110
|
+
fire(g, "asta:expand");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Closing a folder that hides the focused row moves focus up to the folder.
|
|
114
|
+
if (!open && li.contains(document.activeElement) && document.activeElement !== li) treeFocus(li);
|
|
115
|
+
}
|
|
116
|
+
function treeSelect(li) {
|
|
117
|
+
if (off(li)) return;
|
|
118
|
+
const tree = treeOf(li);
|
|
119
|
+
for (const x of all(tree, '[role="treeitem"][aria-selected="true"]')) {
|
|
120
|
+
x.setAttribute("aria-selected", "false");
|
|
121
|
+
rowOf(x)?.removeAttribute("data-selected");
|
|
122
|
+
}
|
|
123
|
+
li.setAttribute("aria-selected", "true");
|
|
124
|
+
rowOf(li)?.setAttribute("data-selected", "");
|
|
125
|
+
const input = tree.querySelector(":scope > [data-asta-tree-value]");
|
|
126
|
+
if (input) {
|
|
127
|
+
input.value = li.dataset.id;
|
|
128
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
129
|
+
}
|
|
130
|
+
fire(li, "asta:select", { id: li.dataset.id });
|
|
131
|
+
}
|
|
132
|
+
scans.push((root) => {
|
|
133
|
+
for (const g of within(root, "[data-asta-tree-lazy]")) if (!g.hidden) fire(g, "asta:expand");
|
|
134
|
+
});
|
|
135
|
+
document.addEventListener("click", (e) => {
|
|
136
|
+
const row = el(e)?.closest("[data-asta-tree-row]");
|
|
137
|
+
if (!row) return;
|
|
138
|
+
const li = row.parentElement;
|
|
139
|
+
if (off(li)) return;
|
|
140
|
+
treeFocus(li);
|
|
141
|
+
// The chevron toggles without selecting.
|
|
142
|
+
if (el(e).closest("[data-asta-tree-chevron]") && isFolder(li)) return treeOpen(li, !isOpen(li));
|
|
143
|
+
treeSelect(li);
|
|
144
|
+
if (isFolder(li)) treeOpen(li, !isOpen(li));
|
|
145
|
+
});
|
|
146
|
+
let typed = { text: "", at: 0 };
|
|
147
|
+
document.addEventListener("keydown", (e) => {
|
|
148
|
+
const li = el(e);
|
|
149
|
+
if (!li?.matches('[data-asta-tree] [role="treeitem"]')) return;
|
|
150
|
+
const tree = treeOf(li);
|
|
151
|
+
const rows = treeVisible(tree);
|
|
152
|
+
const i = rows.indexOf(li);
|
|
153
|
+
const parent = li.parentElement.closest('[role="treeitem"]');
|
|
154
|
+
let handled = true;
|
|
155
|
+
switch (e.key) {
|
|
156
|
+
case "ArrowDown":
|
|
157
|
+
treeFocus(rows[i + 1]);
|
|
158
|
+
break;
|
|
159
|
+
case "ArrowUp":
|
|
160
|
+
treeFocus(rows[i - 1]);
|
|
161
|
+
break;
|
|
162
|
+
case "Home":
|
|
163
|
+
treeFocus(rows[0]);
|
|
164
|
+
break;
|
|
165
|
+
case "End":
|
|
166
|
+
treeFocus(rows[rows.length - 1]);
|
|
167
|
+
break;
|
|
168
|
+
case "ArrowRight":
|
|
169
|
+
if (isFolder(li) && !off(li)) {
|
|
170
|
+
if (!isOpen(li)) treeOpen(li, true);
|
|
171
|
+
else treeFocus(li.querySelector(':scope > [role="group"] > [role="treeitem"]'));
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
case "ArrowLeft":
|
|
175
|
+
if (isFolder(li) && isOpen(li)) treeOpen(li, false);
|
|
176
|
+
else if (parent && tree.contains(parent)) treeFocus(parent);
|
|
177
|
+
break;
|
|
178
|
+
case "Enter":
|
|
179
|
+
rowOf(li)?.click();
|
|
180
|
+
break;
|
|
181
|
+
case " ":
|
|
182
|
+
treeSelect(li);
|
|
183
|
+
break;
|
|
184
|
+
case "*": {
|
|
185
|
+
const group = li.parentElement;
|
|
186
|
+
for (const s of all(group, ':scope > [role="treeitem"]')) treeOpen(s, true);
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
default:
|
|
190
|
+
handled = false;
|
|
191
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
192
|
+
const k = e.key.toLowerCase();
|
|
193
|
+
typed = { text: e.timeStamp - typed.at > 600 ? k : typed.text + k, at: e.timeStamp };
|
|
194
|
+
const start = typed.text.length === 1 ? i + 1 : i;
|
|
195
|
+
const order = [...rows.slice(start), ...rows.slice(0, start)];
|
|
196
|
+
const hit = order.find((r) => (rowOf(r)?.textContent || "").trim().toLowerCase().startsWith(typed.text));
|
|
197
|
+
if (hit) {
|
|
198
|
+
treeFocus(hit);
|
|
199
|
+
handled = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (handled) e.preventDefault();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
/* ---------- Timeline ---------- */
|
|
207
|
+
|
|
208
|
+
const seen = new IntersectionObserver(
|
|
209
|
+
(entries) => {
|
|
210
|
+
for (const en of entries) {
|
|
211
|
+
if (!en.isIntersecting) continue;
|
|
212
|
+
en.target.dataset.inview = "true";
|
|
213
|
+
seen.unobserve(en.target);
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
{ threshold: 0.2 },
|
|
217
|
+
);
|
|
218
|
+
scans.push((root) => within(root, '[data-asta-timeline][data-inview="false"]').forEach((t) => seen.observe(t)));
|
|
219
|
+
|
|
220
|
+
/* ---------- Kanban ---------- */
|
|
221
|
+
|
|
222
|
+
const kbCards = (root) => all(root, "[data-asta-kanban-card]");
|
|
223
|
+
const kbListOf = (root, col) => root.querySelector(`[data-asta-kanban-col="${CSS.escape(col)}"] [data-asta-kanban-list]`);
|
|
224
|
+
function kbWhere(card) {
|
|
225
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
226
|
+
const list = all(col, "[data-asta-kanban-card]");
|
|
227
|
+
return `${col.getAttribute("aria-label")}, position ${list.indexOf(card) + 1} of ${list.length}`;
|
|
228
|
+
}
|
|
229
|
+
function kbSay(root, msg) {
|
|
230
|
+
const s = root.querySelector("[data-asta-kanban-status]");
|
|
231
|
+
if (s) s.textContent = msg;
|
|
232
|
+
}
|
|
233
|
+
function kbCounts(root) {
|
|
234
|
+
for (const col of all(root, "[data-asta-kanban-col]")) {
|
|
235
|
+
const c = col.querySelector("[data-asta-kanban-count]");
|
|
236
|
+
const n = String(col.querySelectorAll("[data-asta-kanban-card]").length);
|
|
237
|
+
if (c && c.textContent !== n) {
|
|
238
|
+
c.textContent = n;
|
|
239
|
+
if (!reduce()) replay(c, "data-bump");
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// Save a move: fill the hidden fields and fire asta:move, which the board's hx-post listens for.
|
|
244
|
+
function kbCommit(root, card) {
|
|
245
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
246
|
+
const detail = {
|
|
247
|
+
card: card.dataset.astaKanbanCard,
|
|
248
|
+
column: col.dataset.astaKanbanCol,
|
|
249
|
+
position: all(col, "[data-asta-kanban-card]").indexOf(card),
|
|
250
|
+
};
|
|
251
|
+
for (const [k, v] of Object.entries(detail)) {
|
|
252
|
+
const f = root.querySelector(`[data-asta-kanban-field="${k}"]`);
|
|
253
|
+
if (f) f.value = String(v);
|
|
254
|
+
}
|
|
255
|
+
kbCounts(root);
|
|
256
|
+
kbSay(root, `Dropped ${card.dataset.title} in ${kbWhere(card)}.`);
|
|
257
|
+
fire(root, "asta:move", detail);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let drag = null;
|
|
261
|
+
const gap = document.createElement("li");
|
|
262
|
+
gap.setAttribute("data-asta-kanban-gap", "");
|
|
263
|
+
gap.setAttribute("aria-hidden", "true");
|
|
264
|
+
document.addEventListener("dragstart", (e) => {
|
|
265
|
+
const card = el(e)?.closest("[data-asta-kanban-card]");
|
|
266
|
+
if (!card) return;
|
|
267
|
+
const root = card.closest("[data-asta-kanban]");
|
|
268
|
+
drag = { card, root, h: card.offsetHeight, dropped: false };
|
|
269
|
+
e.dataTransfer.effectAllowed = "move";
|
|
270
|
+
e.dataTransfer.setData("text/plain", card.dataset.astaKanbanCard);
|
|
271
|
+
gap.style.height = `${drag.h}px`;
|
|
272
|
+
// Let the browser take its drag picture first, then fade the original.
|
|
273
|
+
requestAnimationFrame(() => card.setAttribute("data-dragging", ""));
|
|
274
|
+
});
|
|
275
|
+
document.addEventListener("dragover", (e) => {
|
|
276
|
+
if (!drag) return;
|
|
277
|
+
const col = el(e)?.closest("[data-asta-kanban-col]");
|
|
278
|
+
if (!col || !drag.root.contains(col)) return;
|
|
279
|
+
e.preventDefault();
|
|
280
|
+
e.dataTransfer.dropEffect = "move";
|
|
281
|
+
const list = col.querySelector("[data-asta-kanban-list]");
|
|
282
|
+
const cards = all(list, "[data-asta-kanban-card]").filter((c) => c !== drag.card);
|
|
283
|
+
// The first card whose middle is below the pointer.
|
|
284
|
+
const before = cards.find((c) => {
|
|
285
|
+
const r = c.getBoundingClientRect();
|
|
286
|
+
return e.clientY < r.top + r.height / 2;
|
|
287
|
+
});
|
|
288
|
+
if (gap.parentElement === list && gap.nextElementSibling === (before ?? null)) return;
|
|
289
|
+
if (!before && gap.parentElement === list && !gap.nextElementSibling) return;
|
|
290
|
+
const moving = [...new Set([...kbCards(drag.root)].filter((c) => c !== drag.card))];
|
|
291
|
+
flip(moving, () => {
|
|
292
|
+
if (drag.card.parentElement) drag.card.hidden = true;
|
|
293
|
+
list.insertBefore(gap, before ?? null);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
document.addEventListener("drop", (e) => {
|
|
297
|
+
if (!drag || !gap.parentElement) return;
|
|
298
|
+
e.preventDefault();
|
|
299
|
+
const { card, root } = drag;
|
|
300
|
+
gap.replaceWith(card);
|
|
301
|
+
card.hidden = false;
|
|
302
|
+
drag.dropped = true;
|
|
303
|
+
kbCommit(root, card);
|
|
304
|
+
});
|
|
305
|
+
document.addEventListener("dragend", () => {
|
|
306
|
+
if (!drag) return;
|
|
307
|
+
const { card, root, dropped } = drag;
|
|
308
|
+
if (!dropped) {
|
|
309
|
+
const moving = kbCards(root).filter((c) => c !== card);
|
|
310
|
+
flip(moving, () => {
|
|
311
|
+
gap.remove();
|
|
312
|
+
card.hidden = false;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
gap.remove();
|
|
316
|
+
card.hidden = false;
|
|
317
|
+
card.removeAttribute("data-dragging");
|
|
318
|
+
drag = null;
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// Keyboard: Space picks up, arrows move, Space drops, Escape cancels.
|
|
322
|
+
let held = null;
|
|
323
|
+
let moving = false;
|
|
324
|
+
function kbDrop(cancel, focus) {
|
|
325
|
+
if (!held) return;
|
|
326
|
+
const { card, root, from, next } = held;
|
|
327
|
+
held = null;
|
|
328
|
+
card.removeAttribute("data-picked");
|
|
329
|
+
if (cancel) {
|
|
330
|
+
moving = true;
|
|
331
|
+
flip(kbCards(root), () => from.insertBefore(card, next && next.parentElement === from ? next : null));
|
|
332
|
+
moving = false;
|
|
333
|
+
kbCounts(root);
|
|
334
|
+
if (focus) card.focus();
|
|
335
|
+
kbSay(root, `Cancelled. ${card.dataset.title} is back in ${kbWhere(card)}.`);
|
|
336
|
+
} else kbCommit(root, card);
|
|
337
|
+
}
|
|
338
|
+
document.addEventListener("keydown", (e) => {
|
|
339
|
+
const card = el(e);
|
|
340
|
+
if (!card?.matches("[data-asta-kanban-card]")) return;
|
|
341
|
+
const root = card.closest("[data-asta-kanban]");
|
|
342
|
+
if (e.key === " " || e.key === "Enter") {
|
|
343
|
+
e.preventDefault();
|
|
344
|
+
if (held?.card === card) kbDrop(false);
|
|
345
|
+
else if (!held) {
|
|
346
|
+
held = { card, root, from: card.parentElement, next: card.nextElementSibling };
|
|
347
|
+
card.setAttribute("data-picked", "");
|
|
348
|
+
kbSay(root, `Picked up ${card.dataset.title}. Use the arrow keys to move it, Space to drop, Escape to cancel.`);
|
|
349
|
+
}
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (held?.card !== card) return;
|
|
353
|
+
if (e.key === "Escape") {
|
|
354
|
+
e.preventDefault();
|
|
355
|
+
return kbDrop(true, true);
|
|
356
|
+
}
|
|
357
|
+
const dir = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }[e.key];
|
|
358
|
+
if (!dir) return;
|
|
359
|
+
e.preventDefault();
|
|
360
|
+
const cols = all(root, "[data-asta-kanban-col]");
|
|
361
|
+
const col = card.closest("[data-asta-kanban-col]");
|
|
362
|
+
const ci = cols.indexOf(col);
|
|
363
|
+
const target = cols[Math.min(cols.length - 1, Math.max(0, ci + dir[0]))];
|
|
364
|
+
const here = all(col, "[data-asta-kanban-card]").indexOf(card);
|
|
365
|
+
const list = target.querySelector("[data-asta-kanban-list]");
|
|
366
|
+
const others = all(list, "[data-asta-kanban-card]").filter((c) => c !== card);
|
|
367
|
+
const index = dir[0] ? Math.min(here, others.length) : Math.min(others.length, Math.max(0, here + dir[1]));
|
|
368
|
+
if (target === col && index === here) return;
|
|
369
|
+
moving = true;
|
|
370
|
+
flip(kbCards(root), () => list.insertBefore(card, others[index] ?? null));
|
|
371
|
+
card.focus();
|
|
372
|
+
moving = false;
|
|
373
|
+
kbCounts(root);
|
|
374
|
+
kbSay(root, `${card.dataset.title}: ${kbWhere(card)}.`);
|
|
375
|
+
});
|
|
376
|
+
// Focus leaving the held card cancels the pick-up.
|
|
377
|
+
document.addEventListener("focusout", (e) => {
|
|
378
|
+
if (!held || moving || el(e) !== held.card) return;
|
|
379
|
+
const card = held.card;
|
|
380
|
+
setTimeout(() => {
|
|
381
|
+
if (held?.card === card && document.activeElement !== card) kbDrop(true, false);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
/* ---------- EventCalendar ---------- */
|
|
386
|
+
|
|
387
|
+
const parseDay = (k) => new Date(`${k}T00:00:00Z`);
|
|
388
|
+
const dayKey = (d) => d.toISOString().slice(0, 10);
|
|
389
|
+
const addDays = (d, n) => new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() + n));
|
|
390
|
+
const addMonths = (d, n) => {
|
|
391
|
+
const last = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + n + 1, 0)).getUTCDate();
|
|
392
|
+
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + n, Math.min(d.getUTCDate(), last)));
|
|
393
|
+
};
|
|
394
|
+
let calPending = null;
|
|
395
|
+
function calSelect(root, cell) {
|
|
396
|
+
for (const c of all(root, '[role="gridcell"][aria-selected="true"]')) c.setAttribute("aria-selected", "false");
|
|
397
|
+
cell.setAttribute("aria-selected", "true");
|
|
398
|
+
calFocus(root, cell, false);
|
|
399
|
+
const k = cell.dataset.day;
|
|
400
|
+
for (const p of all(root, "[data-asta-cal-panel]")) p.hidden = p.dataset.astaCalPanel !== k;
|
|
401
|
+
const input = root.querySelector("[data-asta-cal-value]");
|
|
402
|
+
if (input) {
|
|
403
|
+
input.value = k;
|
|
404
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
405
|
+
}
|
|
406
|
+
fire(root, "asta:select", { day: k });
|
|
407
|
+
}
|
|
408
|
+
function calFocus(root, cell, focus = true) {
|
|
409
|
+
for (const c of all(root, '[role="gridcell"][tabindex="0"]')) c.tabIndex = -1;
|
|
410
|
+
cell.tabIndex = 0;
|
|
411
|
+
if (focus) cell.focus();
|
|
412
|
+
}
|
|
413
|
+
// Load another month with htmx, then put focus on the day.
|
|
414
|
+
function calGo(root, k, focus) {
|
|
415
|
+
const url = root.dataset.tpl.replace("__M__", k.slice(0, 7)).replace("__D__", k);
|
|
416
|
+
calPending = { id: root.id, key: k, focus, dir: k.slice(0, 7) > root.dataset.month ? "next" : "prev" };
|
|
417
|
+
if (!window.htmx) return void (location.href = url);
|
|
418
|
+
const push = root.getAttribute("hx-push-url") !== "false";
|
|
419
|
+
htmx.ajax("GET", url, { source: root, target: `#${root.id}`, select: `#${root.id}`, swap: "outerHTML show:none" }).then(() => {
|
|
420
|
+
if (push) history.pushState({ htmx: true }, "", url);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
document.addEventListener("click", (e) => {
|
|
424
|
+
const t = el(e);
|
|
425
|
+
const nav = t?.closest("[data-asta-cal-nav]");
|
|
426
|
+
if (nav) {
|
|
427
|
+
const root = nav.closest("[data-asta-cal]");
|
|
428
|
+
const kind = nav.dataset.astaCalNav;
|
|
429
|
+
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;
|
|
430
|
+
calPending = { id: root.id, dir, focus: false };
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const cell = t?.closest('[data-asta-cal] [role="gridcell"]');
|
|
434
|
+
if (!cell) return;
|
|
435
|
+
const root = cell.closest("[data-asta-cal]");
|
|
436
|
+
if (cell.hasAttribute("data-out")) calGo(root, cell.dataset.day, true);
|
|
437
|
+
else calSelect(root, cell);
|
|
438
|
+
});
|
|
439
|
+
document.addEventListener("keydown", (e) => {
|
|
440
|
+
const cell = el(e);
|
|
441
|
+
if (!cell?.matches('[data-asta-cal] [role="gridcell"]')) return;
|
|
442
|
+
const root = cell.closest("[data-asta-cal]");
|
|
443
|
+
const d = parseDay(cell.dataset.day);
|
|
444
|
+
const week = Number(root.dataset.week || 1);
|
|
445
|
+
const offset = (d.getUTCDay() - week + 7) % 7;
|
|
446
|
+
const to = {
|
|
447
|
+
ArrowLeft: () => addDays(d, -1),
|
|
448
|
+
ArrowRight: () => addDays(d, 1),
|
|
449
|
+
ArrowUp: () => addDays(d, -7),
|
|
450
|
+
ArrowDown: () => addDays(d, 7),
|
|
451
|
+
Home: () => addDays(d, -offset),
|
|
452
|
+
End: () => addDays(d, 6 - offset),
|
|
453
|
+
PageUp: () => addMonths(d, e.shiftKey ? -12 : -1),
|
|
454
|
+
PageDown: () => addMonths(d, e.shiftKey ? 12 : 1),
|
|
455
|
+
}[e.key];
|
|
456
|
+
if (to) {
|
|
457
|
+
e.preventDefault();
|
|
458
|
+
const k = dayKey(to());
|
|
459
|
+
const next = root.querySelector(`[role="gridcell"][data-day="${k}"]:not([data-out])`);
|
|
460
|
+
if (next) calFocus(root, next);
|
|
461
|
+
else calGo(root, k, true);
|
|
462
|
+
} else if (e.key === "Enter" || e.key === " ") {
|
|
463
|
+
e.preventDefault();
|
|
464
|
+
cell.click();
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
scans.push(() => {
|
|
468
|
+
if (!calPending) return;
|
|
469
|
+
const root = document.getElementById(calPending.id);
|
|
470
|
+
if (!root?.matches("[data-asta-cal]")) return;
|
|
471
|
+
const { dir, key, focus } = calPending;
|
|
472
|
+
calPending = null;
|
|
473
|
+
const grid = root.querySelector("[data-asta-cal-grid]");
|
|
474
|
+
if (grid && dir && !reduce()) grid.dataset.dir = dir;
|
|
475
|
+
const cell = key && root.querySelector(`[role="gridcell"][data-day="${key}"]`);
|
|
476
|
+
if (cell && focus) calFocus(root, cell);
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
/* ---------- FileCard ---------- */
|
|
480
|
+
|
|
481
|
+
document.addEventListener("click", (e) => {
|
|
482
|
+
const b = el(e)?.closest("[data-asta-file-star]");
|
|
483
|
+
if (!b) return;
|
|
484
|
+
const on = b.getAttribute("aria-pressed") !== "true";
|
|
485
|
+
const title = (b.getAttribute("aria-label") || "").replace(/^(Un)?star /, "");
|
|
486
|
+
b.setAttribute("aria-pressed", String(on));
|
|
487
|
+
b.value = String(on);
|
|
488
|
+
b.setAttribute("aria-label", `${on ? "Unstar" : "Star"} ${title}`);
|
|
489
|
+
if (on) replay(b, "data-popped");
|
|
490
|
+
fire(b, "asta:star", { starred: on });
|
|
491
|
+
});
|
|
492
|
+
scans.push((root) => {
|
|
493
|
+
for (const b of within(root, "[data-asta-file-star]")) {
|
|
494
|
+
const on = b.getAttribute("aria-pressed") === "true";
|
|
495
|
+
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)}`);
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
/* ---------- ImageCompare ---------- */
|
|
500
|
+
|
|
501
|
+
const clamp = (n) => Math.min(100, Math.max(0, n));
|
|
502
|
+
function cmpSet(root, n) {
|
|
503
|
+
const v = Math.round(clamp(n) * 10) / 10;
|
|
504
|
+
root.style.setProperty("--pos", String(v));
|
|
505
|
+
root.toggleAttribute("data-hide-before", v < 18);
|
|
506
|
+
root.toggleAttribute("data-hide-after", v > 82);
|
|
507
|
+
const h = root.querySelector("[data-asta-compare-handle]");
|
|
508
|
+
if (h) {
|
|
509
|
+
h.setAttribute("aria-valuenow", String(Math.round(v)));
|
|
510
|
+
h.setAttribute("aria-valuetext", `${Math.round(v)}% ${root.dataset.before}, ${Math.round(100 - v)}% ${root.dataset.after}`);
|
|
511
|
+
}
|
|
512
|
+
fire(root, "asta:change", { value: v });
|
|
513
|
+
}
|
|
514
|
+
const cmpPos = (root) => parseFloat(root.style.getPropertyValue("--pos")) || 50;
|
|
515
|
+
const cmpAt = (root, x) => {
|
|
516
|
+
const r = root.getBoundingClientRect();
|
|
517
|
+
return r.width ? ((x - r.left) / r.width) * 100 : cmpPos(root);
|
|
518
|
+
};
|
|
519
|
+
let press = null;
|
|
520
|
+
const cmpHandle = (root) => root.querySelector("[data-asta-compare-handle]");
|
|
521
|
+
document.addEventListener("pointerdown", (e) => {
|
|
522
|
+
const root = el(e)?.closest("[data-asta-compare]");
|
|
523
|
+
if (!root || e.button !== 0) return;
|
|
524
|
+
const touch = e.pointerType === "touch";
|
|
525
|
+
press = { root, id: e.pointerId, x: e.clientX, y: e.clientY, touch, moved: false };
|
|
526
|
+
if (touch) return;
|
|
527
|
+
root.setPointerCapture(e.pointerId);
|
|
528
|
+
cmpSet(root, cmpAt(root, e.clientX));
|
|
529
|
+
cmpHandle(root)?.focus({ preventScroll: true });
|
|
530
|
+
});
|
|
531
|
+
// Keeps focus on the handle (a press on the frame would blur it) and stops text selection.
|
|
532
|
+
document.addEventListener("mousedown", (e) => {
|
|
533
|
+
if (el(e)?.closest("[data-asta-compare]")) e.preventDefault();
|
|
534
|
+
});
|
|
535
|
+
document.addEventListener("pointermove", (e) => {
|
|
536
|
+
const p = press;
|
|
537
|
+
if (!p || p.id !== e.pointerId) return;
|
|
538
|
+
if (!p.moved) {
|
|
539
|
+
const dx = Math.abs(e.clientX - p.x);
|
|
540
|
+
const dy = Math.abs(e.clientY - p.y);
|
|
541
|
+
// Touch waits for a horizontal move, so a vertical swipe scrolls the page.
|
|
542
|
+
if (p.touch ? dx < 6 || dx <= dy : dx < 2) return;
|
|
543
|
+
p.moved = true;
|
|
544
|
+
if (p.touch) {
|
|
545
|
+
p.root.setPointerCapture(e.pointerId);
|
|
546
|
+
cmpHandle(p.root)?.focus({ preventScroll: true });
|
|
547
|
+
}
|
|
548
|
+
p.root.setAttribute("data-dragging", "");
|
|
549
|
+
}
|
|
550
|
+
cmpSet(p.root, cmpAt(p.root, e.clientX));
|
|
551
|
+
});
|
|
552
|
+
const cmpUp = (e) => {
|
|
553
|
+
const p = press;
|
|
554
|
+
if (!p || p.id !== e.pointerId) return;
|
|
555
|
+
press = null;
|
|
556
|
+
if (p.touch && !p.moved && e.type === "pointerup") {
|
|
557
|
+
cmpSet(p.root, cmpAt(p.root, e.clientX));
|
|
558
|
+
cmpHandle(p.root)?.focus({ preventScroll: true });
|
|
559
|
+
}
|
|
560
|
+
p.root.removeAttribute("data-dragging");
|
|
561
|
+
};
|
|
562
|
+
document.addEventListener("pointerup", cmpUp);
|
|
563
|
+
document.addEventListener("pointercancel", cmpUp);
|
|
564
|
+
document.addEventListener("keydown", (e) => {
|
|
565
|
+
const h = el(e);
|
|
566
|
+
if (!h?.matches("[data-asta-compare-handle]")) return;
|
|
567
|
+
const root = h.closest("[data-asta-compare]");
|
|
568
|
+
const pos = cmpPos(root);
|
|
569
|
+
const step = parseFloat(root.dataset.step) || 2;
|
|
570
|
+
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];
|
|
571
|
+
if (to === undefined) return;
|
|
572
|
+
e.preventDefault();
|
|
573
|
+
cmpSet(root, to);
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
/* ---------- Carousel ---------- */
|
|
577
|
+
|
|
578
|
+
const carTrack = (root) => root.querySelector("[data-asta-carousel-track]");
|
|
579
|
+
const carSlides = (root) => all(root, "[data-asta-carousel-slide]");
|
|
580
|
+
const carStep = (root) => {
|
|
581
|
+
const s = carSlides(root);
|
|
582
|
+
return s.length > 1 ? s[1].offsetLeft - s[0].offsetLeft : carTrack(root).clientWidth || 1;
|
|
583
|
+
};
|
|
584
|
+
function carShow(root, i) {
|
|
585
|
+
const slides = carSlides(root);
|
|
586
|
+
const n = slides.length;
|
|
587
|
+
if (String(i) === root.dataset.index) return;
|
|
588
|
+
root.dataset.index = String(i);
|
|
589
|
+
slides.forEach((s, k) => {
|
|
590
|
+
s.toggleAttribute("inert", k !== i);
|
|
591
|
+
if (k === i) s.removeAttribute("aria-hidden");
|
|
592
|
+
else s.setAttribute("aria-hidden", "true");
|
|
593
|
+
});
|
|
594
|
+
for (const d of all(root, "[data-asta-carousel-dot]")) {
|
|
595
|
+
const k = Number(d.dataset.astaCarouselDot);
|
|
596
|
+
const on = k === i;
|
|
597
|
+
if (on) d.setAttribute("aria-current", "true");
|
|
598
|
+
else d.removeAttribute("aria-current");
|
|
599
|
+
d.style.width = `${on ? 30 : 14}px`;
|
|
600
|
+
d.style.transform = `translateX(${k * 14 + (k > i ? 16 : 0)}px)`;
|
|
601
|
+
}
|
|
602
|
+
const loop = root.hasAttribute("data-loop");
|
|
603
|
+
const [prev, next] = all(root, "[data-asta-carousel-step]");
|
|
604
|
+
if (prev) prev.disabled = !loop && i === 0;
|
|
605
|
+
if (next) next.disabled = !loop && i === n - 1;
|
|
606
|
+
fire(root, "asta:slide", { index: i });
|
|
607
|
+
}
|
|
608
|
+
function carGo(root, i, wrap) {
|
|
609
|
+
const n = carSlides(root).length;
|
|
610
|
+
if (!n) return;
|
|
611
|
+
const to = wrap || root.hasAttribute("data-loop") ? (i + n) % n : Math.max(0, Math.min(n - 1, i));
|
|
612
|
+
root._target = to;
|
|
613
|
+
carShow(root, to);
|
|
614
|
+
carTrack(root).scrollTo({ left: to * carStep(root), behavior: reduce() ? "auto" : "smooth" });
|
|
615
|
+
}
|
|
616
|
+
function carPlay(root) {
|
|
617
|
+
const on = root.hasAttribute("data-autoplay") && !reduce() && root._visible && !root._hover && !root._focus;
|
|
618
|
+
root.toggleAttribute("data-playing", !!on);
|
|
619
|
+
const t = carTrack(root);
|
|
620
|
+
if (t) t.setAttribute("aria-live", on ? "off" : "polite");
|
|
621
|
+
}
|
|
622
|
+
const carSeen = new IntersectionObserver((entries) => {
|
|
623
|
+
for (const en of entries) {
|
|
624
|
+
en.target._visible = en.isIntersecting;
|
|
625
|
+
carPlay(en.target);
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
scans.push((root) => {
|
|
629
|
+
for (const c of within(root, "[data-asta-carousel]")) {
|
|
630
|
+
if (c._init) continue;
|
|
631
|
+
c._init = true;
|
|
632
|
+
if (c.dataset.autoplay) c.style.setProperty("--asta-autoplay", `${c.dataset.autoplay}ms`);
|
|
633
|
+
carSeen.observe(c);
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
document.addEventListener(
|
|
637
|
+
"scroll",
|
|
638
|
+
(e) => {
|
|
639
|
+
const t = el(e);
|
|
640
|
+
if (!t?.matches?.("[data-asta-carousel-track]") || t._raf) return;
|
|
641
|
+
t._raf = requestAnimationFrame(() => {
|
|
642
|
+
t._raf = 0;
|
|
643
|
+
const root = t.closest("[data-asta-carousel]");
|
|
644
|
+
const i = Math.round(t.scrollLeft / carStep(root));
|
|
645
|
+
if (root._target !== undefined) {
|
|
646
|
+
if (i !== root._target) return;
|
|
647
|
+
root._target = undefined;
|
|
648
|
+
}
|
|
649
|
+
carShow(root, i);
|
|
650
|
+
});
|
|
651
|
+
},
|
|
652
|
+
true,
|
|
653
|
+
);
|
|
654
|
+
document.addEventListener(
|
|
655
|
+
"scrollend",
|
|
656
|
+
(e) => {
|
|
657
|
+
const t = el(e);
|
|
658
|
+
if (!t?.matches?.("[data-asta-carousel-track]")) return;
|
|
659
|
+
const root = t.closest("[data-asta-carousel]");
|
|
660
|
+
root._target = undefined;
|
|
661
|
+
carShow(root, Math.round(t.scrollLeft / carStep(root)));
|
|
662
|
+
},
|
|
663
|
+
true,
|
|
664
|
+
);
|
|
665
|
+
document.addEventListener("click", (e) => {
|
|
666
|
+
const t = el(e);
|
|
667
|
+
const dot = t?.closest("[data-asta-carousel-dot]");
|
|
668
|
+
const step = t?.closest("[data-asta-carousel-step]");
|
|
669
|
+
if (!dot && !step) return;
|
|
670
|
+
const root = (dot || step).closest("[data-asta-carousel]");
|
|
671
|
+
if (dot) carGo(root, Number(dot.dataset.astaCarouselDot));
|
|
672
|
+
else carGo(root, Number(root.dataset.index) + Number(step.dataset.astaCarouselStep));
|
|
673
|
+
});
|
|
674
|
+
document.addEventListener("keydown", (e) => {
|
|
675
|
+
const root = el(e);
|
|
676
|
+
if (!root?.matches("[data-asta-carousel]")) return;
|
|
677
|
+
const i = Number(root.dataset.index);
|
|
678
|
+
const to = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: carSlides(root).length - 1 }[e.key];
|
|
679
|
+
if (to === undefined) return;
|
|
680
|
+
e.preventDefault();
|
|
681
|
+
carGo(root, to);
|
|
682
|
+
});
|
|
683
|
+
document.addEventListener("animationend", (e) => {
|
|
684
|
+
if (e.animationName !== "asta-carousel-fill") return;
|
|
685
|
+
const root = el(e).closest("[data-asta-carousel]");
|
|
686
|
+
if (root) carGo(root, Number(root.dataset.index) + 1, true);
|
|
687
|
+
});
|
|
688
|
+
for (const [type, on] of [
|
|
689
|
+
["pointerover", true],
|
|
690
|
+
["pointerout", false],
|
|
691
|
+
])
|
|
692
|
+
document.addEventListener(type, (e) => {
|
|
693
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
694
|
+
if (!root || root.contains(e.relatedTarget)) return;
|
|
695
|
+
root._hover = on;
|
|
696
|
+
carPlay(root);
|
|
697
|
+
});
|
|
698
|
+
document.addEventListener("focusin", (e) => {
|
|
699
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
700
|
+
if (!root) return;
|
|
701
|
+
// Only keyboard focus pauses autoplay. Clicking an arrow or a dot shouldn't stop it.
|
|
702
|
+
let visible = true;
|
|
703
|
+
try {
|
|
704
|
+
visible = el(e).matches(":focus-visible");
|
|
705
|
+
} catch {}
|
|
706
|
+
if (visible) {
|
|
707
|
+
root._focus = true;
|
|
708
|
+
carPlay(root);
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
document.addEventListener("focusout", (e) => {
|
|
712
|
+
const root = el(e)?.closest("[data-asta-carousel]");
|
|
713
|
+
if (!root || root.contains(e.relatedTarget)) return;
|
|
714
|
+
root._focus = false;
|
|
715
|
+
carPlay(root);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
/* ---------- NotificationCenter ---------- */
|
|
719
|
+
|
|
720
|
+
const ncParts = (root) => ({
|
|
721
|
+
trigger: root.querySelector("[data-asta-nc-trigger]"),
|
|
722
|
+
panel: root.querySelector("[data-asta-nc-panel]"),
|
|
723
|
+
});
|
|
724
|
+
function ncOpen(root, on) {
|
|
725
|
+
const { trigger, panel } = ncParts(root);
|
|
726
|
+
if (!trigger || !panel) return;
|
|
727
|
+
if (on) {
|
|
728
|
+
for (const o of document.querySelectorAll("[data-asta-nc][data-open]")) if (o !== root) ncOpen(o, false);
|
|
729
|
+
root.setAttribute("data-open", "");
|
|
730
|
+
trigger.setAttribute("aria-expanded", "true");
|
|
731
|
+
panel.hidden = false;
|
|
732
|
+
panel.style.left = panel.style.right = "";
|
|
733
|
+
// Keep the panel on screen on narrow viewports. Measure the anchor, not the panel: the panel is mid-scale.
|
|
734
|
+
const box = root.getBoundingClientRect();
|
|
735
|
+
const w = panel.offsetWidth;
|
|
736
|
+
if (root.dataset.align === "start") {
|
|
737
|
+
if (box.left + w > innerWidth - 8) panel.style.left = `${innerWidth - 8 - w - box.left}px`;
|
|
738
|
+
} else if (box.right - w < 8) panel.style.right = `${box.right - w - 8}px`;
|
|
739
|
+
replay(root.querySelector("[data-asta-nc-list]"), "data-fresh");
|
|
740
|
+
} else if (root.hasAttribute("data-open")) {
|
|
741
|
+
root.removeAttribute("data-open");
|
|
742
|
+
trigger.setAttribute("aria-expanded", "false");
|
|
743
|
+
setTimeout(() => {
|
|
744
|
+
if (!root.hasAttribute("data-open")) panel.hidden = true;
|
|
745
|
+
}, reduce() ? 0 : 170);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
function ncSync(root) {
|
|
749
|
+
const { trigger, panel } = ncParts(root);
|
|
750
|
+
const entries = all(root, "[data-asta-nc-entry]");
|
|
751
|
+
const n = entries.filter((x) => !x.hasAttribute("data-read")).length;
|
|
752
|
+
const label = root.dataset.label;
|
|
753
|
+
trigger.setAttribute("aria-label", n ? `${label}, ${n} unread` : label);
|
|
754
|
+
const badge = root.querySelector("[data-asta-nc-badge]");
|
|
755
|
+
const count = root.querySelector("[data-asta-nc-count]");
|
|
756
|
+
const text = n > 99 ? "99+" : String(n);
|
|
757
|
+
if (badge) badge.hidden = n === 0;
|
|
758
|
+
if (count && count.textContent !== text) {
|
|
759
|
+
count.textContent = text;
|
|
760
|
+
if (!reduce()) replay(count, "data-bump");
|
|
761
|
+
}
|
|
762
|
+
const tc = root.querySelector("[data-asta-nc-tabcount]");
|
|
763
|
+
if (tc) {
|
|
764
|
+
tc.textContent = String(n);
|
|
765
|
+
tc.hidden = n === 0;
|
|
766
|
+
}
|
|
767
|
+
root.querySelector("[data-asta-nc-all]")?.toggleAttribute("aria-disabled", n === 0);
|
|
768
|
+
// The empty state follows what the current tab shows.
|
|
769
|
+
const unreadTab = panel.dataset.tab === "unread";
|
|
770
|
+
const shown = unreadTab ? entries.filter((x) => !x.hasAttribute("data-read") || x.hasAttribute("data-keep")).length : entries.length;
|
|
771
|
+
const empty = root.querySelector("[data-asta-nc-empty]");
|
|
772
|
+
if (empty) {
|
|
773
|
+
empty.hidden = shown > 0;
|
|
774
|
+
for (const p of all(empty, "[data-unread]")) {
|
|
775
|
+
p.dataset.all ??= p.textContent;
|
|
776
|
+
p.textContent = unreadTab ? p.dataset.unread : p.dataset.all;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function ncRead(root, entries, say) {
|
|
781
|
+
let changed = false;
|
|
782
|
+
for (const x of entries) {
|
|
783
|
+
if (x.hasAttribute("data-read")) continue;
|
|
784
|
+
changed = true;
|
|
785
|
+
x.setAttribute("data-read", "");
|
|
786
|
+
const dot = x.querySelector("[data-asta-nc-dot]");
|
|
787
|
+
if (dot) {
|
|
788
|
+
dot.tabIndex = -1;
|
|
789
|
+
dot.setAttribute("aria-hidden", "true");
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (changed) ncSync(root);
|
|
793
|
+
if (say) {
|
|
794
|
+
const s = root.querySelector("[data-asta-nc-status]");
|
|
795
|
+
if (s) s.textContent = say;
|
|
796
|
+
}
|
|
797
|
+
return changed;
|
|
798
|
+
}
|
|
799
|
+
function ncTab(root, tab) {
|
|
800
|
+
const { panel } = ncParts(root);
|
|
801
|
+
for (const t of all(root, "[data-asta-nc-tab]")) {
|
|
802
|
+
const on = t.dataset.astaNcTab === tab;
|
|
803
|
+
t.setAttribute("aria-selected", String(on));
|
|
804
|
+
t.tabIndex = on ? 0 : -1;
|
|
805
|
+
}
|
|
806
|
+
// What was unread when the Unread tab opened stays listed, so rows don't vanish as you read them.
|
|
807
|
+
for (const x of all(root, "[data-asta-nc-entry]")) x.toggleAttribute("data-keep", tab === "unread" && !x.hasAttribute("data-read"));
|
|
808
|
+
panel.dataset.tab = tab;
|
|
809
|
+
const list = root.querySelector("[data-asta-nc-list]");
|
|
810
|
+
list?.setAttribute("aria-label", tab === "unread" ? "Unread notifications" : "All notifications");
|
|
811
|
+
if (list) replay(list, "data-fresh");
|
|
812
|
+
ncSync(root);
|
|
813
|
+
}
|
|
814
|
+
document.addEventListener("click", (e) => {
|
|
815
|
+
const t = el(e);
|
|
816
|
+
const trigger = t?.closest("[data-asta-nc-trigger]");
|
|
817
|
+
if (trigger) {
|
|
818
|
+
const root = trigger.closest("[data-asta-nc]");
|
|
819
|
+
ncOpen(root, !root.hasAttribute("data-open"));
|
|
820
|
+
}
|
|
821
|
+
for (const root of document.querySelectorAll("[data-asta-nc][data-open]")) if (!root.contains(t)) ncOpen(root, false);
|
|
822
|
+
const root = t?.closest("[data-asta-nc]");
|
|
823
|
+
if (!root || trigger) return;
|
|
824
|
+
const tab = t.closest("[data-asta-nc-tab]");
|
|
825
|
+
if (tab) return ncTab(root, tab.dataset.astaNcTab);
|
|
826
|
+
if (t.closest("[data-asta-nc-all]")) return void ncRead(root, all(root, "[data-asta-nc-entry]"), "All notifications marked as read.");
|
|
827
|
+
const dot = t.closest("[data-asta-nc-dot]");
|
|
828
|
+
const entry = t.closest("[data-asta-nc-entry]");
|
|
829
|
+
if (dot) {
|
|
830
|
+
ncRead(root, [entry], "Marked as read.");
|
|
831
|
+
entry.querySelector("[data-asta-nc-item]")?.focus();
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (t.closest("[data-asta-nc-item]") && entry && ncRead(root, [entry]) && root.dataset.url) {
|
|
835
|
+
// The row may be a link that navigates away, so this post must outlive the page.
|
|
836
|
+
fetch(root.dataset.url, {
|
|
837
|
+
method: "POST",
|
|
838
|
+
keepalive: true,
|
|
839
|
+
headers: { "HX-Request": "true" },
|
|
840
|
+
body: new URLSearchParams({ id: entry.dataset.astaNcEntry }),
|
|
841
|
+
}).catch(() => {});
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
document.addEventListener("keydown", (e) => {
|
|
845
|
+
const t = el(e);
|
|
846
|
+
const root = t?.closest("[data-asta-nc]");
|
|
847
|
+
if (!root?.hasAttribute("data-open")) return;
|
|
848
|
+
if (e.key === "Escape") {
|
|
849
|
+
e.preventDefault();
|
|
850
|
+
ncOpen(root, false);
|
|
851
|
+
ncParts(root).trigger.focus();
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
const tab = t.closest("[data-asta-nc-tab]");
|
|
855
|
+
if (tab && (e.key === "ArrowLeft" || e.key === "ArrowRight")) {
|
|
856
|
+
e.preventDefault();
|
|
857
|
+
const other = tab.dataset.astaNcTab === "all" ? "unread" : "all";
|
|
858
|
+
ncTab(root, other);
|
|
859
|
+
root.querySelector(`[data-asta-nc-tab="${other}"]`)?.focus();
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
if (!t.closest("[data-asta-nc-list]") || !["ArrowDown", "ArrowUp", "Home", "End"].includes(e.key)) return;
|
|
863
|
+
const rows = all(root, "[data-asta-nc-item]").filter((r) => r.offsetParent);
|
|
864
|
+
if (!rows.length) return;
|
|
865
|
+
e.preventDefault();
|
|
866
|
+
const i = rows.indexOf(document.activeElement);
|
|
867
|
+
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)));
|
|
868
|
+
rows[next]?.focus();
|
|
869
|
+
});
|
|
870
|
+
|
|
871
|
+
/* ---------- Comments ---------- */
|
|
872
|
+
|
|
873
|
+
const cmSubmit = (form) => form.querySelector('button[type="submit"]');
|
|
874
|
+
function cmCheck(form) {
|
|
875
|
+
const b = cmSubmit(form);
|
|
876
|
+
if (b) b.disabled = !form.querySelector("textarea")?.value.trim();
|
|
877
|
+
}
|
|
878
|
+
function cmClose(box, focus) {
|
|
879
|
+
const opener = box._opener;
|
|
880
|
+
box.remove();
|
|
881
|
+
if (focus && opener?.isConnected) opener.focus();
|
|
882
|
+
}
|
|
883
|
+
function cmTotal(root) {
|
|
884
|
+
const t = root.querySelector("[data-asta-comments-total]");
|
|
885
|
+
if (t) t.textContent = String(root.querySelectorAll("[data-asta-comment]").length);
|
|
886
|
+
}
|
|
887
|
+
scans.push((root) => {
|
|
888
|
+
for (const r of within(root, "[data-asta-comments]")) cmTotal(r);
|
|
889
|
+
if (root !== document) root.closest?.("[data-asta-comments]") && cmTotal(root.closest("[data-asta-comments]"));
|
|
890
|
+
});
|
|
891
|
+
document.addEventListener("input", (e) => {
|
|
892
|
+
const form = el(e)?.closest("[data-asta-comment-form]");
|
|
893
|
+
if (form) cmCheck(form);
|
|
894
|
+
});
|
|
895
|
+
document.addEventListener("keydown", (e) => {
|
|
896
|
+
const area = el(e);
|
|
897
|
+
const form = area?.closest("[data-asta-comment-form]");
|
|
898
|
+
if (!form || area.tagName !== "TEXTAREA") return;
|
|
899
|
+
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
900
|
+
e.preventDefault();
|
|
901
|
+
if (area.value.trim()) form.requestSubmit();
|
|
902
|
+
} else if (e.key === "Escape" && form.hasAttribute("data-reply")) {
|
|
903
|
+
e.preventDefault();
|
|
904
|
+
cmClose(form.closest("[data-asta-comment-replybox]"), true);
|
|
905
|
+
}
|
|
906
|
+
});
|
|
907
|
+
document.addEventListener("click", (e) => {
|
|
908
|
+
const t = el(e);
|
|
909
|
+
const cancel = t?.closest("[data-asta-comment-cancel]");
|
|
910
|
+
if (cancel) return cmClose(cancel.closest("[data-asta-comment-replybox]"), true);
|
|
911
|
+
const reply = t?.closest("[data-asta-comment-reply]");
|
|
912
|
+
if (reply) {
|
|
913
|
+
const root = reply.closest("[data-asta-comments]");
|
|
914
|
+
const thread = root.querySelector(`[data-asta-comment-thread="${CSS.escape(reply.dataset.astaCommentReply)}"]`);
|
|
915
|
+
const replies = thread?.querySelector("[data-asta-comment-replies]");
|
|
916
|
+
const tpl = root.querySelector("template[data-asta-comments-replybox]");
|
|
917
|
+
if (!replies || !tpl) return;
|
|
918
|
+
let box = replies.querySelector(":scope > [data-asta-comment-replybox]");
|
|
919
|
+
if (!box) {
|
|
920
|
+
box = tpl.content.firstElementChild.cloneNode(true);
|
|
921
|
+
replies.append(box);
|
|
922
|
+
window.htmx?.process(box);
|
|
923
|
+
}
|
|
924
|
+
box._opener = thread.querySelector("[data-asta-comment-reply]");
|
|
925
|
+
const name = box._opener?.dataset.name || "";
|
|
926
|
+
box.querySelector('input[name="parent"]').value = reply.dataset.astaCommentReply;
|
|
927
|
+
const area = box.querySelector("textarea");
|
|
928
|
+
area.placeholder = `Reply to ${name}…`;
|
|
929
|
+
if (reply.dataset.mention) area.value = `@${reply.dataset.mention} `;
|
|
930
|
+
cmCheck(box.querySelector("form"));
|
|
931
|
+
area.focus();
|
|
932
|
+
area.setSelectionRange(area.value.length, area.value.length);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const like = t?.closest("[data-asta-comment-like]");
|
|
936
|
+
if (like) {
|
|
937
|
+
const on = like.getAttribute("aria-pressed") !== "true";
|
|
938
|
+
const c = like.querySelector("[data-asta-comment-likes]");
|
|
939
|
+
const n = Math.max(0, Number(c?.textContent || 0) + (on ? 1 : -1));
|
|
940
|
+
like.setAttribute("aria-pressed", String(on));
|
|
941
|
+
like.value = String(on);
|
|
942
|
+
like.setAttribute("aria-label", `Like, ${n}`);
|
|
943
|
+
if (c) c.textContent = String(n);
|
|
944
|
+
if (!reduce()) replay(like, "data-popped");
|
|
945
|
+
fire(like, "asta:like", { liked: on });
|
|
946
|
+
}
|
|
947
|
+
});
|
|
948
|
+
document.addEventListener("htmx:afterRequest", (e) => {
|
|
949
|
+
const form = e.detail?.elt;
|
|
950
|
+
if (!form?.matches?.("[data-asta-comment-form]") || !e.detail.successful) return;
|
|
951
|
+
const area = form.querySelector("textarea");
|
|
952
|
+
if (area) area.value = "";
|
|
953
|
+
cmCheck(form);
|
|
954
|
+
if (form.hasAttribute("data-reply")) cmClose(form.closest("[data-asta-comment-replybox]"), true);
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
/* ---------- MotionButton ---------- */
|
|
958
|
+
|
|
959
|
+
const mbSeen = new IntersectionObserver((entries) => {
|
|
960
|
+
for (const en of entries) en.target.toggleAttribute("data-offscreen", !en.isIntersecting);
|
|
961
|
+
});
|
|
962
|
+
scans.push((root) => {
|
|
963
|
+
for (const b of within(root, '[data-asta-mb="beam"]')) mbSeen.observe(b);
|
|
964
|
+
});
|
|
965
|
+
for (const type of ["pointerover", "pointerout"])
|
|
966
|
+
document.addEventListener(type, (e) => {
|
|
967
|
+
const b = el(e)?.closest('[data-asta-mb="fill"], [data-asta-mb="shine"]');
|
|
968
|
+
if (!b || b.contains(e.relatedTarget)) return;
|
|
969
|
+
if (b.dataset.astaMb === "fill") {
|
|
970
|
+
// The circle grows from where the pointer came in and shrinks toward where it leaves.
|
|
971
|
+
const r = b.getBoundingClientRect();
|
|
972
|
+
b.style.setProperty("--fx", `${e.clientX - r.left}px`);
|
|
973
|
+
b.style.setProperty("--fy", `${e.clientY - r.top}px`);
|
|
974
|
+
} else if (type === "pointerover") b.toggleAttribute("data-shine");
|
|
975
|
+
});
|
|
976
|
+
document.addEventListener("focusin", (e) => {
|
|
977
|
+
const b = el(e)?.closest('[data-asta-mb="shine"]');
|
|
978
|
+
if (b) b.toggleAttribute("data-shine");
|
|
979
|
+
});
|
|
980
|
+
document.addEventListener("click", (e) => {
|
|
981
|
+
const b = el(e)?.closest('[data-asta-mb="copy"], [data-asta-mb="confirm"]');
|
|
982
|
+
if (!b) return;
|
|
983
|
+
clearTimeout(b._t);
|
|
984
|
+
if (b.dataset.astaMb === "copy") {
|
|
985
|
+
navigator.clipboard?.writeText(b.dataset.value || "").catch(() => {});
|
|
986
|
+
b.setAttribute("data-done", "");
|
|
987
|
+
const s = b.querySelector("[data-asta-mb-status]");
|
|
988
|
+
if (s) s.textContent = "Copied to clipboard";
|
|
989
|
+
b._t = setTimeout(() => {
|
|
990
|
+
b.removeAttribute("data-done");
|
|
991
|
+
if (s) s.textContent = "";
|
|
992
|
+
}, 1600);
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
const ms = Number(b.dataset.timeout) || 3000;
|
|
996
|
+
if (b.dataset.stage === "idle") {
|
|
997
|
+
b.style.setProperty("--asta-timeout", `${ms}ms`);
|
|
998
|
+
b.dataset.stage = "ask";
|
|
999
|
+
b._t = setTimeout(() => (b.dataset.stage = "idle"), ms);
|
|
1000
|
+
} else if (b.dataset.stage === "ask") {
|
|
1001
|
+
b.dataset.stage = "done";
|
|
1002
|
+
fire(b, "asta:confirm");
|
|
1003
|
+
b._t = setTimeout(() => (b.dataset.stage = "idle"), 1800);
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
document.addEventListener("focusout", (e) => {
|
|
1007
|
+
const b = el(e)?.closest('[data-asta-mb="confirm"][data-stage="ask"]');
|
|
1008
|
+
if (b) {
|
|
1009
|
+
clearTimeout(b._t);
|
|
1010
|
+
b.dataset.stage = "idle";
|
|
1011
|
+
}
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
// First look, once every section above has added its scan.
|
|
1015
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => scan(document));
|
|
1016
|
+
else scan(document);
|
|
1017
|
+
})();
|