MediaWiki:Mobile.js: Difference between revisions
Appearance
Content deleted Content added
Created page with "→All JavaScript here will be loaded for users of the mobile site: // Back to top (CapSach) — all skins; mobile widths (function () { var ID = 'cps-backtotop'; function ensureButton() { if (document.getElementById(ID)) return; var btn = document.createElement('button'); btn.id = ID; btn.type = 'button'; btn.setAttribute('aria-label', 'Back to top'); btn.title = 'Back to top'; btn.innerHTML = '<span aria-hidden="true">↑</span>';..." |
No edit summary |
||
| (31 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
// CapSach — Sticky TOC overlay (UNRESTRICTED: Works on iPad/Desktop/Mobile) |
|||
/* All JavaScript here will be loaded for users of the mobile site */ |
|||
// Back to top (CapSach) — all skins; mobile widths |
|||
(function () { |
(function () { |
||
var ID = 'cps-backtotop'; |
|||
// 1. REMOVED the "min-width: 768px" check. Now runs everywhere. |
|||
// Only run on pages where it makes sense (Articles/MainPage) |
|||
function ensureButton() { |
|||
if (window.mw && mw.config && mw.config.get) { |
|||
if (document.getElementById(ID)) return; |
|||
var isAllowed = mw.config.get('wgIsArticle') || mw.config.get('wgIsMainPage'); |
|||
if (!isAllowed) return; |
|||
} |
|||
// Find the content root; MobileFrontend restructures DOM, so be flexible |
|||
var btn = document.createElement('button'); |
|||
var root = |
|||
document.querySelector('#mw-content-text .mw-parser-output') || |
|||
btn.type = 'button'; |
|||
document.querySelector('.mw-parser-output') || |
|||
btn.setAttribute('aria-label', 'Back to top'); |
|||
document.getElementById('mw-content-text') || |
|||
btn.title = 'Back to top'; |
|||
document.querySelector('#content') || |
|||
btn.innerHTML = '<span aria-hidden="true">↑</span>'; |
|||
document.body; |
|||
// Collect headings (H2–H6). Prefer spans with .mw-headline (stable anchor ids) |
|||
document.body.appendChild(btn); |
|||
var items = []; |
|||
updateVisibility(); |
|||
var headings = root.querySelectorAll('h2, h3, h4, h5, h6'); |
|||
headings.forEach(function (h) { |
|||
var level = parseInt(h.tagName.slice(1), 10); |
|||
if (level < 2 || level > 6) return; |
|||
var headline = h.querySelector('.mw-headline') || h; |
|||
var id = headline.id || h.id; |
|||
var text = (headline.textContent || h.textContent || '').trim(); |
|||
if (!id || !text) return; |
|||
items.push({ id: id, text: text, level: level }); |
|||
}); |
|||
// Show only if there are enough headings to be useful |
|||
var prefersReducedMotion = false; |
|||
// CHANGED: Lowered requirement to 1 heading so it always shows if there is any structure |
|||
try { |
|||
if (items.length < 1) return; |
|||
prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
|||
} catch (e) {} |
|||
// Create trigger button (bottom-left; avoids “Back to top” on bottom-right) |
|||
btn.addEventListener('click', function () { |
|||
var btn = document.createElement('button'); |
|||
btn.id = 'cps-open-toc'; |
|||
btn.type = 'button'; |
|||
btn.setAttribute('aria-label', 'Open table of contents'); |
|||
btn.innerHTML = '<span class="icon" aria-hidden="true">≡</span><span class="label">TOC</span>'; |
|||
document.body.appendChild(btn); |
|||
// Overlay + panel |
|||
var overlay = document.createElement('div'); |
|||
overlay.id = 'cps-toc-overlay'; |
|||
overlay.setAttribute('aria-hidden', 'true'); |
|||
var panel = document.createElement('div'); |
|||
panel.id = 'cps-toc-panel'; |
|||
panel.setAttribute('role', 'dialog'); |
|||
panel.setAttribute('aria-modal', 'true'); |
|||
panel.setAttribute('aria-label', 'Table of contents'); |
|||
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'); |
|||
list.id = 'cps-toc-list'; |
|||
items.forEach(function (it) { |
|||
var li = document.createElement('li'); |
|||
li.setAttribute('data-level', String(it.level)); |
|||
var a = document.createElement('a'); |
|||
a.href = '#' + it.id; |
|||
a.textContent = it.text; |
|||
li.appendChild(a); |
|||
list.appendChild(li); |
|||
}); |
|||
panel.appendChild(header); |
|||
panel.appendChild(list); |
|||
overlay.appendChild(panel); |
|||
document.body.appendChild(overlay); |
|||
// Focus handling |
|||
var lastFocus = null; |
|||
function openOverlay() { |
|||
lastFocus = document.activeElement; |
|||
overlay.classList.add('is-open'); |
|||
overlay.setAttribute('aria-hidden', 'false'); |
|||
document.body.style.overflow = 'hidden'; |
|||
// Focus first link for accessibility |
|||
var firstLink = list.querySelector('a'); |
|||
if (firstLink) firstLink.focus({ preventScroll: true }); |
|||
} |
|||
function closeOverlay() { |
|||
overlay.classList.remove('is-open'); |
|||
overlay.setAttribute('aria-hidden', 'true'); |
|||
document.body.style.overflow = ''; |
|||
if (lastFocus && lastFocus.focus) lastFocus.focus({ preventScroll: true }); |
|||
} |
|||
// Force button display immediately |
|||
btn.style.display = 'flex'; |
|||
btn.addEventListener('click', openOverlay); |
|||
overlay.addEventListener('click', function (e) { |
|||
// Click outside the bottom sheet closes |
|||
if (e.target === overlay) closeOverlay(); |
|||
}); |
|||
overlay.querySelector('#cps-toc-close').addEventListener('click', closeOverlay); |
|||
overlay.addEventListener('keydown', function (e) { |
|||
if (e.key === 'Escape') closeOverlay(); |
|||
}); |
|||
// Navigate and try to ensure mobile-collapsed sections are visible |
|||
list.addEventListener('click', function (e) { |
|||
var a = e.target.closest('a'); |
|||
if (!a) return; |
|||
e.preventDefault(); |
|||
var targetId = a.getAttribute('href').slice(1); |
|||
var target = document.getElementById(targetId); |
|||
closeOverlay(); |
|||
if (target) { |
|||
try { |
try { |
||
target.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|||
} catch ( |
} catch (_) { |
||
target.scrollIntoView(true); |
|||
window.scrollTo(0, 0); |
|||
} |
} |
||
}); |
|||
// Update URL hash after a tick (so browser back works) |
|||
window.addEventListener('scroll', onScroll, { passive: true }); |
|||
setTimeout(function () { |
|||
window.addEventListener('resize', onScroll, { passive: true }); |
|||
if (history && history.replaceState) { |
|||
} |
|||
history.replaceState(null, '', '#' + targetId); |
|||
} else { |
|||
location.hash = targetId; |
|||
} |
|||
}, 200); |
|||
// MobileFrontend: headings may be inside collapsed sections. |
|||
var ticking = false; |
|||
// Heuristic: click the nearest toggle if present. |
|||
function onScroll() { |
|||
var maybeToggle = target.closest('.collapsible-block, .mf-section') || |
|||
if (!ticking) { |
|||
target.closest('section'); |
|||
window.requestAnimationFrame(function () { |
|||
if (maybeToggle && maybeToggle.classList.contains('collapsed')) { |
|||
updateVisibility(); |
|||
// Try to open; fallback by clicking the first heading inside |
|||
ticking = false; |
|||
var headingToggle = maybeToggle.querySelector('.section-heading, h2, h3, h4, h5, h6'); |
|||
}); |
|||
if (headingToggle) headingToggle.click(); |
|||
ticking = true; |
|||
} |
|||
} |
} |
||
} |
}); |
||
// 2. REMOVED the "resize" event listener that was hiding the button. |
|||
function updateVisibility() { |
|||
// The button now persists on all screen sizes. |
|||
var show = window.pageYOffset > 300 && |
|||
})(); |
|||
document.documentElement.scrollHeight > window.innerHeight * 2; |
|||
var el = document.getElementById(ID); |
|||
if (el) el.classList.toggle('is-visible', show); |
|||
} |
|||
$(document).ready(function() { |
|||
// Run on initial load |
|||
// Check if the button already exists to prevent duplicates |
|||
if (document.readyState === 'loading') { |
|||
if ($('#custom-email-btn').length === 0) { |
|||
document.addEventListener('DOMContentLoaded', ensureButton); |
|||
// Create the email button element |
|||
ensureButton(); |
|||
var emailBtn = $('<a>', { |
|||
} |
|||
id: 'custom-email-btn', |
|||
href: 'mailto:services@axabrain.com', |
|||
// Simple accessible title |
|||
title: 'Contact AXA BRAIN Services' |
|||
}); |
|||
// Add it to the body of the page |
|||
// Also run when MediaWiki re-initializes page content (preview, some SPA-ish flows) |
|||
$('body').append(emailBtn); |
|||
if (window.mw && mw.hook) { |
|||
} |
|||
mw.hook('wikipage.content').add(ensureButton); |
|||
}); |
|||
} |
|||
})(); |
|||
Latest revision as of 21:46, 3 February 2026
// CapSach — Sticky TOC overlay (UNRESTRICTED: Works on iPad/Desktop/Mobile)
(function () {
// 1. REMOVED the "min-width: 768px" check. Now runs everywhere.
// Only run on pages where it makes sense (Articles/MainPage)
if (window.mw && mw.config && mw.config.get) {
var isAllowed = mw.config.get('wgIsArticle') || mw.config.get('wgIsMainPage');
if (!isAllowed) return;
}
// Find the content root; MobileFrontend restructures DOM, so be flexible
var root =
document.querySelector('#mw-content-text .mw-parser-output') ||
document.querySelector('.mw-parser-output') ||
document.getElementById('mw-content-text') ||
document.querySelector('#content') ||
document.body;
// Collect headings (H2–H6). Prefer spans with .mw-headline (stable anchor ids)
var items = [];
var headings = root.querySelectorAll('h2, h3, h4, h5, h6');
headings.forEach(function (h) {
var level = parseInt(h.tagName.slice(1), 10);
if (level < 2 || level > 6) return;
var headline = h.querySelector('.mw-headline') || h;
var id = headline.id || h.id;
var text = (headline.textContent || h.textContent || '').trim();
if (!id || !text) return;
items.push({ id: id, text: text, level: level });
});
// Show only if there are enough headings to be useful
// CHANGED: Lowered requirement to 1 heading so it always shows if there is any structure
if (items.length < 1) return;
// Create trigger button (bottom-left; avoids “Back to top” on bottom-right)
var btn = document.createElement('button');
btn.id = 'cps-open-toc';
btn.type = 'button';
btn.setAttribute('aria-label', 'Open table of contents');
btn.innerHTML = '<span class="icon" aria-hidden="true">≡</span><span class="label">TOC</span>';
document.body.appendChild(btn);
// Overlay + panel
var overlay = document.createElement('div');
overlay.id = 'cps-toc-overlay';
overlay.setAttribute('aria-hidden', 'true');
var panel = document.createElement('div');
panel.id = 'cps-toc-panel';
panel.setAttribute('role', 'dialog');
panel.setAttribute('aria-modal', 'true');
panel.setAttribute('aria-label', 'Table of contents');
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');
list.id = 'cps-toc-list';
items.forEach(function (it) {
var li = document.createElement('li');
li.setAttribute('data-level', String(it.level));
var a = document.createElement('a');
a.href = '#' + it.id;
a.textContent = it.text;
li.appendChild(a);
list.appendChild(li);
});
panel.appendChild(header);
panel.appendChild(list);
overlay.appendChild(panel);
document.body.appendChild(overlay);
// Focus handling
var lastFocus = null;
function openOverlay() {
lastFocus = document.activeElement;
overlay.classList.add('is-open');
overlay.setAttribute('aria-hidden', 'false');
document.body.style.overflow = 'hidden';
// Focus first link for accessibility
var firstLink = list.querySelector('a');
if (firstLink) firstLink.focus({ preventScroll: true });
}
function closeOverlay() {
overlay.classList.remove('is-open');
overlay.setAttribute('aria-hidden', 'true');
document.body.style.overflow = '';
if (lastFocus && lastFocus.focus) lastFocus.focus({ preventScroll: true });
}
// Force button display immediately
btn.style.display = 'flex';
btn.addEventListener('click', openOverlay);
overlay.addEventListener('click', function (e) {
// Click outside the bottom sheet closes
if (e.target === overlay) closeOverlay();
});
overlay.querySelector('#cps-toc-close').addEventListener('click', closeOverlay);
overlay.addEventListener('keydown', function (e) {
if (e.key === 'Escape') closeOverlay();
});
// Navigate and try to ensure mobile-collapsed sections are visible
list.addEventListener('click', function (e) {
var a = e.target.closest('a');
if (!a) return;
e.preventDefault();
var targetId = a.getAttribute('href').slice(1);
var target = document.getElementById(targetId);
closeOverlay();
if (target) {
try {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
} catch (_) {
target.scrollIntoView(true);
}
// Update URL hash after a tick (so browser back works)
setTimeout(function () {
if (history && history.replaceState) {
history.replaceState(null, '', '#' + targetId);
} else {
location.hash = targetId;
}
}, 200);
// MobileFrontend: headings may be inside collapsed sections.
// Heuristic: click the nearest toggle if present.
var maybeToggle = target.closest('.collapsible-block, .mf-section') ||
target.closest('section');
if (maybeToggle && maybeToggle.classList.contains('collapsed')) {
// Try to open; fallback by clicking the first heading inside
var headingToggle = maybeToggle.querySelector('.section-heading, h2, h3, h4, h5, h6');
if (headingToggle) headingToggle.click();
}
}
});
// 2. REMOVED the "resize" event listener that was hiding the button.
// The button now persists on all screen sizes.
})();
$(document).ready(function() {
// Check if the button already exists to prevent duplicates
if ($('#custom-email-btn').length === 0) {
// Create the email button element
var emailBtn = $('<a>', {
id: 'custom-email-btn',
href: 'mailto:services@axabrain.com',
// Simple accessible title
title: 'Contact AXA BRAIN Services'
});
// Add it to the body of the page
$('body').append(emailBtn);
}
});