forked from rauschma/html_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
footnotes.js
64 lines (60 loc) · 2.19 KB
/
footnotes.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
(function () {
var FOOTNOTE_REGEX = /^\([0-9]+\)$/;
var REFERENCE_REGEX = /^\[[0-9]+\]$/;
var oldOnLoad = window.onload;
window.onload = function (event) {
if (document.getElementsByClassName) {
var elems = document.getElementsByClassName("ptr");
for (var i = 0; i<elems.length; i++) {
var elem = elems[i];
var ptrText = elem.innerHTML;
if (FOOTNOTE_REGEX.test(ptrText)) {
elem.className = "ptr footptr";
elem.onclick = toggle;
} else if (REFERENCE_REGEX.test(ptrText)) {
elem.className = "ptr refptr";
}
elem.setAttribute("href", "#"+ptrText);
}
addListItemIds("references", "[", "]");
addListItemIds("footnotes", "(", ")");
}
if (typeof oldOnLoad === "function") {
oldOnLoad(event);
}
};
function addListItemIds(parentId, before, after) {
var refs = document.getElementById(parentId);
if (refs && refs.getElementsByTagName) {
var elems = refs.getElementsByTagName("li");
for (var i = 0; i<elems.length; i++) {
var elem = elems[i];
elem.setAttribute("id", before+(i+1)+after);
}
}
}
var currentDiv = null;
var currentId = null;
function toggle(event) {
var parent = this.parentNode;
if (currentDiv) {
parent.removeChild(currentDiv);
currentDiv = null;
}
var footnoteId = this.innerHTML;
if (currentId === footnoteId) {
currentId = null;
} else {
currentId = footnoteId;
currentDiv = document.createElement("div");
var footHtml = document.getElementById(footnoteId).innerHTML;
currentDiv.innerHTML = footHtml;
currentDiv.className = "foot-tooltip";
parent.insertBefore(currentDiv, this.nextSibling);
setTimeout(function () {
currentDiv.style.opacity = "1";
}, 0);
}
event.preventDefault();
}
}());