MediaWiki:Common.js: Difference between revisions

From GladiusRO Wiki
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 into the existing left sidebar.
  * 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 moveTOCToSidebar() {
     function moveTOCBelowLogo() {
         var toc = document.querySelector('#toc');
         var toc = document.querySelector('#toc');


         /*
         /*
         * Stop if:
         * Stop if the page has no TOC
        * 1. The current page has no table of contents.
         * or the TOC has already been moved.
         * 2. 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 wiki's existing left sidebar.
         * Find the existing left sidebar.
        * The first selector is normally used by Vector.
        * The other selectors provide support for custom skins.
         */
         */
         var sidebar =
         var sidebar =
Line 33: Line 30:


         /*
         /*
         * Create a container for the TOC.
         * Create the new TOC container.
         */
         */
         var tocContainer = document.createElement('div');
         var tocContainer = document.createElement('div');
Line 43: Line 40:


         /*
         /*
         * Move the existing TOC into the new container.
         * Move the original article TOC into the container.
         */
         */
         tocContainer.appendChild(toc);
         tocContainer.appendChild(toc);


         /*
         /*
         * Insert Contents at the top of the sidebar,
         * Try to find the GladiusRO sidebar logo.
        * before Main page, Recent changes and other links.
         */
         */
         sidebar.insertBefore(tocContainer, sidebar.firstChild);
         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 when the page first loads.
     * Run after the HTML page is ready.
     */
     */
     moveTOCToSidebar();
     function initialiseGladiusTOC() {
        window.setTimeout(moveTOCBelowLogo, 100);
    }
 
    initialiseGladiusTOC();


     /*
     /*
     * Run again when MediaWiki dynamically updates page content.
     * Run when MediaWiki updates article content.
     */
     */
     mw.hook('wikipage.conten*').add(function () {
     mw.hook('wikipage.content').add(function () {
         moveT*CToSidebar();
         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();
    });
});