-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrequirements.js
65 lines (57 loc) · 1.52 KB
/
requirements.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
/**
* These are the requirements to successfully leave a page
* and the links on the top, marked or unmarked.
*/
var requirements = [];
function required(callable) {
requirements.push(callable);
}
function getId() {
return getCookieId(current_page_id);
}
function getLinkId(link) {
return getCookieId(link.id);
}
function getCookieId(page_id) {
/* The id of pages for the cookies */
var numbering_regex = /\d+-\d+/;
var numbering = numbering_regex.exec(page_id);
return "requirements.js-step-" + numbering[0];
}
function updateRequirements() {
var allTrue = true;
for (var i = 0; i < requirements.length; i += 1) {
var requirement = requirements[i];
allTrue = requirement() && allTrue;
}
if (current_page_id) {
allTrue = allTrue || getCookie(getId());
setCookie(getId(), allTrue);
}
updateLinks();
}
function updateLinks() {
var links = document.getElementsByClassName("step");
for (var i = 0; i < links.length; i += 1) {
var link = links[i];
if (link.id) {
var status = getCookie(getLinkId(link));
if (status) {
link.classList.add("done");
} else {
link.classList.remove("done");
}
}
}
if (getCookie(getId())) {
document.body.classList.add("requirements-satisfied");
} else {
document.body.classList.remove("requirements-satisfied");
}
}
function loadRequirements(){
loadPlayfields();
loadQuizzes();
updateRequirements(); // turn pages green if they are not interactive
};
window.addEventListener("load", loadRequirements);