Skip to content

Commit 93f3ac7

Browse files
committed
Wire up code sandbox to url hash
1 parent 1cae9a2 commit 93f3ac7

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

html/js/code.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ addEventListener("load", function() {
5656
}
5757

5858
var per = document.querySelector("#per_chapter");
59-
per.addEventListener("change", function() { selectContext(per.value); });
59+
per.addEventListener("change", function() {
60+
selectContext(per.value);
61+
document.location.hash = "#" + (per.value == "box" ? chapters.value : per.value);
62+
});
6063
var fileList = document.querySelector("#files");
6164
var fileInfo = document.querySelector("#fileInfo");
6265

@@ -80,12 +83,11 @@ addEventListener("load", function() {
8083
a.href = file;
8184
a.textContent = file.replace(/^code\//, "");
8285
});
83-
selectContext(per.value);
8486
}
8587

86-
function findExercise(id) {
88+
function findExercise(id, chapter) {
8789
var parts = id.split(".");
88-
var chapter = getChapter(parts[0]);
90+
if (!chapter) chapter = getChapter(parts[0]);
8991
for (var i = 0; i < chapter.exercises.length; i++)
9092
if (chapter.exercises[i].number == +parts[1])
9193
return chapter.exercises[i];
@@ -109,11 +111,11 @@ addEventListener("load", function() {
109111
setEditorCode(code, guessed);
110112
visible = "box";
111113
} else {
112-
var exercise = findExercise(value);
114+
var exercise = findExercise(value, chapter);
113115
context = {include: chapter.include,
114116
solution: exercise.solution,
115117
type: exercise.type};
116-
setEditorCode(code, exercise.type);
118+
setEditorCode(exercise.code, exercise.type);
117119
visible = "exercise";
118120
document.querySelector("#download").href = exercise.file;
119121
}
@@ -163,6 +165,34 @@ addEventListener("load", function() {
163165
exercise.chapter = chapter;
164166
});
165167
});
166-
chapters.addEventListener("change", function() { selectChapter(chapters.value); });
167-
selectChapter(chapters.value);
168+
chapters.addEventListener("change", function() {
169+
selectChapter(chapters.value);
170+
selectContext(per.value);
171+
document.location.hash = "#" + chapters.value;
172+
});
173+
174+
function parseFragment() {
175+
var hash = document.location.hash.slice(1);
176+
var valid = /^(\d)+(?:\.(\d+))?$/.exec(hash);
177+
if (valid) {
178+
var chapter = getChapter(Number(valid[1]));
179+
var exercise = chapter && valid[2] && findExercise(hash, chapter);
180+
if (!chapter || valid[2] && !exercise) valid = null;
181+
}
182+
if (valid) {
183+
if (chapters.value != valid[1]) {
184+
chapters.value = valid[1];
185+
selectChapter(Number(valid[1]));
186+
}
187+
var perValue = exercise ? hash : "box";
188+
if (per.value != perValue) {
189+
per.value = perValue;
190+
selectContext(perValue);
191+
}
192+
return true;
193+
}
194+
}
195+
196+
parseFragment() || (selectChapter(0) && selectContext(per.value));
197+
addEventListener("hashchange", parseFragment);
168198
});

0 commit comments

Comments
 (0)