MediaWiki:BingoSheet.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
// --- | // --- Bingo Board --- | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
var containers = document.querySelectorAll(".bingoContainer"); | var containers = document.querySelectorAll(".bingoContainer"); | ||
if (!containers.length) return; | if (!containers.length) return; | ||
containers.forEach(function(container) { | containers.forEach(function(container) { | ||
container.innerHTML = ""; | container.innerHTML = ""; | ||
var itemsAttr = container.getAttribute("data-items"); | var itemsAttr = container.getAttribute("data-items"); | ||
var | var rawItems = itemsAttr.split("|").filter(i => i.trim() !== ""); | ||
if (!items. | |||
if (!rawItems.length) return; | |||
// Parse item names + tooltip text | |||
var items = rawItems.map(function(entry){ | |||
var parts = entry.split("||"); | |||
return { | |||
name: parts[0].trim(), | |||
tooltip: parts[1] ? parts[1].trim() : parts[0].trim() | |||
}; | |||
}); | |||
var table = document.createElement("table"); | var table = document.createElement("table"); | ||
table.style.border = "2px solid #596e96"; | table.style.border = "2px solid #596e96"; | ||
| Line 18: | Line 28: | ||
table.style.borderRadius = "8px"; | table.style.borderRadius = "8px"; | ||
table.style.width = "100%"; | table.style.width = "100%"; | ||
table.style.maxWidth = " | table.style.maxWidth = "520px"; | ||
container.appendChild(table); | container.appendChild(table); | ||
| Line 25: | Line 35: | ||
var cols = Math.ceil(items.length / rows); | var cols = Math.ceil(items.length / rows); | ||
// Build | // Build all plinkp templates in ONE request | ||
var allTemplates = items.map( | var allTemplates = items.map(i => `{{plinkp|${i.name}}}`).join("\n"); | ||
$.getJSON("/api.php", { | $.getJSON("/api.php", { | ||
action: "parse", | action: "parse", | ||
text: allTemplates, | text: allTemplates, | ||
format: "json" | format: "json" | ||
}).done(function(data) { | }).done(function(data){ | ||
if (!data.parse || !data.parse.text) return; | if (!data.parse || !data.parse.text) return; | ||
var tempDiv = document.createElement("div"); | var tempDiv = document.createElement("div"); | ||
tempDiv.innerHTML = data.parse.text["*"]; | tempDiv.innerHTML = data.parse.text["*"]; | ||
var imgs = tempDiv.querySelectorAll("img"); | var imgs = tempDiv.querySelectorAll("img"); | ||
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 >= items.length) break; | if (index >= items.length) break; | ||
let item = items[index]; | |||
let cell = row.insertCell(); | let cell = row.insertCell(); | ||
cell.style.padding = "10px"; | cell.style.padding = "10px"; | ||
cell.style.border = "1px solid #444"; | cell.style.border = "1px solid #444"; | ||
| Line 57: | Line 73: | ||
cell.style.userSelect = "none"; | cell.style.userSelect = "none"; | ||
let id = `${container.id}_cell_${index}`; | let id = `${container.id}_cell_${index}`; | ||
cell.id = id; | cell.id = id; | ||
// Add image | // Hover tooltip | ||
cell.title = item.tooltip; | |||
// Add image | |||
if (imgs[index]) { | if (imgs[index]) { | ||
let img = document.createElement("img"); | let img = document.createElement("img"); | ||
img.src = imgs[index].src; | img.src = imgs[index].src; | ||
img.alt = | img.alt = item.name; | ||
img.style.width = "40px"; | img.style.width = "40px"; | ||
img.style.height = " | img.style.height = "40px"; | ||
img.style.objectFit = "contain"; | |||
img.style.display = "block"; | img.style.display = "block"; | ||
img.style.margin = "0 auto | img.style.margin = "0 auto 6px auto"; | ||
cell.appendChild(img); | cell.appendChild(img); | ||
} | } | ||
// Item name | |||
let text = document.createElement("div"); | let text = document.createElement("div"); | ||
text.textContent = | text.textContent = item.name; | ||
text.style.fontSize = "13px"; | |||
cell.appendChild(text); | cell.appendChild(text); | ||
// Load | // Load stored completion 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 | // Hover highlight | ||
cell.addEventListener("mouseenter", function () { | cell.addEventListener("mouseenter", function () { | ||
if (localStorage.getItem(this.id) !== "true") this.style.background = "#3b4a6b"; | if (localStorage.getItem(this.id) !== "true") | ||
this.style.background = "#3b4a6b"; | |||
}); | }); | ||
cell.addEventListener("mouseleave", function () { | cell.addEventListener("mouseleave", function () { | ||
if (localStorage.getItem(this.id) !== "true") this.style.background = "#313e59"; | if (localStorage.getItem(this.id) !== "true") | ||
this.style.background = "#313e59"; | |||
}); | }); | ||
// 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); | ||
this.style.background = !done ? "rgba(76, 175, 80, 0.5)" : "#313e59"; | |||
this.style.background = !done | |||
? "rgba(76,175,80,0.5)" | |||
: "#313e59"; | |||
updateProgress(container, items.length); | updateProgress(container, items.length); | ||
}); | }); | ||
| Line 99: | Line 136: | ||
} | } | ||
// Progress display | // Progress display | ||
var progressDiv = document.createElement("div"); | var progressDiv = document.createElement("div"); | ||
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 = " | 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 < items.length; i++) { | for (var i = 0; i < items.length; i++) { | ||
var | |||
if ( | var cell = document.getElementById(`${container.id}_cell_${i}`); | ||
localStorage.setItem( | |||
if (cell) { | |||
localStorage.setItem(cell.id, false); | |||
cell.style.background = "#313e59"; | |||
} | } | ||
} | } | ||
updateProgress(container, items.length); | updateProgress(container, items.length); | ||
}); | }); | ||
container.appendChild(resetBtn); | container.appendChild(resetBtn); | ||
| Line 127: | Line 172: | ||
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 | |||
if ( | var cell = document.getElementById(`${container.id}_cell_${i}`); | ||
if (cell && localStorage.getItem(cell.id) === "true") | |||
completed++; | |||
} | } | ||
var progressDiv = container.querySelector(".bingoProgress"); | var progressDiv = container.querySelector(".bingoProgress"); | ||
if (progressDiv) progressDiv.textContent = `Completed ${completed} of ${total} items`; | |||
if (progressDiv) | |||
progressDiv.textContent = `Completed ${completed} of ${total} items`; | |||
} | } | ||
}); | }); | ||
}); | }); | ||
Revision as of 12:17, 7 March 2026
// --- Bingo Board ---
mw.hook('wikipage.content').add(function () {
var containers = document.querySelectorAll(".bingoContainer");
if (!containers.length) return;
containers.forEach(function(container) {
container.innerHTML = "";
var itemsAttr = container.getAttribute("data-items");
var rawItems = itemsAttr.split("|").filter(i => i.trim() !== "");
if (!rawItems.length) return;
// Parse item names + tooltip text
var items = rawItems.map(function(entry){
var parts = entry.split("||");
return {
name: parts[0].trim(),
tooltip: parts[1] ? parts[1].trim() : parts[0].trim()
};
});
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(items.length));
var cols = Math.ceil(items.length / rows);
// Build all plinkp templates in ONE request
var allTemplates = items.map(i => `{{plinkp|${i.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 >= items.length) break;
let item = items[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 = "background 0.2s ease";
cell.style.userSelect = "none";
let id = `${container.id}_cell_${index}`;
cell.id = id;
// Hover tooltip
cell.title = item.tooltip;
// Add 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 = "40px";
img.style.objectFit = "contain";
img.style.display = "block";
img.style.margin = "0 auto 6px auto";
cell.appendChild(img);
}
// Item name
let text = document.createElement("div");
text.textContent = item.name;
text.style.fontSize = "13px";
cell.appendChild(text);
// Load stored completion state
if (localStorage.getItem(id) === "true") {
cell.style.background = "rgba(76,175,80,0.5)";
}
// Hover highlight
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";
updateProgress(container, items.length);
});
}
}
// Progress display
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 < items.length; i++) {
var cell = document.getElementById(`${container.id}_cell_${i}`);
if (cell) {
localStorage.setItem(cell.id, false);
cell.style.background = "#313e59";
}
}
updateProgress(container, items.length);
});
container.appendChild(resetBtn);
updateProgress(container, items.length);
});
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`;
}
});
});