MediaWiki:TeamBingo.js: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
(Created page with "// --- Team Bingo Boards with Captains --- mw.hook('wikipage.content').add(function () { // Get current wiki username (MediaWiki global) var currentUser = mw.config.get('wgUserName'); var containers = document.querySelectorAll(".teamBingoContainer"); if (!containers.length) return; containers.forEach(function(container) { container.innerHTML = ""; // Read captain from data attribute var captain = container.getAttribute("dat...")
 
No edit summary
Line 1: Line 1:
// --- Team Bingo Boards with Captains ---
// --- Team Bingo Board with Shared Info Panel and Captain Control ---
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {
    // Get current wiki username (MediaWiki global)
    var currentUser = mw.config.get('wgUserName');
     var containers = document.querySelectorAll(".teamBingoContainer");
     var containers = document.querySelectorAll(".teamBingoContainer");
     if (!containers.length) return;
     if (!containers.length) return;
    // Use single shared info box
    var infoBox = document.getElementById("teamBingoInfoBox");
    if (!infoBox) {
        console.warn("Shared info box #teamBingoInfoBox not found");
        return;
    }


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


         // Read captain from data attribute
         // Get captain username for this team
         var captain = container.getAttribute("data-captain");
         var captain = container.getAttribute("data-captain");


         // Wrap board + info box
         // Get items: Item::Tooltip
        var wrapper = document.createElement("div");
        wrapper.style.display = "flex";
        wrapper.style.gap = "20px";
        wrapper.style.alignItems = "flex-start";
        container.parentNode.insertBefore(wrapper, container);
        wrapper.appendChild(container);
 
        // Create fixed info box
        var infoBox = document.createElement("div");
        infoBox.style.padding = "10px";
        infoBox.style.border = "1px solid #596e96";
        infoBox.style.backgroundColor = "#313e59";
        infoBox.style.color = "#fff";
        infoBox.style.borderRadius = "4px";
        infoBox.style.minWidth = "180px";
        infoBox.style.maxWidth = "220px";
        infoBox.style.whiteSpace = "normal";
        infoBox.style.wordWrap = "break-word";
        infoBox.style.fontSize = "14px";
        infoBox.style.transition = "all 0.2s ease";
        infoBox.innerHTML = "<strong>Hover a tile to see info here.</strong>";
        wrapper.appendChild(infoBox);
 
        // Parse items: Item Name::Tooltip (use %% for line breaks)
         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() !== "");
Line 57: Line 36:
         table.style.borderCollapse = "collapse";
         table.style.borderCollapse = "collapse";
         table.style.borderRadius = "8px";
         table.style.borderRadius = "8px";
         table.style.width = "600px";
         table.style.width = "600px"; // fixed width
         container.appendChild(table);
         container.appendChild(table);


Line 63: Line 42:
         var cols = Math.ceil(parsedItems.length / rows);
         var cols = Math.ceil(parsedItems.length / rows);


         // Fetch all images in one API call
         // Single API call for all images
         var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");
         var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n");
         $.getJSON("/api.php", {
         $.getJSON("/api.php", {
Line 138: Line 117:
                     cellContainer.appendChild(checkmark);
                     cellContainer.appendChild(checkmark);


                     // Hover shows info
                     // Hover shows info in the shared box
                     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";
                         }
                         }
                        // Clear info box
                         infoBox.innerHTML = "";
                         infoBox.innerHTML = "";
                        // Name header
                         let header = document.createElement("div");
                         let header = document.createElement("div");
                         header.style.fontWeight = "bold";
                         header.style.fontWeight = "bold";
Line 149: Line 132:
                         header.textContent = item.name;
                         header.textContent = item.name;
                         infoBox.appendChild(header);
                         infoBox.appendChild(header);
                        // Image
                         if (imgs[index]) {
                         if (imgs[index]) {
                             let hoverImg = document.createElement("img");
                             let hoverImg = document.createElement("img");
Line 158: Line 143:
                             infoBox.appendChild(hoverImg);
                             infoBox.appendChild(hoverImg);
                         }
                         }
                        // Multi-line tooltip
                         item.tooltip.split("%%").forEach(line => {
                         item.tooltip.split("%%").forEach(line => {
                             let div = document.createElement("div");
                             let div = document.createElement("div");
Line 169: Line 156:
                             this.style.background = "#313e59";
                             this.style.background = "#313e59";
                         }
                         }
                         infoBox.innerHTML = "<strong>Hover a tile to see info here.</strong>";
                        // Optionally: restore default info
                         infoBox.innerHTML = "Hover over a tile to see its info here.";
                     });
                     });


                     // Click toggle complete (captain only)
                     // Click toggle complete — only if current user is captain
                     cell.addEventListener("click", function() {
                     cell.addEventListener("click", function() {
                         if (currentUser !== captain) return;
                         if (mw.config.get("wgUserName") !== captain) return;
                         let done = localStorage.getItem(this.id) === "true";
                         let done = localStorage.getItem(this.id) === "true";
                         localStorage.setItem(this.id, !done);
                         localStorage.setItem(this.id, !done);
                         this.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";
                         this.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";
                         checkmark.style.display = !done ? "block" : "none";
                         checkmark.style.display = !done ? "block" : "none";
                        updateProgress(container, parsedItems.length);
                     });
                     });


Line 190: Line 179:
                 }
                 }
             }
             }
            // Progress counter
            let progressDiv = document.createElement("div");
            progressDiv.style.marginTop = "10px";
            progressDiv.style.color = "#fff";
            progressDiv.className = "bingoProgress";
            container.appendChild(progressDiv);
            // Reset button — only for captains
            let resetBtn = document.createElement("button");
            resetBtn.textContent = "Reset Bingo";
            resetBtn.style.marginTop = "5px";
            resetBtn.style.cursor = "pointer";
            resetBtn.addEventListener("click", function () {
                if (mw.config.get("wgUserName") !== captain) return;
                for (let i = 0; i < parsedItems.length; i++) {
                    let cb = document.getElementById(`${container.id}_cell_${i}`);
                    if (cb) {
                        localStorage.setItem(cb.id, false);
                        cb.style.background = "#313e59";
                        let check = cb.querySelector(".bingoCheckmark");
                        if (check) check.style.display = "none";
                    }
                }
                updateProgress(container, parsedItems.length);
            });
            container.appendChild(resetBtn);
            updateProgress(container, parsedItems.length);
         });
         });
        function updateProgress(container, total) {
            let completed = 0;
            for (let i = 0; i < total; i++) {
                let cb = document.getElementById(`${container.id}_cell_${i}`);
                if (cb && localStorage.getItem(cb.id) === "true") completed++;
            }
            let progressDiv = container.querySelector(".bingoProgress");
            if (progressDiv)
                progressDiv.textContent = `Completed ${completed} of ${total} items`;
        }
     });
     });
});
});

