-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.js
More file actions
120 lines (105 loc) · 3.08 KB
/
main.js
File metadata and controls
120 lines (105 loc) · 3.08 KB
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
/* Add an edit in repl link to code blocks */
function checkForReplLinks() {
// const replCode = document.querySelectorAll('pre');
// [...replCode].forEach(pre => {
// console.log(pre);
// const text = encodeURI(pre.innerText);
// const link = document.createElement('a');
// link.title = 'Run this code in the REPL';
// link.innerText = 'Run this code in the REPL';
// link.href = '/repl/?code=' + text;
// const paragraph = document.createElement('p');
// paragraph.appendChild(link);
// const wrapper = document.createElement('div');
// pre.parentNode.insertBefore(wrapper, pre);
// wrapper.appendChild(pre);
// wrapper.appendChild(paragraph);
// });
const replCode = document.querySelectorAll('.repl-code');
[...replCode].forEach(code => {
const codeText = encodeURI(code.innerText);
const link = document.createElement('a');
link.title = 'Run this code in the REPL';
link.innerText = 'Run this code in the REPL';
link.href = '/repl/?code=' + codeText;
const paragraph = document.createElement('p');
paragraph.appendChild(link);
code.appendChild(paragraph);
});
}
window.addEventListener('DOMContentLoaded', event => {
if (window.localStorage) {
window.topicsCompleted = getTopicsFromLocalStorage();
updateUI();
}
checkForReplLinks();
});
function updateUI() {
checkForCompletedButtons();
checkSideBar();
}
function checkForCompletedButtons() {
[...getButtons()].forEach(button => {
const topic = button.dataset.topic;
if (isCompleted(topic)) {
markButtonAsCompleted(button);
} else {
markButtonAsNotCompleted(button);
}
});
}
function getButtons() {
return document.querySelectorAll('.completed-button');
}
function toggleCompletedTopic(topic) {
if (isCompleted(topic)) {
removeCompletedTopic(topic);
updateUI();
} else {
addCompletedTopic(topic);
updateUI();
}
}
function isCompleted(topic) {
return window.topicsCompleted && window.topicsCompleted.includes(topic);
}
function markButtonAsCompleted(button) {
button.classList.add('completed');
button.innerText = 'Completed!';
}
function markButtonAsNotCompleted(button) {
button.classList.remove('completed');
button.innerText = 'Mark as completed';
}
function getTopicsFromLocalStorage() {
return JSON.parse(window.localStorage.getItem('topicsCompleted'));
}
function addCompletedTopic(topic) {
let topics = getTopicsFromLocalStorage();
if (!topics) {
topics = [];
}
topics.push(topic);
save(topics);
}
function save(topics) {
window.topicsCompleted = topics;
window.localStorage.setItem('topicsCompleted', JSON.stringify(topics));
}
function removeCompletedTopic(topic) {
const topics = getTopicsFromLocalStorage();
save(topics.filter(t => t !== topic));
}
function getSidebarItems() {
return document.querySelectorAll('.topics li');
}
function checkSideBar() {
[...getSidebarItems()].forEach(item => {
const topic = item.dataset.topic;
if (isCompleted(topic)) {
item.classList.add('completed');
} else {
item.classList.remove('completed');
}
});
}