2019-08-15 17:40:13 +00:00
|
|
|
/*
|
|
|
|
* Citizen - ToC JS
|
|
|
|
* https://starcitizen.tools
|
|
|
|
*
|
|
|
|
* Smooth scroll fallback and Scrollspy
|
|
|
|
*/
|
|
|
|
|
|
|
|
const SmoothScroll = () => {
|
2019-12-25 23:24:31 +00:00
|
|
|
if (!('scrollBehavior' in document.documentElement.style)) {
|
|
|
|
const navLinks = document.querySelectorAll('#toc a');
|
|
|
|
|
|
|
|
const eventListener = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
document.querySelector(navLinks[n].hash)
|
|
|
|
.scrollIntoView({
|
|
|
|
behavior: "smooth"
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let n in navLinks) {
|
|
|
|
if (navLinks.hasOwnProperty(n)) {
|
|
|
|
navLinks[n].addEventListener('click', eventListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-08-15 17:40:13 +00:00
|
|
|
|
|
|
|
const ScrollSpy = () => {
|
2019-12-25 23:24:31 +00:00
|
|
|
const sections = document.querySelectorAll('.mw-headline');
|
|
|
|
|
|
|
|
window.addEventListener('scroll', () => {
|
|
|
|
const scrollPos = document.documentElement.scrollTop || document.body.scrollTop;
|
2019-08-15 17:40:13 +00:00
|
|
|
|
2019-12-25 23:24:31 +00:00
|
|
|
for (let s in sections) {
|
|
|
|
if (sections.hasOwnProperty(s) && sections[s].offsetTop <= scrollPos) {
|
|
|
|
const id = mw.util.escapeIdForAttribute(sections[s].id);
|
|
|
|
document.querySelector('.active').classList.remove('active');
|
2019-08-15 17:40:13 +00:00
|
|
|
|
2019-12-25 23:24:31 +00:00
|
|
|
const node = document.querySelector(`a[href * = "${id}"]`).parentNode;
|
|
|
|
if (node !== null) {
|
|
|
|
node.classList.add('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2019-08-15 17:40:13 +00:00
|
|
|
|
2019-08-19 04:24:40 +00:00
|
|
|
const CheckToC = () => {
|
2019-12-25 23:24:31 +00:00
|
|
|
if (document.getElementById('toc')) {
|
|
|
|
SmoothScroll();
|
|
|
|
ScrollSpy();
|
|
|
|
}
|
|
|
|
};
|
2019-08-19 04:24:40 +00:00
|
|
|
|
2019-08-19 05:30:24 +00:00
|
|
|
if (document.readyState !== 'loading') {
|
2019-12-25 23:24:31 +00:00
|
|
|
CheckToC();
|
2019-08-19 05:30:24 +00:00
|
|
|
} else if (document.addEventListener) {
|
2019-12-25 23:24:31 +00:00
|
|
|
// All modern browsers to register DOMContentLoaded
|
|
|
|
document.addEventListener('DOMContentLoaded', CheckToC);
|
2019-08-19 05:30:24 +00:00
|
|
|
} else {
|
2019-12-25 23:24:31 +00:00
|
|
|
// Old IE browsers
|
|
|
|
document.attachEvent('onreadystatechange', function () {
|
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
CheckToC();
|
|
|
|
}
|
|
|
|
});
|
2019-08-19 04:24:40 +00:00
|
|
|
}
|