forked from btholt/four-semesters-of-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpres.js
89 lines (75 loc) · 2.73 KB
/
pres.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
'use strict';
(function() {
var counter = 0;
var startPres = function(id) {
var node = document.getElementById(id);
node.addEventListener('click', handleStart);
};
var handleStart = function() {
showOnly(0);
document.addEventListener('keyup', advance);
}
var advance = function(e) {
if (e.keyCode === 37) {
// left arrow
counter--;
showOnly(counter);
}
else if (e.keyCode === 39) {
counter++;
showOnly(counter);
}
}
var showOnly = function(index) {
var nodes = document.getElementsByTagName('article');
for (var i = 0; i < nodes.length; i++) {
if (counter === i) {
console.log(i, nodes[i]);
var replace = 'invisible';
if (nodes[i].className.indexOf(' invisible') >= 0) {
replace = ' invisible';
}
nodes[i].className = nodes[i].className.replace(replace, '');
nodes[i].scrollIntoView();
}
else if (nodes[i].className.indexOf('invisible') < 0) {
nodes[i].className += (nodes[i].className.length) ? ' invisible' : 'invisible';
}
}
};
startPres('start');
// https://gist.github.com/mathewbyrne/1280286
const slugify = (text) => {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
const generateToc = () => {
const getAllTitles = () => [...document.querySelectorAll('.chapter-group-title, .chapter-title')]
const listItems = getAllTitles()
.map( elem => {
const slug = slugify(elem.innerHTML)
elem.setAttribute('id', slug);
const li = document.createElement('li');
const a = document.createElement('a');
a.textContent = elem.textContent
a.href = `#${slug}`
li.appendChild(a)
return li
})
listItems.shift(); // remove the 'table of content' heading
const ul = document.createElement('ul')
ul.classList.add('toc')
listItems.forEach(item => ul.appendChild(item))
return ul
}
const insertToc = () => {
const ul = generateToc()
const tocDiv = document.querySelector("#table-of-content").closest('.chapter').querySelector('.chapter-text')
tocDiv.appendChild(ul);
}
insertToc()
})();