-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigationMap.js
130 lines (113 loc) · 4.24 KB
/
NavigationMap.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
var NavigationMap = (function() {
const __roman_table = [
[1000, "M"],
[ 900, "CM"],
[ 500, "D"],
[ 400, "CD"],
[ 100, "C"],
[ 90, "XC"],
[ 50, "L"],
[ 40, "XL"],
[ 10, "X"],
[ 9, "IX"],
[ 5, "V"],
[ 4, "IV"],
[ 1, "I"]
];
const __number_to_roman = x => {
let symbols = [];
for(let [value, symbol] of __roman_table)
while(x >= value) {
x -= value;
symbols.push(symbol);
}
return symbols.join("")
};
function NavigationMap() {
this.__NavigationMap_uuidsKeeper = {
uuids: {},
newUuid: function() {
let uuid; do {
uuid = Math.floor(Math.random()*1000000000);
} while(this.uuids[uuid]);
this.uuids[uuid] = true;
return "NavigationMap_" + uuid;
}
};
}
NavigationMap.prototype = {
__NavigationMap_uuidsKeeper: undefined, // In constructor
convertTitleToLink: function(textContent, { titleElement, liElement }) {
let anchor = document.createElement("a");
let link = document.createElement("a");
let uuid = this.__NavigationMap_uuidsKeeper.newUuid();
anchor.setAttribute("name", uuid);
link.setAttribute("href", "#"+uuid);
textContent = textContent || (titleElement && titleElement.textContent);
anchor.textContent = textContent;
link.textContent = textContent;
if(titleElement) {
titleElement.innerHTML = "";
titleElement.appendChild(anchor);
}
if(liElement) {
liElement.innerHTML = "";
liElement.appendChild(link);
}
return { anchor, link };
},
createRomanCounterElement: function(decimalCounters, { target, classList }) {
let element = document.createElement("span");
element.textContent = decimalCounters
.map(__number_to_roman)
.join(".") + (". ");
if (target) {
target.insertBefore(element, target.firstElementChild);
}
if (classList) {
for(let clz of classList)
element.classList.add(clz);
}
return element;
},
generateTOC: function(sections) {
let navElement = document.createElement("ul");
let level1Counter = 0;
for(let section of sections) {
level1Counter ++;
let itemElement = document.createElement("li");
let h1Element = section.querySelector("h1");
this.convertTitleToLink(null, {
titleElement: h1Element,
liElement: itemElement
});
this.createRomanCounterElement([level1Counter], {
target: h1Element
})
navElement.appendChild(itemElement); {
let subtitles = section.querySelectorAll("h2");
if(subtitles.length == 0)
continue;
let subNavElement = document.createElement("ul");
let level2Counter = 0;
for(let h2Element of subtitles) {
level2Counter ++;
let itemElement = document.createElement("li");
this.convertTitleToLink(null, {
titleElement: h2Element,
liElement: itemElement
});
this.createRomanCounterElement([level1Counter, level2Counter], {
target: h2Element,
classList: ["NavigationMap_romanCounter"]
})
subNavElement.appendChild(itemElement);
}
itemElement.appendChild(subNavElement);
}
}
return navElement;
}
};
return NavigationMap;
})();