MediaWiki: Common.js: Difference between revisions

From Psalms: Layer by Layer
Jump to: navigation, search
mNo edit summary
Tag: Reverted
mNo edit summary
Tag: Manual revert
Line 57: Line 57:


// console.log("Common.js is loaded.");
// console.log("Common.js is loaded.");
document.addEventListener("DOMContentLoaded", function () {
    // Attach a delegated event listener to the document or a static parent element
    document.body.addEventListener("click", function (event) {
        // Check if the clicked element is a dynamically created link
        if (event.target.matches("a[data-custom-action]")) {
            event.preventDefault(); // Prevent the default behavior
            var action = event.target.getAttribute("data-custom-action");
            console.log("Custom action triggered:", action);
            // Call your custom function based on the action
            if (action === "exampleAction") {
                exampleFunction();
            }
        }
    });
    // Example custom function
    function exampleFunction() {
        alert("Dynamic link clicked!");
    }
});

Revision as of 17:03, 15 January 2025

/* 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.");