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:
// --- Fixed-Width Bingo Board with Hover & Persistence ---
// --- Bingo Board with Hover Images & Custom Tooltip ---
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {


Line 10: Line 10:


         // Get items from template
         // Get items from template
        // Format: Item Name::Custom Hover Text
         var itemsAttr = container.getAttribute("data-items");
         var itemsAttr = container.getAttribute("data-items");
         var items = itemsAttr.split("|").filter(i => i.trim() !== "");
         var items = itemsAttr.split("|").filter(i => i.trim() !== "");
         if (!items.length) return;
         if (!items.length) return;


         // Parse item names + tooltips
         // Parse item names + tooltip
         var parsedItems = items.map(function(entry){
         var parsedItems = items.map(function(entry){
             var parts = entry.split("::");
             var parts = entry.split("::");
Line 25: Line 26:
         // Build table
         // Build table
         var table = document.createElement("table");
         var table = document.createElement("table");
         table.style.tableLayout = "fixed";             // fixed-width columns
         table.style.tableLayout = "fixed";   // uniform column widths
         table.style.border = "2px solid #596e96";
         table.style.border = "2px solid #596e96";
         table.style.borderCollapse = "collapse";
         table.style.borderCollapse = "collapse";
         table.style.borderRadius = "8px";
         table.style.borderRadius = "8px";
         table.style.width = "100%";
         table.style.width = "100%";
         table.style.maxWidth = "600px";
         table.style.maxWidth = "500px";


         container.appendChild(table);
         container.appendChild(table);
Line 61: Line 62:


                     let cell = row.insertCell();
                     let cell = row.insertCell();
                    cell.style.width = `${100 / cols}%`;
                     cell.style.padding = "10px";
                     cell.style.padding = "10px";
                     cell.style.border = "1px solid #444";
                     cell.style.border = "1px solid #444";
                     cell.style.textAlign = "center";
                     cell.style.textAlign = "center";
                    cell.style.background = "#313e59";
                     cell.style.color = "#fff";
                     cell.style.color = "#fff";
                    cell.style.background = "#313e59";
                     cell.style.cursor = "pointer";
                     cell.style.cursor = "help";
                     cell.style.transition = "all 0.2s ease";
                     cell.style.transition = "all 0.2s ease";
                     cell.style.userSelect = "none";
                     cell.style.userSelect = "none";
                    // Fixed width for uniform tiles
                    cell.style.width = `${100 / cols}%`;
                     cell.style.wordWrap = "break-word";
                     cell.style.wordWrap = "break-word";
                     cell.style.overflow = "hidden";
                     cell.style.overflow = "hidden";
Line 78: Line 77:
                     cell.id = id;
                     cell.id = id;


                     // Tooltip
                     // Hover tooltip using your template
                     cell.title = item.tooltip;
                    // {{Hover info4|text=...|...}}
                     let hoverMarkup = `{{Hover info4|text=[[File:${item.name}.png|link=]]|[[File:${item.name}.png|link=|18px]] ${item.tooltip}}}`;
                    cell.innerHTML = hoverMarkup;


                    // Add image
                     // Load completed state (light green faded)
                    if (imgs[index]) {
                        let img = document.createElement("img");
                        img.src = imgs[index].src;
                        img.alt = item.name;
                        img.style.width = "40px";
                        img.style.height = "auto";
                        img.style.display = "block";
                        img.style.margin = "0 auto 5px auto";
                        cell.appendChild(img);
                    }
 
                    // Item name
                    let text = document.createElement("div");
                    text.textContent = item.name;
                    cell.appendChild(text);
 
                     // Load completed state
                     if (localStorage.getItem(id) === "true") {
                     if (localStorage.getItem(id) === "true") {
                         cell.style.background = "rgba(76, 175, 80, 0.5)";
                         cell.style.background = "rgba(76, 175, 80, 0.5)";
                     }
                     }


                     // Hover effect
                     // Hover effect: darken tile if not completed
                     cell.addEventListener("mouseenter", function () {
                     cell.addEventListener("mouseenter", function () {
                         if (localStorage.getItem(this.id) !== "true") {
                         if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b";
                            this.style.background = "#3b4a6b";
                        }
                     });
                     });


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


                     // Click toggle
                     // Click toggle: mark complete
                     cell.addEventListener("click", function () {
                     cell.addEventListener("click", function () {
                         var done = localStorage.getItem(this.id) === "true";
                         var done = localStorage.getItem(this.id) === "true";
Line 155: Line 135:
         });
         });


        // Update progress counter
         function updateProgress(container, total) {
         function updateProgress(container, total) {
             var completed = 0;
             var completed = 0;
Line 168: Line 149:
                 progressDiv.textContent = `Completed ${completed} of ${total} items`;
                 progressDiv.textContent = `Completed ${completed} of ${total} items`;
         }
         }
     });
     });
});
});

Revision as of 12:56, 7 March 2026

// --- Bingo Board with Hover Images & Custom Tooltip ---
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
        // Format: Item Name::Custom Hover Text
        var itemsAttr = container.getAttribute("data-items");
        var items = itemsAttr.split("|").filter(i => i.trim() !== "");
        if (!items.length) return;

        // Parse item names + tooltip
        var parsedItems = items.map(function(entry){
            var parts = entry.split("::");
            return {
                name: parts[0].trim(),
                tooltip: parts[1] ? parts[1].trim() : parts[0].trim()
            };
        });

        // Build table
        var table = document.createElement("table");
        table.style.tableLayout = "fixed";   // uniform column widths
        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(parsedItems.length));
        var cols = Math.ceil(parsedItems.length / rows);

        // Build a single API request with all plinkp templates
        var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");

        $.getJSON("/api.php", {
            action: "parse",
            text: allTemplates,
            format: "json"
        }).done(function(data) {
            if (!data.parse || !data.parse.text) return;

            var tempDiv = document.createElement("div");
            tempDiv.innerHTML = data.parse.text["*"];
            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 >= parsedItems.length) break;

                    let item = parsedItems[index];

                    let cell = row.insertCell();
                    cell.style.width = `${100 / cols}%`;
                    cell.style.padding = "10px";
                    cell.style.border = "1px solid #444";
                    cell.style.textAlign = "center";
                    cell.style.background = "#313e59";
                    cell.style.color = "#fff";
                    cell.style.cursor = "pointer";
                    cell.style.transition = "all 0.2s ease";
                    cell.style.userSelect = "none";
                    cell.style.wordWrap = "break-word";
                    cell.style.overflow = "hidden";

                    let id = `${container.id}_cell_${index}`;
                    cell.id = id;

                    // Hover tooltip using your template
                    // {{Hover info4|text=...|...}}
                    let hoverMarkup = `{{Hover info4|text=[[File:${item.name}.png|link=]]|[[File:${item.name}.png|link=|18px]] ${item.tooltip}}}`;
                    cell.innerHTML = hoverMarkup;

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

                    // Hover effect: darken tile if not completed
                    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: mark complete
                    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, parsedItems.length);
                    });
                }
            }

            // Progress display
            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 < parsedItems.length; i++) {
                    var cb = document.getElementById(`${container.id}_cell_${i}`);
                    if (cb) {
                        localStorage.setItem(cb.id, false);
                        cb.style.background = "#313e59";
                    }
                }
                updateProgress(container, parsedItems.length);
            });

            container.appendChild(resetBtn);

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

        // Update progress counter
        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`;
        }

    });
});