-
Notifications
You must be signed in to change notification settings - Fork 17
/
inhaltsverzeichnis-glossar.js
84 lines (71 loc) · 2.19 KB
/
inhaltsverzeichnis-glossar.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
var inhaltsverzeichnis = {
show : function () {
var ul = document.createElement("ul");
ul.id = "inhaltsverzeichnis";
var h2s = document.getElementsByTagName("h2");
for (var i = 0, h2, li, a, text, textnode; h2 = h2s[i]; i++) {
li = document.createElement("li");
a = document.createElement("a");
a.href = "#" + h2.id;
text = h2.innerText || h2.textContent;
if (!text) {
return false;
}
textnode = document.createTextNode(text);
a.appendChild(textnode);
li.appendChild(a);
ul.appendChild(li);
}
var h1 = document.getElementsByTagName("h1").item(0);
h1.parentNode.insertBefore(ul, h1.nextSibling);
}
};
var glossary = {
show : function () {
var div = document.createElement("div");
div.id = "glossary";
var h2 = document.createElement("h2");
h2.id = "glossar";
var textnode = document.createTextNode("Glossar");
h2.appendChild(textnode);
div.appendChild(h2);
var ul = document.createElement("ul");
div.appendChild(ul);
var dfns = document.getElementsByTagName("dfn");
for (var i = 0, dfn, begriff, li, span, jumpTarget; dfn = dfns[i]; i++) {
li = document.createElement("li");
li.begriff = dfn.textContent || dfn.innerText || dfn.text;
li.idFragment = li.begriff.toLowerCase().replace(/ /g, "-").replace(/[^a-z\-]/ig, "");
li.dfn = dfn;
li.onclick = li.showTerm = glossary.showTerm;
ul.appendChild(li);
span = document.createElement("span");
textnode = document.createTextNode(li.begriff);
span.appendChild(textnode);
li.appendChild(span);
if (new RegExp("^#?begriff-" + li.idFragment, "i").test(location.hash)) {
jumpTarget = li;
}
}
document.body.appendChild(div);
if (jumpTarget) {
window.setTimeout(function () { jumpTarget.showTerm(); }, 0);
}
},
showTerm : function (e) {
e = e || window.event;
var dfn = this.dfn;
var parentNode, tagName;
while (parentNode = dfn.parentNode) {
tagName = parentNode.nodeName.toLowerCase();
if (/^p|li|dt|dd|th|td$/.test(tagName)) {
break;
}
}
var id = "begriff-" + this.idFragment;
parentNode.id = id;
window.location.hash = parentNode.id;
}
};
addEvent(window, "load", glossary.show);
addEvent(window, "load", inhaltsverzeichnis.show);