-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
479 lines (434 loc) · 17.5 KB
/
script.js
File metadata and controls
479 lines (434 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
(function () {
'use strict';
const tree = window.CONVERSATION_TREE || {};
const app = document.getElementById('app');
const STORAGE_KEY = 'purpose-of-life-state-v2';
const LANGUAGE_KEY = 'purpose-of-life-language-v1';
const BN_TRANSLATIONS = window.BN_TRANSLATIONS || {};
const UI_TEXT = {
start: { en: 'Start', bn: 'শুরু করুন' },
continuePrevious: { en: 'Continue Previous', bn: 'আগের জায়গা থেকে চলুন' },
mainMenu: { en: 'Main Menu', bn: 'মূল মেনু' },
back: { en: 'Back', bn: 'পেছনে' },
restart: { en: 'Restart', bn: 'আবার শুরু' },
reference: { en: 'Reference', bn: 'রেফারেন্স' },
copyReflection: { en: 'Copy Reflection', bn: 'লেখা কপি করুন' },
share: { en: 'Share', bn: 'শেয়ার করুন' },
step: { en: 'Step', bn: 'ধাপ' },
about: { en: 'About', bn: 'সম্পর্কে' },
backHome: { en: 'Back Home', bn: 'হোমে ফিরুন' },
startReflection: { en: 'Start Reflection', bn: 'ভাবনা শুরু করুন' },
startConfirm: {
en: 'Start a new reflection from the beginning? Your saved progress will be reset.',
bn: 'শুরু থেকে নতুনভাবে শুরু করবেন? আগের অগ্রগতি মুছে যাবে।'
},
restartConfirm: {
en: 'Restart this reflection from the beginning?',
bn: 'শুরু থেকে আবার শুরু করবেন?'
},
copied: { en: 'Reflection copied.', bn: 'লেখা কপি হয়েছে।' },
copyFailed: { en: 'Copy failed in this browser.', bn: 'এই ব্রাউজারে কপি করা যায়নি।' },
notConnected: { en: 'This path is not connected yet.', bn: 'এই পথটি এখনো যুক্ত করা হয়নি।' },
copyTitle: { en: 'Purpose of Life', bn: 'জীবনের উদ্দেশ্য' },
language: { en: 'Language', bn: 'ভাষা' }
};
let state = loadState();
let lang = loadLanguage();
let showingLanding = false;
function loadState() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return { currentId: null, history: [], started: false, tags: [] };
const parsed = JSON.parse(raw);
if (!parsed || !tree[parsed.currentId]) return { currentId: null, history: [], started: false, tags: [] };
return {
currentId: parsed.currentId,
history: Array.isArray(parsed.history) ? parsed.history.filter((id) => tree[id]) : [],
started: Boolean(parsed.started),
tags: Array.isArray(parsed.tags) ? [...new Set(parsed.tags.filter(Boolean))] : []
};
} catch (error) {
return { currentId: null, history: [], started: false, tags: [] };
}
}
function saveState() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
}
function loadLanguage() {
try {
const saved = localStorage.getItem(LANGUAGE_KEY);
if (saved === 'en' || saved === 'bn') return saved;
} catch (error) {
// Keep English if storage is blocked.
}
const browserLanguage = (navigator.language || '').toLowerCase();
return browserLanguage.startsWith('bn') ? 'bn' : 'en';
}
function saveLanguage() {
localStorage.setItem(LANGUAGE_KEY, lang);
}
function setLanguage(nextLang) {
if (nextLang !== 'en' && nextLang !== 'bn') return;
lang = nextLang;
saveLanguage();
render();
}
function t(value, selectedLang = lang) {
if (value && typeof value === 'object') {
return value[selectedLang] || value.en || value.bn || '';
}
if (selectedLang === 'bn') return BN_TRANSLATIONS[value] || value || '';
return value || '';
}
function ui(key) {
return t(UI_TEXT[key]);
}
function renderLanguageSwitcher() {
return `
<div class="language-switcher" role="group" aria-label="${escapeHtml(ui('language'))}">
<button class="language-option ${lang === 'en' ? 'active' : ''}" data-action="language" data-lang="en" aria-pressed="${lang === 'en'}">English</button>
<button class="language-option ${lang === 'bn' ? 'active' : ''}" data-action="language" data-lang="bn" aria-pressed="${lang === 'bn'}">বাংলা</button>
</div>
`;
}
function clearState() {
localStorage.removeItem(STORAGE_KEY);
state = { currentId: null, history: [], started: false, tags: [] };
}
function escapeHtml(value) {
return String(value || '')
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
function getNode() {
return tree[state.currentId] || null;
}
function startReflection(startId = 'start') {
showingLanding = false;
state = { currentId: startId, history: [], started: true, tags: [] };
saveState();
render();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function startNewReflection() {
const saved = loadState();
const hasSaved = Boolean(saved.started && saved.currentId);
if (hasSaved) {
const ok = confirm(ui('startConfirm'));
if (!ok) return;
}
startReflection('start');
}
function continuePreviousReflection() {
const saved = loadState();
if (!saved.started || !saved.currentId) {
renderLanding();
return;
}
showingLanding = false;
state = saved;
render();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function addTags(tags) {
if (!Array.isArray(tags) || !tags.length) return;
state.tags = [...new Set([...(state.tags || []), ...tags.filter(Boolean)])];
}
function goTo(nextId, tags = []) {
if (!tree[nextId]) {
showToast(ui('notConnected'));
return;
}
showingLanding = false;
addTags(tags);
if (state.currentId) state.history.push(state.currentId);
state.currentId = nextId;
state.started = true;
saveState();
render();
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function goBack() {
if (!state.history.length) {
showingLanding = true;
renderLanding();
return;
}
const previousId = state.history.pop();
state.currentId = previousId;
saveState();
render();
}
function restart() {
const ok = confirm(ui('restartConfirm'));
if (!ok) return;
startReflection('start');
}
function render() {
document.documentElement.lang = lang === 'bn' ? 'bn' : 'en';
if (showingLanding) {
renderLanding();
return;
}
if (!state.started || !state.currentId) {
renderLanding();
return;
}
renderConversation();
}
function renderLanding() {
showingLanding = true;
const saved = loadState();
const hasSaved = Boolean(saved.started && saved.currentId);
app.innerHTML = `
<section class="screen hero hero-mystery">
<div class="hero-card">
<div>
<div class="landing-topline">
<span class="kicker">${escapeHtml(t('A Quiet Question'))}</span>
${renderLanguageSwitcher()}
</div>
<h1>${escapeHtml(t('Purpose of Life'))}</h1>
<p>
${escapeHtml(t('Do not try to guess where this goes. Start with the question in front of you. Be honest with yourself. The next question will open from there.'))}
</p>
<div class="hero-actions">
<button class="btn btn-primary" data-action="start">${escapeHtml(ui('start'))}</button>
${hasSaved ? `<button class="btn btn-secondary" data-action="continue">${escapeHtml(ui('continuePrevious'))}</button>` : ''}
</div>
</div>
<p class="mystery-note">
${escapeHtml(t('No score. No timer. No one watching. Just you and your honest answers.'))}
</p>
</div>
<aside class="side-card mystery-card">
<div>
<span class="mystery-symbol">◇</span>
<h2>${escapeHtml(t('Before you start'))}</h2>
<p>
${escapeHtml(t('This works best when you answer without trying to impress anyone. Choose what you really think, even if you are unsure.'))}
</p>
</div>
<div class="quiet-rule">
${escapeHtml(t('Your answers shape the next question. Some questions may feel simple, but simple questions can show what a person is really living for.'))}
</div>
<div class="mystery-prompts">
<span>${escapeHtml(t('What are you looking for?'))}</span>
<span>${escapeHtml(t('What comes after that?'))}</span>
<span>${escapeHtml(t('What remains?'))}</span>
</div>
</aside>
</section>
`;
}
function landingPath(index, title, text) {
return `
<div class="path-item">
<span class="path-index">${index}</span>
<span><strong>${escapeHtml(title)}</strong><span>${escapeHtml(text)}</span></span>
</div>
`;
}
function renderAbout() {
app.innerHTML = `
<section class="screen about-card">
${renderLanguageSwitcher()}
<span class="node-label">${escapeHtml(ui('about'))}</span>
<h1>${escapeHtml(t('About Purpose of Life'))}</h1>
<p>
${escapeHtml(t('This is a quiet guided reflection. It asks one question at a time and adapts the next question based on your answer.'))}
</p>
<p>
${escapeHtml(t('There is no score and no timer. The path works best when you answer honestly, without trying to predict where it is going.'))}
</p>
<ul>
<li>${escapeHtml(t('It saves progress locally on your device.'))}</li>
<li>${escapeHtml(t('You can go back, restart, or return to the main menu.'))}</li>
<li>${escapeHtml(t('The first questions are universal and do not assume your background.'))}</li>
</ul>
<div class="result-actions">
<button class="btn btn-primary" data-action="start">${escapeHtml(ui('startReflection'))}</button>
<button class="btn btn-ghost" data-action="home">${escapeHtml(ui('backHome'))}</button>
</div>
</section>
`;
}
function renderConversation() {
showingLanding = false;
const node = getNode();
if (!node) {
clearState();
renderLanding();
return;
}
const step = state.history.length + 1;
const progress = Math.min(100, Math.max(7, Math.round((step / 18) * 100))); // slower progress feels more like a guided journey
const label = getThemeLabel(node.theme || node.type);
app.innerHTML = `
<section class="screen conversation-layout">
<article class="conversation-card">
<div>
<div class="topbar">
<div class="brand-mini"><span class="mark">◇</span>${escapeHtml(t('Purpose of Life'))}</div>
<div class="nav-actions">
${renderLanguageSwitcher()}
<button class="btn btn-ghost btn-small main-menu-button" data-action="home">← ${escapeHtml(ui('mainMenu'))}</button>
<button class="btn btn-ghost btn-small" data-action="back" ${state.history.length ? '' : 'disabled'}>${escapeHtml(ui('back'))}</button>
<button class="btn btn-ghost btn-small" data-action="restart">${escapeHtml(ui('restart'))}</button>
</div>
</div>
<div class="progress-wrap">
<div class="progress-meta">
<span>${escapeHtml(ui('step'))} ${step}</span>
<span>${escapeHtml(label)}</span>
</div>
<div class="progress-track"><div class="progress-fill" style="width: ${progress}%"></div></div>
</div>
<span class="node-label">${escapeHtml(label)}</span>
<h1 class="question-title">${escapeHtml(t(node.question || node.title))}</h1>
${node.description ? `<p class="node-description">${escapeHtml(t(node.description))}</p>` : ''}
${renderReference(node.reference)}
${renderNodeControls(node)}
</div>
</article>
</section>
`;
}
function renderReference(reference) {
if (!reference) return '';
return `
<div class="reference-card">
<strong>${escapeHtml(ui('reference'))}</strong>
<p>${escapeHtml(t(reference.text))}</p>
${reference.url ? `<a href="${escapeHtml(reference.url)}" target="_blank" rel="noreferrer">${escapeHtml(t(reference.source))} ↗</a>` : `<span>${escapeHtml(t(reference.source))}</span>`}
</div>
`;
}
function renderNodeControls(node) {
if (node.type === 'result') {
const actions = (node.actions || []).map(renderActionButton).join('');
return `
<div class="result-actions">${actions}</div>
<div class="utility-actions">
<button class="btn btn-ghost btn-small" data-action="copy">${escapeHtml(ui('copyReflection'))}</button>
<button class="btn btn-ghost btn-small" data-action="share">${escapeHtml(ui('share'))}</button>
</div>
`;
}
const answers = (node.answers || []).map((answer) => `
<button class="answer-btn" data-next-id="${escapeHtml(answer.nextId)}" data-tags="${escapeHtml((answer.tags || []).join(','))}">
<span>${escapeHtml(t(answer.label))}</span>
<span>→</span>
</button>
`).join('');
return `<div class="answers">${answers}</div>`;
}
function renderActionButton(action) {
if (action.type === 'external') {
const url = action.url === 'https://YOUR_ISLAM_QUIZ_LINK_HERE' ? window.ISLAM_QUIZ_URL : action.url;
return `<a class="btn btn-primary" href="${escapeHtml(url)}" target="_blank" rel="noreferrer">${escapeHtml(t(action.label))}</a>`;
}
if (action.type === 'restart') {
return `<button class="btn btn-secondary" data-action="restart">${escapeHtml(t(action.label))}</button>`;
}
return `<button class="btn btn-primary" data-next-id="${escapeHtml(action.nextId)}">${escapeHtml(t(action.label))}</button>`;
}
function getThemeLabel(theme) {
const labels = {
purpose: { en: 'Purpose', bn: 'উদ্দেশ্য' },
goals: { en: 'Goals', bn: 'লক্ষ্য' },
meaning: { en: 'Meaning', bn: 'মানে' },
death: { en: 'Death', bn: 'মৃত্যু' },
test: { en: 'Life Test', bn: 'জীবনের পরীক্ষা' },
happiness: { en: 'Happiness', bn: 'শান্তি' },
morality: { en: 'Morality', bn: 'ভালো-মন্দ' },
justice: { en: 'Justice', bn: 'ন্যায়' },
belief: { en: 'Belief', bn: 'বিশ্বাস' },
guidance: { en: 'Guidance', bn: 'পথনির্দেশ' },
islam: { en: 'Islam', bn: 'ইসলাম' },
tawhid: { en: 'Tawḥīd', bn: 'তাওহীদ' },
aqeedah: { en: 'Aqeedah', bn: 'আকীদাহ' },
iman: { en: 'Īmān', bn: 'ঈমান' },
afterlife: { en: 'Hereafter', bn: 'আখিরাত' },
hope: { en: 'Hope', bn: 'আশা' },
action: { en: 'Next Step', bn: 'পরবর্তী ধাপ' },
question: { en: 'Question', bn: 'প্রশ্ন' },
reflection: { en: 'Reflection', bn: 'ভাবনা' },
result: { en: 'Summary', bn: 'সারাংশ' }
};
return t(labels[theme] || labels.reflection);
}
function getReflectionText() {
const node = getNode();
if (!node) return '';
const lines = [
t(node.title || node.question || UI_TEXT.copyTitle),
'',
t(node.description || '')
];
if (node.reference) {
lines.push('', `${ui('reference')}: ${t(node.reference.text)} (${t(node.reference.source)})`);
}
return lines.join('\n').trim();
}
async function copyReflection() {
const text = getReflectionText();
if (!text) return;
try {
await navigator.clipboard.writeText(text);
showToast(ui('copied'));
} catch (error) {
showToast(ui('copyFailed'));
}
}
async function shareReflection() {
const text = getReflectionText();
if (!text) return;
if (navigator.share) {
try {
await navigator.share({ title: ui('copyTitle'), text });
return;
} catch (error) {
// User cancelled or browser blocked share. Fall back to copy.
}
}
copyReflection();
}
function showToast(message) {
const old = document.querySelector('.toast');
if (old) old.remove();
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
document.body.appendChild(toast);
window.setTimeout(() => toast.remove(), 2400);
}
function handleClick(event) {
const target = event.target.closest('[data-action], [data-next-id]');
if (!target) return;
const nextId = target.getAttribute('data-next-id');
const action = target.getAttribute('data-action');
const tags = (target.getAttribute('data-tags') || '').split(',').map((tag) => tag.trim()).filter(Boolean);
if (action === 'language') {
setLanguage(target.getAttribute('data-lang'));
return;
}
if (nextId) {
goTo(nextId, tags);
return;
}
if (action === 'start') startNewReflection();
if (action === 'continue') continuePreviousReflection();
if (action === 'about') renderAbout();
if (action === 'home') renderLanding();
if (action === 'back') goBack();
if (action === 'restart') restart();
if (action === 'copy') copyReflection();
if (action === 'share') shareReflection();
}
app.addEventListener('click', handleClick);
render();
})();