MediaWiki:BingoSheet.js: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
// --- Bingo Board with plinkp images and green completed tiles ---
// --- Optimized Bingo Board with single API call ---
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {
     var containers = document.querySelectorAll(".bingoContainer");
     var containers = document.querySelectorAll(".bingoContainer");
Line 19: Line 19:
         table.style.width = "100%";
         table.style.width = "100%";
         table.style.maxWidth = "500px";
         table.style.maxWidth = "500px";
        container.appendChild(table);


         var rows = Math.ceil(Math.sqrt(items.length));
         var rows = Math.ceil(Math.sqrt(items.length));
         var cols = Math.ceil(items.length / rows);
         var cols = Math.ceil(items.length / rows);


         for (let r = 0; r < rows; r++) {
         // Build a single API request with all plinkp templates
            var row = table.insertRow();
        var allTemplates = items.map(name => `{{plinkp|${name}}}`).join("\n");
             for (let c = 0; c < cols; c++) {
        $.getJSON("/api.php", {
                let index = r * cols + c;
             action: "parse",
                if (index >= items.length) break;
            text: allTemplates,
            format: "json"
        }).done(function(data) {
            if (!data.parse || !data.parse.text) return;


                let cell = row.insertCell();
            // Create a temporary div to parse HTML
                cell.style.padding = "10px";
            var tempDiv = document.createElement("div");
                cell.style.border = "1px solid #596e96";
            tempDiv.innerHTML = data.parse.text["*"];
                cell.style.textAlign = "center";
                cell.style.color = "#fff";
                cell.style.background = "#313e59";
                cell.style.cursor = "pointer";
                cell.style.transition = "background 0.2s ease";
                cell.style.userSelect = "none";


                let itemName = items[index];
            // Extract all images
                let id = `${container.id}_cell_${index}`;
            var imgs = tempDiv.querySelectorAll("img");
                cell.id = id;


                // Use plinkp template for images
            for (let r = 0; r < rows; r++) {
                (function(cell, itemName){
                var row = table.insertRow();
                    $.getJSON("/api.php", {
                for (let c = 0; c < cols; c++) {
                        action: "parse",
                    let index = r * cols + c;
                        text: `{{plinkp|${itemName}}}`,
                     if (index >= items.length) break;
                        format: "json"
                    }).done(function(data) {
                        if (data.parse && data.parse.text) {
                            var html = document.createElement("div");
                            html.innerHTML = data.parse.text["*"];
                            var imgTag = html.querySelector("img");
                            if (imgTag) {
                                var img = document.createElement("img");
                                img.src = imgTag.src;
                                img.alt = itemName;
                                img.style.width = "40px";
                                img.style.height = "40px";
                                img.style.display = "block";
                                img.style.margin = "0 auto 5px auto";
                                cell.appendChild(img);
                            }
                            var text = document.createElement("div");
                            text.textContent = itemName;
                            cell.appendChild(text);
                        }
                     });
                })(cell, itemName);


                // Load completed state (light green, faded)
                    let cell = row.insertCell();
                if (localStorage.getItem(id) === "true") {
                    cell.style.padding = "10px";
                     cell.style.background = "rgba(76, 175, 80, 0.5)"; // light green faded
                    cell.style.border = "1px solid #444";
                }
                    cell.style.textAlign = "center";
                    cell.style.color = "#fff";
                     cell.style.background = "#313e59";
                    cell.style.cursor = "pointer";
                    cell.style.transition = "background 0.2s ease";
                    cell.style.userSelect = "none";


                // Hover effect
                    let itemName = items[index];
                cell.addEventListener("mouseenter", function () {
                     let id = `${container.id}_cell_${index}`;
                     if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b";
                    cell.id = id;
                });
                cell.addEventListener("mouseleave", function () {
                    if (localStorage.getItem(this.id) !== "true") this.style.background = "#313e59";
                });


                // Click toggle
                    // Add image and name
                cell.addEventListener("click", function () {
                    if (imgs[index]) {
                    var done = localStorage.getItem(this.id) === "true";
                        let img = document.createElement("img");
                    localStorage.setItem(this.id, !done);
                        img.src = imgs[index].src;
                    this.style.background = !done ? "rgba(76, 175, 80, 0.5)" : "#313e59";
                        img.alt = itemName;
                     updateProgress(container, items.length);
                        img.style.width = "40px";
                });
                        img.style.height = "40px";
            }
                        img.style.display = "block";
        }
                        img.style.margin = "0 auto 5px auto";
                        cell.appendChild(img);
                    }
                    let text = document.createElement("div");
                     text.textContent = itemName;
                    cell.appendChild(text);


        container.appendChild(table);
                    // Load completed state (light green faded)
                    if (localStorage.getItem(id) === "true") {
                        cell.style.background = "rgba(76, 175, 80, 0.5)";
                    }


        // Progress display (dedicated div)
                    // Hover effect
        var progressDiv = document.createElement("div");
                    cell.addEventListener("mouseenter", function () {
        progressDiv.style.marginTop = "10px";
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b";
        progressDiv.style.color = "#fff";
                    });
        progressDiv.className = "bingoProgress";
                    cell.addEventListener("mouseleave", function () {
        container.appendChild(progressDiv);
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#313e59";
                    });


        // Reset button
                    // Click toggle
        var resetBtn = document.createElement("button");
                    cell.addEventListener("click", function () {
        resetBtn.textContent = "Reset Bingo";
                        var done = localStorage.getItem(this.id) === "true";
        resetBtn.style.marginTop = "5px";
                        localStorage.setItem(this.id, !done);
        resetBtn.style.cursor = "pointer";
                        this.style.background = !done ? "rgba(76, 175, 80, 0.5)" : "#313e59";
        resetBtn.addEventListener("click", function () {
                        updateProgress(container, items.length);
            for (var i = 0; i < items.length; i++) {
                    });
                var cb = document.getElementById(`${container.id}_cell_${i}`);
                if (cb) {
                    localStorage.setItem(cb.id, false);
                    cb.style.background = "#313e59";
                 }
                 }
             }
             }
            // Progress display (dedicated div)
            var progressDiv = document.createElement("div");
            progressDiv.style.marginTop = "10px";
            progressDiv.style.color = "#fff";
            progressDiv.className = "bingoProgress";
            container.appendChild(progressDiv);
            // Reset button
            var resetBtn = document.createElement("button");
            resetBtn.textContent = "Reset Bingo";
            resetBtn.style.marginTop = "5px";
            resetBtn.style.cursor = "pointer";
            resetBtn.addEventListener("click", function () {
                for (var i = 0; i < items.length; i++) {
                    var cb = document.getElementById(`${container.id}_cell_${i}`);
                    if (cb) {
                        localStorage.setItem(cb.id, false);
                        cb.style.background = "#313e59";
                    }
                }
                updateProgress(container, items.length);
            });
            container.appendChild(resetBtn);
             updateProgress(container, items.length);
             updateProgress(container, items.length);
         });
         });
        container.appendChild(resetBtn);


         updateProgress(container, items.length);
         function updateProgress(container, total) {
            var completed = 0;
            for (var i = 0; i < total; i++) {
                var cb = document.getElementById(`${container.id}_cell_${i}`);
                if (cb && localStorage.getItem(cb.id) === "true") completed++;
            }
            var progressDiv = container.querySelector(".bingoProgress");
            if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`;
        }
     });
     });
    // Update progress using the dedicated progress div
    function updateProgress(container, total) {
        var completed = 0;
        for (var i = 0; i < total; i++) {
            var cb = document.getElementById(`${container.id}_cell_${i}`);
            if (cb && localStorage.getItem(cb.id) === "true") completed++;
        }
        var progressDiv = container.querySelector(".bingoProgress");
        if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`;
    }
});
});

