-
Notifications
You must be signed in to change notification settings - Fork 2
/
table_of_contents.js
69 lines (63 loc) · 1.91 KB
/
table_of_contents.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
(function() {
function emptyDiv(n) {
while (n.hasChildNodes()) n.removeChild(n.lastChild);
}
function newElement(type, parent, classes = [], text = null) {
const n = document.createElement(type);
if (text != null) n.innerText = text;
for (const cls of classes) n.classList.add(cls);
if (parent != null) parent.appendChild(n);
return n;
}
function newLink(parent, classes = [], url = null, text = null) {
const n = newElement('a', parent, classes, text);
if (url != null) n.href = url;
return n;
}
let _idCounter = 0;
function setUniqueDomId(node) {
if (node.id != '') return node.id;
while (true) {
const id = `heading_${_idCounter}`;
++_idCounter;
if (document.getElementById(id) == null) {
node.id = id;
return id;
}
}
}
class TableOfContentsImpl {
constructor(node) {
this.node = node;
this.topBtn = null;
window.setTimeout(() => {
emptyDiv(this.node);
const ol = newElement('ol', this.node, ['contents_list']);
for (const h of document.getElementsByClassName(kTableOfContentsClass)) {
const id = setUniqueDomId(h);
const li = newElement('li', ol, ['contents_list_item']);
newLink(li, ['contents_link'], `#${id}`, h.innerText);
}
this.topBtn = newElement('div', document.body, ['scroll_to_top_button']);
this.topBtn.onclick = () => {
document.body.scrollTop = document.documentElement.scrollTop = 0;
};
window.addEventListener('scroll', () => {
if (this.node.getBoundingClientRect().bottom < 0) {
this.topBtn.classList.add('enabled');
} else {
this.topBtn.classList.remove('enabled');
}
});
}, 0);
}
}
class TableOfContents extends HTMLElement {
constructor() {
super();
this.impl = new TableOfContentsImpl(this);
}
}
window.addEventListener(
'load', () => customElements.define('table-of-contents', TableOfContents));
})();