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 Tools section.
  * Move Contents below the Tools section.
  */
  */


(function ($, mw) {
(function () {
     'use strict';
     'use strict';


     function moveTOCBelowTools() {
    var attempts = 0;
    var maximumAttempts = 30;
 
     function moveGladiusTOC() {
         var toc = document.getElementById('toc');
         var toc = document.getElementById('toc');
         var tools = document.getElementById('p-tb');
         var tools = document.getElementById('p-tb');
        var existingContainer =
            document.getElementById('gladius-sidebar-toc');


         /*
         /*
         * Stop when:
         * Stop if Contents was already moved.
        * - The page does not have a TOC.
        * - The Tools section cannot be found.
        * - The TOC was already moved.
         */
         */
         if (!toc || !tools) {
         if (existingContainer) {
             return;
             return true;
         }
         }


         if (document.getElementById('gladius-sidebar-toc')) {
        /*
             return;
        * Wait until both the article TOC
        * and the Tools sidebar panel exist.
        */
         if (!toc || !tools || !tools.parentNode) {
             return false;
         }
         }


         /*
         /*
         * Create a new sidebar section for Contents.
         * Create a new container.
         */
         */
         var tocContainer = document.createElement('div');
         var tocContainer = document.createElement('div');
Line 33: Line 39:
         tocContainer.className = 'portal mw-portlet';
         tocContainer.className = 'portal mw-portlet';
         tocContainer.setAttribute('role', 'navigation');
         tocContainer.setAttribute('role', 'navigation');
         tocContainer.setAttribute('aria-label', 'Page contents');
         tocContainer.setAttribute(
            'aria-label',
            'Page contents'
        );


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


         /*
         /*
         * Insert Contents immediately below the Tools section.
         * Insert Contents immediately after Tools.
         */
         */
         if (tools.parentNode) {
        tools.insertAdjacentElement(
             tools.parentNode.insertBefore(
            'afterend',
                 tocContainer,
            tocContainer
                 tools.nextSibling
        );
 
        return true;
    }
 
    function retryMoveGladiusTOC() {
        attempts++;
 
        if (moveGladiusTOC()) {
            return;
        }
 
         if (attempts < maximumAttempts) {
             window.setTimeout(
                 retryMoveGladiusTOC,
                 200
             );
             );
         }
         }
Line 52: Line 76:


     /*
     /*
     * Wait until the MediaWiki page is ready.
     * Start after the complete page has loaded.
     */
     */
     $(function () {
     if (document.readyState === 'loading') {
         moveTOCBelowTools();
         document.addEventListener(
     });
            'DOMContentLoaded',
            retryMoveGladiusTOC
        );
     } else {
        retryMoveGladiusTOC();
    }


     /*
     /*
     * Also support dynamically loaded article content.
     * Try once more after all images and skin
    * components have loaded.
     */
     */
     mw.hook('wikipage.content').*dd(function () {
     window.addEventListener(
         window.se*Timeout(moveTOCBelowTools, 50);
        'load',
  * });
         retryMoveGladiusTOC
 
    );
}(jQuery, mediaWiki));
}());

Revision as of 02:46, 2 August 2026

/*
 * GladiusRO Wiki
 * Move Contents below the Tools section.
 */

(function () {
    'use strict';

    var attempts = 0;
    var maximumAttempts = 30;

    function moveGladiusTOC() {
        var toc = document.getElementById('toc');
        var tools = document.getElementById('p-tb');
        var existingContainer =
            document.getElementById('gladius-sidebar-toc');

        /*
         * Stop if Contents was already moved.
         */
        if (existingContainer) {
            return true;
        }

        /*
         * Wait until both the article TOC
         * and the Tools sidebar panel exist.
         */
        if (!toc || !tools || !tools.parentNode) {
            return false;
        }

        /*
         * Create a new container.
         */
        var tocContainer = document.createElement('div');

        tocContainer.id = 'gladius-sidebar-toc';
        tocContainer.className = 'portal mw-portlet';
        tocContainer.setAttribute('role', 'navigation');
        tocContainer.setAttribute(
            'aria-label',
            'Page contents'
        );

        /*
         * Move the original TOC out of the article.
         */
        tocContainer.appendChild(toc);

        /*
         * Insert Contents immediately after Tools.
         */
        tools.insertAdjacentElement(
            'afterend',
            tocContainer
        );

        return true;
    }

    function retryMoveGladiusTOC() {
        attempts++;

        if (moveGladiusTOC()) {
            return;
        }

        if (attempts < maximumAttempts) {
            window.setTimeout(
                retryMoveGladiusTOC,
                200
            );
        }
    }

    /*
     * Start after the complete page has loaded.
     */
    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            retryMoveGladiusTOC
        );
    } else {
        retryMoveGladiusTOC();
    }

    /*
     * Try once more after all images and skin
     * components have loaded.
     */
    window.addEventListener(
        'load',
        retryMoveGladiusTOC
    );
}());