MediaWiki:BingoSheet.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 1: | Line 1: | ||
// --- Bingo Board with | // --- Bingo Board with Hover info5, Names, Checkmarks, and Progress --- | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
| Line 8: | Line 8: | ||
container.innerHTML = ""; | container.innerHTML = ""; | ||
// Get items | // Get items: 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; | ||
var parsedItems = items.map(entry => { | |||
var parsedItems = items.map | |||
var parts = entry.split("::"); | var parts = entry.split("::"); | ||
return { | return { | ||
| Line 24: | Line 23: | ||
// Build table | // Build table | ||
var table = document.createElement("table"); | var table = document.createElement("table"); | ||
table.style.tableLayout = "fixed"; | table.style.tableLayout = "fixed"; | ||
table.style.border = "2px solid #596e96"; | table.style.border = "2px solid #596e96"; | ||
table.style.borderCollapse = "collapse"; | table.style.borderCollapse = "collapse"; | ||
| Line 35: | Line 34: | ||
var cols = Math.ceil(parsedItems.length / rows); | var cols = Math.ceil(parsedItems.length / rows); | ||
// Wrap | // Wrap Hover info5 templates for API | ||
var allTemplates = parsedItems.map(item => | var allTemplates = parsedItems.map(item => | ||
`<div class="bingoItem">{{Hover | `<div class="bingoItem">{{Hover info5|text=[[File:${item.name}.png|link=]]|${item.tooltip}}}</div>` | ||
).join("\n"); | ).join("\n"); | ||
| Line 73: | Line 72: | ||
cell.style.userSelect = "none"; | cell.style.userSelect = "none"; | ||
cell.style.wordWrap = "break-word"; | cell.style.wordWrap = "break-word"; | ||
cell.style.overflow = "visible"; | cell.style.overflow = "visible"; | ||
cell.style.position = "relative"; | |||
let id = `${container.id}_cell_${index}`; | let id = `${container.id}_cell_${index}`; | ||
cell.id = id; | cell.id = id; | ||
// | // Container for hover + image | ||
let | let cellContainer = document.createElement("div"); | ||
cellContainer.style.position = "relative"; | |||
cellContainer.style.width = "100%"; | |||
cellContainer.style.height = "100%"; | |||
// | // Add hover node | ||
if (hoverNodes[nodeIndex]) { | if (hoverNodes[nodeIndex]) { | ||
let popupNode = hoverNodes[nodeIndex]; | |||
popupNode.style.position = "absolute"; | |||
popupNode.style.top = "0"; | |||
popupNode.style.left = "0"; | |||
popupNode.style.width = "100%"; | |||
popupNode.style.height = "100%"; | |||
popupNode.style.display = "block"; | |||
cellContainer.appendChild(popupNode); | |||
nodeIndex++; | nodeIndex++; | ||
} | } | ||
// | // Checkmark overlay | ||
let checkmark = document.createElement("div"); | |||
checkmark.textContent = "✅"; | |||
checkmark.style.position = "absolute"; | |||
checkmark.style.top = "5px"; | |||
checkmark.style.right = "5px"; | |||
checkmark.style.fontSize = "20px"; | |||
checkmark.style.display = "none"; | |||
cellContainer.appendChild(checkmark); | |||
// Item name below image | |||
let nameDiv = document.createElement("div"); | let nameDiv = document.createElement("div"); | ||
nameDiv.textContent = item.name; | nameDiv.textContent = item.name; | ||
nameDiv.style.marginTop = "5px"; | nameDiv.style.marginTop = "5px"; | ||
nameDiv.style.pointerEvents = "none"; | nameDiv.style.pointerEvents = "none"; | ||
cellContainer.appendChild(nameDiv); | |||
cell.appendChild( | cell.appendChild(cellContainer); | ||
// Load completed state | // 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)"; | ||
checkmark.style.display = "block"; | |||
} | } | ||
| Line 119: | Line 135: | ||
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"; | |||
updateProgress(container, parsedItems.length); | updateProgress(container, parsedItems.length); | ||
}); | }); | ||
| Line 143: | Line 160: | ||
localStorage.setItem(cb.id, false); | localStorage.setItem(cb.id, false); | ||
cb.style.background = "#313e59"; | cb.style.background = "#313e59"; | ||
cb.querySelector("div:nth-child(2)").style.display = "none"; // hide checkmark | |||
} | } | ||
} | } | ||
Revision as of 13:07, 7 March 2026
// --- Bingo Board with Hover info5, Names, Checkmarks, and Progress ---
mw.hook('wikipage.content').add(function () {
var containers = document.querySelectorAll(".bingoContainer");
if (!containers.length) return;
containers.forEach(function(container) {
container.innerHTML = "";
// Get items: format Item Name::Custom Hover Text
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 = "100%";
table.style.maxWidth = "600px";
container.appendChild(table);
var rows = Math.ceil(Math.sqrt(parsedItems.length));
var cols = Math.ceil(parsedItems.length / rows);
// Wrap Hover info5 templates for API
var allTemplates = parsedItems.map(item =>
`<div class="bingoItem">{{Hover info5|text=[[File:${item.name}.png|link=]]|${item.tooltip}}}</div>`
).join("\n");
// Single API call
$.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 hoverNodes = tempDiv.querySelectorAll(".bingoItem");
let nodeIndex = 0;
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 = "visible";
cell.style.position = "relative";
let id = `${container.id}_cell_${index}`;
cell.id = id;
// Container for hover + image
let cellContainer = document.createElement("div");
cellContainer.style.position = "relative";
cellContainer.style.width = "100%";
cellContainer.style.height = "100%";
// Add hover node
if (hoverNodes[nodeIndex]) {
let popupNode = hoverNodes[nodeIndex];
popupNode.style.position = "absolute";
popupNode.style.top = "0";
popupNode.style.left = "0";
popupNode.style.width = "100%";
popupNode.style.height = "100%";
popupNode.style.display = "block";
cellContainer.appendChild(popupNode);
nodeIndex++;
}
// Checkmark overlay
let checkmark = document.createElement("div");
checkmark.textContent = "✅";
checkmark.style.position = "absolute";
checkmark.style.top = "5px";
checkmark.style.right = "5px";
checkmark.style.fontSize = "20px";
checkmark.style.display = "none";
cellContainer.appendChild(checkmark);
// Item name below image
let nameDiv = document.createElement("div");
nameDiv.textContent = item.name;
nameDiv.style.marginTop = "5px";
nameDiv.style.pointerEvents = "none";
cellContainer.appendChild(nameDiv);
cell.appendChild(cellContainer);
// Load completed state
if (localStorage.getItem(id) === "true") {
cell.style.background = "rgba(76, 175, 80, 0.5)";
checkmark.style.display = "block";
}
// Hover darken
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";
checkmark.style.display = !done ? "block" : "none";
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";
cb.querySelector("div:nth-child(2)").style.display = "none"; // hide checkmark
}
}
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`;
}
});
});