-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyapp.js
82 lines (76 loc) · 2.61 KB
/
myapp.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
class App {
showAnswer() {
this.eWidget.style.display = "none";
const entry = this.entries[this.currentIndex];
entry.style.display = "block";
}
showQuestion() {
const entry = this.entries[this.currentIndex];
while (this.eWidget.firstChild) {
this.eWidget.removeChild(this.eWidget.firstChild);
}
const eHG = entry.getElementsByClassName("hg")[0];
const clonedHG = eHG.cloneNode(true);
this.eWidget.appendChild(clonedHG);
this.eWidget.style.display = "block";
}
init() {
this.running = true;
document.getElementById("widget-starter").style.display = "none";
const entries = document.querySelectorAll(".entry");
// hide them all
entries.forEach((entry) => {
entry.style.display = "none";
});
this.entries = Array.from(entries).sort((a, b) => 0.5 - Math.random());
this.currentIndex = 0;
this.eWidget = document.getElementById("widget")
this.eWidget.className = "entry";
this.eWidget.style.borderTop = "1px solid #909090";
this.eWidget.innerHTML = `<h1>Quiz</h1>`
this.eWidget.style.display = "block";
this.status = "";
document.onclick = () => {
switch (this.status) {
case "":
this.showQuestion();
this.status = "asking";
break;
case "asking":
this.showAnswer();
this.status = "answering";
break;
case "answering":
const cur = this.entries[this.currentIndex];
cur.style.display = "none";
this.currentIndex++;
if (this.currentIndex === this.entries.length) {
alert("Finished");
this.status = "";
this.finish();
return;
}
this.showQuestion();
this.status = "asking";
break;
}
};
}
finish() {
this.entries.forEach((entry) => {
entry.style.display = "block";
});
this.eWidget.style.display = "none";
this.currentIndex = 0;
this.running = false;
}
}
const app = new App();
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('widget-starter-button').addEventListener("click", ()=>{
if (app.running) {
return;
}
app.init();
});
});