Revision as of 15:32, 7 March 2026

// --- Team Bingo Board with Shared Info Panel and Captain Control ---
mw.hook('wikipage.content').add(function () {
    var containers = document.querySelectorAll(".teamBingoContainer");
    if (!containers.length) return;

    // Use single shared info box
    var infoBox = document.getElementById("teamBingoInfoBox");
    if (!infoBox) {
        console.warn("Shared info box #teamBingoInfoBox not found");
        return;
    }

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

        // Get captain username for this team
        var captain = container.getAttribute("data-captain");

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

        var parsedItems = items.map(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";
        table.style.border = "2px solid #596e96";
        table.style.borderCollapse = "collapse";
        table.style.borderRadius = "8px";
        table.style.width = "600px"; // fixed width
        container.appendChild(table);

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

        // Single API call for all images
        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 #596e96";
                    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 = "visible";
                    cell.style.position = "relative";

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

                    // Container for image, name, checkmark
                    let cellContainer = document.createElement("div");
                    cellContainer.style.position = "relative";
                    cellContainer.style.width = "100%";
                    cellContainer.style.height = "100%";

                    // Image
                    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";
                        cellContainer.appendChild(img);
                    }

                    // Name
                    let nameDiv = document.createElement("div");
                    nameDiv.textContent = item.name;
                    nameDiv.style.marginTop = "5px";
                    nameDiv.style.pointerEvents = "none";
                    cellContainer.appendChild(nameDiv);

                    // Checkmark top-right
                    let checkmark = document.createElement("div");
                    checkmark.textContent = "✔";
                    checkmark.className = "bingoCheckmark";
                    checkmark.style.position = "absolute";
                    checkmark.style.top = "2px";
                    checkmark.style.right = "2px";
                    checkmark.style.fontSize = "20px";
                    checkmark.style.color = "#4CAF50";
                    checkmark.style.background = "none";
                    checkmark.style.display = "none";
                    cellContainer.appendChild(checkmark);

                    // Hover shows info in the shared box
                    cell.addEventListener("mouseenter", function() {
                        if (localStorage.getItem(this.id) !== "true") {
                            this.style.background = "#3b4a6b";
                        }

                        // Clear info box
                        infoBox.innerHTML = "";

                        // Name header
                        let header = document.createElement("div");
                        header.style.fontWeight = "bold";
                        header.style.marginBottom = "5px";
                        header.textContent = item.name;
                        infoBox.appendChild(header);

                        // Image
                        if (imgs[index]) {
                            let hoverImg = document.createElement("img");
                            hoverImg.src = imgs[index].src;
                            hoverImg.style.width = "40px";
                            hoverImg.style.height = "auto";
                            hoverImg.style.display = "block";
                            hoverImg.style.marginBottom = "5px";
                            infoBox.appendChild(hoverImg);
                        }

                        // Multi-line tooltip
                        item.tooltip.split("%%").forEach(line => {
                            let div = document.createElement("div");
                            div.innerHTML = line.trim();
                            infoBox.appendChild(div);
                        });
                    });

                    cell.addEventListener("mouseleave", function() {
                        if (localStorage.getItem(this.id) !== "true") {
                            this.style.background = "#313e59";
                        }
                        // Optionally: restore default info
                        infoBox.innerHTML = "Hover over a tile to see its info here.";
                    });

                    // Click toggle complete — only if current user is captain
                    cell.addEventListener("click", function() {
                        if (mw.config.get("wgUserName") !== captain) return;
                        let done = localStorage.getItem(this.id) === "true";
                        localStorage.setItem(this.id, !done);
                        this.style.background = !done ? "rgba(76,175,80,0.5)" : "#313e59";
                        checkmark.style.display = !done ? "block" : "none";
                        updateProgress(container, parsedItems.length);
                    });

                    // Load saved state
                    if (localStorage.getItem(id) === "true") {
                        cell.style.background = "rgba(76,175,80,0.5)";
                        checkmark.style.display = "block";
                    }

                    cell.appendChild(cellContainer);
                }
            }

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

            // Reset button — only for captains
            let resetBtn = document.createElement("button");
            resetBtn.textContent = "Reset Bingo";
            resetBtn.style.marginTop = "5px";
            resetBtn.style.cursor = "pointer";

            resetBtn.addEventListener("click", function () {
                if (mw.config.get("wgUserName") !== captain) return;
                for (let i = 0; i < parsedItems.length; i++) {
                    let cb = document.getElementById(`${container.id}_cell_${i}`);
                    if (cb) {
                        localStorage.setItem(cb.id, false);
                        cb.style.background = "#313e59";
                        let check = cb.querySelector(".bingoCheckmark");
                        if (check) check.style.display = "none";
                    }
                }
                updateProgress(container, parsedItems.length);
            });

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

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