-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (130 loc) · 4.27 KB
/
script.js
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
"use strict";
function $(id) {
return document.getElementById(id);
}
var problemsObj = {
"---": {
codeSrc: "null",
statement: "<p>null</p>",
code: "null",
data: "null"
}
};
$("clearBtn").onclick = clearAnswer;
$("problemSelector").addEventListener("change", function (event) {
loadProblem(event.target.value); // event.target.value is a string
});
// an alias for command line usage
function go(problem) {
loadProblem(problem);
}
function loadProblem(problem) {
//console.log("switch to problem ", problem);
problem = String(problem).padStart(3, "0");
// only use the fetch API if we haven't already
if (problemsObj[problem] === undefined) {
fetchProblem(problem);
}
else {
loadProblemElements(problem);
}
}
function fetchProblem(id) {
problemsObj[id] = {};
fetch("./problems/statements/" + id + ".html").then(function (res) {
var text = res.text().then(function (text) {
problemsObj[id].statement = text;
fetch("./problems/solutions/" + id + ".js").then(function (res) {
var text = res.text().then(function (text) {
problemsObj[id].code = text;
problemsObj[id].codeSrc = "./problems/solutions/" + id + ".js";
fetch("./problems/data/" + id + ".txt").then(function (res) {
var text = res
.text()
.then(function (text) {
if (res.ok) {
problemsObj[id].data = text;
}
else {
problemsObj[id].data = "null";
}
})
.then(function () {
loadProblemElements(id);
});
});
});
});
});
});
}
function loadProblemElements(id) {
if (id !== "---") {
var script = document.createElement("script");
document.head.appendChild(script);
script.src = problemsObj[id].codeSrc;
script.async = false;
script.onload = function () {
$("executeBtn").onclick = function () {
displayAnswer(id);
};
};
}
else {
// turn off functionality in the null page
$("executeBtn").onclick = function () {
$("output").textContent = "null";
};
}
$("statementDiv").querySelector(".content").innerHTML =
problemsObj[id].statement;
$("codeDiv").querySelector(".content").innerHTML = problemsObj[id].code;
$("dataDiv").querySelector(".content").innerHTML = problemsObj[id].data;
hljs.highlightBlock($("codeDiv").querySelector(".content"));
toggleField($("statementDiv"), "on");
toggleField($("codeDiv"), "on");
toggleField($("dataDiv"), "off");
clearAnswer();
//console.log("DOM filled with problem elements");
}
function addExpandoListeners() {
var targets = document.querySelectorAll(".expando");
targets.forEach(function (item) {
item.querySelector(".label").addEventListener("click", function () {
toggleField(item);
});
});
}
function toggleField(node, onOrOff) {
if (onOrOff === void 0) { onOrOff = "toggle"; }
if (onOrOff == "toggle") {
if (node.show) {
onOrOff = "off";
}
else {
onOrOff = "on";
}
}
if (onOrOff == "on") {
node.show = true;
node.querySelector("span > .open-box").textContent = "[-]";
node.querySelector(".content").classList.remove("hidden");
}
else {
node.show = false;
node.querySelector("span > .open-box").textContent = "[+]";
node.querySelector(".content").classList.add("hidden");
}
}
function displayAnswer(id) {
$("output").textContent = window["euler" + id]();
}
function clearAnswer() {
$("output").textContent = null;
}
//init
addExpandoListeners();
loadProblemElements("---");
toggleField($("statementDiv"), "off");
toggleField($("codeDiv"), "off");
console.log("hello there.\nproblems can be conveniently loaded from the console by typing `go(id)`,\nthen executed with `euler{id}()`");