@kashi-ui/elements 2.21.0 → 2.23.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.
Files changed (79) hide show
  1. package/README.md +6 -1
  2. package/dist/base.js +4 -0
  3. package/dist/buttons.d.ts +82 -0
  4. package/dist/buttons.js +487 -0
  5. package/dist/cards.d.ts +30 -0
  6. package/dist/cards.js +183 -0
  7. package/dist/cdn/{appointment-wMGBoHw9.js → appointment-DOXBUwIU.js} +1 -1
  8. package/dist/cdn/{base-Dg1Mb8Jk.js → base-D4tCOysh.js} +1 -1
  9. package/dist/cdn/{basic-B5ZxgagA.js → basic-DuY4RslZ.js} +1 -1
  10. package/dist/cdn/buttons-zhSdiaXr.js +1 -0
  11. package/dist/cdn/calendar-BulPeVAJ.js +16 -0
  12. package/dist/cdn/card-BKiwJF34.js +1 -0
  13. package/dist/cdn/cards-DKarXHsC.js +1 -0
  14. package/dist/cdn/charts-ETYOAJyB.js +1 -0
  15. package/dist/cdn/chat-D5ZIMnNp.js +4 -0
  16. package/dist/cdn/choice-DBvC2Puo.js +1 -0
  17. package/dist/cdn/{combobox-CBT7yp94.js → combobox-BGuPAAt3.js} +1 -1
  18. package/dist/cdn/{commerce-CsyixnVK.js → commerce-SzWdpWJG.js} +1 -1
  19. package/dist/cdn/content-BgfBfy8j.js +1 -0
  20. package/dist/cdn/controls--OMZkJYe.js +1 -0
  21. package/dist/cdn/display-BfYWAjNu.js +1 -0
  22. package/dist/cdn/{fields-Cx84-qz1.js → fields-B-jTUQiI.js} +1 -1
  23. package/dist/cdn/forms-DgRiYmdV.js +1 -0
  24. package/dist/cdn/{holidays-DO82gXhM.js → holidays-Cu6IfbTL.js} +1 -1
  25. package/dist/cdn/hours-CeiVqPKD.js +1 -0
  26. package/dist/cdn/inputs-BkJ70Xkc.js +1 -0
  27. package/dist/cdn/kashi-elements.js +1 -1
  28. package/dist/cdn/legal-id-Mt8FX0fV.js +1 -0
  29. package/dist/cdn/{map-DlpA916P.js → map-TfSt3CjL.js} +2 -2
  30. package/dist/cdn/media-BRBINvzU.js +1 -0
  31. package/dist/cdn/{menu-CqTvk8Ok.js → menu-DTNVFXrb.js} +1 -1
  32. package/dist/cdn/money-CjGI2VIQ.js +1 -0
  33. package/dist/cdn/{nav-BHv7H1Fy.js → nav-CT5RVOzy.js} +1 -1
  34. package/dist/cdn/{overlay-BIEEJ4Sh.js → overlay-DC5-I6hM.js} +1 -1
  35. package/dist/cdn/parts-5fVaWtRe.js +1 -0
  36. package/dist/cdn/pickers-BwDRsDRo.js +1 -0
  37. package/dist/cdn/relative-OC1fV3Xh.js +1 -0
  38. package/dist/cdn/rich-text-CS6xEim6.js +1 -0
  39. package/dist/cdn/sheets-eVMx4d-5.js +1 -0
  40. package/dist/cdn/{table-B5XwDDS9.js → table-tgB4iwcM.js} +1 -1
  41. package/dist/cdn/{text-Bml-pVjo.js → text-C6fzEBBh.js} +1 -1
  42. package/dist/cdn/{time-DrXgFeQy.js → time-CkqqUKJv.js} +1 -1
  43. package/dist/cdn/{time-BQsTSvql.js → time-CwOYrtK-.js} +1 -1
  44. package/dist/cdn/validate-Dr2f6i6s.js +1 -0
  45. package/dist/cdn/work-DoNAKgVU.js +1 -0
  46. package/dist/cdn.js +11 -0
  47. package/dist/chat.d.ts +127 -0
  48. package/dist/chat.js +734 -0
  49. package/dist/choice.d.ts +98 -0
  50. package/dist/choice.js +597 -0
  51. package/dist/content.d.ts +164 -0
  52. package/dist/content.js +726 -0
  53. package/dist/hours.d.ts +29 -0
  54. package/dist/hours.js +179 -0
  55. package/dist/index.d.ts +64 -2
  56. package/dist/index.js +22 -1
  57. package/dist/manifest.json +843 -1
  58. package/dist/media.d.ts +51 -0
  59. package/dist/media.js +363 -0
  60. package/dist/parts.d.ts +31 -0
  61. package/dist/parts.js +114 -0
  62. package/dist/qr.d.ts +7 -0
  63. package/dist/qr.js +271 -0
  64. package/dist/rich-text.d.ts +40 -0
  65. package/dist/rich-text.js +375 -0
  66. package/dist/sheets.d.ts +128 -0
  67. package/dist/sheets.js +661 -0
  68. package/dist/validate.d.ts +40 -0
  69. package/dist/validate.js +224 -0
  70. package/dist/work.d.ts +117 -0
  71. package/dist/work.js +895 -0
  72. package/package.json +4 -4
  73. package/dist/cdn/calendar-Cjp5Rqer.js +0 -16
  74. package/dist/cdn/charts-DvQ4OwdS.js +0 -1
  75. package/dist/cdn/controls-B-s4IF_4.js +0 -1
  76. package/dist/cdn/display-MorJMDEF.js +0 -1
  77. package/dist/cdn/forms-CQeP9iiW.js +0 -1
  78. package/dist/cdn/inputs-Bqcs9Xy7.js +0 -1
  79. package/dist/cdn/pickers-BT9VPT4o.js +0 -1