Revision as of 02:33, 7 March 2026

// --- Optimized Bingo Board with single API call ---
mw.hook('wikipage.content').add(function () {
    var containers = document.querySelectorAll(".bingoContainer");
    if (!containers.length) return;

    containers.forEach(function(container) {
        container.innerHTML = "";

        // Get items from template
        var itemsAttr = container.getAttribute("data-items");
        var items = itemsAttr.split("|").filter(i => i.trim() !== "");
        if (!items.length) return;

        // Build table
        var table = document.createElement("table");
        table.style.border = "2px solid #596e96";
        table.style.borderCollapse = "collapse";
        table.style.borderRadius = "8px";
        table.style.width = "100%";
        table.style.maxWidth = "500px";

        container.appendChild(table);

        var rows = Math.ceil(Math.sqrt(items.length));
        var cols = Math.ceil(items.length / rows);

        // Build a single API request with all plinkp templates
        var allTemplates = items.map(name => `{{plinkp|${name}}}`).join("\n");
        $.getJSON("/api.php", {
            action: "parse",
            text: allTemplates,
            format: "json"
        }).done(function(data) {
            if (!data.parse || !data.parse.text) return;

            // Create a temporary div to parse HTML
            var tempDiv = document.createElement("div");
            tempDiv.innerHTML = data.parse.text["*"];

            // Extract all images
            var imgs = tempDiv.querySelectorAll("img");

            for (let r = 0; r < rows; r++) {
                var row = table.insertRow();
                for (let c = 0; c < cols; c++) {
                    let index = r * cols + c;
                    if (index >= items.length) break;

                    let cell = row.insertCell();
                    cell.style.padding = "10px";
                    cell.style.border = "1px solid #444";
                    cell.style.textAlign = "center";
                    cell.style.color = "#fff";
                    cell.style.background = "#313e59";
                    cell.style.cursor = "pointer";
                    cell.style.transition = "background 0.2s ease";
                    cell.style.userSelect = "none";

                    let itemName = items[index];
                    let id = `${container.id}_cell_${index}`;
                    cell.id = id;

                    // Add image and name
                    if (imgs[index]) {
                        let img = document.createElement("img");
                        img.src = imgs[index].src;
                        img.alt = itemName;
                        img.style.width = "40px";
                        img.style.height = "40px";
                        img.style.display = "block";
                        img.style.margin = "0 auto 5px auto";
                        cell.appendChild(img);
                    }
                    let text = document.createElement("div");
                    text.textContent = itemName;
                    cell.appendChild(text);

                    // Load completed state (light green faded)
                    if (localStorage.getItem(id) === "true") {
                        cell.style.background = "rgba(76, 175, 80, 0.5)";
                    }

                    // Hover effect
                    cell.addEventListener("mouseenter", function () {
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b";
                    });
                    cell.addEventListener("mouseleave", function () {
                        if (localStorage.getItem(this.id) !== "true") this.style.background = "#313e59";
                    });

                    // Click toggle
                    cell.addEventListener("click", function () {
                        var done = localStorage.getItem(this.id) === "true";
                        localStorage.setItem(this.id, !done);
                        this.style.background = !done ? "rgba(76, 175, 80, 0.5)" : "#313e59";
                        updateProgress(container, items.length);
                    });
                }
            }

            // Progress display (dedicated div)
            var progressDiv = document.createElement("div");
            progressDiv.style.marginTop = "10px";
            progressDiv.style.color = "#fff";
            progressDiv.className = "bingoProgress";
            container.appendChild(progressDiv);

            // Reset button
            var resetBtn = document.createElement("button");
            resetBtn.textContent = "Reset Bingo";
            resetBtn.style.marginTop = "5px";
            resetBtn.style.cursor = "pointer";
            resetBtn.addEventListener("click", function () {
                for (var i = 0; i < items.length; i++) {
                    var cb = document.getElementById(`${container.id}_cell_${i}`);
                    if (cb) {
                        localStorage.setItem(cb.id, false);
                        cb.style.background = "#313e59";
                    }
                }
                updateProgress(container, items.length);
            });
            container.appendChild(resetBtn);

            updateProgress(container, items.length);
        });

        function updateProgress(container, total) {
            var completed = 0;
            for (var i = 0; i < total; i++) {
                var cb = document.getElementById(`${container.id}_cell_${i}`);
                if (cb && localStorage.getItem(cb.id) === "true") completed++;
            }
            var progressDiv = container.querySelector(".bingoProgress");
            if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`;
        }
    });
});