Converted toc script from ES6 to ES5

This commit is contained in:
alistair3149 2020-02-15 17:33:53 -05:00
parent 54f03d7c3a
commit ed22023fce
No known key found for this signature in database
GPG key ID: 94D081060FD3DD9C

View file

@ -5,66 +5,67 @@
* Smooth scroll fallback and Scrollspy * Smooth scroll fallback and Scrollspy
*/ */
const SmoothScroll = () => { var SmoothScroll = function SmoothScroll() {
if ( !( 'scrollBehavior' in document.documentElement.style ) ) { if (!("scrollBehavior" in document.documentElement.style)) {
const navLinks = document.querySelectorAll( '#toc a' ), var navLinks = document.querySelectorAll("#toc a"),
eventListener = e => { eventListener = function eventListener(e) {
e.preventDefault(); e.preventDefault();
e.target.scrollIntoView( { e.target.scrollIntoView({
behavior: 'smooth' behavior: "smooth"
} ); });
}; };
for ( let link in navLinks ) { for (var link in navLinks) {
if ( Object.prototype.hasOwnProperty.call( navLinks, link ) ) { if (Object.prototype.hasOwnProperty.call(navLinks, link)) {
navLinks[ link ].addEventListener( 'click', eventListener ); navLinks[link].addEventListener("click", eventListener);
} }
} }
} }
}, },
ScrollSpy = function ScrollSpy() {
var sections = document.querySelectorAll(".mw-headline");
window.addEventListener("scroll", function() {
var scrollPos =
document.documentElement.scrollTop || document.body.scrollTop;
ScrollSpy = () => { for (var section in sections) {
const sections = document.querySelectorAll( '.mw-headline' ); if (
Object.prototype.hasOwnProperty.call(sections, section) &&
sections[section].offsetTop <= scrollPos
) {
var id = mw.util.escapeIdForAttribute(sections[section].id),
node = document.querySelector('a[href * = "'.concat(id, '"]'))
.parentNode,
active = document.querySelector(".active");
window.addEventListener( 'scroll', () => { if (active !== null) {
const scrollPos = document.documentElement.scrollTop || document.body.scrollTop; active.classList.remove("active");
}
for ( let section in sections ) { if (node !== null) {
if ( Object.prototype.hasOwnProperty.call( sections, section ) && node.classList.add("active");
sections[ section ].offsetTop <= scrollPos ) { }
const id = mw.util.escapeIdForAttribute( sections[ section ].id ), }
node = document.querySelector( `a[href * = "${id}"]` ).parentNode, }
active = document.querySelector( '.active' ); });
},
CheckToC = function CheckToC() {
if (document.getElementById("toc")) {
SmoothScroll();
ScrollSpy();
}
};
if ( active !== null ) { if (document.readyState !== "loading") {
active.classList.remove( 'active' ); CheckToC();
} } else if (document.addEventListener) {
// All modern browsers to register DOMContentLoaded
if ( node !== null ) { document.addEventListener("DOMContentLoaded", CheckToC);
node.classList.add( 'active' );
}
}
}
} );
},
CheckToC = () => {
if ( document.getElementById( 'toc' ) ) {
SmoothScroll();
ScrollSpy();
}
};
if ( document.readyState !== 'loading' ) {
CheckToC();
} else if ( document.addEventListener ) {
// All modern browsers to register DOMContentLoaded
document.addEventListener( 'DOMContentLoaded', CheckToC );
} else { } else {
// Old IE browsers // Old IE browsers
document.attachEvent( 'onreadystatechange', function () { document.attachEvent("onreadystatechange", function() {
if ( document.readyState === 'complete' ) { if (document.readyState === "complete") {
CheckToC(); CheckToC();
} }
} ); });
} }