package/dist/sheets.js ADDED
@@ -0,0 +1,661 @@
1
+ // کشوی کناری، برگهٔ پایین، نوار ناوبری بالای سایت و مرکز اعلان‌ها.
2
+ import { formatRelative } from "@kashi-ui/persian";
3
+ import { afterParse, bool, define, digitsOf, emit, esc, HTMLBase, ICONS, lang, msg, num, uid } from "./base.js";
4
+ import { closeAnimated, cx, icon, jsonAttr, takeSlot, toDate, unslotted } from "./parts.js";
5
+ /**
6
+ * پایهٔ پنجره‌های روی <dialog> بومی: open، show() و close()، Esc، کلیک روی پس‌زمینه (dismissible)،
7
+ * انیمیشن خروج و برگشت فوکوس به دکمه‌ای که پنجره را باز کرده بود.
8
+ */
9
+ class Modal extends HTMLBase {
10
+ constructor() {
11
+ super(...arguments);
12
+ this.dialog = null;
13
+ this.opener = null;
14
+ this.key = uid("kashi-modal");
15
+ }
16
+ connectedCallback() {
17
+ afterParse(() => {
18
+ if (!this.isConnected)
19
+ return;
20
+ if (!this.dialog)
21
+ this.setup();
22
+ this.decorate();
23
+ this.sync();
24
+ });
25
+ }
26
+ attributeChangedCallback(n, old, next) {
27
+ if (!this.dialog || old === next)
28
+ return;
29
+ if (n === "open")
30
+ this.sync();
31
+ else
32
+ this.decorate();
33
+ }
34
+ get open() {
35
+ return this.hasAttribute("open");
36
+ }
37
+ set open(v) {
38
+ this.toggleAttribute("open", !!v);
39
+ }
40
+ show() {
41
+ this.open = true;
42
+ }
43
+ close(returnValue) {
44
+ this.open = false;
45
+ if (returnValue !== undefined)
46
+ this.dataset.returnValue = returnValue;
47
+ }
48
+ setup() {
49
+ const footer = takeSlot(this, "footer");
50
+ const body = unslotted(this);
51
+ const d = document.createElement("dialog");
52
+ d.setAttribute("aria-labelledby", `${this.key}-title`);
53
+ this.build(d, body, footer);
54
+ this.replaceChildren(d);
55
+ this.dialog = d;
56
+ d.addEventListener("cancel", (e) => {
57
+ e.preventDefault();
58
+ this.close();
59
+ });
60
+ d.addEventListener("click", (e) => {
61
+ if (e.target === d && bool(this, "dismissible", true))
62
+ this.close();
63
+ const closer = e.target.closest("[data-kashi-close], .k-dialog__close");
64
+ if (closer && d.contains(closer))
65
+ this.close(closer.getAttribute("data-kashi-close") || undefined);
66
+ });
67
+ d.addEventListener("close", () => {
68
+ this.removeAttribute("open");
69
+ d.style.translate = "";
70
+ this.opener?.focus?.();
71
+ emit(this, "kashi-close", { returnValue: this.dataset.returnValue ?? "" });
72
+ delete this.dataset.returnValue;
73
+ });
74
+ }
75
+ heading(extra = "") {
76
+ return `<div class="k-dialog__heading${extra}"><h2 class="k-dialog__title" id="${this.key}-title"></h2><p class="k-dialog__description" id="${this.key}-desc"></p></div>`;
77
+ }
78
+ closeButton() {
79
+ return `<button type="button" class="k-btn k-btn--tertiary k-btn--icon k-btn--sm k-dialog__close">${ICONS.close}</button>`;
80
+ }
81
+ texts() {
82
+ const d = this.dialog;
83
+ d.querySelector(".k-dialog__title").textContent = this.getAttribute("heading") ?? "";
84
+ const desc = d.querySelector(".k-dialog__description");
85
+ const description = this.getAttribute("description");
86
+ desc.textContent = description ?? "";
87
+ desc.hidden = !description;
88
+ if (description)
89
+ d.setAttribute("aria-describedby", desc.id);
90
+ else
91
+ d.removeAttribute("aria-describedby");
92
+ d.querySelector(".k-dialog__close")?.setAttribute("aria-label", this.getAttribute("close-label") ?? msg().common.close);
93
+ }
94
+ wrap(cls, nodes) {
95
+ const el = document.createElement("div");
96
+ el.className = cls;
97
+ el.append(...nodes);
98
+ return el;
99
+ }
100
+ sync() {
101
+ const d = this.dialog;
102
+ if (!d || !this.isConnected)
103
+ return;
104
+ if (this.open) {
105
+ d.removeAttribute("data-closing");
106
+ if (!d.open) {
107
+ this.opener = document.activeElement;
108
+ d.showModal?.();
109
+ this.opened();
110
+ emit(this, "kashi-open", {});
111
+ }
112
+ }
113
+ else if (d.open)
114
+ closeAnimated(d, this.dataset.returnValue);
115
+ }
116
+ opened() { }
117
+ }
118
+ // ---------- کشو ----------
119
+ /**
120
+ * کشوی کناری روی <dialog> بومی برای فیلترها، جزئیات یا منوی گوشی: heading، description، side (start یا end)، size و dismissible.
121
+ * محتوای خود عنصر بدنه است و slot="footer" دکمه‌های پایین. دکمه با data-kashi-open="شناسه" بازش می‌کند.
122
+ */
123
+ export class KashiDrawer extends Modal {
124
+ build(d, body, footer) {
125
+ d.innerHTML = `<div class="k-dialog__header k-drawer__header">${this.heading()}${this.closeButton()}</div>`;
126
+ d.append(this.wrap("k-dialog__body k-drawer__body", body));
127
+ if (footer.length)
128
+ d.append(this.wrap("k-dialog__footer k-drawer__footer", footer));
129
+ }
130
+ decorate() {
131
+ const d = this.dialog;
132
+ const side = this.getAttribute("side") === "start" ? "start" : "end";
133
+ const size = this.getAttribute("size") ?? "md";
134
+ d.className = cx("k-dialog k-drawer", `k-dialog--${side}`, size !== "md" && `k-drawer--${size}`, this.dataset.drawerClass);
135
+ d.dataset.placement = side;
136
+ this.texts();
137
+ }
138
+ }
139
+ KashiDrawer.observedAttributes = ["open", "heading", "description", "side", "size", "close-label"];
140
+ // ---------- برگهٔ پایین ----------
141
+ const CLOSE_RATIO = 0.3;
142
+ const FLING = 0.6;
143
+ /**
144
+ * برگهٔ پایین برای گوشی: با دستگیره کشیده می‌شود و روی snap-points (مثل "0.5, 0.9" از ارتفاع صفحه) می‌ایستد؛
145
+ * کشیدن به پایین یا Esc می‌بندد. دستگیره با کیبورد: Enter ایست بعدی، ↑↓ تغییر ارتفاع. hide-title و show-close.
146
+ */
147
+ export class KashiBottomSheet extends Modal {
148
+ constructor() {
149
+ super(...arguments);
150
+ this.current = 0;
151
+ this.drag = null;
152
+ }
153
+ /** ایست فعلی (اندیس snap-points) */
154
+ get snap() {
155
+ return this.current;
156
+ }
157
+ set snap(i) {
158
+ this.current = Math.max(0, Math.min(i, this.points.length - 1));
159
+ if (this.dialog)
160
+ this.paintSnap();
161
+ }
162
+ get points() {
163
+ return (this.getAttribute("snap-points") ?? "").split(/[\s,]+/).map(Number).filter((n) => n > 0 && n <= 1);
164
+ }
165
+ build(d, body, footer) {
166
+ d.innerHTML = `<div class="k-sheet__grab"><span class="k-sheet__handle" aria-hidden="true"></span><div class="k-dialog__header k-sheet__header">${this.heading()}</div></div>`;
167
+ d.append(this.wrap("k-dialog__body k-sheet__body", body));
168
+ if (footer.length)
169
+ d.append(this.wrap("k-dialog__footer k-sheet__footer", footer));
170
+ const grab = d.querySelector(".k-sheet__grab");
171
+ grab.addEventListener("pointerdown", (e) => this.down(e));
172
+ grab.addEventListener("pointermove", (e) => this.move(e));
173
+ grab.addEventListener("pointerup", () => this.up());
174
+ grab.addEventListener("pointercancel", () => this.up());
175
+ grab.addEventListener("click", (e) => {
176
+ if (!e.target.closest("button.k-sheet__handle"))
177
+ return;
178
+ const pts = this.points;
179
+ this.setSnap(this.current === pts.length - 1 ? 0 : this.current + 1);
180
+ });
181
+ grab.addEventListener("keydown", (e) => {
182
+ if (!e.target.closest("button.k-sheet__handle"))
183
+ return;
184
+ const pts = this.points;
185
+ if (e.key === "ArrowUp" && this.current < pts.length - 1) {
186
+ e.preventDefault();
187
+ this.setSnap(this.current + 1);
188
+ }
189
+ else if (e.key === "ArrowDown" && this.current > 0) {
190
+ e.preventDefault();
191
+ this.setSnap(this.current - 1);
192
+ }
193
+ });
194
+ }
195
+ decorate() {
196
+ const d = this.dialog;
197
+ const pts = this.points;
198
+ d.className = cx("k-dialog k-dialog--bottom k-sheet", pts.length > 0 && "k-sheet--snap");
199
+ d.dataset.placement = "bottom";
200
+ const hide = bool(this, "hide-title"), showClose = bool(this, "show-close");
201
+ const header = d.querySelector(".k-sheet__header");
202
+ header.className = cx("k-dialog__header k-sheet__header", hide && !showClose && "k-sheet__header--hidden");
203
+ header.querySelector(".k-dialog__heading").className = cx("k-dialog__heading", hide && "k-sr-only");
204
+ const btn = header.querySelector(".k-dialog__close");
205
+ if (showClose && !btn)
206
+ header.insertAdjacentHTML("beforeend", this.closeButton());
207
+ else if (!showClose)
208
+ btn?.remove();
209
+ // با ایست‌ها، دستگیره یک دکمهٔ واقعی است
210
+ const handle = d.querySelector(".k-sheet__handle");
211
+ if (pts.length && handle.tagName !== "BUTTON")
212
+ handle.outerHTML = '<button type="button" class="k-sheet__handle"></button>';
213
+ else if (!pts.length && handle.tagName === "BUTTON")
214
+ handle.outerHTML = '<span class="k-sheet__handle" aria-hidden="true"></span>';
215
+ const snapAttr = this.getAttribute("snap");
216
+ if (snapAttr !== null)
217
+ this.current = Math.max(0, Math.min(Number(snapAttr) || 0, Math.max(0, pts.length - 1)));
218
+ this.texts();
219
+ this.paintSnap();
220
+ }
221
+ opened() {
222
+ const snapAttr = this.getAttribute("snap");
223
+ this.current = snapAttr !== null ? Number(snapAttr) || 0 : 0;
224
+ this.paintSnap();
225
+ }
226
+ paintSnap() {
227
+ const d = this.dialog;
228
+ const pts = this.points;
229
+ if (pts.length)
230
+ d.style.setProperty("--k-sheet-height", `${pts[Math.min(this.current, pts.length - 1)] * 100}dvh`);
231
+ else
232
+ d.style.removeProperty("--k-sheet-height");
233
+ const handle = d.querySelector("button.k-sheet__handle");
234
+ if (handle) {
235
+ const expanded = this.current === pts.length - 1;
236
+ handle.setAttribute("aria-label", expanded ? msg().sheet.collapse : msg().sheet.expand);
237
+ handle.setAttribute("aria-expanded", String(expanded));
238
+ }
239
+ }
240
+ setSnap(i) {
241
+ this.snap = i;
242
+ emit(this, "kashi-snap", { index: this.current });
243
+ }
244
+ down(e) {
245
+ if (e.button !== 0 || e.target.closest(".k-dialog__close"))
246
+ return;
247
+ const d = this.dialog;
248
+ this.drag = { y: e.clientY, t: e.timeStamp, h: d.getBoundingClientRect().height, dy: 0, v: 0 };
249
+ e.currentTarget.setPointerCapture?.(e.pointerId);
250
+ d.setAttribute("data-dragging", "");
251
+ }
252
+ move(e) {
253
+ const s = this.drag;
254
+ if (!s)
255
+ return;
256
+ const dy = e.clientY - s.y;
257
+ s.v = (dy - s.dy) / Math.max(1, e.timeStamp - s.t);
258
+ s.dy = dy;
259
+ s.t = e.timeStamp;
260
+ const pts = this.points;
261
+ const maxUp = pts.length ? pts[pts.length - 1] * innerHeight - s.h : 0;
262
+ const y = dy < 0 ? -Math.min(-dy, Math.max(0, maxUp)) - Math.max(0, -dy - Math.max(0, maxUp)) * 0.15 : dy;
263
+ this.dialog.style.translate = `0 ${y}px`;
264
+ }
265
+ up() {
266
+ const s = this.drag;
267
+ const d = this.dialog;
268
+ this.drag = null;
269
+ if (!s)
270
+ return;
271
+ d.removeAttribute("data-dragging");
272
+ d.style.translate = "";
273
+ const pts = this.points;
274
+ const target = s.h - s.dy;
275
+ if (bool(this, "dismissible", true) && (s.dy > s.h * CLOSE_RATIO || (s.v > FLING && s.dy > 24))) {
276
+ if (!pts.length || this.current === 0 || s.v > FLING * 1.5 || target < pts[0] * innerHeight * 0.6) {
277
+ d.style.translate = `0 ${Math.max(0, s.dy)}px`;
278
+ this.close();
279
+ return;
280
+ }
281
+ }
282
+ if (!pts.length)
283
+ return;
284
+ const next = Math.abs(s.v) > FLING
285
+ ? Math.max(0, Math.min(pts.length - 1, this.current + (s.v < 0 ? 1 : -1)))
286
+ : pts.reduce((best, p, i) => (Math.abs(p * innerHeight - target) < Math.abs(pts[best] * innerHeight - target) ? i : best), 0);
287
+ if (next !== this.current)
288
+ this.setSnap(next);
289
+ }
290
+ }
291
+ KashiBottomSheet.observedAttributes = ["open", "heading", "description", "hide-title", "snap-points", "snap", "show-close", "close-label"];
292
+ // ---------- نوار ناوبری ----------
293
+ const MENU = icon('<path d="M3.75 5.5h12.5M3.75 10h12.5M3.75 14.5h12.5"/>', 1.5);
294
+ /**
295
+ * نوار ناوبری بالای سایت: slot="brand" لوگو، فرزندان <a> پیوندها (aria-current="page" صفحهٔ فعلی، data-badge نشان)،
296
+ * slot="actions" دکمه‌ها و slot="menu-footer" پایین منوی گوشی. زیر collapse-below پیوندها به کشوی منو می‌روند.
297
+ */
298
+ export class KashiNavbar extends HTMLBase {
299
+ constructor() {
300
+ super(...arguments);
301
+ this.header = null;
302
+ this.nav = null;
303
+ this.drawer = null;
304
+ this.ro = null;
305
+ this.onScroll = () => {
306
+ this.header?.toggleAttribute("data-scrolled", bool(this, "sticky", true) && (window.scrollY ?? 0) > 4);
307
+ };
308
+ }
309
+ connectedCallback() {
310
+ afterParse(() => {
311
+ if (!this.isConnected)
312
+ return;
313
+ if (!this.header)
314
+ this.build();
315
+ this.update();
316
+ addEventListener("scroll", this.onScroll, { passive: true });
317
+ this.onScroll();
318
+ if (this.nav && typeof ResizeObserver !== "undefined" && !this.ro) {
319
+ this.ro = new ResizeObserver(() => this.toCurrent());
320
+ this.ro.observe(this.nav);
321
+ }
322
+ });
323
+ }
324
+ disconnectedCallback() {
325
+ removeEventListener("scroll", this.onScroll);
326
+ this.ro?.disconnect();
327
+ this.ro = null;
328
+ }
329
+ attributeChangedCallback() {
330
+ if (this.header)
331
+ this.update();
332
+ }
333
+ build() {
334
+ const brand = takeSlot(this, "brand");
335
+ const actions = takeSlot(this, "actions");
336
+ const footer = takeSlot(this, "menu-footer");
337
+ const links = [...this.children].filter((c) => c.tagName === "A");
338
+ const header = document.createElement("header");
339
+ header.innerHTML = '<div class="k-navbar__inner"><a class="k-navbar__brand"></a><div class="k-navbar__actions"></div></div>';
340
+ const brandEl = header.querySelector(".k-navbar__brand");
341
+ brandEl.append(...brand);
342
+ const decorate = (a, cls) => {
343
+ a.className = cls;
344
+ const badge = a.dataset.badge;
345
+ if (!a.querySelector(":scope > span")) {
346
+ const s = document.createElement("span");
347
+ s.append(...[...a.childNodes].filter((n) => n.nodeName.toLowerCase() !== "svg"));
348
+ a.append(s);
349
+ }
350
+ a.querySelectorAll(":scope > svg").forEach((svg) => {
351
+ const w = document.createElement("span");
352
+ w.className = "k-navbar__icon";
353
+ w.setAttribute("aria-hidden", "true");
354
+ svg.replaceWith(w);
355
+ w.append(svg);
356
+ a.prepend(w);
357
+ });
358
+ if (badge && !a.querySelector(".k-navbar__badge"))
359
+ a.insertAdjacentHTML("beforeend", `<span class="k-navbar__badge">${esc(badge)}</span>`);
360
+ return a;
361
+ };
362
+ const actionsBox = header.querySelector(".k-navbar__actions");
363
+ actionsBox.append(...actions);
364
+ if (links.length) {
365
+ const nav = document.createElement("nav");
366
+ nav.className = "k-navbar__nav";
367
+ nav.innerHTML = '<span class="k-navbar__pill" aria-hidden="true"></span>';
368
+ const mobile = document.createElement("nav");
369
+ mobile.className = "k-navbar__mobile";
370
+ for (const a of links) {
371
+ nav.append(decorate(a, "k-navbar__link"));
372
+ mobile.append(decorate(a.cloneNode(true), "k-navbar__mobile-link"));
373
+ }
374
+ brandEl.after(nav);
375
+ this.nav = nav;
376
+ nav.addEventListener("pointerover", (e) => this.place(e.target.closest(".k-navbar__link")));
377
+ nav.addEventListener("focusin", (e) => this.place(e.target.closest(".k-navbar__link")));
378
+ nav.addEventListener("pointerleave", () => this.toCurrent());
379
+ nav.addEventListener("focusout", () => this.toCurrent());
380
+ actionsBox.insertAdjacentHTML("beforeend", `<button type="button" class="k-btn k-btn--tertiary k-btn--icon k-btn--sm k-navbar__menu" aria-haspopup="dialog" aria-expanded="false">${MENU}</button>`);
381
+ const drawer = document.createElement("kashi-drawer");
382
+ drawer.setAttribute("side", "start");
383
+ drawer.setAttribute("size", "sm");
384
+ drawer.dataset.drawerClass = "k-navbar__drawer";
385
+ drawer.append(mobile);
386
+ if (footer.length) {
387
+ const f = document.createElement("div");
388
+ f.slot = "footer";
389
+ f.style.display = "contents";
390
+ f.append(...footer);
391
+ drawer.append(f);
392
+ }
393
+ const btn = actionsBox.querySelector(".k-navbar__menu");
394
+ btn.addEventListener("click", () => drawer.show());
395
+ drawer.addEventListener("kashi-open", () => btn.setAttribute("aria-expanded", "true"));
396
+ drawer.addEventListener("kashi-close", () => btn.setAttribute("aria-expanded", "false"));
397
+ mobile.addEventListener("click", (e) => {
398
+ if (e.target.closest("a"))
399
+ drawer.close();
400
+ });
401
+ header.append(drawer);
402
+ this.drawer = drawer;
403
+ }
404
+ this.replaceChildren(header);
405
+ this.header = header;
406
+ }
407
+ place(el) {
408
+ const n = this.nav;
409
+ if (!n)
410
+ return;
411
+ if (!el)
412
+ return void n.style.setProperty("--_pill-o", "0");
413
+ n.style.setProperty("--_pill-x", `${el.offsetLeft}px`);
414
+ n.style.setProperty("--_pill-w", `${el.offsetWidth}px`);
415
+ n.style.setProperty("--_pill-o", "1");
416
+ }
417
+ toCurrent() {
418
+ this.place(this.nav?.querySelector('[aria-current="page"]') ?? null);
419
+ }
420
+ update() {
421
+ const h = this.header;
422
+ const sticky = bool(this, "sticky", true);
423
+ h.className = cx("k-navbar", sticky && "k-navbar--sticky", this.getAttribute("variant") === "pill" && "k-navbar--pill", bool(this, "centered") && "k-navbar--centered", bool(this, "floating") && "k-navbar--floating", bool(this, "transparent") && "k-navbar--transparent", `k-navbar--collapse-${this.getAttribute("collapse-below") ?? "md"}`);
424
+ h.querySelector(".k-navbar__brand").setAttribute("href", this.getAttribute("brand-href") ?? "/");
425
+ const M = msg();
426
+ const label = this.getAttribute("label") ?? M.navbar.label;
427
+ this.nav?.setAttribute("aria-label", label);
428
+ h.querySelector(".k-navbar__mobile")?.setAttribute("aria-label", label);
429
+ h.querySelector(".k-navbar__menu")?.setAttribute("aria-label", M.navbar.menu);
430
+ this.drawer?.setAttribute("heading", M.navbar.menuTitle);
431
+ this.toCurrent();
432
+ }
433
+ }
434
+ KashiNavbar.observedAttributes = ["brand-href", "sticky", "variant", "centered", "floating", "transparent", "collapse-below", "label"];
435
+ const BELL = icon('<path d="M5 8a5 5 0 0 1 10 0c0 4 1.5 5.5 1.5 5.5h-13S5 12 5 8ZM8.5 16.5a1.6 1.6 0 0 0 3 0"/>');
436
+ const GEAR = icon('<path d="M10 12.5a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5ZM16 10a6 6 0 0 0-.1-1l1.6-1.3-1.5-2.6-2 .7a6 6 0 0 0-1.7-1L12 2.8H9l-.4 2a6 6 0 0 0-1.7 1l-2-.7-1.5 2.6L5 9a6 6 0 0 0 0 2l-1.6 1.3 1.5 2.6 2-.7a6 6 0 0 0 1.7 1l.4 2h3l.4-2a6 6 0 0 0 1.7-1l2 .7 1.5-2.6L15.9 11c.1-.3.1-.7.1-1Z"/>');
437
+ const CHECK_ALL = icon('<path d="m2.5 10.5 3 3 6-7M9 13.2l.3.3 6-7"/>');
438
+ const day = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
439
+ /**
440
+ * فهرست اعلان‌ها با زبانه‌های «همه» و «خوانده‌نشده»، گروه امروز و دیروز و زمان نسبی؛ داده با items (JSON یا JavaScript).
441
+ * کلیک یا دکمهٔ نقطه اعلان را خوانده می‌کند (kashi-read)؛ kashi-select برای اعلان بدون href.
442
+ */
443
+ export class KashiNotificationList extends HTMLBase {
444
+ constructor() {
445
+ super(...arguments);
446
+ this.data = null;
447
+ this.tab = "all";
448
+ this.root = null;
449
+ }
450
+ connectedCallback() {
451
+ afterParse(() => {
452
+ if (!this.isConnected)
453
+ return;
454
+ if (!this.root) {
455
+ this.root = document.createElement("div");
456
+ this.root.className = "k-notif";
457
+ this.mount(this.root);
458
+ this.root.addEventListener("click", (e) => this.onClick(e));
459
+ }
460
+ this.render();
461
+ });
462
+ }
463
+ attributeChangedCallback(n, old, next) {
464
+ if (old === next)
465
+ return;
466
+ if (n === "items")
467
+ this.data = null;
468
+ if (this.root)
469
+ this.render();
470
+ }
471
+ mount(root) {
472
+ this.replaceChildren(root);
473
+ }
474
+ get items() {
475
+ return this.data ?? jsonAttr(this, "items", []);
476
+ }
477
+ set items(v) {
478
+ this.data = [...(v ?? [])];
479
+ if (this.root)
480
+ this.render();
481
+ }
482
+ /** تعداد اعلان‌های خوانده‌نشده */
483
+ get unread() {
484
+ return this.items.filter((x) => !x.read).length;
485
+ }
486
+ /** خوانده کردن یک اعلان */
487
+ markRead(id) {
488
+ this.data = this.items.map((x) => (x.id === id ? { ...x, read: true } : x));
489
+ emit(this, "kashi-read", { id });
490
+ this.render();
491
+ }
492
+ markAllRead() {
493
+ this.data = this.items.map((x) => ({ ...x, read: true }));
494
+ emit(this, "kashi-read-all", {});
495
+ this.render();
496
+ }
497
+ onClick(e) {
498
+ const t = e.target;
499
+ const tab = t.closest("[data-tab]");
500
+ if (tab) {
501
+ this.tab = tab.dataset.tab;
502
+ this.render();
503
+ this.root.querySelector(`[data-tab="${this.tab}"]`)?.focus();
504
+ return;
505
+ }
506
+ if (t.closest("[data-act=all]"))
507
+ return this.markAllRead();
508
+ if (t.closest("[data-act=settings]"))
509
+ return emit(this, "kashi-settings", {});
510
+ const dot = t.closest(".k-notif__dot");
511
+ if (dot)
512
+ return this.markRead(dot.closest("[data-id]").dataset.id);
513
+ const main = t.closest(".k-notif__main");
514
+ if (main) {
515
+ const id = main.closest("[data-id]").dataset.id;
516
+ const item = this.items.find((x) => x.id === id);
517
+ if (!item)
518
+ return;
519
+ emit(this, "kashi-select", { item });
520
+ if (!item.read)
521
+ this.markRead(id);
522
+ }
523
+ }
524
+ render() {
525
+ const M = msg(), N = M.notifications;
526
+ const items = this.items;
527
+ const unread = items.filter((x) => !x.read).length;
528
+ const list = this.tab === "unread" ? items.filter((x) => !x.read) : items;
529
+ const now = toDate(this.getAttribute("now")) ?? new Date();
530
+ const today = day(now);
531
+ const groups = [[N.today, []], [N.yesterday, []], [N.earlier, []]];
532
+ const sorted = [...list].sort((a, b) => (toDate(b.time)?.getTime() ?? 0) - (toDate(a.time)?.getTime() ?? 0));
533
+ for (const it of sorted) {
534
+ const d = day(toDate(it.time) ?? now);
535
+ groups[d >= today ? 0 : d >= today - 86400000 ? 1 : 2][1].push(it);
536
+ }
537
+ // فهرست دوباره ساخته می‌شود؛ فوکوس به همان دکمه (یا همان اعلان) برمی‌گردد
538
+ const active = this.root.contains(document.activeElement) ? document.activeElement : null;
539
+ const focusKey = active && {
540
+ id: active.closest("[data-id]")?.dataset.id,
541
+ tab: active.dataset.tab,
542
+ act: active.dataset.act,
543
+ };
544
+ const viewAll = this.getAttribute("view-all-href");
545
+ const row = (it) => {
546
+ const t = toDate(it.time) ?? now;
547
+ const inner = `${it.image ? `<span class="k-notif__media"${it.tone ? ` data-tone="${it.tone}"` : ""}><img src="${esc(it.image)}" alt=""></span>` : it.tone ? `<span class="k-notif__media" data-tone="${it.tone}">${BELL}</span>` : ""}<span class="k-notif__text"><span class="k-notif__item-title">${esc(it.title)}</span>${it.body ? `<span class="k-notif__body">${esc(it.body)}</span>` : ""}<time class="k-notif__time" datetime="${t.toISOString()}">${esc(formatRelative(t, { now, locale: lang(), digits: digitsOf(this) }))}</time></span>`;
548
+ return `<li class="k-notif__item" data-id="${esc(it.id)}"${it.read ? "" : " data-unread"}>${it.href ? `<a class="k-notif__main" href="${esc(it.href)}">${inner}</a>` : `<button type="button" class="k-notif__main">${inner}</button>`}${it.read ? "" : `<button type="button" class="k-notif__dot" aria-label="${esc(N.markRead)}" title="${esc(N.markRead)}"><span aria-hidden="true"></span></button>`}</li>`;
549
+ };
550
+ this.root.innerHTML = `<div class="k-notif__head"><h2 class="k-notif__title">${esc(this.getAttribute("heading") ?? N.label)}</h2>${unread > 0 ? `<button type="button" class="k-notif__link" data-act="all">${CHECK_ALL}${esc(N.markAll)}</button>` : ""}${bool(this, "settings") ? `<button type="button" class="k-notif__icon" data-act="settings" aria-label="${esc(N.settings)}">${GEAR}</button>` : ""}</div><div class="k-notif__tabs" role="group">${["all", "unread"].map((t) => `<button type="button" class="k-notif__tab" data-tab="${t}" aria-pressed="${this.tab === t}">${esc(N[t])}${t === "unread" && unread > 0 ? `<span class="k-notif__count">${num(this, unread)}</span>` : ""}</button>`).join("")}</div>${list.length === 0 ? `<div class="k-notif__empty"><span class="k-notif__empty-icon">${BELL}</span><p>${esc(this.tab === "unread" ? N.emptyUnread : N.empty)}</p></div>` : `<div class="k-notif__scroll" tabindex="-1">${groups.filter(([, g]) => g.length).map(([name, g]) => `<section class="k-notif__group" aria-label="${esc(name)}"><h3 class="k-notif__group-title">${esc(name)}</h3><ul class="k-notif__list">${g.map(row).join("")}</ul></section>`).join("")}</div>`}${viewAll ? `<a class="k-notif__all" href="${esc(viewAll)}">${esc(N.viewAll)}</a>` : ""}`;
551
+ if (focusKey) {
552
+ const r = this.root;
553
+ const el = focusKey.tab ? r.querySelector(`[data-tab="${focusKey.tab}"]`)
554
+ : focusKey.act ? r.querySelector(`[data-act="${focusKey.act}"]`)
555
+ : focusKey.id ? r.querySelector(`[data-id="${focusKey.id.replace(/["\\]/g, "\\$&")}"] .k-notif__main`) : null;
556
+ (el ?? r.querySelector(".k-notif__tab"))?.focus();
557
+ }
558
+ this.rendered(unread);
559
+ }
560
+ rendered(_unread) { }
561
+ }
562
+ KashiNotificationList.observedAttributes = ["items", "heading", "view-all-href", "settings", "now", "digits"];
563
+ /** دکمهٔ زنگ با شمار خوانده‌نشده‌ها که فهرست اعلان‌ها را در پاپ‌اور باز می‌کند؛ همان ویژگی‌های kashi-notification-list به‌اضافهٔ placement */
564
+ export class KashiNotificationCenter extends KashiNotificationList {
565
+ constructor() {
566
+ super(...arguments);
567
+ this.last = -1;
568
+ this.key = uid("kashi-notif");
569
+ this.onOutside = (e) => {
570
+ if (!this.contains(e.target))
571
+ this.close(false);
572
+ };
573
+ }
574
+ mount(root) {
575
+ const anchor = document.createElement("span");
576
+ anchor.className = "k-popover-anchor";
577
+ anchor.innerHTML = `<button type="button" class="k-btn k-btn--tertiary k-btn--icon k-notif__bell" aria-haspopup="dialog" aria-expanded="false" aria-controls="${this.key}">${BELL}</button><div id="${this.key}" role="dialog" tabindex="-1" hidden><div class="k-popover__body"></div></div>`;
578
+ this.bell = anchor.querySelector("button");
579
+ this.panel = anchor.querySelector("[role=dialog]");
580
+ this.panel.firstElementChild.append(root);
581
+ this.replaceChildren(anchor);
582
+ this.bell.addEventListener("click", () => (this.open ? this.close(false) : this.show()));
583
+ this.panel.addEventListener("keydown", (e) => {
584
+ if (e.key === "Escape") {
585
+ e.stopPropagation();
586
+ this.close(true);
587
+ }
588
+ });
589
+ this.panel.addEventListener("focusout", (e) => {
590
+ const next = e.relatedTarget;
591
+ if (next && !this.contains(next))
592
+ this.close(false);
593
+ });
594
+ }
595
+ disconnectedCallback() {
596
+ document.removeEventListener("pointerdown", this.onOutside);
597
+ }
598
+ get open() {
599
+ return !!this.panel && !this.panel.hidden;
600
+ }
601
+ set open(v) {
602
+ if (v)
603
+ this.show();
604
+ else
605
+ this.close(false);
606
+ }
607
+ show() {
608
+ if (!this.panel || this.open)
609
+ return;
610
+ this.panel.hidden = false;
611
+ this.bell.setAttribute("aria-expanded", "true");
612
+ (this.panel.querySelector(".k-notif__tab") ?? this.panel).focus();
613
+ document.addEventListener("pointerdown", this.onOutside);
614
+ emit(this, "kashi-open", {});
615
+ }
616
+ close(focus = true) {
617
+ if (!this.panel || this.panel.hidden)
618
+ return;
619
+ this.panel.hidden = true;
620
+ this.bell.setAttribute("aria-expanded", "false");
621
+ document.removeEventListener("pointerdown", this.onOutside);
622
+ if (focus)
623
+ this.bell.focus();
624
+ emit(this, "kashi-close", {});
625
+ }
626
+ rendered(unread) {
627
+ const M = msg(), N = M.notifications;
628
+ const [, align] = (this.getAttribute("placement") ?? "bottom-end").split("-");
629
+ this.panel.className = cx("k-popover", align === "end" && "k-popover--end", "k-notif-pop");
630
+ this.panel.setAttribute("aria-label", N.label);
631
+ this.bell.setAttribute("aria-label", unread ? N.button(num(this, unread)) : N.label);
632
+ const old = this.bell.querySelector(".k-notif__badge");
633
+ // با رسیدن اعلان تازه، شمارنده یک بار تکان می‌خورد (عنصر تازه یعنی انیمیشن تازه)
634
+ if (unread > 0 && (unread !== this.last || !old)) {
635
+ old?.remove();
636
+ this.bell.insertAdjacentHTML("beforeend", `<span class="k-notif__badge" aria-hidden="true">${unread > 99 ? `${num(this, 99)}+` : num(this, unread)}</span>`);
637
+ }
638
+ else if (!unread)
639
+ old?.remove();
640
+ this.last = unread;
641
+ }
642
+ }
643
+ KashiNotificationCenter.observedAttributes = [...KashiNotificationList.observedAttributes, "placement"];
644
+ define("kashi-drawer", KashiDrawer);
645
+ define("kashi-bottom-sheet", KashiBottomSheet);
646
+ define("kashi-navbar", KashiNavbar, "block");
647
+ define("kashi-notification-list", KashiNotificationList, "block");
648
+ define("kashi-notification-center", KashiNotificationCenter);
649
+ // دکمه‌هایی که بدون JavaScript کشو یا برگه را باز می‌کنند: data-kashi-open="شناسه"
650
+ if (typeof document !== "undefined") {
651
+ document.addEventListener("click", (e) => {
652
+ const opener = e.target?.closest?.("[data-kashi-open]");
653
+ if (!opener)
654
+ return;
655
+ const el = document.getElementById(opener.getAttribute("data-kashi-open"));
656
+ if (el instanceof KashiDrawer || el instanceof KashiBottomSheet) {
657
+ e.preventDefault();
658
+ el.show();
659
+ }
660
+ });
661
+ }