MediaWiki:Common.js: Difference between revisions
From AnarchyMU Wiki
No edit summary |
No edit summary |
||
| Line 19: | Line 19: | ||
} | } | ||
})(); | })(); | ||
/* Update | |||
/* Update 043: robust nested Stage dropdown regrouping - v3 with proper click handling and styling */ | |||
(function () { | (function () { | ||
function textOf(el) { | function textOf(el) { | ||
| Line 46: | Line 47: | ||
} | } | ||
} | } | ||
return | return null; | ||
} | } | ||
| Line 101: | Line 102: | ||
} | } | ||
function makeToggle(stageLi, toggleBtn) { | function makeToggle(stageLi, toggleBtn, stageLink) { | ||
return function (ev) { | return function (ev) { | ||
ev.preventDefault(); | ev.preventDefault(); | ||
| Line 108: | Line 109: | ||
toggleBtn.setAttribute('aria-expanded', open ? 'true' : 'false'); | toggleBtn.setAttribute('aria-expanded', open ? 'true' : 'false'); | ||
toggleBtn.textContent = open ? '▾' : '▸'; | toggleBtn.textContent = open ? '▾' : '▸'; | ||
return false; | |||
}; | }; | ||
} | } | ||
| Line 125: | Line 127: | ||
for (i = 0; i < items.length; i++) { | for (i = 0; i < items.length; i++) { | ||
var li = items[i]; | var li = items[i]; | ||
var | var link = directLink(li); | ||
var label = textOf(link); | |||
if (isStageLabel(label)) { | if (isStageLabel(label)) { | ||
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'; | ||
// 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 = document.createElement('ul'); | ||
| Line 139: | Line 149: | ||
toggle.setAttribute('aria-expanded', 'false'); | toggle.setAttribute('aria-expanded', 'false'); | ||
toggle.textContent = '▸'; | toggle.textContent = '▸'; | ||
toggle.addEventListener('click', | |||
var toggleHandler = makeToggle(currentStageLi, toggle, stageLink); | |||
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(toggle); | ||
| Line 166: | Line 188: | ||
list.dataset.stagesNestedApplied = '1'; | list.dataset.stagesNestedApplied = '1'; | ||
console.log('[UPDATE-043] Stages nested dropdown applied successfully.'); | |||
} | } | ||
Revision as of 10:31, 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 043: robust nested Stage dropdown regrouping - v3 with proper click handling and styling */
(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 isStageLabel(label) {
return /^stage\s+[1-4]$/i.test(label);
}
function isMiniLabel(label) {
return /^mini\s+stage\s+\d+$/i.test(label);
}
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 (isStageLabel(label)) stageCount++;
if (isMiniLabel(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, stageLink) {
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 i;
for (i = 0; i < items.length; i++) {
var li = items[i];
var link = directLink(li);
var label = textOf(link);
if (isStageLabel(label)) {
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, stageLink);
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;
}
if (isMiniLabel(label) && currentMiniUl) {
currentMiniUl.appendChild(li.cloneNode(true));
continue;
}
currentStageLi = null;
currentMiniUl = 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-043] Stages nested dropdown applied successfully.');
}
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();
}
})();