MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* | /* | ||
* GladiusRO Wiki | * GladiusRO Wiki | ||
* Move the article Table of Contents | * Move the article Table of Contents below the sidebar logo. | ||
*/ | */ | ||
mw.loader.using(['mediawiki.util']).then(function () { | mw.loader.using(['mediawiki.util']).then(function () { | ||
function | function moveTOCBelowLogo() { | ||
var toc = document.querySelector('#toc'); | var toc = document.querySelector('#toc'); | ||
/* | /* | ||
* Stop if | * Stop if the page has no TOC | ||
* or the TOC has already been moved. | |||
* | |||
*/ | */ | ||
if (!toc || document.querySelector('#gladius-sidebar-toc')) { | if (!toc || document.querySelector('#gladius-sidebar-toc')) { | ||
| Line 19: | Line 18: | ||
/* | /* | ||
* Find the | * Find the existing left sidebar. | ||
*/ | */ | ||
var sidebar = | var sidebar = | ||
| Line 33: | Line 30: | ||
/* | /* | ||
* Create | * Create the new TOC container. | ||
*/ | */ | ||
var tocContainer = document.createElement('div'); | var tocContainer = document.createElement('div'); | ||
| Line 43: | Line 40: | ||
/* | /* | ||
* Move the | * Move the original article TOC into the container. | ||
*/ | */ | ||
tocContainer.appendChild(toc); | tocContainer.appendChild(toc); | ||
/* | /* | ||
* | * Try to find the GladiusRO sidebar logo. | ||
*/ | */ | ||
sidebar.insertBefore(tocContainer, sidebar. | var logoImage = sidebar.querySelector( | ||
'img[src*="logo"], ' + | |||
'img[src*="Logo"], ' + | |||
'img[alt*="Gladius"], ' + | |||
'img[title*="Gladius"]' | |||
); | |||
/* | |||
* If the logo image is found, locate the outer element | |||
* containing the logo. | |||
*/ | |||
if (logoImage) { | |||
var logoContainer = | |||
logoImage.closest('.portal') || | |||
logoImage.closest('.mw-portlet') || | |||
logoImage.closest('div') || | |||
logoImage.parentElement; | |||
/* | |||
* Insert Contents immediately below the logo. | |||
*/ | |||
if (logoContainer && logoContainer.parentNode) { | |||
logoContainer.parentNode.insertBefore( | |||
tocContainer, | |||
logoContainer.nextSibling | |||
); | |||
} else { | |||
sidebar.appendChild(tocContainer); | |||
} | |||
} else { | |||
/* | |||
* Fallback: | |||
* Put Contents before the navigation links, | |||
* without placing the TOC above the logo. | |||
*/ | |||
var navigation = | |||
* sidebar.querySelect*r('#p-navigation') || | |||
* sidebar.querySelector('.portal'* || | |||
sidebar.querySelector('.mw-portlet'); | |||
if (navigation) { | |||
navigation.parentNode.insertBefore( | |||
tocContainer, | |||
navigation | |||
); | |||
} else { | |||
sidebar.appendChild(tocContainer); | |||
} | |||
} | |||
document.body.classList.add('gladius-toc-enabled'); | document.body.classList.add('gladius-toc-enabled'); | ||
| Line 57: | Line 101: | ||
/* | /* | ||
* Run | * Run after the HTML page is ready. | ||
*/ | */ | ||
function initialiseGladiusTOC() { | |||
window.setTimeout(moveTOCBelowLogo, 100); | |||
} | |||
initialiseGladiusTOC(); | |||
/* | /* | ||
* Run | * Run when MediaWiki updates article content. | ||
*/ | */ | ||
mw.hook('wikipage. | mw.hook('wikipage.content').add(function () { | ||
initialiseGladiusTOC(); | |||
}); | }); | ||
}); | }); | ||
Revision as of 02:34, 2 August 2026
/*
* GladiusRO Wiki
* Move the article Table of Contents below the sidebar logo.
*/
mw.loader.using(['mediawiki.util']).then(function () {
function moveTOCBelowLogo() {
var toc = document.querySelector('#toc');
/*
* Stop if the page has no TOC
* or the TOC has already been moved.
*/
if (!toc || document.querySelector('#gladius-sidebar-toc')) {
return;
}
/*
* Find the existing left sidebar.
*/
var sidebar =
document.querySelector('#mw-panel') ||
document.querySelector('#mw-navigation') ||
document.querySelector('.sidebar');
if (!sidebar) {
return;
}
/*
* Create the new TOC container.
*/
var tocContainer = document.createElement('div');
tocContainer.id = 'gladius-sidebar-toc';
tocContainer.className = 'mw-portlet portal';
tocContainer.setAttribute('role', 'navigation');
tocContainer.setAttribute('aria-label', 'Page contents');
/*
* Move the original article TOC into the container.
*/
tocContainer.appendChild(toc);
/*
* Try to find the GladiusRO sidebar logo.
*/
var logoImage = sidebar.querySelector(
'img[src*="logo"], ' +
'img[src*="Logo"], ' +
'img[alt*="Gladius"], ' +
'img[title*="Gladius"]'
);
/*
* If the logo image is found, locate the outer element
* containing the logo.
*/
if (logoImage) {
var logoContainer =
logoImage.closest('.portal') ||
logoImage.closest('.mw-portlet') ||
logoImage.closest('div') ||
logoImage.parentElement;
/*
* Insert Contents immediately below the logo.
*/
if (logoContainer && logoContainer.parentNode) {
logoContainer.parentNode.insertBefore(
tocContainer,
logoContainer.nextSibling
);
} else {
sidebar.appendChild(tocContainer);
}
} else {
/*
* Fallback:
* Put Contents before the navigation links,
* without placing the TOC above the logo.
*/
var navigation =
* sidebar.querySelect*r('#p-navigation') ||
* sidebar.querySelector('.portal'* ||
sidebar.querySelector('.mw-portlet');
if (navigation) {
navigation.parentNode.insertBefore(
tocContainer,
navigation
);
} else {
sidebar.appendChild(tocContainer);
}
}
document.body.classList.add('gladius-toc-enabled');
}
/*
* Run after the HTML page is ready.
*/
function initialiseGladiusTOC() {
window.setTimeout(moveTOCBelowLogo, 100);
}
initialiseGladiusTOC();
/*
* Run when MediaWiki updates article content.
*/
mw.hook('wikipage.content').add(function () {
initialiseGladiusTOC();
});
});