forked from PreTeXtBook/pretext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretext.js
178 lines (157 loc) · 6.81 KB
/
pretext.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
* pretext.js
*******************************************************************************
* The main front-end controller for PreTeXt documents.
*
* Homepage: pretextbook.org
* Repository: https://github.com/PreTeXtBook/JS_core
*
* Authors: David Farmer, Rob Beezer
*
*******************************************************************************
*/
function scrollTocToActive() {
//Try to figure out current TocItem from URL
let fileNameWHash = window.location.href.split("/").pop();
let fileName = fileNameWHash.split("#")[0];
//Find just the filename in ToC
let tocEntry = document.querySelector('#ptx-toc a[href="' + fileName + '"]');
if (!tocEntry) {
return; //complete failure, get out
}
//See if we can also match fileName#hash
let tocEntryWHash = document.querySelector(
'#ptx-toc a[href="' + fileNameWHash + '"]'
);
if (tocEntryWHash) {
//Matched something below a subsection - activate the list item that contains it
tocEntryWHash.closest("li").classList.add("active");
}
//Now activate ToC item for fileName and scroll to it
// Don't use scrollIntoView because it changes users tab position in Chrome
// and messes up keyboard navigation
tocEntry.closest("li").classList.add("active");
// Scroll only if the tocEntry is below the bottom half of the window,
// scrolling to that position.
document.querySelector("#ptx-toc").scrollTop = tocEntry.offsetTop - 0.4 * self.innerHeight;
}
function toggletoc() {
thesidebar = document.getElementById("ptx-sidebar");
if (thesidebar.classList.contains("hidden") || thesidebar.classList.contains("visible")) {
thesidebar.classList.toggle("hidden");
thesidebar.classList.toggle("visible");
} else if (thesidebar.offsetParent === null) { /* not currently visible */
thesidebar.classList.toggle("visible");
} else {
thesidebar.classList.toggle("hidden");
}
scrollTocToActive();
}
window.addEventListener("DOMContentLoaded",function(event) {
thetocbutton = document.getElementsByClassName("toc-toggle")[0];
thetocbutton.addEventListener('click', () => toggletoc() );
});
window.addEventListener("DOMContentLoaded",function(event) {
scrollTocToActive();
});
/* jump to next page if reader tries to scroll past the bottom */
// var hitbottom = false;
// window.onscroll = function(ev) {
// if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
// // you're at the bottom of the page
// console.log("Bottom of page");
// if (hitbottom) {
// console.log("hit bottom again");
// thenextbutton = document.getElementsByClassName("next-button")[0];
// thenextbutton.click();
// } else {
// hitbottom = true;
// /* only jump to next page if hard scroll in quick succession */
// window.scrollBy(0, -20);
// setTimeout(function (){ hitbottom = false }, 1000);
// }
// }
// };
//-----------------------------------------------------------------------------
// Dynamic TOC logic
//-----------------------------------------------------------------------------
//item is assumed to be expander in toc-item
function toggleTOCItem(expander) {
let listItem = expander.closest(".toc-item");
listItem.classList.toggle("expanded");
let expanded = listItem.classList.contains("expanded");
let itemType = getTOCItemType(listItem);
if(expanded) {
expander.title = "Close" + (itemType !== "" ? " " + itemType : "");
} else {
expander.title = "Expand" + (itemType !== "" ? " " + itemType : "");
}
//should be one of each... for/of for safety and built in null avoidance
for (const childUL of listItem.querySelectorAll(":scope > ul.toc-item-list")) {
for (const childItem of childUL.querySelectorAll(":scope > li.toc-item")) {
if(expanded) {
childItem.classList.add("visible");
childItem.classList.remove("hidden");
} else {
childItem.classList.remove("visible");
childItem.classList.add("hidden");
}
}
}
}
//finds item type from classes or empty string on failure
function getTOCItemType(item) {
//Type should be class that looks like toc-X where X is not item. Find it and return X
for(let className of item.classList) {
if(className !== "toc-item" && className.length > 3 && className.slice(0,4) === "toc-")
return className.slice(4);
}
return "";
}
//finds depth of toc-item as defined by number .toc-item-lists it is in
function getTOCItemDepth(item) {
let depth = 0;
let curParent = item.closest(".toc-item-list");
while(curParent !== null) {
depth++;
curParent = curParent.parentElement.closest(".toc-item-list");
}
return depth;
}
window.addEventListener("DOMContentLoaded", function(event) {
if(document.querySelector(".ptx-toc.focused") === null)
return; //only in focused mode
let maxDepth = 1000; //how deep TOC goes
//check toc for depth class and get value from there
for(let className of document.querySelector(".ptx-toc").classList)
if(className.length > 5 && className.slice(0,5) === "depth")
maxDepth = Number(className.slice(5));
let preexpandedLevels = 1; //how many levels to preexpand
let tocDataSet = document.querySelector(".ptx-toc").dataset;
if(typeof tocDataSet.preexpandedLevels !== 'undefined')
preexpandedLevels = Number(tocDataSet.preexpandedLevels);
let tocItems = document.querySelectorAll(".ptx-toc ul.structural > .toc-item");
for (const tocItem of tocItems) {
let hasChildren = tocItem.querySelector('ul.structural') !== null;
let depth = getTOCItemDepth(tocItem);
if(hasChildren && depth < maxDepth) {
let expander = document.createElement("button");
expander.classList.add('toc-expander');
expander.classList.add('toc-chevron-surround');
expander.title = 'toc-expander';
expander.innerHTML = '<span class="icon material-symbols-outlined" aria-hidden="true">chevron_left</span>';
tocItem.querySelector(".toc-title-box").append(expander);
expander.addEventListener('click', () => {
toggleTOCItem(expander);
});
let isActive = tocItem.classList.contains("contains-active") || tocItem.classList.contains("active");
let preExpanded = isActive || depth < preexpandedLevels;
let itemType = getTOCItemType(tocItem);
if(preExpanded) {
toggleTOCItem(expander);
} else {
expander.title = "Expand" + (itemType !== "" ? " " + itemType : "");
}
}
}
});