MediaWiki:Mobile.js: Difference between revisions
Content deleted Content added
No edit summary Tag: Manual revert |
No edit summary |
||
Line 113:
});
// Helpers — find collapsed parent section and its H2 heading/anchor
function findCollapsedAncestor(el) {
// Newer MF markup: <section aria-expanded="false">…</section>
var sec = el.closest && el.closest('section[aria-expanded="false"]');
if (sec) return { type: 'section', node: sec };
// Older MF markup: content block with no .open-block; header is previous sibling
var block = el.closest && el.closest('.collapsible-block');
if (block && !block.classList.contains('open-block')) {
return { type: 'block', node: block };
}
return null;
}
function findSectionHeading(collapsed) {
if (!collapsed) return null;
if (collapsed.type === 'section') {
// Prefer the H2 in this section; fall back to any .section-heading
return collapsed.node.querySelector('h2.section-heading, h2, .section-heading');
// .collapsible-block: heading toggle is the previous element
var prev = collapsed.node.previousElementSibling;
if (prev && (prev.matches('h2, .section-heading, .collapsible-heading'))) return prev;
return null;
}
function findAnchorForHeading(heading) {
if (!heading) return null;
// In most wikis the anchor id lives on span.mw-headline (inside h2)
var span = heading.querySelector && heading.querySelector('.mw-headline[id]');
if (span) return span;
// Otherwise the H2 itself may carry the id
return heading.id ? heading : null;
}
function scrollToElement(el) {
try { el.scrollIntoView({ behavior: 'smooth', block: 'start' }); }
catch (_) { el.scrollIntoView(true); }
}
// ⬇️ Replace your existing list click handler with this one
list.addEventListener('click', function (e) {
var a = e.target && e.target.closest('a');
if (!a) return;
e.preventDefault();
var targetId = a.getAttribute('href').slice(1);
var target = document.getElementById(targetId);
closeOverlay();
// Determine the clicked level (we set this in <li data-level="…"> when building the list)
var li = a.closest('li');
var level = li ? parseInt(li.getAttribute('data-level') || '0', 10) : 0;
// If user tapped an H3+ and its H2 section is collapsed → go to that H2 instead
if (target && level >= 3) {
var collapsed = findCollapsedAncestor(target);
if (collapsed) {
var h2 = findSectionHeading(collapsed);
var anchorEl = findAnchorForHeading(h2) || h2;
if (anchorEl) {
scrollToElement(anchorEl);
// Update URL to the H2's anchor if available
if (anchorEl.id) {
setTimeout(function () {
if (history && history.replaceState) {
history.replaceState(null, '', '#' + anchorEl.id);
} else {
location.hash = anchorEl.id;
}
}, 200);
}
return; // done
}
}
}
// Default behavior: go to the requested target (H2 or H3) when not inside a collapsed section
if (target) {
scrollToElement(target);
setTimeout(function () {
if (history && history.replaceState) {
history.replaceState(null, '', '#' + targetId);
} else {
location.hash = targetId;
}
}, 200);
}
});
// Re-hide on rotation/resize to tablet/desktop
| |||