Clean up and comment javascript in puzzle.html

This commit is contained in:
Neale Pickett 2018-10-10 22:56:15 +00:00
parent 10daac3988
commit ab0cd35d2f
1 changed files with 30 additions and 12 deletions

View File

@ -13,23 +13,31 @@ function init() {
let points = params.get("points"); let points = params.get("points");
let puzzleId = params.get("pid"); let puzzleId = params.get("pid");
let puzzle = document.getElementById("puzzle");
let base = "content/" + categoryName + "/" + puzzleId + "/"; let base = "content/" + categoryName + "/" + puzzleId + "/";
let fn = base + "puzzle.json";
fetch(fn) fetch(base + "puzzle.json")
.then(function(resp) { .then(resp => {
return resp.json(); return resp.json();
}).then(function(obj) { })
.then(obj => {
// Populate authors
document.getElementById("authors").textContent = obj.authors.join(", "); document.getElementById("authors").textContent = obj.authors.join(", ");
// If answers are provided, this is the devel server
if (obj.answers) { if (obj.answers) {
devel_addin(obj, document.getElementById("devel")); devel_addin(obj, document.getElementById("devel"));
} }
// Load scripts
for (let script of obj.scripts) { for (let script of obj.scripts) {
let st = document.createElement("script"); let st = document.createElement("script");
document.head.appendChild(st); document.head.appendChild(st);
st.src = base + script; st.src = base + script;
} }
// List associated files
for (let fn of obj.files) { for (let fn of obj.files) {
let li = document.createElement("li"); let li = document.createElement("li");
let a = document.createElement("a"); let a = document.createElement("a");
@ -45,19 +53,29 @@ function init() {
se.outerHTML = se.outerHTML.replace(/(src|href)="([^/]+)"/i, "$1=\"" + base + "$2\""); se.outerHTML = se.outerHTML.replace(/(src|href)="([^/]+)"/i, "$1=\"" + base + "$2\"");
} }
let puzzle = document.getElementById("puzzle"); // Replace puzzle children with what's in `doc`
while (puzzle.firstChild) puzzle.firstChild.remove(); Array.from(puzzle.childNodes).map(e => e.remove());
for (let e of doc.body.childNodes) puzzle.appendChild(e); Array.from(doc.body.childNodes).map(e => puzzle.appendChild(e));
}).catch(function(err) { })
console.log("Error", err); .catch(err => {
// Show error to the user
Array.from(puzzle.childNodes).map(e => e.remove());
let p = document.createElement("p");
puzzle.appendChild(p);
p.classList.add("Error");
p.textContent = err;
}); });
document.querySelector("body > h1").innerText = categoryName + " " + points document.title = categoryName + " " + points;
document.querySelector("body > h1").innerText = document.title;
document.querySelector("input[name=cat]").value = categoryName; document.querySelector("input[name=cat]").value = categoryName;
document.querySelector("input[name=points]").value = points; document.querySelector("input[name=points]").value = points;
} }
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init); document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
</script> </script>
</head> </head>
<body> <body>