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 (phones);
(function () {
/* ---------------- utilities ---------------- */
function isMobileSite() {
// Stable
return document.body && document.body.classList.contains('mw-mf'); /
}
function once(id) { return !document.getElementById(id); }
function onReady(fn) { if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fn); else fn(); }
function getContentRoot() {
Line 33 ⟶ 28:
}
//
function smoothScrollTo(el) {
var offset = 0;
try {
var cs = getComputedStyle(node);
if (cs.position === 'fixed' || cs.position === 'sticky') {
Line 50 ⟶ 44:
}
// In MW 1.43+ IDs may be on .mw-headline (inside the heading wrapper). :contentReference[oaicite:2]{index=2}
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;
}
/* ---------------- section open logic ---------------- */
// Try to locate the *actual* toggle that MobileFrontend attached.
function getH2Toggle(h2El) {
if (!h2El) return null;
var
//
var ctrl =
if (!ctrl)
// Legacy: the H2 or its sibling acts as the toggle.
if (!ctrl && (h2El.hasAttribute('aria-expanded') || h2El.classList.contains('section-heading') || h2El.classList.contains('collapsible-heading'))) ctrl = h2El;
if (!ctrl && h2El.previousElementSibling && (h2El.previousElementSibling.matches('[aria-expanded], .section-heading, .collapsible-heading'))) ctrl = h2El.previousElementSibling;
if (!ctrl && h2El.nextElementSibling && (h2El.nextElementSibling.matches('[aria-expanded], .section-heading, .collapsible-heading'))) ctrl = h2El.nextElementSibling;
return ctrl;
}
function h2IsCollapsed(h2El) {
if (!h2El) return false;
// Newer pattern: wrapper section with aria-expanded controls visibility.
if (sec) return sec.getAttribute('aria-expanded') === 'false';
// Toggle state on heading/button itself.
var t = getH2Toggle(h2El);
if (t) {
var ae = t.getAttribute('aria-expanded');
if (ae === 'false') return true;
if (ae === 'true') return false;
var cid =
if (cid) {
var block = document.getElementById(cid);
Line 97 ⟶ 89:
}
}
// Legacy: sibling .collapsible-block right after H2
var sib = h2El.nextElementSibling;
if (sib && sib.classList.contains('collapsible-block')) return !sib.classList.contains('open-block');
return false;
}
// Synthesize a robust "activation" to trigger MF's own handler if present.
function activate(el) {
var
try { el.dispatchEvent(new
try { el.dispatchEvent(new MouseEvent('
try { el.dispatchEvent(new
}
// **Guarantee** the H2 section is open:
// 1) try MF's toggle; 2) if still closed, **force-open** the DOM (wrapper+block).
function ensureH2Open(h2El) {
return new Promise(function (resolve) {
Line 123 ⟶ 110:
var toggle = getH2Toggle(h2El);
var
function finish() { if (!done) { done = true; if (obs) obs.disconnect(); resolve(); } }
if (window.MutationObserver) {
obs = new MutationObserver(function () {
if (!h2IsCollapsed(h2El))
});
var targets = [];
if (toggle) targets.push(toggle);
var sec = h2El.closest('section[aria-expanded]');
if (sec) targets.push(sec);
var cid = toggle && toggle.getAttribute('aria-controls');
var block = cid && document.getElementById(cid);
if (block) targets.push(block);
targets.forEach(function
obs.observe(t, { attributes: true, attributeFilter: ['aria-expanded', 'class', 'hidden', 'style'] });
});
}
//
if (toggle)
//
setTimeout(function
if (!h2IsCollapsed(h2El)) return finish();
// Wrapper
var sec = h2El.closest('section[aria-expanded]');
if (sec) sec.setAttribute('aria-expanded', 'true');
if (toggle) {
var cid = toggle.getAttribute('aria-controls');
var block = cid && document.getElementById(cid);
if (block) {
block.hidden = false;
block.removeAttribute('hidden');
block.classList.add('open-block'); // legacy open marker
block.style.display = ''; // clear any inline display:none
}
}
//
var sib = h2El.nextElementSibling;
if (sib && sib.classList.contains('collapsible-block')) {
sib.hidden = false;
sib.removeAttribute('hidden');
sib.classList.add('open-block');
sib.style.display = '';
}
// Final safety: even if some CSS remains, proceed; scrolling to H3 will now work.
finish();
}, 120);
// Hard stop to prevent hangs
});
}
/* ---------------- main initializer ---------------- */
function initTOC() {
if (window.matchMedia('(min-width: 768px)').matches) return; // phones only
if (mw.config && !mw.config.get('wgIsArticle')) return; // content pages only
if (!once('cps-open-toc')) return; // avoid duplicates
var root = getContentRoot();
// Build heading list
var items = [];
var indexById = Object.create(null);
Line 208 ⟶ 187:
var level = parseInt(h.tagName.slice(1), 10);
if (level < 2 || level > 6) return;
var anchor = h.querySelector('.mw-headline[id]') || h; // works pre/post Heading-HTML changes :contentReference[oaicite:3]{index=3}
var id = anchor.id || h.id;
var text = (anchor.textContent || h.textContent || '').trim();
Line 217 ⟶ 196:
});
if (items.length < 3) return; //
// UI: trigger button
Line 240 ⟶ 219:
var header = document.createElement('div');
header.id = 'cps-toc-header';
header.innerHTML = '<h2 id="cps-toc-title">Contents</h2><button id="cps-toc-close" type="button" aria-label="Close">×</button>';
var list = document.createElement('ul');
Line 262 ⟶ 239:
document.body.appendChild(overlay);
//
var lastFocus = null;
function openOverlay() {
Line 269 ⟶ 246:
overlay.setAttribute('aria-hidden', 'false');
document.body.style.overflow = 'hidden';
var
if (
}
function closeOverlay() {
Line 278 ⟶ 255:
if (lastFocus && lastFocus.focus) lastFocus.focus({ preventScroll: true });
}
btn.style.display = 'flex';
btn.addEventListener('click', openOverlay);
Line 284 ⟶ 262:
overlay.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeOverlay(); });
function parentH2For(id) {
var idx = indexById[id];
Line 292 ⟶ 269:
}
//
list.addEventListener('click', function (e) {
var a = e.target && e.target.closest('a');
Line 302 ⟶ 279:
var level = li ? parseInt(li.getAttribute('data-level') || '0', 10) : 0;
closeOverlay(); // let layout settle first (iOS)
if (level >= 3) {
var p = parentH2For(targetId);
Line 310 ⟶ 286:
var h2El = headingElById[p.id];
ensureH2Open(h2El).then(function () {
// Prefer the intended H3
var dest = document.getElementById(targetId) || anchorForHeadingEl(h2El) || h2El;
if (!dest) return;
Line 326 ⟶ 302:
}
// H2 (or no parent
var
(level === 2 ? anchorForHeadingEl(headingElById[targetId]) : null);
if (!
requestAnimationFrame(function () {
smoothScrollTo(
setTimeout(function () {
if (history && history.replaceState) history.replaceState(null, '', '#' + (
else location.hash =
}, 120);
});
});
// Hide/show
window.addEventListener('resize', function () {
if (window.matchMedia('(min-width: 768px)').matches) { btn.style.display = 'none'; closeOverlay(); }
Line 346 ⟶ 322:
}
/* ---------------- bootstrap ---------------- */
if (window.mw && mw.loader) {
//
mw.loader.using('mobile.startup').then(function () {
onReady(function () { if (isMobileSite()) initTOC(); });
if (mw.hook) mw.hook('wikipage.content').add(function () { if (isMobileSite()) initTOC();
});
} else {
| |||