MediaWiki:BingoSheet.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
// --- | // --- Wiki Template-Based Bingo Board --- | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
| Line 9: | Line 9: | ||
container.innerHTML = ""; | container.innerHTML = ""; | ||
// Get items from template | |||
// Format: item name::tooltip text, separated by | | |||
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(function(entry){ | var parsedItems = items.map(function(entry){ | ||
var parts = entry.split("::"); | var parts = entry.split("::"); | ||
| Line 22: | Line 23: | ||
}); | }); | ||
// Create table | |||
var table = document.createElement("table"); | var table = document.createElement("table"); | ||
table.style.border = "2px solid #596e96"; | table.style.border = "2px solid #596e96"; | ||
table.style.borderCollapse = "collapse"; | table.style.borderCollapse = "collapse"; | ||
| Line 29: | Line 30: | ||
table.style.width = "100%"; | table.style.width = "100%"; | ||
table.style.maxWidth = "520px"; | table.style.maxWidth = "520px"; | ||
container.appendChild(table); | container.appendChild(table); | ||
| Line 35: | Line 35: | ||
var cols = Math.ceil(parsedItems.length / rows); | var cols = Math.ceil(parsedItems.length / rows); | ||
// Build plinkp templates | // Build a single API call with all plinkp templates | ||
var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}}}`).join("\n"); | var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}|${item.tooltip}}}`).join("\n"); | ||
$.getJSON("/api.php", { | $.getJSON("/api.php", { | ||
| Line 49: | Line 49: | ||
tempDiv.innerHTML = data.parse.text["*"]; | tempDiv.innerHTML = data.parse.text["*"]; | ||
var | // Get all rendered plinkp tiles | ||
var renderedTiles = tempDiv.children; | |||
for (let r = 0; r < rows; r++) { | for (let r = 0; r < rows; r++) { | ||
var row = table.insertRow(); | var row = table.insertRow(); | ||
for (let c = 0; c < cols; c++) { | for (let c = 0; c < cols; c++) { | ||
let index = r * cols + c; | let index = r * cols + c; | ||
if (index >= parsedItems.length) break; | if (index >= parsedItems.length) break; | ||
let item = parsedItems[index]; | let item = parsedItems[index]; | ||
let cell = row.insertCell(); | let cell = row.insertCell(); | ||
| Line 78: | Line 76: | ||
cell.id = id; | cell.id = id; | ||
// - | // Insert wiki-rendered tile (with hoverbox template) | ||
if (renderedTiles[index]) { | |||
cell.appendChild(renderedTiles[index]); | |||
if ( | |||
} | } | ||
// | // Mark completed if saved | ||
if (localStorage.getItem(id) === "true") { | if (localStorage.getItem(id) === "true") { | ||
markComplete(cell); | markComplete(cell); | ||
| Line 131: | Line 88: | ||
// Hover animation | // Hover animation | ||
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"; | ||
| Line 137: | Line 93: | ||
} | } | ||
}); | }); | ||
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"; | ||
| Line 148: | Line 102: | ||
// Click toggle | // Click toggle | ||
cell.addEventListener("click", function () { | cell.addEventListener("click", function () { | ||
var done = localStorage.getItem(this.id) === "true"; | var done = localStorage.getItem(this.id) === "true"; | ||
localStorage.setItem(this.id, !done); | localStorage.setItem(this.id, !done); | ||
if (!done) | if (!done) markComplete(this); | ||
else resetTile(this); | |||
updateProgress(container, parsedItems.length); | updateProgress(container, parsedItems.length); | ||
| Line 164: | Line 113: | ||
} | } | ||
// Progress | // Progress counter | ||
var progressDiv = document.createElement("div"); | var progressDiv = document.createElement("div"); | ||
progressDiv.className = "bingoProgress"; | progressDiv.className = "bingoProgress"; | ||
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 = "6px"; | resetBtn.style.marginTop = "6px"; | ||
resetBtn.style.cursor = "pointer"; | resetBtn.style.cursor = "pointer"; | ||
resetBtn.addEventListener("click", function(){ | resetBtn.addEventListener("click", function(){ | ||
for (var i = 0; i < parsedItems.length; i++) { | for (var i = 0; i < parsedItems.length; i++) { | ||
var cell = document.getElementById(`${container.id}_cell_${i}`); | var cell = document.getElementById(`${container.id}_cell_${i}`); | ||
if (cell) { | if (cell) { | ||
localStorage.setItem(cell.id, false); | localStorage.setItem(cell.id, false); | ||
| Line 190: | Line 133: | ||
} | } | ||
} | } | ||
updateProgress(container, parsedItems.length); | updateProgress(container, parsedItems.length); | ||
}); | }); | ||
container.appendChild(resetBtn); | container.appendChild(resetBtn); | ||
| Line 200: | Line 141: | ||
function markComplete(cell) { | function markComplete(cell) { | ||
cell.style.background = "rgba(76,175,80,0.45)"; | cell.style.background = "rgba(76,175,80,0.45)"; | ||
cell.style.transform = "scale(1)"; | cell.style.transform = "scale(1)"; | ||
if (!cell.querySelector(".bingo-check")) { | if (!cell.querySelector(".bingo-check")) { | ||
let check = document.createElement("div"); | let check = document.createElement("div"); | ||
check.textContent = "✔"; | check.textContent = "✔"; | ||
check.className = "bingo-check"; | check.className = "bingo-check"; | ||
check.style.position = "absolute"; | check.style.position = "absolute"; | ||
check.style.top = "4px"; | check.style.top = "4px"; | ||
| Line 216: | Line 153: | ||
check.style.fontSize = "18px"; | check.style.fontSize = "18px"; | ||
check.style.color = "#8cff8c"; | check.style.color = "#8cff8c"; | ||
cell.appendChild(check); | cell.appendChild(check); | ||
} | } | ||
| Line 222: | Line 158: | ||
function resetTile(cell) { | function resetTile(cell) { | ||
cell.style.background = "#313e59"; | cell.style.background = "#313e59"; | ||
cell.style.transform = "scale(1)"; | cell.style.transform = "scale(1)"; | ||
let check = cell.querySelector(".bingo-check"); | let check = cell.querySelector(".bingo-check"); | ||
if (check) check.remove(); | if (check) check.remove(); | ||
| Line 231: | Line 165: | ||
function updateProgress(container, total) { | function updateProgress(container, total) { | ||
var completed = 0; | var completed = 0; | ||
for (var i = 0; i < total; i++) { | for (var i = 0; i < total; i++) { | ||
var cell = document.getElementById(`${container.id}_cell_${i}`); | var cell = document.getElementById(`${container.id}_cell_${i}`); | ||
if (cell && localStorage.getItem(cell.id) === "true") completed++; | |||
if (cell && localStorage.getItem(cell.id) === "true") | |||
} | } | ||
var progressDiv = container.querySelector(".bingoProgress"); | var 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 12:39, 7 March 2026
// --- Wiki Template-Based Bingo Board ---
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::tooltip text, separated by |
var itemsAttr = container.getAttribute("data-items");
var items = itemsAttr.split("|").filter(i => i.trim() !== "");
if (!items.length) return;
var parsedItems = items.map(function(entry){
var parts = entry.split("::");
return {
name: parts[0].trim(),
tooltip: parts[1] ? parts[1].trim() : parts[0].trim()
};
});
// Create table
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 = "520px";
container.appendChild(table);
var rows = Math.ceil(Math.sqrt(parsedItems.length));
var cols = Math.ceil(parsedItems.length / rows);
// Build a single API call with all plinkp templates
var allTemplates = parsedItems.map(item => `{{plinkp|${item.name}|${item.tooltip}}}`).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["*"];
// Get all rendered plinkp tiles
var renderedTiles = tempDiv.children;
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.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 = "all 0.15s ease";
cell.style.userSelect = "none";
cell.style.position = "relative";
cell.style.overflow = "visible";
let id = `${container.id}_cell_${index}`;
cell.id = id;
// Insert wiki-rendered tile (with hoverbox template)
if (renderedTiles[index]) {
cell.appendChild(renderedTiles[index]);
}
// Mark completed if saved
if (localStorage.getItem(id) === "true") {
markComplete(cell);
}
// Hover animation
cell.addEventListener("mouseenter", function () {
if (localStorage.getItem(this.id) !== "true") {
this.style.background = "#3b4a6b";
this.style.transform = "scale(1.05)";
}
});
cell.addEventListener("mouseleave", function () {
if (localStorage.getItem(this.id) !== "true") {
this.style.background = "#313e59";
this.style.transform = "scale(1)";
}
});
// Click toggle
cell.addEventListener("click", function () {
var done = localStorage.getItem(this.id) === "true";
localStorage.setItem(this.id, !done);
if (!done) markComplete(this);
else resetTile(this);
updateProgress(container, parsedItems.length);
});
}
}
// Progress counter
var progressDiv = document.createElement("div");
progressDiv.className = "bingoProgress";
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 = "6px";
resetBtn.style.cursor = "pointer";
resetBtn.addEventListener("click", function(){
for (var i = 0; i < parsedItems.length; i++) {
var cell = document.getElementById(`${container.id}_cell_${i}`);
if (cell) {
localStorage.setItem(cell.id, false);
resetTile(cell);
}
}
updateProgress(container, parsedItems.length);
});
container.appendChild(resetBtn);
updateProgress(container, parsedItems.length);
});
function markComplete(cell) {
cell.style.background = "rgba(76,175,80,0.45)";
cell.style.transform = "scale(1)";
if (!cell.querySelector(".bingo-check")) {
let check = document.createElement("div");
check.textContent = "✔";
check.className = "bingo-check";
check.style.position = "absolute";
check.style.top = "4px";
check.style.right = "6px";
check.style.fontSize = "18px";
check.style.color = "#8cff8c";
cell.appendChild(check);
}
}
function resetTile(cell) {
cell.style.background = "#313e59";
cell.style.transform = "scale(1)";
let check = cell.querySelector(".bingo-check");
if (check) check.remove();
}
function updateProgress(container, total) {
var completed = 0;
for (var i = 0; i < total; i++) {
var cell = document.getElementById(`${container.id}_cell_${i}`);
if (cell && localStorage.getItem(cell.id) === "true") completed++;
}
var progressDiv = container.querySelector(".bingoProgress");
if (progressDiv)
progressDiv.textContent = `Completed ${completed} of ${total} items`;
}
});
});