-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
exercises.js
166 lines (149 loc) · 5.2 KB
/
exercises.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* jshint esversion: 6 */
function pageSetup(isExample = false) {
const zeroMdRendered = Promise.all(
[...document.querySelectorAll("zero-md")].map((zeroMd) => {
return new Promise((resolve, reject) => {
addEventListener("zero-md-rendered", (ev) => {
// ev.detail.stamped.body says true even when there is no markdow-body?
// could be a bug in zero-md - or a misunderstanding of the apoi
if (zeroMd.shadowRoot.querySelector("div.markdown-body")) {
resolve(true);
}
});
});
})
);
// wait for all zeroMd to be rendered before initialising
zeroMdRendered.then(initExercises);
function initExercises() {
let exercises = findExercises();
setupToc(exercises);
addDomElements(exercises);
exercises.forEach(hideExercise);
let exerciseIdFromUrl = window.location.hash.substring(1);
let initialExercise =
exercises.find((exercise) => exercise.id === exerciseIdFromUrl) ||
exercises[0];
selectExercise(initialExercise);
}
function findExercises() {
let $exercises = [...document.querySelectorAll(".exercise")];
return $exercises.map(($exercise) => {
$h2 = $exercise.querySelector("h2");
title = $h2.innerText;
return {$exercise, $h2, title};
});
}
function setupToc(exercises) {
let $toc = document.getElementById("toc");
let $ol = document.createElement("ol");
exercises.forEach((exercise) => {
$li = document.createElement("li");
$li.setAttribute("role", "button");
$li.innerText = exercise.title;
$li.addEventListener("click", () => {
selectExercise(exercise);
$toc.classList.toggle("open");
});
$ol.appendChild($li);
exercise.$li = $li;
});
$toc.appendChild($ol);
}
function addDomElements(exercises) {
const exercise_or_example = isExample ? "example" : "exercise";
for (let i = 0; i < exercises.length; i++) {
let exercise = exercises[i];
let $nav = document.createElement("nav");
let $instructions = exercise.$exercise.querySelector(".instructions");
$nav.setAttribute("class", "nav-buttons");
if (i > 0) {
$nav.appendChild(
createNavElement(`previous ${exercise_or_example}`, exercises[i - 1])
);
}
if (i < exercises.length - 1) {
$nav.appendChild(
createNavElement(`next ${exercise_or_example}`, exercises[i + 1])
);
}
exercise.$exercise.insertBefore($nav, exercise.$h2);
exercise.$blockly = document.createElement("div");
exercise.$exercise.insertBefore(
exercise.$blockly,
exercise.$h2.nextElementSibling
);
exercise.id = i;
if (exercise.$exercise.id) {
exercise.id = exercise.$exercise.id;
}
// TODO write a mutation observer to detect when the zero-md has rendered so we can
// write a better layout
exercise.blocklyDomEditor = new BlocklyDomEditor(
exercise.$blockly,
exercise.id
);
let initJsonBlockly = undefined;
let initHtml = undefined;
let useLocalStorage = isExample ? false : true;
if (isExample) {
let $zeroMd = exercise.$exercise.querySelector("zero-md");
// Start code is a string value of "v" in the querystring generated by "Share" button.
let start_code = $zeroMd.getAttribute("start_code");
if (start_code) {
let data = JSON.parse(
JSONCrush.uncrush(decodeURIComponent(start_code))
);
initHtml = data.h;
initJsonBlockly = data.j;
}
} else {
let $startCode = exercise.$exercise.querySelector(".start_code"); // can be removed when all migrated to markdown
if (!$startCode) {
// get the first <pre class="language-html"> inside rendered markdown
let $zeroMd = exercise.$exercise.querySelector("zero-md");
if ($zeroMd) {
let $instructionsRoot = $zeroMd.shadowRoot;
$startCode = $instructionsRoot.querySelector("pre.language-html");
}
}
if ($startCode) initHtml = $startCode.innerText;
}
exercise.blocklyDomEditor.init(
initHtml,
initJsonBlockly,
useLocalStorage
);
}
}
function createNavElement(name, exercise) {
$navElement = document.createElement("button");
$navElement.innerText = name;
$navElement.addEventListener("click", (e) => {
e.preventDefault();
selectExercise(exercise);
});
return $navElement;
}
let currentExercise = null;
function selectExercise(exercise) {
if (currentExercise) {
hideExercise(currentExercise);
}
showExercise(exercise);
window.location.hash = exercise.id;
currentExercise = exercise;
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
function hideExercise(exercise) {
exercise.$exercise.style.display = "none";
exercise.$li.classList.remove("current");
exercise.blocklyDomEditor.hide();
}
function showExercise(exercise) {
exercise.$exercise.style.display = "block";
exercise.$li.classList.add("current");
exercise.blocklyDomEditor.show();
}
}