MediaWiki:BingoSheet.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
// --- Bingo Board with Hover | // --- Bingo Board with Full-Tile Hover, Images, Names, Checkmarks --- | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
| Line 34: | Line 34: | ||
var cols = Math.ceil(parsedItems.length / rows); | var cols = Math.ceil(parsedItems.length / rows); | ||
// | // Single API call for all plinkp images | ||
var allTemplates = parsedItems.map(item => | var allTemplates = parsedItems.map(item => | ||
` | `{{plinkp|${item.name}}}` | ||
).join("\n"); | ).join("\n"); | ||
$.getJSON("/api.php", { | $.getJSON("/api.php", { | ||
action: "parse", | action: "parse", | ||
| Line 49: | Line 48: | ||
var tempDiv = document.createElement("div"); | var tempDiv = document.createElement("div"); | ||
tempDiv.innerHTML = data.parse.text["*"]; | tempDiv.innerHTML = data.parse.text["*"]; | ||
var | var imgs = tempDiv.querySelectorAll("img"); | ||
for (let r = 0; r < rows; r++) { | for (let r = 0; r < rows; r++) { | ||
| Line 72: | Line 69: | ||
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"; | 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 | // Container for image, name, and hover | ||
let cellContainer = document.createElement("div"); | let cellContainer = document.createElement("div"); | ||
cellContainer.style.position = "relative"; | cellContainer.style.position = "relative"; | ||
| Line 84: | Line 81: | ||
cellContainer.style.height = "100%"; | cellContainer.style.height = "100%"; | ||
// | // Image | ||
if ( | if (imgs[index]) { | ||
let | 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( | cellContainer.appendChild(img); | ||
} | } | ||
// Name | |||
let nameDiv = document.createElement("div"); | |||
nameDiv.textContent = item.name; | |||
nameDiv.style.marginTop = "5px"; | |||
nameDiv.style.pointerEvents = "none"; | |||
cellContainer.appendChild(nameDiv); | |||
// Hover popup | |||
let hoverDiv = document.createElement("div"); | |||
hoverDiv.textContent = item.tooltip; | |||
hoverDiv.style.position = "absolute"; | |||
hoverDiv.style.top = "-2.5em"; | |||
hoverDiv.style.left = "50%"; | |||
hoverDiv.style.transform = "translateX(-50%)"; | |||
hoverDiv.style.backgroundColor = "#313e59"; | |||
hoverDiv.style.color = "#fff"; | |||
hoverDiv.style.padding = "5px"; | |||
hoverDiv.style.border = "1px solid #000"; | |||
hoverDiv.style.borderRadius = "3px"; | |||
hoverDiv.style.whiteSpace = "nowrap"; | |||
hoverDiv.style.pointerEvents = "none"; | |||
hoverDiv.style.display = "none"; | |||
hoverDiv.style.zIndex = "10"; | |||
cellContainer.appendChild(hoverDiv); | |||
// Show hover on full tile | |||
cell.addEventListener("mouseenter", function() { | |||
if (localStorage.getItem(this.id) !== "true") { | |||
this.style.background = "#3b4a6b"; | |||
hoverDiv.style.display = "block"; | |||
} | |||
}); | |||
cell.addEventListener("mouseleave", function() { | |||
if (localStorage.getItem(this.id) !== "true") { | |||
this.style.background = "#313e59"; | |||
hoverDiv.style.display = "none"; | |||
} | |||
}); | |||
// Checkmark overlay | // Checkmark overlay | ||
| Line 107: | Line 142: | ||
cellContainer.appendChild(checkmark); | cellContainer.appendChild(checkmark); | ||
// | // Click toggle | ||
let | cell.addEventListener("click", function() { | ||
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"; | |||
hoverDiv.style.display = !done ? "none" : hoverDiv.style.display; | |||
updateProgress(container, parsedItems.length); | |||
}); | |||
// Load completed state | // Load saved 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"; | checkmark.style.display = "block"; | ||
} | } | ||
cell.appendChild(cellContainer); | |||
cell. | |||
} | } | ||
} | } | ||
| Line 156: | Line 177: | ||
resetBtn.addEventListener("click", function () { | resetBtn.addEventListener("click", function () { | ||
for (var i = 0; i < parsedItems.length; i++) { | for (var i = 0; i < parsedItems.length; i++) { | ||
let cb = document.getElementById(`${container.id}_cell_${i}`); | |||
if (cb) { | if (cb) { | ||
localStorage.setItem(cb.id, false); | localStorage.setItem(cb.id, false); | ||
cb.style.background = "#313e59"; | cb.style.background = "#313e59"; | ||
cb.querySelector("div:nth-child( | let check = cb.querySelector("div:nth-child(3)"); | ||
if (check) check.style.display = "none"; | |||
} | } | ||
} | } | ||
| Line 170: | Line 192: | ||
}); | }); | ||
function updateProgress(container, total) { | function updateProgress(container, total) { | ||
let completed = 0; | |||
for ( | for (let i = 0; i < total; i++) { | ||
let cb = document.getElementById(`${container.id}_cell_${i}`); | |||
if (cb && localStorage.getItem(cb.id) === "true") completed++; | if (cb && localStorage.getItem(cb.id) === "true") completed++; | ||
} | } | ||
let progressDiv = container.querySelector(".bingoProgress"); | |||
if (progressDiv) | if (progressDiv) | ||
progressDiv.textContent = `Completed ${completed} of ${total} items`; | progressDiv.textContent = `Completed ${completed} of ${total} items`; | ||
Revision as of 13:08, 7 March 2026
// --- Bingo Board with Full-Tile Hover, Images, Names, Checkmarks ---
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);
// Single API call for all plinkp 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 #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 image, name, and hover
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);
// Hover popup
let hoverDiv = document.createElement("div");
hoverDiv.textContent = item.tooltip;
hoverDiv.style.position = "absolute";
hoverDiv.style.top = "-2.5em";
hoverDiv.style.left = "50%";
hoverDiv.style.transform = "translateX(-50%)";
hoverDiv.style.backgroundColor = "#313e59";
hoverDiv.style.color = "#fff";
hoverDiv.style.padding = "5px";
hoverDiv.style.border = "1px solid #000";
hoverDiv.style.borderRadius = "3px";
hoverDiv.style.whiteSpace = "nowrap";
hoverDiv.style.pointerEvents = "none";
hoverDiv.style.display = "none";
hoverDiv.style.zIndex = "10";
cellContainer.appendChild(hoverDiv);
// Show hover on full tile
cell.addEventListener("mouseenter", function() {
if (localStorage.getItem(this.id) !== "true") {
this.style.background = "#3b4a6b";
hoverDiv.style.display = "block";
}
});
cell.addEventListener("mouseleave", function() {
if (localStorage.getItem(this.id) !== "true") {
this.style.background = "#313e59";
hoverDiv.style.display = "none";
}
});
// 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);
// Click toggle
cell.addEventListener("click", function() {
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";
hoverDiv.style.display = !done ? "none" : hoverDiv.style.display;
updateProgress(container, parsedItems.length);
});
// Load saved completed state
if (localStorage.getItem(id) === "true") {
cell.style.background = "rgba(76,175,80,0.5)";
checkmark.style.display = "block";
}
cell.appendChild(cellContainer);
}
}
// 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++) {
let cb = document.getElementById(`${container.id}_cell_${i}`);
if (cb) {
localStorage.setItem(cb.id, false);
cb.style.background = "#313e59";
let check = cb.querySelector("div:nth-child(3)");
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`;
}
});
});