MediaWiki:Common.js: Difference between revisions
From AnarchyMU Wiki
No edit summary |
No edit summary |
||
| Line 20: | Line 20: | ||
})(); | })(); | ||
/* Update | /* Update 044: robust nested Stage dropdown - v4 with correct mini-stage grouping */ | ||
(function () { | (function () { | ||
function textOf(el) { | function textOf(el) { | ||
| Line 28: | Line 28: | ||
function norm(s) { | function norm(s) { | ||
return (s || '').replace(/\s+/g, ' ').trim().toLowerCase(); | return (s || '').replace(/\s+/g, ' ').trim().toLowerCase(); | ||
} | |||
function urlOf(link) { | |||
if (!link) return ''; | |||
var href = link.getAttribute('href') || ''; | |||
var parts = href.split('/'); | |||
return parts[parts.length - 1] || ''; | |||
} | } | ||
| Line 34: | Line 41: | ||
} | } | ||
function | function getStageNumber(label) { | ||
var m = label.match(/^stage\s+([1-4])$/i); | |||
return m ? m[1] : null; | |||
} | |||
function isMiniLabelForStage(url, stageNum) { | |||
if (stageNum === '1') { | |||
// Stage 1 minis are like "Mini_Stage_X" (no prefix) | |||
return /^Mini_Stage_\d+$/.test(url); | |||
} else { | |||
// Stage 2/3/4 minis are like "Stage_2_Mini_Stage_X" | |||
var pattern = new RegExp('^Stage_' + stageNum + '_Mini_Stage_\\d+$'); | |||
return pattern.test(url); | |||
} | |||
} | } | ||
| Line 74: | Line 93: | ||
for (i = 0; i < lis.length; i++) { | for (i = 0; i < lis.length; i++) { | ||
var label = norm(textOf(directLink(lis[i]))); | var label = norm(textOf(directLink(lis[i]))); | ||
if ( | if (/^stage\s+[1-4]$/i.test(label)) stageCount++; | ||
if ( | if (/^mini\s+stage\s+\d+$/i.test(label)) miniCount++; | ||
if (label === 'overview') overviewCount++; | if (label === 'overview') overviewCount++; | ||
} | } | ||
| Line 102: | Line 121: | ||
} | } | ||
function makeToggle(stageLi, toggleBtn | function makeToggle(stageLi, toggleBtn) { | ||
return function (ev) { | return function (ev) { | ||
ev.preventDefault(); | ev.preventDefault(); | ||
| Line 123: | Line 142: | ||
var currentStageLi = null; | var currentStageLi = null; | ||
var currentMiniUl = null; | var currentMiniUl = null; | ||
var currentStageNum = null; | |||
var i; | var i; | ||
| Line 129: | Line 149: | ||
var link = directLink(li); | var link = directLink(li); | ||
var label = textOf(link); | var label = textOf(link); | ||
var url = urlOf(link); | |||
if (isStageLabel(label)) { | if (isStageLabel(label)) { | ||
var stageNum = getStageNumber(label); | |||
currentStageNum = stageNum; | |||
currentStageLi = li.cloneNode(true); | currentStageLi = li.cloneNode(true); | ||
currentStageLi.className = (currentStageLi.className ? currentStageLi.className + ' ' : '') + 'stages-inner-parent'; | currentStageLi.className = (currentStageLi.className ? currentStageLi.className + ' ' : '') + 'stages-inner-parent'; | ||
| Line 150: | Line 174: | ||
toggle.textContent = '▸'; | toggle.textContent = '▸'; | ||
var toggleHandler = makeToggle(currentStageLi, toggle | var toggleHandler = makeToggle(currentStageLi, toggle); | ||
toggle.addEventListener('click', toggleHandler); | toggle.addEventListener('click', toggleHandler); | ||
| Line 169: | Line 193: | ||
} | } | ||
if ( | // Check if this is a mini-stage that belongs to the current stage | ||
if (currentMiniUl && currentStageNum && isMiniLabelForStage(url, currentStageNum)) { | |||
currentMiniUl.appendChild(li.cloneNode(true)); | currentMiniUl.appendChild(li.cloneNode(true)); | ||
continue; | continue; | ||
} | } | ||
// Not a stage, not a matching mini; reset and add as-is | |||
currentStageLi = null; | currentStageLi = null; | ||
currentMiniUl = null; | currentMiniUl = null; | ||
currentStageNum = null; | |||
rebuilt.push(li.cloneNode(true)); | rebuilt.push(li.cloneNode(true)); | ||
} | } | ||
| Line 188: | Line 215: | ||
list.dataset.stagesNestedApplied = '1'; | list.dataset.stagesNestedApplied = '1'; | ||
console.log('[UPDATE- | console.log('[UPDATE-044] Stages nested dropdown applied with correct mini-stage grouping.'); | ||
} | } | ||
Revision as of 10:35, 20 March 2026
/* Any JavaScript here will be loaded for all users on every page load. */
// Ensures class card images use the intended foreground image if data-src is present.
(function () {
function applyDataSrc() {
var images = document.querySelectorAll('.mw-card-image img[data-src]');
for (var i = 0; i < images.length; i++) {
var img = images[i];
var dataSrc = img.getAttribute('data-src');
if (dataSrc && img.getAttribute('src') !== dataSrc) {
img.setAttribute('src', dataSrc);
}
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', applyDataSrc);
} else {
applyDataSrc();
}
})();
/* Update 044: robust nested Stage dropdown - v4 with correct mini-stage grouping */
(function () {
function textOf(el) {
return (el && el.textContent ? el.textContent : '').replace(/\s+/g, ' ').trim();
}
function norm(s) {
return (s || '').replace(/\s+/g, ' ').trim().toLowerCase();
}
function urlOf(link) {
if (!link) return '';
var href = link.getAttribute('href') || '';
var parts = href.split('/');
return parts[parts.length - 1] || '';
}
function isStageLabel(label) {
return /^stage\s+[1-4]$/i.test(label);
}
function getStageNumber(label) {
var m = label.match(/^stage\s+([1-4])$/i);
return m ? m[1] : null;
}
function isMiniLabelForStage(url, stageNum) {
if (stageNum === '1') {
// Stage 1 minis are like "Mini_Stage_X" (no prefix)
return /^Mini_Stage_\d+$/.test(url);
} else {
// Stage 2/3/4 minis are like "Stage_2_Mini_Stage_X"
var pattern = new RegExp('^Stage_' + stageNum + '_Mini_Stage_\\d+$');
return pattern.test(url);
}
}
function directLink(li) {
if (!li || !li.childNodes) return null;
var i;
for (i = 0; i < li.childNodes.length; i++) {
var n = li.childNodes[i];
if (n && n.nodeType === 1 && n.tagName && n.tagName.toLowerCase() === 'a') {
return n;
}
}
return null;
}
function childLis(container) {
if (!container || !container.children) return [];
var out = [];
var i;
for (i = 0; i < container.children.length; i++) {
var c = container.children[i];
if (c && c.tagName && c.tagName.toLowerCase() === 'li') {
out.push(c);
}
}
return out;
}
function scoreContainer(container) {
var lis = childLis(container);
if (!lis.length) return 0;
var stageCount = 0;
var miniCount = 0;
var overviewCount = 0;
var i;
for (i = 0; i < lis.length; i++) {
var label = norm(textOf(directLink(lis[i])));
if (/^stage\s+[1-4]$/i.test(label)) stageCount++;
if (/^mini\s+stage\s+\d+$/i.test(label)) miniCount++;
if (label === 'overview') overviewCount++;
}
if (stageCount >= 4 && miniCount >= 8) {
return (stageCount * 100) + miniCount + (overviewCount ? 10 : 0);
}
return 0;
}
function findStagesListContainer() {
var candidates = document.querySelectorAll('ul, ol');
var best = null;
var bestScore = 0;
var i;
for (i = 0; i < candidates.length; i++) {
var score = scoreContainer(candidates[i]);
if (score > bestScore) {
bestScore = score;
best = candidates[i];
}
}
return best;
}
function makeToggle(stageLi, toggleBtn) {
return function (ev) {
ev.preventDefault();
ev.stopPropagation();
var open = stageLi.classList.toggle('open');
toggleBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
toggleBtn.textContent = open ? '▾' : '▸';
return false;
};
}
function applyNestedStages() {
var list = findStagesListContainer();
if (!list || list.dataset.stagesNestedApplied === '1') return;
var items = childLis(list);
if (!items.length) return;
var rebuilt = [];
var currentStageLi = null;
var currentMiniUl = null;
var currentStageNum = null;
var i;
for (i = 0; i < items.length; i++) {
var li = items[i];
var link = directLink(li);
var label = textOf(link);
var url = urlOf(link);
if (isStageLabel(label)) {
var stageNum = getStageNumber(label);
currentStageNum = stageNum;
currentStageLi = li.cloneNode(true);
currentStageLi.className = (currentStageLi.className ? currentStageLi.className + ' ' : '') + 'stages-inner-parent';
// Make the stage link NOT navigate and act as toggle
var stageLink = directLink(currentStageLi);
if (stageLink) {
stageLink.style.fontWeight = 'bold';
stageLink.href = '#';
}
currentMiniUl = document.createElement('ul');
currentMiniUl.className = 'stages-inner-submenu';
var toggle = document.createElement('button');
toggle.type = 'button';
toggle.className = 'stages-inner-toggle';
toggle.setAttribute('aria-expanded', 'false');
toggle.textContent = '▸';
var toggleHandler = makeToggle(currentStageLi, toggle);
toggle.addEventListener('click', toggleHandler);
// Also make the stage link itself toggleable
if (stageLink) {
stageLink.addEventListener('click', function (ev) {
ev.preventDefault();
ev.stopPropagation();
toggle.click();
return false;
});
}
currentStageLi.appendChild(toggle);
currentStageLi.appendChild(currentMiniUl);
rebuilt.push(currentStageLi);
continue;
}
// Check if this is a mini-stage that belongs to the current stage
if (currentMiniUl && currentStageNum && isMiniLabelForStage(url, currentStageNum)) {
currentMiniUl.appendChild(li.cloneNode(true));
continue;
}
// Not a stage, not a matching mini; reset and add as-is
currentStageLi = null;
currentMiniUl = null;
currentStageNum = null;
rebuilt.push(li.cloneNode(true));
}
while (list.firstChild) {
list.removeChild(list.firstChild);
}
for (i = 0; i < rebuilt.length; i++) {
list.appendChild(rebuilt[i]);
}
list.dataset.stagesNestedApplied = '1';
console.log('[UPDATE-044] Stages nested dropdown applied with correct mini-stage grouping.');
}
function boot() {
applyNestedStages();
window.setTimeout(applyNestedStages, 350);
window.setTimeout(applyNestedStages, 900);
window.setTimeout(applyNestedStages, 1800);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', boot);
} else {
boot();
}
})();