-
Notifications
You must be signed in to change notification settings - Fork 0
/
heading-overview.js
137 lines (105 loc) · 4.52 KB
/
heading-overview.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
/**
* Created by Leonhard Gahr on 04.04.2017.
*/
init();
function init() {
var seperator = " - ";
var headings = document.getElementsByClassName("heading-overview");
var reveal = document.getElementById("reveal");
var body = document.getElementsByClassName("reveal")[0];
var bodyChild = body.children[0];
var topicsElement = document.createElement("div");
topicsElement.className = "topics";
var headElement = document.createElement("div");
headElement.className = "head";
var textElements = getTextElements(headings, seperator, false);
textElements.pop();
for (var index in textElements) {
headElement.insertBefore(textElements[index], headElement.nextSibling)
}
topicsElement.insertBefore(headElement, topicsElement.nextSibling);
bodyChild.parentNode.insertBefore(topicsElement, bodyChild.nextSibling);
addRevealListener();
if (Reveal.getCurrentSlide().classList.contains("no-heading-overview")) {
document.getElementsByClassName("topics")[0].style.opacity = 0;
}
}
function appendClass(element, newClass) {
element.className = element.className + " " + newClass
}
function addRevealListener() {
var elements = document.getElementsByClassName("heading-overview");
var indices = [];
for (var i = 0; i < elements.length; i++) {
var slideIndices = Reveal.getIndices(elements[i].parentNode).h;
appendClass(document.getElementsByClassName("heading-preview")[i], slideIndices);
indices.push(slideIndices);
}
var recentElement;
Reveal.addEventListener('slidechanged', function () {
if (Reveal.getCurrentSlide().classList.contains("no-heading-overview")) {
document.getElementsByClassName("topics")[0].style.opacity = 0
} else {
document.getElementsByClassName("topics")[0].style.opacity = 1
}
if (indices.indexOf(Reveal.getIndices().h) !== -1) {
if (recentElement) {
recentElement.classList.remove("active");
recentElement.classList.add("inactive")
}
var element = document.getElementsByClassName("" + Reveal.getIndices().h)[0];
recentElement = element;
if (!element.classList.contains("active")) {
element.classList.remove("inactive");
element.classList.add("active");
}
} else {
if (recentElement) {
recentElement.classList.remove("active");
recentElement.classList.add("inactive")
}
}
});
Reveal.addEventListener('overviewshown', function () {
document.getElementsByClassName("topics")[0].style.opacity = 0
});
Reveal.addEventListener('overviewhidden', function () {
document.getElementsByClassName("topics")[0].style.opacity = 1
});
generateTableOfContents()
}
function getTextElements(headings, seperator, noTag) {
var textElements = [];
for (var key in headings) {
if (headings.hasOwnProperty(key) && headings[key].nodeType === 1) {
if (noTag) {
textElements.push(headings[key].innerHTML.replace("\n", " "))
} else {
var element = document.createElement("h4");
if (headings[key].getAttribute("data-heading-overview") !== null) {
element.innerText = headings[key].getAttribute("data-heading-overview");
} else {
element.innerText = headings[key].innerHTML.replace("\n", " ");
}
element.className = headings[key].innerText.replace(" ", "").toLowerCase() + " heading-preview";
var seperatorElement = document.createElement("h4");
seperatorElement.innerText = seperator;
textElements.push(element, seperatorElement)
}
}
}
return textElements
}
function generateTableOfContents() {
var table_slide = document.getElementsByClassName("table-of-contents")[0];
if (table_slide === undefined) {
return
}
var textElements = getTextElements(document.getElementsByClassName("heading-overview"), "", true);
var list = document.createElement("ul");
for (var i in textElements) {
list.innerHTML += "<li>" + textElements[i] + "</li>"
}
table_slide.innerHTML = "<" + table_slide.getAttribute("data-heading-tag") + ">" + table_slide.getAttribute("data-heading") + "</" + table_slide.getAttribute("data-heading-tag") + ">";
table_slide.innerHTML += list.outerHTML;
}