@allons-y/course-kit 0.0.0-stage → 0.2.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/LICENSE +373 -0
- package/README.md +90 -2
- package/assets/fonts/instrument-sans-italic-var.woff2 +0 -0
- package/assets/fonts/instrument-sans-var.woff2 +0 -0
- package/assets/fonts/pt-mono.woff2 +0 -0
- package/assets/scripts/copy.js +99 -0
- package/assets/scripts/deck.js +161 -0
- package/assets/scripts/email.js +19 -0
- package/assets/scripts/practice.js +545 -0
- package/assets/scripts/theme-panel.js +647 -0
- package/assets/scripts/theme-restore.js +17 -0
- package/assets/styles/base.css +2091 -0
- package/assets/styles/deck.css +968 -0
- package/assets/styles/font.css +36 -0
- package/assets/styles/notes.css +137 -0
- package/assets/styles/print.css +428 -0
- package/assets/styles/semantics.css +101 -0
- package/components/practice/check-controls.webc +50 -0
- package/components/practice/choice-list.webc +86 -0
- package/components/practice/cite-panel.webc +108 -0
- package/components/practice/practice-figure.webc +47 -0
- package/components/practice/practice-fill.webc +154 -0
- package/components/practice/practice-grid.webc +82 -0
- package/components/practice/practice-judge.webc +165 -0
- package/components/practice/practice-match.webc +178 -0
- package/components/practice/practice-question.webc +141 -0
- package/components/practice/practice-selfcheck.webc +40 -0
- package/components/practice/practice-terms.webc +95 -0
- package/components/practice/practice-tree.webc +91 -0
- package/components/practice/practice-worksheet.webc +369 -0
- package/components/site/copyright.webc +13 -0
- package/components/site/course-documents.webc +27 -0
- package/components/site/course-now.webc +393 -0
- package/components/site/course-summary.webc +136 -0
- package/components/site/demo-weeks.webc +91 -0
- package/components/site/glossary-entry.webc +31 -0
- package/components/site/glossary-panel.webc +1482 -0
- package/components/site/glossary.webc +45 -0
- package/components/site/grading.webc +50 -0
- package/components/site/instructor.webc +432 -0
- package/components/site/masthead.webc +147 -0
- package/components/site/nav-links.webc +174 -0
- package/components/site/policies.webc +17 -0
- package/components/site/practice-index.webc +31 -0
- package/components/site/schedule-overview.webc +145 -0
- package/components/site/schedule-weeks.webc +589 -0
- package/components/site/site-footer.webc +119 -0
- package/components/site/slide-timetable.webc +152 -0
- package/components/site/syllabus.webc +370 -0
- package/components/ui/arrow-link.webc +19 -0
- package/components/ui/assigned-list.webc +101 -0
- package/components/ui/callout.webc +11 -0
- package/components/ui/card-list.webc +124 -0
- package/components/ui/codepen-embed.webc +123 -0
- package/components/ui/download-button.webc +49 -0
- package/components/ui/eyebrow.webc +18 -0
- package/components/ui/icons/github.webc +6 -0
- package/components/ui/icons/google.webc +9 -0
- package/components/ui/link-list.webc +130 -0
- package/components/ui/link-section.webc +40 -0
- package/components/ui/meeting-card.webc +73 -0
- package/components/ui/meeting-decks.webc +38 -0
- package/components/ui/table-rubric.webc +251 -0
- package/components/ui/today-link.webc +43 -0
- package/components/ui/today-marker.webc +149 -0
- package/components/ui/week-block.webc +53 -0
- package/examples/starter/_data/course.json +20 -0
- package/examples/starter/_data/dates.json +16 -0
- package/examples/starter/_data/grading.json +6 -0
- package/examples/starter/_data/instructor.json +7 -0
- package/examples/starter/_data/policies.json +9 -0
- package/examples/starter/_data/scale.json +8 -0
- package/examples/starter/_data/schedule.json +22 -0
- package/examples/starter/_data/theme.json +17 -0
- package/examples/starter/content/index.webc +12 -0
- package/examples/starter/content/schedule.webc +12 -0
- package/examples/starter/content/slides/index.11tydata.js +5 -0
- package/examples/starter/content/slides/index.webc +12 -0
- package/examples/starter/content/slides/week-1-day-1.md +40 -0
- package/examples/starter/eleventy.config.js +8 -0
- package/glue/deck-notes.data.js +41 -0
- package/glue/deck-notes.webc +32 -0
- package/glue/lesson-day.data.js +17 -0
- package/glue/lesson-day.webc +0 -0
- package/glue/lesson-plans.data.js +13 -0
- package/glue/lesson-plans.webc +2 -0
- package/index.js +6 -0
- package/layouts/base.webc +79 -0
- package/layouts/deck.webc +46 -0
- package/layouts/demo.webc +16 -0
- package/layouts/document.webc +10 -0
- package/layouts/lesson-detail.webc +109 -0
- package/layouts/lesson.webc +71 -0
- package/layouts/notes.webc +23 -0
- package/layouts/worksheet.webc +52 -0
- package/lib/assets.js +42 -0
- package/lib/bundles.js +79 -0
- package/lib/check.js +110 -0
- package/lib/components.js +19 -0
- package/lib/copy.js +656 -0
- package/lib/data/build.js +22 -0
- package/lib/data/fonts.js +16 -0
- package/lib/data/glossary.js +304 -0
- package/lib/data/index.js +8 -0
- package/lib/data/lessons.js +173 -0
- package/lib/data/project.js +22 -0
- package/lib/data/read.js +61 -0
- package/lib/data/site.js +70 -0
- package/lib/data/term.js +80 -0
- package/lib/extensions.js +70 -0
- package/lib/files.js +14 -0
- package/lib/filters.js +584 -0
- package/lib/fit.js +106 -0
- package/lib/glue.js +26 -0
- package/lib/lesson-materials.js +66 -0
- package/lib/markdown.js +568 -0
- package/lib/minify.js +53 -0
- package/lib/paths.js +5 -0
- package/lib/pdf.js +134 -0
- package/lib/plugin.js +231 -0
- package/lib/plugins.js +34 -0
- package/lib/postcss.js +93 -0
- package/lib/theme-server.js +88 -0
- package/lib/theme.js +185 -0
- package/lib/worksheet-dialect.js +415 -0
- package/lib/worksheets.js +276 -0
- package/package.json +119 -4
- package/schemas/course.schema.json +161 -0
- package/schemas/dates.schema.json +165 -0
- package/schemas/grading.schema.json +59 -0
- package/schemas/instructor.schema.json +76 -0
- package/schemas/policies.schema.json +90 -0
- package/schemas/project.schema.json +209 -0
- package/schemas/scale.schema.json +68 -0
- package/schemas/schedule.schema.json +260 -0
- package/schemas/theme.schema.json +704 -0
- package/schemas/worksheet.schema.json +449 -0
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Grading for a practice worksheet page (see docs/components.md).
|
|
3
|
+
*
|
|
4
|
+
* Progressive enhancement: the page is a complete, printable worksheet with
|
|
5
|
+
* the script off — every control is a real input, and the answers sit behind
|
|
6
|
+
* the "Show the answer" disclosures. This file only adds the checking.
|
|
7
|
+
*
|
|
8
|
+
* The expected answers are read from the rendered markup rather than a second
|
|
9
|
+
* copy of the data: each question's `data-answers` attribute is written by the
|
|
10
|
+
* template from the same object the questions come from.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { t } = window.ckCopy;
|
|
14
|
+
|
|
15
|
+
/** Strip the decoration students may or may not type: tags, punctuation, case. */
|
|
16
|
+
const normalize = (value) =>
|
|
17
|
+
value
|
|
18
|
+
.toLowerCase()
|
|
19
|
+
.replaceAll(/[<>/]/g, "")
|
|
20
|
+
.replaceAll(/[·,;|]+/g, " ")
|
|
21
|
+
.replaceAll(/\s+/g, " ")
|
|
22
|
+
.trim();
|
|
23
|
+
|
|
24
|
+
/* eleventy.worksheets.js answers a built page from its own key and has to
|
|
25
|
+
compare the way the page does. One copy of the rule, exercised by both. */
|
|
26
|
+
window.practice = { normalize };
|
|
27
|
+
|
|
28
|
+
/** The worksheet a control or question belongs to. */
|
|
29
|
+
const rootOf = (element) => element.closest("[data-worksheet]");
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {HTMLElement} root a `[data-worksheet]` element
|
|
33
|
+
* @param {string} scope a CSS selector for the region to grade
|
|
34
|
+
*/
|
|
35
|
+
const questionsIn = (root, scope) => {
|
|
36
|
+
// The whole-worksheet scope names the root itself, which querySelector never returns.
|
|
37
|
+
const region = root.matches(scope) ? root : root.querySelector(scope);
|
|
38
|
+
return [...(region?.querySelectorAll(".question") ?? [])];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** The checked radio in a group, or null. */
|
|
42
|
+
const checkedValue = (question, name) => {
|
|
43
|
+
const input = question.querySelector(`input[name="${name}"]:checked`);
|
|
44
|
+
return input ? input.value : null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Mark an element for the eye as well as the tally. */
|
|
48
|
+
const mark = (element, correct) => {
|
|
49
|
+
if (!element) return;
|
|
50
|
+
element.dataset.mark = correct ? "right" : "wrong";
|
|
51
|
+
const key = element.querySelector(".answer-key");
|
|
52
|
+
if (key) key.hidden = correct;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const clearMarks = (question) => {
|
|
56
|
+
for (const marked of question.querySelectorAll("[data-mark]")) delete marked.dataset.mark;
|
|
57
|
+
for (const key of question.querySelectorAll(".answer-key")) key.hidden = true;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Question kinds register themselves: each kind's component carries a script that sets
|
|
62
|
+
* `window.ckPractice.kinds[kind] = { grade(question, key, id, helpers), enhance?(question, helpers) }`.
|
|
63
|
+
* This script reads the registry when it needs it (at an event, and once the page has loaded), so it does not matter which script loads first.
|
|
64
|
+
* A kind with no `grade`, like `selfcheck`, is left to the student.
|
|
65
|
+
*/
|
|
66
|
+
const registry = (window.ckPractice ??= { kinds: {} });
|
|
67
|
+
const kinds = () => registry.kinds;
|
|
68
|
+
|
|
69
|
+
/* ---- Status ------------------------------------------------------------
|
|
70
|
+
* unattempted | answered | right | wrong | partial | self, on `data-state`
|
|
71
|
+
* of the question and of its entry in the question list. */
|
|
72
|
+
|
|
73
|
+
const STATUS_KEY = {
|
|
74
|
+
unattempted: "statusUnattempted",
|
|
75
|
+
answered: "statusAnswered",
|
|
76
|
+
right: "statusRight",
|
|
77
|
+
wrong: "statusWrong",
|
|
78
|
+
partial: "statusPartial",
|
|
79
|
+
self: "statusSelf",
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const isGradable = (question) => typeof kinds()[question.dataset.kind]?.grade === "function";
|
|
83
|
+
|
|
84
|
+
/** Whether any part of the question holds a value. */
|
|
85
|
+
const hasValue = (question) =>
|
|
86
|
+
[...question.querySelectorAll("input[type='radio']")].some((input) => input.checked) ||
|
|
87
|
+
[...question.querySelectorAll("input[type='text'], select")].some((control) => control.value.trim() !== "");
|
|
88
|
+
|
|
89
|
+
/** @param {HTMLElement} question @param {string} state */
|
|
90
|
+
const setState = (question, state) => {
|
|
91
|
+
question.dataset.state = state;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** Remove the feedback line, marks and revealed reasoning of a question. */
|
|
95
|
+
function clearFeedback(question) {
|
|
96
|
+
clearMarks(question);
|
|
97
|
+
const feedback = question.querySelector(".feedback");
|
|
98
|
+
feedback.hidden = true;
|
|
99
|
+
feedback.textContent = "";
|
|
100
|
+
question.querySelector(".why").hidden = true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Grade one question: mark its controls, write its feedback line, set its
|
|
105
|
+
* status. Questions with no grader are left to the student.
|
|
106
|
+
* @param {HTMLElement} question
|
|
107
|
+
* @returns {"right"|"wrong"|"partial"|"unattempted"|"self"}
|
|
108
|
+
*/
|
|
109
|
+
function check(question) {
|
|
110
|
+
if (!isGradable(question)) {
|
|
111
|
+
const reveal = question.querySelector("[data-reveal]");
|
|
112
|
+
if (reveal) reveal.open = true;
|
|
113
|
+
return "self";
|
|
114
|
+
}
|
|
115
|
+
const { kind, question: id } = question.dataset;
|
|
116
|
+
clearMarks(question);
|
|
117
|
+
const { answered, right, total, blank } = kinds()[kind].grade(question, JSON.parse(question.dataset.answers ?? "{}"), id, helpers);
|
|
118
|
+
const feedback = question.querySelector(".feedback");
|
|
119
|
+
const why = question.querySelector(".why");
|
|
120
|
+
feedback.hidden = false;
|
|
121
|
+
|
|
122
|
+
if (!answered) {
|
|
123
|
+
setState(question, "unattempted");
|
|
124
|
+
feedback.textContent = t("worksheet", "feedbackUnattempted");
|
|
125
|
+
why.hidden = true;
|
|
126
|
+
return "unattempted";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const wrong = total - right - blank > 0;
|
|
130
|
+
const state = right === total ? "right" : wrong ? "wrong" : "partial";
|
|
131
|
+
setState(question, state);
|
|
132
|
+
feedback.textContent =
|
|
133
|
+
state === "right"
|
|
134
|
+
? total > 1
|
|
135
|
+
? t("worksheet", "feedbackRightAll", { total })
|
|
136
|
+
: t("worksheet", "feedbackRight")
|
|
137
|
+
: t("worksheet", "feedbackParts", { right, total }) +
|
|
138
|
+
(blank > 0 ? t("common", "sep") + t("worksheet", "feedbackBlank", { count: blank }) : "");
|
|
139
|
+
why.hidden = false;
|
|
140
|
+
return state;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Clear one question back to unattempted. */
|
|
144
|
+
function reset(question) {
|
|
145
|
+
clearFeedback(question);
|
|
146
|
+
for (const input of question.querySelectorAll("input[type='radio']")) input.checked = false;
|
|
147
|
+
for (const input of question.querySelectorAll("input[type='text']")) input.value = "";
|
|
148
|
+
for (const select of question.querySelectorAll("select")) select.value = "";
|
|
149
|
+
for (const row of question.querySelectorAll("[data-dropped], [data-over]")) {
|
|
150
|
+
delete row.dataset.dropped;
|
|
151
|
+
delete row.dataset.over;
|
|
152
|
+
}
|
|
153
|
+
markUsedWords(question);
|
|
154
|
+
for (const details of question.querySelectorAll("[data-reveal]")) details.open = false;
|
|
155
|
+
setState(question, isGradable(question) ? "unattempted" : "self");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Mirror each question's status onto its entry in the question list. */
|
|
159
|
+
function syncNav(root) {
|
|
160
|
+
for (const question of questionsIn(root, "#worksheet")) {
|
|
161
|
+
const item = root.querySelector(`[data-nav-for="${question.dataset.question}"]`);
|
|
162
|
+
if (!item) continue;
|
|
163
|
+
const state = question.dataset.state ?? "unattempted";
|
|
164
|
+
item.dataset.state = state;
|
|
165
|
+
const status = item.querySelector(".nav-status");
|
|
166
|
+
if (status) status.textContent = t("worksheet", STATUS_KEY[state]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* ---- Saved answers ------------------------------------------------------
|
|
171
|
+
* localStorage, one key per question: `ck-ws:v1:<storage-id>:<question-id>`.
|
|
172
|
+
* Every access is in try/catch; with storage unavailable (a private window,
|
|
173
|
+
* a file:// copy, an upload to Brightspace) the page works and saves nothing. */
|
|
174
|
+
|
|
175
|
+
const PREFIX = "ck-ws:v1:";
|
|
176
|
+
|
|
177
|
+
/** djb2, base 36: enough to notice that a question's key changed. */
|
|
178
|
+
const hashOf = (text) => {
|
|
179
|
+
let hash = 5381;
|
|
180
|
+
for (const char of text) hash = ((hash << 5) + hash + char.codePointAt(0)) | 0;
|
|
181
|
+
return (hash >>> 0).toString(36);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const storageKey = (root, question) => `${PREFIX}${root.dataset.storageId}:${question.dataset.question}`;
|
|
185
|
+
|
|
186
|
+
const storageGet = (key) => {
|
|
187
|
+
try {
|
|
188
|
+
return localStorage.getItem(key);
|
|
189
|
+
} catch {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const storageSet = (key, value) => {
|
|
194
|
+
try {
|
|
195
|
+
localStorage.setItem(key, value);
|
|
196
|
+
} catch {
|
|
197
|
+
// Storage is unavailable or full: nothing is saved.
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const storageRemove = (key) => {
|
|
201
|
+
try {
|
|
202
|
+
localStorage.removeItem(key);
|
|
203
|
+
} catch {
|
|
204
|
+
// Nothing to remove.
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/** The question's current answers: radios by `name`, selects and texts by blank. */
|
|
209
|
+
function collect(question) {
|
|
210
|
+
const answers = {};
|
|
211
|
+
for (const input of question.querySelectorAll("input[type='radio']:checked")) answers[input.name] = input.value;
|
|
212
|
+
for (const [tag, selector] of [
|
|
213
|
+
["select", "select"],
|
|
214
|
+
["text", "input[type='text']"],
|
|
215
|
+
]) {
|
|
216
|
+
for (const [index, control] of [...question.querySelectorAll(selector)].entries()) {
|
|
217
|
+
if (control.value.trim() !== "") answers[`${tag}:${control.dataset.blank ?? index}`] = control.value;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return answers;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Put saved answers back into the question's controls. */
|
|
224
|
+
function apply(question, answers) {
|
|
225
|
+
for (const [key, value] of Object.entries(answers)) {
|
|
226
|
+
const [tag, blank] = key.split(":");
|
|
227
|
+
if (blank === undefined) {
|
|
228
|
+
const input = [...question.querySelectorAll("input[type='radio']")].find((item) => item.name === key && item.value === value);
|
|
229
|
+
if (input) input.checked = true;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const selector = tag === "select" ? "select" : "input[type='text']";
|
|
233
|
+
const controls = [...question.querySelectorAll(selector)];
|
|
234
|
+
const control = controls.find((item) => item.dataset.blank === blank) ?? controls[Number(blank)];
|
|
235
|
+
if (control) control.value = value;
|
|
236
|
+
}
|
|
237
|
+
markUsedWords(question);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Save a question, or drop its key when it is empty. */
|
|
241
|
+
function save(root, question, checked = false) {
|
|
242
|
+
const answers = collect(question);
|
|
243
|
+
const key = storageKey(root, question);
|
|
244
|
+
if (!Object.keys(answers).length) return storageRemove(key);
|
|
245
|
+
storageSet(key, JSON.stringify({ v: 1, h: hashOf(question.dataset.answers ?? ""), t: Date.now(), checked, a: answers }));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Restore a worksheet's saved answers. @returns {number} the newest save time, or 0 */
|
|
249
|
+
function restore(root) {
|
|
250
|
+
let newest = 0;
|
|
251
|
+
for (const question of questionsIn(root, "#worksheet")) {
|
|
252
|
+
if (!isGradable(question)) continue;
|
|
253
|
+
const key = storageKey(root, question);
|
|
254
|
+
const raw = storageGet(key);
|
|
255
|
+
if (raw === null) continue;
|
|
256
|
+
let saved;
|
|
257
|
+
try {
|
|
258
|
+
saved = JSON.parse(raw);
|
|
259
|
+
} catch {
|
|
260
|
+
saved = null;
|
|
261
|
+
}
|
|
262
|
+
if (saved?.v !== 1 || saved.h !== hashOf(question.dataset.answers ?? "") || typeof saved.a !== "object") {
|
|
263
|
+
storageRemove(key);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
apply(question, saved.a);
|
|
267
|
+
newest = Math.max(newest, saved.t ?? 0);
|
|
268
|
+
if (saved.checked) check(question);
|
|
269
|
+
else setState(question, hasValue(question) ? "answered" : "unattempted");
|
|
270
|
+
}
|
|
271
|
+
return newest;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Old-shape keys are ignored and pruned. */
|
|
275
|
+
function prune() {
|
|
276
|
+
try {
|
|
277
|
+
for (const key of Object.keys(localStorage)) {
|
|
278
|
+
if (key.startsWith("ck-ws:") && !key.startsWith(PREFIX)) localStorage.removeItem(key);
|
|
279
|
+
}
|
|
280
|
+
} catch {
|
|
281
|
+
// No storage, nothing to prune.
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/* ---- Scores and summary -------------------------------------------------- */
|
|
286
|
+
|
|
287
|
+
const tallyFor = (root, scope) => root.querySelector(`[data-tally="${scope}"]`);
|
|
288
|
+
|
|
289
|
+
/** @param {HTMLElement[]} questions */
|
|
290
|
+
function counts(questions) {
|
|
291
|
+
const found = { right: 0, wrong: 0, partial: 0, unattempted: 0, self: 0, total: 0 };
|
|
292
|
+
for (const question of questions) {
|
|
293
|
+
if (!isGradable(question)) {
|
|
294
|
+
found.self += 1;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
found.total += 1;
|
|
298
|
+
const state = question.dataset.state;
|
|
299
|
+
if (state in found) found[state] += 1;
|
|
300
|
+
else found.unattempted += 1;
|
|
301
|
+
}
|
|
302
|
+
return found;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** `2 of 23 correct · 1 to try again · 20 unattempted` */
|
|
306
|
+
function tallyText(found) {
|
|
307
|
+
const parts = [t("worksheet", "tallyCorrect", { right: found.right, total: found.total })];
|
|
308
|
+
if (found.wrong) parts.push(t("worksheet", "tallyWrong", { count: found.wrong }));
|
|
309
|
+
if (found.partial) parts.push(t("worksheet", "tallyPartial", { count: found.partial }));
|
|
310
|
+
if (found.unattempted) parts.push(t("worksheet", "tallyUnattempted", { count: found.unattempted }));
|
|
311
|
+
if (found.self) parts.push(t("worksheet", "tallySelf", { count: found.self }));
|
|
312
|
+
return parts.join(t("common", "sep"));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Fill and show the results panel with every question. */
|
|
316
|
+
function summarize(root, found, questions) {
|
|
317
|
+
const panel = root.querySelector("[data-summary]");
|
|
318
|
+
if (!panel) return;
|
|
319
|
+
panel.querySelector("[data-score]").textContent = String(found.right);
|
|
320
|
+
panel.querySelector("[data-score-of]").textContent = t("worksheet", "scoreOf", { total: found.total });
|
|
321
|
+
const list = panel.querySelector("[data-summary-list]");
|
|
322
|
+
list.replaceChildren();
|
|
323
|
+
for (const question of questions) {
|
|
324
|
+
const status = isGradable(question) ? (question.dataset.state ?? "unattempted") : "self";
|
|
325
|
+
const item = document.createElement("li");
|
|
326
|
+
item.dataset.status = status;
|
|
327
|
+
const link = document.createElement("a");
|
|
328
|
+
link.href = `#q-${question.dataset.question}`;
|
|
329
|
+
link.textContent = question.dataset.question;
|
|
330
|
+
const label = document.createElement("span");
|
|
331
|
+
label.textContent = t("worksheet", STATUS_KEY[status]);
|
|
332
|
+
item.append(link, " ", label);
|
|
333
|
+
list.append(item);
|
|
334
|
+
}
|
|
335
|
+
panel.querySelector("[data-retry]").hidden = !(found.wrong || found.partial || found.unattempted);
|
|
336
|
+
panel.hidden = false;
|
|
337
|
+
const heading = panel.querySelector("h2");
|
|
338
|
+
heading.tabIndex = -1;
|
|
339
|
+
heading.focus();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const hideSummary = (root) => {
|
|
343
|
+
const panel = root.querySelector("[data-summary]");
|
|
344
|
+
if (panel) panel.hidden = true;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const prefersReducedMotion = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
348
|
+
|
|
349
|
+
document.addEventListener("click", (event) => {
|
|
350
|
+
const checkButton = event.target.closest("[data-check]");
|
|
351
|
+
if (checkButton) {
|
|
352
|
+
const root = rootOf(checkButton);
|
|
353
|
+
const scope = checkButton.dataset.check;
|
|
354
|
+
const questions = questionsIn(root, scope);
|
|
355
|
+
for (const question of questions) {
|
|
356
|
+
check(question);
|
|
357
|
+
if (isGradable(question)) save(root, question, true);
|
|
358
|
+
}
|
|
359
|
+
const found = counts(questions);
|
|
360
|
+
const tally = tallyFor(root, scope);
|
|
361
|
+
if (tally) tally.textContent = tallyText(found);
|
|
362
|
+
if (scope === "#worksheet") summarize(root, found, questions);
|
|
363
|
+
syncNav(root);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const oneButton = event.target.closest("[data-check-one]");
|
|
368
|
+
if (oneButton) {
|
|
369
|
+
const root = rootOf(oneButton);
|
|
370
|
+
const question = oneButton.closest(".question");
|
|
371
|
+
check(question);
|
|
372
|
+
if (isGradable(question)) save(root, question, true);
|
|
373
|
+
syncNav(root);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const retryButton = event.target.closest("[data-retry]");
|
|
378
|
+
if (retryButton) {
|
|
379
|
+
const root = rootOf(retryButton);
|
|
380
|
+
const questions = questionsIn(root, retryButton.dataset.retry);
|
|
381
|
+
const found = counts(questions);
|
|
382
|
+
for (const question of questions) {
|
|
383
|
+
if (question.dataset.state !== "wrong" && question.dataset.state !== "partial") continue;
|
|
384
|
+
reset(question);
|
|
385
|
+
storageRemove(storageKey(root, question));
|
|
386
|
+
}
|
|
387
|
+
hideSummary(root);
|
|
388
|
+
const tally = tallyFor(root, retryButton.dataset.retry);
|
|
389
|
+
if (tally) tally.textContent = t("worksheet", "retryCount", { count: found.wrong + found.partial + found.unattempted });
|
|
390
|
+
syncNav(root);
|
|
391
|
+
const first = questions.find((question) => isGradable(question) && question.dataset.state !== "right");
|
|
392
|
+
const control = first?.querySelector("input, select, textarea");
|
|
393
|
+
if (control) {
|
|
394
|
+
control.focus();
|
|
395
|
+
control.scrollIntoView({ block: "center", behavior: prefersReducedMotion() ? "auto" : "smooth" });
|
|
396
|
+
}
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const resetButton = event.target.closest("[data-reset]");
|
|
401
|
+
if (resetButton) {
|
|
402
|
+
if (!window.confirm(resetButton.dataset.confirm)) return;
|
|
403
|
+
const root = rootOf(resetButton);
|
|
404
|
+
const scope = resetButton.dataset.reset;
|
|
405
|
+
for (const question of questionsIn(root, scope)) {
|
|
406
|
+
reset(question);
|
|
407
|
+
storageRemove(storageKey(root, question));
|
|
408
|
+
}
|
|
409
|
+
const tally = tallyFor(root, scope);
|
|
410
|
+
if (tally) tally.textContent = "";
|
|
411
|
+
hideSummary(root);
|
|
412
|
+
if (scope === "#worksheet") {
|
|
413
|
+
const note = root.querySelector("[data-restore-note]");
|
|
414
|
+
if (note) note.hidden = true;
|
|
415
|
+
}
|
|
416
|
+
syncNav(root);
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
/* Editing an answer: the old verdict no longer applies. Nothing is regraded. */
|
|
421
|
+
const onEdit = (event) => {
|
|
422
|
+
const question = event.target.closest?.(".question");
|
|
423
|
+
if (!question || !isGradable(question)) return;
|
|
424
|
+
const root = rootOf(question);
|
|
425
|
+
if (!root) return;
|
|
426
|
+
clearFeedback(question);
|
|
427
|
+
setState(question, hasValue(question) ? "answered" : "unattempted");
|
|
428
|
+
save(root, question, false);
|
|
429
|
+
syncNav(root);
|
|
430
|
+
};
|
|
431
|
+
document.addEventListener("input", onEdit);
|
|
432
|
+
document.addEventListener("change", onEdit);
|
|
433
|
+
|
|
434
|
+
/* Citation panels are an exclusive group through `<details name>`; Escape
|
|
435
|
+
closes whichever is open. */
|
|
436
|
+
document.addEventListener("keydown", (event) => {
|
|
437
|
+
if (event.key !== "Escape") return;
|
|
438
|
+
for (const open of document.querySelectorAll(".cite[open]")) open.open = false;
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
/*
|
|
442
|
+
* Drag and drop, for the questions whose data asks for it.
|
|
443
|
+
*
|
|
444
|
+
* A pointer affordance layered over the real control, never a replacement for
|
|
445
|
+
* it: the <select> or text input stays the keyboard, touch and screen-reader
|
|
446
|
+
* path (HTML5 drag events do not fire from touch), and a drop just sets its
|
|
447
|
+
* value. Nothing here is needed to answer a question.
|
|
448
|
+
*/
|
|
449
|
+
const DROPPED_MS = 700;
|
|
450
|
+
|
|
451
|
+
/** Mark the bank words that already appear in this question's blanks. */
|
|
452
|
+
function markUsedWords(question) {
|
|
453
|
+
const bank = question.querySelectorAll(".word-bank .word[data-word]");
|
|
454
|
+
if (!bank.length) return;
|
|
455
|
+
const typed = [...question.querySelectorAll(".blanks input[type='text']")]
|
|
456
|
+
.flatMap((input) => normalize(input.value).split(" "))
|
|
457
|
+
.filter(Boolean);
|
|
458
|
+
for (const word of bank) {
|
|
459
|
+
if (typed.includes(normalize(word.dataset.word))) word.dataset.used = "";
|
|
460
|
+
else delete word.dataset.used;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/* The bank entries and rows of one question that take drops: a match and a fill share this. */
|
|
465
|
+
function dragDrop(question) {
|
|
466
|
+
for (const chip of question.querySelectorAll('[draggable="true"][data-letter], [draggable="true"][data-word]')) {
|
|
467
|
+
const payload = chip.dataset.letter ?? chip.dataset.word;
|
|
468
|
+
chip.addEventListener("dragstart", (event) => {
|
|
469
|
+
event.dataTransfer.setData("text/plain", payload);
|
|
470
|
+
event.dataTransfer.effectAllowed = "copy";
|
|
471
|
+
chip.dataset.dragging = "";
|
|
472
|
+
});
|
|
473
|
+
chip.addEventListener("dragend", () => delete chip.dataset.dragging);
|
|
474
|
+
}
|
|
475
|
+
for (const row of question.querySelectorAll("[data-drop]")) {
|
|
476
|
+
// The drop only lands if dragover cancels the default.
|
|
477
|
+
row.addEventListener("dragover", (event) => {
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
event.dataTransfer.dropEffect = "copy";
|
|
480
|
+
row.dataset.over = "";
|
|
481
|
+
});
|
|
482
|
+
row.addEventListener("dragleave", (event) => {
|
|
483
|
+
if (!row.contains(event.relatedTarget)) delete row.dataset.over;
|
|
484
|
+
});
|
|
485
|
+
row.addEventListener("drop", (event) => {
|
|
486
|
+
event.preventDefault();
|
|
487
|
+
delete row.dataset.over;
|
|
488
|
+
const dropped = event.dataTransfer.getData("text/plain");
|
|
489
|
+
if (!dropped || !applyDrop(row, dropped)) return;
|
|
490
|
+
row.dataset.dropped = "";
|
|
491
|
+
setTimeout(() => delete row.dataset.dropped, DROPPED_MS);
|
|
492
|
+
markUsedWords(row.closest(".question"));
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Apply a dropped value to the row's control.
|
|
499
|
+
* A select takes the letter outright; a text blank appends, because one of
|
|
500
|
+
* them holds three words.
|
|
501
|
+
* @returns {boolean} whether anything was set
|
|
502
|
+
*/
|
|
503
|
+
function applyDrop(row, dropped) {
|
|
504
|
+
const select = row.querySelector("select");
|
|
505
|
+
if (select) {
|
|
506
|
+
if (![...select.options].some((option) => option.value === dropped)) return false;
|
|
507
|
+
select.value = dropped;
|
|
508
|
+
select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const input = row.querySelector("input[type='text']");
|
|
513
|
+
if (!input) return false;
|
|
514
|
+
const current = input.value.trim();
|
|
515
|
+
if (normalize(current).split(" ").includes(normalize(dropped))) return false;
|
|
516
|
+
input.value = current ? `${current}, ${dropped}` : dropped;
|
|
517
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** What a kind's script gets to use: the grader's tools and the shared drag and drop. */
|
|
522
|
+
const helpers = { mark, checkedValue, normalize, markUsedWords, dragDrop };
|
|
523
|
+
|
|
524
|
+
/* On load, once every kind's script has registered: let each kind enhance its questions, restore saved answers, start the question list. */
|
|
525
|
+
function init() {
|
|
526
|
+
for (const question of document.querySelectorAll(".question")) kinds()[question.dataset.kind]?.enhance?.(question, helpers);
|
|
527
|
+
prune();
|
|
528
|
+
for (const root of document.querySelectorAll("[data-worksheet]")) {
|
|
529
|
+
for (const question of questionsIn(root, "#worksheet")) {
|
|
530
|
+
if (!isGradable(question)) setState(question, "self");
|
|
531
|
+
else if (!question.dataset.state) setState(question, "unattempted");
|
|
532
|
+
}
|
|
533
|
+
const newest = restore(root);
|
|
534
|
+
const note = root.querySelector("[data-restore-note]");
|
|
535
|
+
if (newest && note) {
|
|
536
|
+
const when = new Intl.DateTimeFormat(document.documentElement.lang || "en-US", { weekday: "short", hour: "numeric", minute: "2-digit" }).format(newest);
|
|
537
|
+
note.textContent = t("worksheet", "restored", { when });
|
|
538
|
+
note.hidden = false;
|
|
539
|
+
}
|
|
540
|
+
syncNav(root);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (document.readyState === "loading" || document.readyState === "interactive") document.addEventListener("DOMContentLoaded", init, { once: true });
|
|
545
|
+
else init();
|