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 below the sidebar logo.
  * Move the article Table of Contents to the bottom
* of the existing left sidebar, below Tools.
  */
  */


mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using(['mediawiki.util']).then(function () {


     function moveTOCBelowLogo() {
     function moveTOCToBottomOfSidebar() {
         var toc = document.querySelector('#toc');
         var toc = document.querySelector('#toc');


         /*
         /*
         * Stop if the page has no TOC
         * Stop if:
         * or the TOC has already been moved.
        * 1. This page has no table of contents.
         * 2. The table of contents was already moved.
         */
         */
         if (!toc || document.querySelector('#gladius-sidebar-toc')) {
         if (!toc || document.querySelector('#gladius-sidebar-toc')) {
Line 18: Line 20:


         /*
         /*
         * Find the existing left sidebar.
         * Find the existing MediaWiki left sidebar.
         */
         */
         var sidebar =
         var sidebar =
Line 30: Line 32:


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


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


         /*
         /*
         * Try to find the GladiusRO sidebar logo.
         * Put Contents at the very bottom of the sidebar.
        * This keeps the logo, navigation and Tools above it.
         */
         */
         var logoImage = sidebar.querySelector(
         sidebar.appendChild(tocContainer);
            '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 101: Line 56:


     /*
     /*
     * Run after the HTML page is ready.
     * Wait briefly so the skin can finish loading
    * the logo, navigation and Tools sections first.
     */
     */
     function initialiseGladiusTOC() {
     function initialiseGladiusTOC() {
         window.setTimeout(moveTOCBelowLogo, 100);
         window.setTimeout(moveTOCToBottomOfSidebar, 150);
     }
     }


    /*
    * Run when the page loads.
    */
     initialiseGladiusTOC();
     initialiseGladiusTOC();


     /*
     /*
     * Run when MediaWiki updates article content.
     * Run again if MediaWiki dynamically refreshes
    * the article content.
     */
     */
     mw.hook('wikipage.content').add(function () {
     mw.hook('wikipage.content').*dd(function () {
         initialiseGladiusTOC();
         initialis*GladiusTOC();
     });
     });
});
});

Revision as of 02:37, 2 August 2026

/*
 * GladiusRO Wiki
 * Move the article Table of Contents to the bottom
 * of the existing left sidebar, below Tools.
 */

mw.loader.using(['mediawiki.util']).then(function () {

    function moveTOCToBottomOfSidebar() {
        var toc = document.querySelector('#toc');

        /*
         * Stop if:
         * 1. This page has no table of contents.
         * 2. The table of contents was already moved.
         */
        if (!toc || document.querySelector('#gladius-sidebar-toc')) {
            return;
        }

        /*
         * Find the existing MediaWiki left sidebar.
         */
        var sidebar =
            document.querySelector('#mw-panel') ||
            document.querySelector('#mw-navigation') ||
            document.querySelector('.sidebar');

        if (!sidebar) {
            return;
        }

        /*
         * Create the sidebar 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 new container.
         */
        tocContainer.appendChild(toc);

        /*
         * Put Contents at the very bottom of the sidebar.
         * This keeps the logo, navigation and Tools above it.
         */
        sidebar.appendChild(tocContainer);

        document.body.classList.add('gladius-toc-enabled');
    }

    /*
     * Wait briefly so the skin can finish loading
     * the logo, navigation and Tools sections first.
     */
    function initialiseGladiusTOC() {
        window.setTimeout(moveTOCToBottomOfSidebar, 150);
    }

    /*
     * Run when the page loads.
     */
    initialiseGladiusTOC();

    /*
     * Run again if MediaWiki dynamically refreshes
     * the article content.
     */
    mw.hook('wikipage.content').*dd(function () {
        initialis*GladiusTOC();
    });
});