-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
225 lines (195 loc) · 7.14 KB
/
Copy pathapp.js
File metadata and controls
225 lines (195 loc) · 7.14 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
/**
* APP.JS
* ------
* 1. Builds the sidebar menu from QUIZ_REGISTRY (quiz-registry.js).
* 2. Handles hash-based routing (#quiz-01 etc.) so links, back/forward,
* and page refresh all work.
* 3. Loads the right quiz file and hands it to the renderer for its type.
*
* Adding renderers for new quiz types: see the RENDERERS object near the
* bottom — add a new key (matching the "type" field in the registry) with
* a function that takes (quizData, container) and fills the container.
*/
const contentArea = document.querySelector('.content-area');
const menuList = document.querySelector('.quiz-menu-list');
const sidebar = document.getElementById('sidebar');
const overlay = document.getElementById('sidebar-overlay');
const menuToggle = document.getElementById('menu-toggle');
/* ---------- Build the sidebar menu from the registry ---------- */
function buildMenu() {
menuList.innerHTML = '';
QUIZ_REGISTRY.forEach(quiz => {
const li = document.createElement('li');
if (quiz.disabled) {
li.innerHTML = `<span class="quiz-link disabled">${quiz.title} (Coming Soon)</span>`;
} else {
li.innerHTML = `<a href="#${quiz.id}" class="quiz-link">${quiz.title}</a>`;
}
menuList.appendChild(li);
});
}
/* ---------- Routing ---------- */
function getQuizFromHash() {
const id = window.location.hash.replace('#', '');
return QUIZ_REGISTRY.find(q => q.id === id && !q.disabled);
}
async function loadRoute() {
const quiz = getQuizFromHash();
if (!quiz) {
renderWelcome();
closeMenu();
return;
}
await loadQuiz(quiz);
closeMenu();
}
window.addEventListener('hashchange', loadRoute);
window.addEventListener('DOMContentLoaded', () => {
buildMenu();
loadRoute();
});
/* ---------- Loading + rendering a quiz ---------- */
async function loadQuiz(quiz) {
contentArea.innerHTML = '<p class="quiz-status">Loading…</p>';
try {
const response = await fetch(quiz.file);
if (!response.ok) throw new Error(`Could not load ${quiz.file}`);
const renderer = RENDERERS[quiz.type];
if (quiz.file.endsWith('.json')) {
if (!renderer) throw new Error(`No renderer registered for type "${quiz.type}"`);
const data = await response.json();
renderer(data, contentArea);
} else {
const html = await response.text();
contentArea.innerHTML = html;
// Scripts inserted via innerHTML do NOT execute automatically —
// recreate each <script> tag so the fragment's own JS actually runs.
contentArea.querySelectorAll('script').forEach(oldScript => {
const newScript = document.createElement('script');
if (oldScript.src) {
newScript.src = oldScript.src;
} else {
newScript.textContent = oldScript.textContent;
}
oldScript.replaceWith(newScript);
});
// HTML-fragment quizzes may define their own init function
// (e.g. window.initQuiz03(container)) — call it if present,
// passing the container so the quiz can scope its querySelectors
// instead of relying on page-wide unique ids.
const initFnName = `init${quiz.id.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())}`;
if (typeof window[initFnName] === 'function') window[initFnName](contentArea);
}
} catch (err) {
contentArea.innerHTML = `<p class="quiz-status">Sorry, this quiz couldn't be loaded.</p>`;
console.error(err);
}
}
function renderWelcome() {
contentArea.innerHTML = `
<div class="quiz-container">
<h1>Welcome to Learning Radar</h1>
<p>Pick a quiz from the menu to get started.</p>
</div>
`;
}
/* ---------- Renderers (one per quiz type) ---------- */
const RENDERERS = {
'multiple-choice': renderMultipleChoice,
// 'drag-drop' and 'hotspot' quizzes ship as ready-made HTML fragments
// (see loadQuiz above), so they don't need a renderer function here.
// If you later want a shared template for those types too, add
// renderer functions here the same way multiple-choice does it.
};
function renderMultipleChoice(data, container) {
let current = 0;
const answers = new Array(data.questions.length).fill(null);
function renderQuestion() {
const q = data.questions[current];
container.innerHTML = `
<div class="quiz-container">
<h1>${data.title}</h1>
<p class="quiz-progress">Question ${current + 1} of ${data.questions.length}</p>
<div class="quiz-content-area">
<h3>${q.question}</h3>
<ul class="options-list">
${q.options.map((opt, i) => `
<li>
<button class="btn option-btn" data-index="${i}">${opt}</button>
</li>
`).join('')}
</ul>
<p class="quiz-feedback" hidden></p>
</div>
<button class="btn next-btn" hidden>
${current === data.questions.length - 1 ? 'See Results' : 'Next Question'}
</button>
</div>
`;
const feedbackEl = container.querySelector('.quiz-feedback');
const nextBtn = container.querySelector('.next-btn');
const optionButtons = container.querySelectorAll('.option-btn');
optionButtons.forEach(btn => {
btn.addEventListener('click', () => {
if (answers[current] !== null) return; // already answered
const chosen = Number(btn.dataset.index);
answers[current] = chosen;
optionButtons.forEach(b => b.disabled = true);
if (chosen === q.correctIndex) {
btn.style.borderColor = 'green';
feedbackEl.textContent = 'Correct!';
} else {
btn.style.borderColor = 'crimson';
optionButtons[q.correctIndex].style.borderColor = 'green';
feedbackEl.textContent = 'Not quite — correct answer highlighted.';
}
feedbackEl.hidden = false;
nextBtn.hidden = false;
});
});
nextBtn.addEventListener('click', () => {
if (current < data.questions.length - 1) {
current++;
renderQuestion();
} else {
renderResults();
}
});
}
function renderResults() {
const score = answers.reduce(
(total, ans, i) => total + (ans === data.questions[i].correctIndex ? 1 : 0),
0
);
container.innerHTML = `
<div class="quiz-container">
<h1>${data.title} — Results</h1>
<p>You scored ${score} out of ${data.questions.length}.</p>
<button class="btn retry-btn">Try Again</button>
</div>
`;
container.querySelector('.retry-btn').addEventListener('click', () => {
current = 0;
answers.fill(null);
renderQuestion();
});
}
renderQuestion();
}
/* ---------- Sidebar open/close (unchanged behavior, now shared) ---------- */
function openMenu() {
sidebar.classList.add('open');
sidebar.setAttribute('aria-hidden', 'false');
overlay.hidden = false;
menuToggle.setAttribute('aria-expanded', 'true');
}
function closeMenu() {
sidebar.classList.remove('open');
sidebar.setAttribute('aria-hidden', 'true');
overlay.hidden = true;
menuToggle.setAttribute('aria-expanded', 'false');
}
menuToggle.addEventListener('click', () => {
sidebar.classList.contains('open') ? closeMenu() : openMenu();
});
overlay.addEventListener('click', closeMenu);