From ed22023fce4621287c911dbc6b6bb22d8a9eee54 Mon Sep 17 00:00:00 2001 From: alistair3149 Date: Sat, 15 Feb 2020 17:33:53 -0500 Subject: [PATCH] Converted toc script from ES6 to ES5 --- resources/scripts/toc.js | 115 ++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/resources/scripts/toc.js b/resources/scripts/toc.js index 229d0bcb..3dd64d49 100644 --- a/resources/scripts/toc.js +++ b/resources/scripts/toc.js @@ -5,66 +5,67 @@ * Smooth scroll fallback and Scrollspy */ -const SmoothScroll = () => { - if ( !( 'scrollBehavior' in document.documentElement.style ) ) { - const navLinks = document.querySelectorAll( '#toc a' ), - eventListener = e => { - e.preventDefault(); - e.target.scrollIntoView( { - behavior: 'smooth' - } ); - }; +var SmoothScroll = function SmoothScroll() { + if (!("scrollBehavior" in document.documentElement.style)) { + var navLinks = document.querySelectorAll("#toc a"), + eventListener = function eventListener(e) { + e.preventDefault(); + e.target.scrollIntoView({ + behavior: "smooth" + }); + }; - for ( let link in navLinks ) { - if ( Object.prototype.hasOwnProperty.call( navLinks, link ) ) { - navLinks[ link ].addEventListener( 'click', eventListener ); - } - } - } - }, + for (var link in navLinks) { + if (Object.prototype.hasOwnProperty.call(navLinks, link)) { + 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 = () => { - const sections = document.querySelectorAll( '.mw-headline' ); + for (var section in sections) { + 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', () => { - const scrollPos = document.documentElement.scrollTop || document.body.scrollTop; + if (active !== null) { + active.classList.remove("active"); + } - for ( let section in sections ) { - if ( Object.prototype.hasOwnProperty.call( sections, section ) && - sections[ section ].offsetTop <= scrollPos ) { - const id = mw.util.escapeIdForAttribute( sections[ section ].id ), - node = document.querySelector( `a[href * = "${id}"]` ).parentNode, - active = document.querySelector( '.active' ); + if (node !== null) { + node.classList.add("active"); + } + } + } + }); + }, + CheckToC = function CheckToC() { + if (document.getElementById("toc")) { + SmoothScroll(); + ScrollSpy(); + } + }; - if ( active !== null ) { - active.classList.remove( 'active' ); - } - - if ( node !== null ) { - 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 ); +if (document.readyState !== "loading") { + CheckToC(); +} else if (document.addEventListener) { + // All modern browsers to register DOMContentLoaded + document.addEventListener("DOMContentLoaded", CheckToC); } else { - // Old IE browsers - document.attachEvent( 'onreadystatechange', function () { - if ( document.readyState === 'complete' ) { - CheckToC(); - } - } ); -} + // Old IE browsers + document.attachEvent("onreadystatechange", function() { + if (document.readyState === "complete") { + CheckToC(); + } + }); +} \ No newline at end of file