MediaWiki:Common.js

From Psalms: Layer by Layer
Revision as of 12:40, 15 January 2025 by Elizabeth.Robar (talk | contribs)
Jump to: navigation, search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

function toggleVisibility(containerId, className) {
    // console.log("Toggling visibility for " + className + " in " + containerId);
    var container = document.getElementById(containerId);
    if (container) {
        var elements = container.querySelectorAll("g." + className);
        for (var i = 0; i < elements.length; i++) {
            var element = elements[i];
            if (element.style.display === "none") {
                element.style.display = ""; // Show element
            } else {
                element.style.display = "none"; // Hide element
            }
        }
    } else {
        console.warn("Container with ID \"" + containerId + "\" not found.");
    }
}

function attachToggleListeners() {
    // Attach event listeners to all toggle links
    var toggleLinks = document.querySelectorAll("[data-container-id][data-class]");
    // console.log("Found " + toggleLinks.length + " links.");
    for (var i = 0; i < toggleLinks.length; i++) {
        (function (toggleLink) {
            toggleLink.addEventListener("click", function (event) {
                event.preventDefault(); // Prevent default link behavior
                var containerId = toggleLink.getAttribute("data-container-id");
                var className = toggleLink.getAttribute("data-class");
                toggleVisibility(containerId, className);
            });
        })(toggleLinks[i]);
    }
}


(function () {
    if (document.readyState === "complete" || document.readyState === "interactive") {
        // console.log("ReadyState is complete.");

		// if the document has already been loaded
		attachToggleListeners();
		        
        
    } else {
		console.log("ReadyState is " + document.readyState);

        document.addEventListener("DOMContentLoaded", function () {

			// if the document has already been loaded
			attachToggleListeners();
        });
    }

})();

// console.log("Common.js is loaded.");