From a40547ec1b5068574ab46f2e47429edf2fba54ce Mon Sep 17 00:00:00 2001 From: alistair3149 Date: Sat, 15 Feb 2020 17:52:32 -0500 Subject: [PATCH] Attempt to convert to ES5 --- resources/scripts/toc.js | 93 ++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/resources/scripts/toc.js b/resources/scripts/toc.js index 3dd64d49..6ee5d78b 100644 --- a/resources/scripts/toc.js +++ b/resources/scripts/toc.js @@ -5,56 +5,57 @@ * Smooth scroll fallback and Scrollspy */ -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" - }); - }; +function SmoothScroll() { + var navLinks, eventListener, link; + if (!("scrollBehavior" in document.documentElement.style)) { + navLinks = document.querySelectorAll("#toc a"), + eventListener = function eventListener(e) { + e.preventDefault(); + e.target.scrollIntoView({ + behavior: "smooth" + }); + }; - for (var link in navLinks) { - if (Object.prototype.hasOwnProperty.call(navLinks, link)) { - navLinks[link].addEventListener("click", eventListener); + for (link in navLinks) { + if (Object.prototype.hasOwnProperty.call(navLinks, link)) { + navLinks[link].addEventListener("click", eventListener); + } + } + } +}, +function ScrollSpy() { + var sections = document.querySelectorAll(".mw-headline"); + window.addEventListener("scroll", function() { + var scrollPos = + document.documentElement.scrollTop || document.body.scrollTop; + + 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"); + + if (active !== null) { + active.classList.remove("active"); + } + + if (node !== null) { + node.classList.add("active"); } } } - }, - ScrollSpy = function ScrollSpy() { - var sections = document.querySelectorAll(".mw-headline"); - window.addEventListener("scroll", function() { - var scrollPos = - document.documentElement.scrollTop || document.body.scrollTop; - - 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"); - - if (active !== null) { - active.classList.remove("active"); - } - - if (node !== null) { - node.classList.add("active"); - } - } - } - }); - }, - CheckToC = function CheckToC() { - if (document.getElementById("toc")) { - SmoothScroll(); - ScrollSpy(); - } - }; + }); +}, +function CheckToC() { + if (document.getElementById("toc")) { + SmoothScroll(); + ScrollSpy(); + } +}; if (document.readyState !== "loading") { CheckToC();