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:
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {


    // Auto-create container if it doesn't exist
     var containers = document.querySelectorAll(".bingoContainer");
     var container = document.getElementById("bingoContainer");
     if (!containers.length) return;
     if (!container) {
        container = document.createElement("div");
        container.id = "bingoContainer";
        container.style.margin = "20px 0";
        document.body.appendChild(container);
    }
    container.innerHTML = ""; // clear container


     var table = document.createElement("table");
     containers.forEach(function(container) {
    table.style.border = "2px solid #596e96";
    table.style.borderCollapse = "collapse";
    table.style.borderRadius = "8px";
    table.style.width = "100%";
    table.style.maxWidth = "500px";


    var rows = 5, cols = 5;
        container.innerHTML = ""; // Clear any previous content


    // Example images array (replace with your wiki images)
        var itemsAttr = container.getAttribute("data-items");
    var images = [
         if (!itemsAttr) return;
        "https://wiki.roatpkz.com/images/Item1.png",
        "https://wiki.roatpkz.com/images/Item2.png",
         "https://wiki.roatpkz.com/images/Item3.png",
        "https://wiki.roatpkz.com/images/Item4.png",
        "https://wiki.roatpkz.com/images/Item5.png",
        "https://wiki.roatpkz.com/images/Item6.png",
        "https://wiki.roatpkz.com/images/Item7.png",
        "https://wiki.roatpkz.com/images/Item8.png",
        "https://wiki.roatpkz.com/images/Item9.png",
        "https://wiki.roatpkz.com/images/Item10.png",
        "https://wiki.roatpkz.com/images/Item11.png",
        "https://wiki.roatpkz.com/images/Item12.png",
        "https://wiki.roatpkz.com/images/Item13.png",
        "https://wiki.roatpkz.com/images/Item14.png",
        "https://wiki.roatpkz.com/images/Item15.png",
        "https://wiki.roatpkz.com/images/Item16.png",
        "https://wiki.roatpkz.com/images/Item17.png",
        "https://wiki.roatpkz.com/images/Item18.png",
        "https://wiki.roatpkz.com/images/Item19.png",
        "https://wiki.roatpkz.com/images/Item20.png",
        "https://wiki.roatpkz.com/images/Item21.png",
        "https://wiki.roatpkz.com/images/Item22.png",
        "https://wiki.roatpkz.com/images/Item23.png",
        "https://wiki.roatpkz.com/images/Item24.png",
        "https://wiki.roatpkz.com/images/Item25.png"
    ];


    // Create grid
        var items = itemsAttr.split("|").filter(i => i.trim() !== "");
    for (var r = 0; r < rows; r++) {
         if (!items.length) return;
         var row = table.insertRow();


         for (var c = 0; c < cols; c++) {
         var table = document.createElement("table");
            var cell = row.insertCell();
        table.style.border = "2px solid #596e96";
            cell.style.padding = "10px";
        table.style.borderCollapse = "collapse";
            cell.style.border = "1px solid #444";
        table.style.borderRadius = "8px";
            cell.style.textAlign = "center";
        table.style.width = "100%";
            cell.style.color = "#fff";
        table.style.maxWidth = "500px";
            cell.style.background = "#313e59"; // uniform tile color
            cell.style.cursor = "pointer";
            cell.style.transition = "background 0.2s ease";
            cell.style.userSelect = "none";


            var id = `cell_${r}_${c}`;
        var rows = Math.ceil(Math.sqrt(items.length));
        var cols = Math.ceil(items.length / rows);


            // Image
        for (var r = 0; r < rows; r++) {
             var img = document.createElement("img");
             var row = table.insertRow();
             img.src = images[r*cols + c];
             for (var c = 0; c < cols; c++) {
            img.alt = `Item ${r*cols+c+1}`;
                var index = r * cols + c;
            img.style.width = "40px";
                if (index >= items.length) break;
            img.style.height = "40px";
            img.style.display = "block";
            img.style.margin = "0 auto 5px auto";
            cell.appendChild(img);


            // Text label
                var cell = row.insertCell();
            cell.appendChild(document.createTextNode(` Item ${r*cols+c+1}`));
                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";


            // Restore state from localStorage
                var itemName = items[index];
            if (localStorage.getItem(id) === "true") {
                var id = `${container.id}_cell_${index}`;
                 cell.style.background = "#222e45"; // darker shade for completed
                cell.id = id;
 
                // Use your plinkt template to get the image
                $.getJSON("/api.php", {
                    action: "parse",
                    text: `{{plinkt|${itemName}}}`,
                    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.insertBefore(img, cell.firstChild);
                        }
                        cell.appendChild(document.createTextNode(itemName));
                    }
                });
 
                if (localStorage.getItem(id) === "true") {
                    cell.style.background = "#222e45";
                }
 
                // Hover effects
                 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 ? "#222e45" : "#313e59";
                    updateProgress(container, items.length);
                });
             }
             }
            // Click to toggle completed
            cell.addEventListener("click", function () {
                var done = localStorage.getItem(this.id) === "true";
                localStorage.setItem(this.id, !done);
                this.style.background = !done ? "#222e45" : "#313e59";
                updateProgress();
            });
         }
         }
    }


    container.appendChild(table);
        container.appendChild(table);


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


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


    // Update progress function
     function updateProgress(container, total) {
     function updateProgress() {
         var completed = 0;
         var checked = 0;
         for (var i = 0; i < total; i++) {
         for (var r = 0; r < rows; r++) {
             var cb = document.getElementById(`${container.id}_cell_${i}`);
             for (var c = 0; c < cols; c++) {
            if (cb && localStorage.getItem(cb.id) === "true") completed++;
                var cb = document.getElementById(`cell_${r}_${c}`);
                if (cb && localStorage.getItem(cb.id) === "true") checked++;
            }
         }
         }
         progressDiv.textContent = `Completed ${checked} of ${rows*cols} items`;
         var progressDiv = container.querySelector("div");
        if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`;
     }
     }


    updateProgress();
});
});

Revision as of 02:16, 7 March 2026

mw.hook('wikipage.content').add(function () {

    var containers = document.querySelectorAll(".bingoContainer");
    if (!containers.length) return;

    containers.forEach(function(container) {

        container.innerHTML = ""; // Clear any previous content

        var itemsAttr = container.getAttribute("data-items");
        if (!itemsAttr) return;

        var items = itemsAttr.split("|").filter(i => i.trim() !== "");
        if (!items.length) return;

        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";

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

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

                var 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";

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

                // Use your plinkt template to get the image
                $.getJSON("/api.php", {
                    action: "parse",
                    text: `{{plinkt|${itemName}}}`,
                    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.insertBefore(img, cell.firstChild);
                        }
                        cell.appendChild(document.createTextNode(itemName));
                    }
                });

                if (localStorage.getItem(id) === "true") {
                    cell.style.background = "#222e45";
                }

                // Hover effects
                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 ? "#222e45" : "#313e59";
                    updateProgress(container, items.length);
                });
            }
        }

        container.appendChild(table);

        // Progress
        var progressDiv = document.createElement("div");
        progressDiv.style.marginTop = "10px";
        progressDiv.style.color = "#fff";
        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("div");
        if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`;
    }

});