MediaWiki:Mobile.js: Difference between revisions
Content deleted Content added
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 8:
}
/* CapSach — Mobile TOC overlay (
(function () {
/
function isMobileSite() {
// Stable
return document.body && document.body.classList.contains('mw-mf'); /* MF site */ /* */
}
function once(id) { return !document.getElementById(id); }
function onReady(fn) {
Line 35 ⟶ 33:
}
// Account for sticky headers when scrolling
function smoothScrollTo(el) {
var offset = 0;
try {
var
var cs =
if (cs.position === 'fixed' || cs.position === 'sticky') {
var r = node.getBoundingClientRect();
Line 53 ⟶ 50:
}
/* ---------- MF section helpers ---------- */
// NEW heading markup: ids can be on <hN> or on .mw-headline (MW 1.43+ wrappers)
function anchorForHeadingEl(hEl) {
if (!hEl) return null;
var span = hEl.querySelector && hEl.querySelector('.mw-headline[id]');
if (span) return span;
return hEl.id ? hEl : null; /* MW 1.44+ may put id on <hN> */
}
// Find the element that actually toggles the H2 section (owns aria-expanded / aria-controls)
function getH2Toggle(h2El) {
if (!h2El) return null;
var w = h2El.closest('.mw-heading') || h2El;
var ctrl = w.querySelector('button[aria-controls], [aria-expanded][aria-controls]');
if (ctrl) return ctrl;
// Next
ctrl =
if (ctrl) return ctrl;
// Legacy: the
if (h2El.hasAttribute('aria-expanded') || h2El.classList.contains('section-heading') ||
h2El.classList.contains('collapsible-heading')) {
return h2El;
}
// As a last resort, some old MF put a separate preceding/next sibling as the toggle
var prev = h2El.previousElementSibling, next = h2El.nextElementSibling;
if (prev && (prev.matches('[aria-expanded], .section-heading, .collapsible-heading'))) return prev;
if (next && (next.matches('[aria-expanded], .section-heading, .collapsible-heading'))) return next;
return null;
}
function h2IsCollapsed(h2El) {
if (!h2El) return false;
Line 81 ⟶ 88:
if (ae === 'false') return true;
if (ae === 'true') return false;
var
if (
var block = document.getElementById(
if (block) {
if (block.hidden || block.getAttribute('hidden') !== null) return true;
Line 90 ⟶ 97:
}
}
//
var
if (
// Newer wrapper pattern: <section aria-expanded="false"> around heading+content
var sec = h2El.closest('section[aria-expanded]');
if (sec) return sec.getAttribute('aria-expanded') === 'false';
return false;
}
function fireActivation(el) {
// Fire a sequence of events so whichever MF handler is present reacts
var opts = { bubbles: true, cancelable: true, view: window };
try { el.dispatchEvent(new PointerEvent('pointerdown', opts)); } catch (_) {}
try { el.dispatchEvent(new MouseEvent('mousedown', opts)); } catch (_) {}
try { el.dispatchEvent(new TouchEvent('touchstart', opts)); } catch (_) {}
try { el.dispatchEvent(new TouchEvent('touchend', opts)); } catch (_) {}
try { el.dispatchEvent(new MouseEvent('mouseup', opts)); } catch (_) {}
try { el.dispatchEvent(new MouseEvent('click', opts)); } catch (_) { el.click(); }
}
// Open H2 if needed; resolve when open (or after timeout)
function ensureH2Open(h2El) {
return new Promise(function (resolve) {
if (!h2El || !h2IsCollapsed(h2El)) return resolve();
var
var triedManual = false;
// Observe changes to aria-expanded
var obs = null;
if (window.MutationObserver) {
obs = new MutationObserver(function () {
if (!h2IsCollapsed(h2El)) {
if (obs) obs.disconnect();
}
});
var targets = [];
if (toggle) targets.push(toggle);
var cid = toggle && toggle.getAttribute('aria-controls');
var controlled = cid && document.getElementById(cid);
if (controlled) targets.push(controlled);
// Also watch the wrapper <section aria-expanded>
var sec = h2El.closest('section[aria-expanded]');
if (sec) targets.push(sec);
if (targets.length) {
targets.forEach(function (t) {
obs.observe(t, { attributes: true, attributeFilter: ['aria-expanded', 'class', 'hidden'] });
});
}
}
// First: try to use MF’s own handler
if (toggle) fireActivation(toggle);
// Fallback (after a short tick) – manually open if still collapsed
setTimeout(function manualIfNeeded() {
if (!h2IsCollapsed(h2El)) return; // already open
if (!triedManual) {
triedManual = true;
// Manual open strategy:
// 1) If we have a controls target, unhide it + mark open-block
var cid = toggle && toggle.getAttribute('aria-controls');
var block = cid && document.getElementById(cid);
block.hidden = false;
block.removeAttribute('hidden');
block.classList.add('open-block');
}
// 2) If a wrapper <section aria-expanded> exists, flip it
var sec = h2El.closest('section[aria-expanded]');
if (sec) sec.setAttribute('aria-expanded', 'true');
// 3) If legacy sibling block exists, mark it open
var sib = h2El.nextElementSibling;
if (sib && sib.classList.contains('collapsible-block')) {
sib.classList.add('open-block');
sib.hidden = false;
sib.removeAttribute('hidden');
}
}
// Final safety timeout: stop waiting after 900ms overall
}, 120);
setTimeout(function () {
if (
resolve(); // even if still closed, we won’t hang
}, 900);
});
}
/
function initTOC() {
//
if (window.matchMedia('(min-width: 768px)').matches) return;
// only in article view
if (mw.config && !mw.config.get('wgIsArticle')) return;
// avoid duplicate buttons
if (!once('cps-open-toc')) return;
var root = getContentRoot();
// Build
var items = [];
var indexById = Object.create(null);
var headingElById = Object.create(null);
var level = parseInt(h.tagName.slice(1), 10);
if (level < 2 || level > 6) return;
var
var
if (!id || !text) return;
items.push({ id: id, text: text, level: level });
indexById[id] = items.length - 1;
Line 183 ⟶ 217:
});
if (items.length < 3) return; //
// UI: trigger button
Line 212 ⟶ 246:
var list = document.createElement('ul');
list.id = 'cps-toc-list';
items.forEach(function (it) {
var li = document.createElement('li');
Line 227 ⟶ 262:
document.body.appendChild(overlay);
//
var lastFocus = null;
function openOverlay() {
Line 234 ⟶ 269:
overlay.setAttribute('aria-hidden', 'false');
document.body.style.overflow = 'hidden';
var
if (
}
function closeOverlay() {
Line 243 ⟶ 278:
if (lastFocus && lastFocus.focus) lastFocus.focus({ preventScroll: true });
}
btn.style.display = 'flex';
btn.addEventListener('click', openOverlay);
Line 250 ⟶ 284:
overlay.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeOverlay(); });
// parent H2 lookup
function parentH2For(id) {
var idx = indexById[id];
Line 258 ⟶ 292:
}
//
list.addEventListener('click', function (e) {
var a = e.target && e.target.closest('a');
Line 267 ⟶ 301:
var li = a.closest('li');
var level = li ? parseInt(li.getAttribute('data-level') || '0', 10) : 0;
closeOverlay();
//
if (level >= 3) {
var p = parentH2For(targetId);
if (p) {
var h2El = headingElById[p.id];
ensureH2Open(h2El
//
var
requestAnimationFrame(function () {
smoothScrollTo(
setTimeout(function () {
if (history && history.replaceState) history.replaceState(null, '', '#' + finalId);
else location.hash = finalId;
},
});
});
Line 297 ⟶ 326:
}
// H2 or no parent found:
var
(level === 2 ? anchorForHeadingEl(headingElById[targetId]) : null);
if (!h) return;
requestAnimationFrame(function () {
smoothScrollTo(
setTimeout(function () {
if (history && history.replaceState) history.replaceState(null, '', '#' + (
else location.hash =
},
});
});
Line 316 ⟶ 346:
}
/
if (window.mw && mw.loader) {
// Ensure
mw.loader.using('mobile.startup').then(function () { /* MF site modules */
onReady(function () { if (isMobileSite()) initTOC(); });
if (mw.hook) mw.hook('wikipage.content').add(function () {
if (isMobileSite()) initTOC();
});
});
} else {
onReady(function () { if (isMobileSite()) initTOC(); });
}
| |||