Files
ProcessControl/_book/site_libs/quarto-contrib/calc-exo-2.10.0/exo.js
T

40 lines
1.5 KiB
JavaScript

// exo — hints unlock one by one within a part; everything unfolds for printing
(function () {
// block opening a locked hint
document.addEventListener("click", function (e) {
var s = e.target.closest ? e.target.closest("details.exo-hint.exo-locked > summary") : null;
if (s) e.preventDefault();
});
// opening hint n of a part unlocks hint n+1 of the same part, wherever it
// sits in the exercise (toggle doesn't bubble: use capture)
document.addEventListener("toggle", function (e) {
var d = e.target;
if (!d.classList || !d.classList.contains("exo-hint") || !d.open) return;
var chain = d.getAttribute("data-exo-chain");
var n = parseInt(d.getAttribute("data-exo-hint"), 10);
if (!chain || isNaN(n)) return;
var hints = document.querySelectorAll("details.exo-hint[data-exo-chain]");
for (var i = 0; i < hints.length; i++) {
if (hints[i].getAttribute("data-exo-chain") === chain &&
parseInt(hints[i].getAttribute("data-exo-hint"), 10) === n + 1) {
hints[i].classList.remove("exo-locked");
}
}
}, true);
// print: open all hints/solutions, restore afterwards
window.addEventListener("beforeprint", function () {
document.querySelectorAll(".exo details").forEach(function (d) {
d.dataset.exoWasOpen = d.open ? "1" : "0";
d.open = true;
});
});
window.addEventListener("afterprint", function () {
document.querySelectorAll(".exo details").forEach(function (d) {
d.open = d.dataset.exoWasOpen === "1";
delete d.dataset.exoWasOpen;
});
});
})();