Asem75 commited on
Commit
39d421b
·
verified ·
1 Parent(s): 2d32db0

Create adhkar_reader.js

Browse files
Files changed (1) hide show
  1. adhkar_reader.js +120 -0
adhkar_reader.js ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ ساعة المسلم — قراءة الأذكار (الصباح والمساء والنوم).
3
+ - نصوص الأذكار من adhkar_reader_registry.json، ونص الآيات (quran_ref) من بيانات القرآن نفسها.
4
+ - مصمَّمة لقارئ الشاشة: عناوين وفقرات عادية، النص حقيقي وليس aria-label، بلا أزرار أو تركيز يقطع القراءة.
5
+ - للقراءة فقط: لا يوجد تتبّع إنجاز.
6
+ */
7
+ (function () {
8
+ 'use strict';
9
+ const base = new URL('.', document.currentScript.src);
10
+ let configPromise;
11
+ const node = (tag, text, cls) => { const n = document.createElement(tag); if (text != null) n.textContent = text; if (cls) n.className = cls; return n; };
12
+ const button = (text, action) => { const b = node('button', text); b.type = 'button'; b.addEventListener('click', action); return b; };
13
+ const fmt = (s, v) => String(s).replace(/\{(\w+)\}/g, (_, k) => String(v[k] ?? ''));
14
+
15
+ function config() {
16
+ if (!configPromise) configPromise = (async () => {
17
+ const ctl = new AbortController(), t = setTimeout(() => ctl.abort(), 15000);
18
+ try {
19
+ const r = await fetch(new URL('adhkar_reader_registry.json', base), { cache: 'no-store', signal: ctl.signal });
20
+ if (!r.ok) throw Error('adhkar_reader_registry.json HTTP ' + r.status);
21
+ const c = await r.json();
22
+ if (c.version !== 1 || !c.ui || !Array.isArray(c.categories) || !c.categories.length) throw Error('Invalid adhkar registry');
23
+ for (const cat of c.categories) {
24
+ if (!cat.id || !cat.title || !Array.isArray(cat.items)) throw Error('Invalid category');
25
+ for (const it of cat.items) if (!(it.text || it.quran_ref) || !Number.isInteger(it.count) || it.count < 1) throw Error('Invalid dhikr in ' + cat.id);
26
+ }
27
+ return c;
28
+ } finally { clearTimeout(t); }
29
+ })().catch(e => { configPromise = null; throw e; });
30
+ return configPromise;
31
+ }
32
+ const timesText = (ui, n) => n === 1 ? ui.once : n === 2 ? ui.twice : n <= 10 ? fmt(ui.few, { n }) : fmt(ui.many, { n });
33
+
34
+ // "2:285-286" -> [[2,285],[2,286]]
35
+ function parseRef(ref) {
36
+ const m = /^(\d{1,3}):(\d{1,3})(?:-(\d{1,3}))?$/.exec(ref); if (!m) return [];
37
+ const out = []; for (let a = Number(m[2]); a <= Number(m[3] || m[2]); a++) out.push(m[1] + ':' + a); return out;
38
+ }
39
+ // Verses come from the Quran data; the spoken text follows the reader's setting for screen readers.
40
+ async function quranParts(ref) {
41
+ const q = window.MuslimClockQuran; if (!q?.loadData) throw Error('quran');
42
+ const source = await q.loadData(), speech = q.speechInfo ? await q.speechInfo() : { mode: 'original', visible: false, original: true, convert: t => t };
43
+ const texts = parseRef(ref).map(k => source.byKey.get(k)?.text);
44
+ if (!texts.length || texts.some(t => !t)) throw Error('verse');
45
+ return { shown: texts.join(' '), spoken: speech.convert(texts.join(' ')), speech };
46
+ }
47
+ function putText(host, shown, spoken, speech) {
48
+ host.lang = 'ar'; host.dir = 'rtl';
49
+ if (speech.visible) host.append(document.createTextNode(spoken));
50
+ else if (speech.original) host.append(document.createTextNode(shown));
51
+ else { const s = node('span', shown); s.setAttribute('aria-hidden', 'true'); host.append(s, node('span', spoken, 'quran-sr-only')); }
52
+ }
53
+ function suggested(c, hour = new Date().getHours()) {
54
+ return c.categories.find(cat => Number.isInteger(cat.from_hour) && Number.isInteger(cat.to_hour) &&
55
+ (cat.from_hour <= cat.to_hour ? hour >= cat.from_hour && hour < cat.to_hour : hour >= cat.from_hour || hour < cat.to_hour));
56
+ }
57
+ function panel(ui, root, back) {
58
+ RegistryApp.openPanel('worship', { title: ui.title, compact: true }, () => {
59
+ document.getElementById('adaptivePopupBody').append(root);
60
+ if (back) document.getElementById('adaptivePopupActions').append(button(ui.back, back));
61
+ });
62
+ const card = document.getElementById('adaptivePopupCard'); if (card) card.dataset.size = 'long';
63
+ }
64
+ const landOn = h => { h.tabIndex = -1; setTimeout(() => h.focus(), 60); };
65
+
66
+ async function open(options = {}) {
67
+ let c;
68
+ try { c = await config(); }
69
+ catch (e) { const root = node('div', 'adhkar_reader_registry.json: ' + e.message); root.append(button('↻', () => open(options))); panel({ title: 'adhkar_reader_registry.json', back: '←' }, root, options.back); return; }
70
+ const ui = c.ui, root = node('div', null, 'adhkar-reader'), view = node('div');
71
+ const status = node('p'); status.setAttribute('role', 'status'); status.setAttribute('aria-live', 'polite');
72
+ root.append(view, status);
73
+
74
+ function list(land = true) {
75
+ view.replaceChildren();
76
+ const h = node('h3', ui.title); view.append(h, node('p', ui.intro));
77
+ const s = suggested(c);
78
+ if (s) { const b = button(fmt(ui.suggest, { title: s.title }), () => category(s.id)); view.append(b); }
79
+ for (const cat of c.categories) {
80
+ const b = button((cat.emoji ? cat.emoji + ' ' : '') + cat.title, () => category(cat.id));
81
+ b.setAttribute('aria-label', cat.title + ' — ' + fmt(ui.count_items, { n: cat.items.length }));
82
+ view.append(b);
83
+ }
84
+ if (land) landOn(h);
85
+ }
86
+ async function category(id) {
87
+ const cat = c.categories.find(x => x.id === id); if (!cat) return;
88
+ view.replaceChildren();
89
+ const h = node('h3', cat.title); view.append(h);
90
+ if (cat.time_hint) view.append(node('p', cat.time_hint));
91
+ view.append(node('p', fmt(ui.category_note, { n: cat.items.length })));
92
+ const n = cat.items.length;
93
+ for (let i = 0; i < n; i++) {
94
+ const it = cat.items[i], art = node('article', null, 'adhkar-item');
95
+ art.style.cssText = 'border:1px solid var(--cell-border,#888);border-radius:12px;padding:10px;margin:10px 0';
96
+ art.append(node('h4', fmt(ui.item_title, { i: i + 1, n })));
97
+ const text = node('p', null, 'adhkar-text'); text.style.cssText = 'font-size:1.15em;line-height:2';
98
+ if (it.quran_ref) {
99
+ try { const part = await quranParts(it.quran_ref); putText(text, part.shown, part.spoken, part.speech); }
100
+ catch { text.textContent = fmt(ui.quran_error, { ref: it.quran_ref }); }
101
+ } else { text.lang = 'ar'; text.dir = 'rtl'; text.textContent = it.text; }
102
+ art.append(text, node('p', fmt(ui.count, { times: timesText(ui, it.count) })));
103
+ if (it.source) art.append(node('p', fmt(ui.source, { source: it.source }), 'quran-meta'));
104
+ if (it.note) art.append(node('p', fmt(ui.note, { note: it.note }), 'quran-meta'));
105
+ view.append(art);
106
+ }
107
+ view.append(button(ui.to_list, () => list()));
108
+ landOn(h);
109
+ }
110
+ panel(ui, root, options.back);
111
+ if (options.category) await category(options.category); else list();
112
+ }
113
+
114
+ async function mountEntry(parent, back) {
115
+ const entry = button('…', () => {}); entry.disabled = true; parent.append(entry);
116
+ try { const c = await config(); if (!entry.isConnected) return; entry.textContent = c.ui.open; entry.disabled = false; entry.onclick = () => open({ back }); }
117
+ catch (e) { if (entry.isConnected) { entry.textContent = 'adhkar_reader_registry.json: ' + e.message; entry.disabled = false; entry.onclick = () => { entry.remove(); mountEntry(parent, back); }; } }
118
+ }
119
+ window.MuslimClockAdhkarReader = Object.freeze({ open, mountEntry, categories: async () => (await config()).categories });
120
+ })();