MediaWiki:Common.js: Difference between revisions

From Realm Of Thrones
Jump to navigation Jump to search
SitX (talk | contribs)
No edit summary
SitX (talk | contribs)
No edit summary
Line 8: Line 8:
     floatingToc.appendChild(toc);
     floatingToc.appendChild(toc);
     document.body.appendChild(floatingToc);
     document.body.appendChild(floatingToc);


     var hideButton = floatingToc.querySelector(".toctogglelabel");
     var hideButton = floatingToc.querySelector(".toctogglelabel");
Line 13: Line 14:
     if (hideButton) {
     if (hideButton) {
         hideButton.addEventListener("click", function (event) {
         hideButton.addEventListener("click", function (event) {
             event.preventDefault();  
             event.preventDefault();
             floatingToc.classList.toggle("collapsed");
             floatingToc.classList.toggle("collapsed");
             event.stopPropagation();  
             event.stopPropagation();
         });
         });
     }
     }


     toc.addEventListener("click", function (event) {
     toc.addEventListener("click", function (event) {
         event.stopPropagation();
         event.stopPropagation();
     });
     });


     floatingToc.addEventListener("click", function (event) {
     floatingToc.addEventListener("click", function (event) {
         if (floatingToc.classList.contains("collapsed") && event.target === floatingToc) {
         if (floatingToc.classList.contains("collapsed") && event.target.id === "floating-toc-show-button") {
             floatingToc.classList.remove("collapsed");
             floatingToc.classList.remove("collapsed");
         }
         }
     });
     });
    var showButton = document.createElement("div");
    showButton.id = "floating-toc-show-button";
    showButton.innerText = "Contents ▼";
    floatingToc.appendChild(showButton);
});
});

Revision as of 03:22, 25 March 2025

document.addEventListener("DOMContentLoaded", function () {
    var toc = document.querySelector("#mw-content-text #toc");
    if (!toc) return;

    var floatingToc = document.createElement("div");
    floatingToc.id = "floating-toc";
    toc.parentNode.removeChild(toc);
    floatingToc.appendChild(toc);
    document.body.appendChild(floatingToc);


    var hideButton = floatingToc.querySelector(".toctogglelabel");

    if (hideButton) {
        hideButton.addEventListener("click", function (event) {
            event.preventDefault();
            floatingToc.classList.toggle("collapsed");
            event.stopPropagation();
        });
    }


    toc.addEventListener("click", function (event) {
        event.stopPropagation();
    });


    floatingToc.addEventListener("click", function (event) {
        if (floatingToc.classList.contains("collapsed") && event.target.id === "floating-toc-show-button") {
            floatingToc.classList.remove("collapsed");
        }
    });


    var showButton = document.createElement("div");
    showButton.id = "floating-toc-show-button";
    showButton.innerText = "Contents ▼";
    floatingToc.appendChild(showButton);
});