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 Contents below the Tools section.
  *
* 1. Move the Table of Contents below Tools.
* 2. Hide technical links from logged-out players.
* 3. Keep technical links visible for logged-in administrators.
* ==========================================================
  */
  */


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


     var attempts = 0;
     var attempts = 0;
     var maximumAttempts = 30;
     var maximumAttempts = 40;


     function moveGladiusTOC() {
    /*
    * Move Contents below the Tools section.
    */
     function moveContentsBelowTools() {
        var tools = document.getElementById('p-tb');
         var toc = document.getElementById('toc');
         var toc = document.getElementById('toc');
         var tools = document.getElementById('p-tb');
         var tocContainer =
        var existingContainer =
             document.getElementById('gladius-sidebar-toc');
             document.getElementById('gladius-sidebar-toc');


         /*
         if (!tools || !tools.parentNode) {
        * Stop if Contents was already moved.
             return false;
        */
        if (existingContainer) {
             return true;
         }
         }


         /*
         /*
         * Wait until both the article TOC
         * If no Gladius container exists yet,
         * and the Tools sidebar panel exist.
         * create one and move the original TOC into it.
         */
         */
         if (!toc || !tools || !tools.parentNode) {
         if (!tocContainer && toc) {
             return false;
             tocContainer = document.createElement('div');
        }


        /*
            tocContainer.id = 'gladius-sidebar-toc';
        * Create a new container.
            tocContainer.className = 'portal mw-portlet';
        */
            tocContainer.setAttribute(
        var tocContainer = document.createElement('div');
                'role',
                'navigation'
            );
            tocContainer.setAttribute(
                'aria-label',
                'Page contents'
            );


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


         /*
         if (!tocContainer) {
        * Move the original TOC out of the article.
            return false;
        */
         }
         tocContainer.appendChild(toc);


         /*
         /*
         * Insert Contents immediately after Tools.
         * Always place the container immediately after Tools.
        *
        * This also corrects the position when an older script
        * accidentally leaves Contents inside the article.
         */
         */
         tools.insertAdjacentElement(
         tools.parentNode.insertBefore(
             'afterend',
             tocContainer,
             tocContainer
             tools.nextSibling
         );
         );


         return true;
         return true;
    }
    function retryMoveGladiusTOC() {
        attempts++;
        if (moveGladiusTOC()) {
            return;
        }
        if (attempts < maximumAttempts) {
            window.setTimeout(
                retryMoveGladiusTOC,
                200
            );
        }
     }
     }


     /*
     /*
     * Start after the complete page has loaded.
     * Hide technical sidebar items from players
    * who are not logged in.
     */
     */
     if (document.readyState === 'loading') {
     function hidePlayerTechnicalLinks() {
         document.addEventListener(
         var userName = mw.config.get('wgUserName');
            'DOMContentLoaded',
            retryMoveGladiusTOC
        );
    } else {
        retryMoveGladiusTOC();
    }


    /*
    * Try once more after all images and skin
    * components have loaded.
    */
    window.addEventListener(
        'load',
        retryMoveGladiusTOC
    );
}());
/*
* GladiusRO Wiki
* Hide technical sidebar links from visitors who are not logged in.
*/
(function (mw) {
    'use strict';
    function hidePublicSidebarLinks() {
         /*
         /*
         * wgUserName is null when the visitor is not logged in.
         * A non-null username means the visitor is logged in.
         * Logged-in administrators will continue seeing these links.
         * Administrators and logged-in users keep the links.
         */
         */
         if (mw.config.get('wgUserName') !== null) {
         if (userName !== null) {
             return;
             return;
         }
         }


        /*
         var elementsToHide = [
        * Hide Recent changes, Random page and MediaWiki Help.
        */
         var navigationItem* = [
             'n-recentchanges',
             'n-recentchanges',
             'n-randompage',
             'n-randompage',
             'n-help'
             'n-help',
            'p-tb'
         ];
         ];


         navigationItems.forEach(function (id) {
         elementsToHide.forEach(function (elementId) {
             var item = document.getElementById(id);
             var element = document.getElementById(elementId);


             if (item) {
             if (element) {
                 item.style.setProperty(
                 element.style.setProperty(
                     'display',
                     'display',
                     'none',
                     'none',
Line 134: Line 100:
             }
             }
         });
         });
    }
    /*
    * Run both functions in the correct order.
    *
    * Contents must be moved first because the script
    * uses the Tools panel as its placement reference.
    * Tools can then safely be hidden from players.
    */
    function initialiseGladiusSidebar() {
        attempts++;
        var moved = moveContentsBelowTools();
        if (moved) {
            hidePlayerTechnicalLinks();
            return;
        }


         /*
         /*
         * Hide the complete Tools section, including:
         * Retry while MediaWiki finishes creating the sidebar.
        * - What links here
        * - Related changes
        * - Special pages
        * - Printable version
        * - Permanent link
        * - Page information
         */
         */
        var toolsPanel = document.getElementById('p-tb');
         if (attempts < maximumAttempts) {
 
             window.setTimeout(
         if (toolsPanel) {
                 initialiseGladiusSidebar,
             toolsPanel.style.setProperty(
                 200
                 'display',
                'none',
                 'important'
             );
             );
         }
         }
Line 156: Line 131:


     /*
     /*
     * Run after the page structure is ready.
     * Start after the HTML is ready.
     */
     */
     if (document.readyState === 'loading') {
     if (document.readyState === 'loading') {
         document.addEventListener(
         document.addEventListener(
             'DOMContentLoaded',
             'DOMContentLoaded',
             hidePublicSidebarLinks
             initialiseGladiusSidebar
         );
         );
     } else {
     } else {
         hidePublicSidebarLinks();
         initialiseGladiusSidebar();
     }
     }


     /*
     /*
     * Run once more after all skin components load.
     * Run again after images and skin resources load.
     */
     */
     window.addEventListener(
     window.addEventListener(
  *    'load',
        'load',
         hidePublicSide*arLinks
         function () {
            attempts = 0;
            initialiseGladiusSidebar();
        }
     );
     );
    /*
    * Run again if MediaWiki updates article content.
    */
    if (mw.hook) {
        mw.hook('wikipage.content').add(function () {
            attempts = 0;
            window.setTimeout(
                initialiseGladiusSidebar,
                100
            );
        });
    }


}(mediaWiki));
}(mediaWiki));

Revision as of 03:06, 2 August 2026

/*
 * ==========================================================
 * GladiusRO Wiki
 *
 * 1. Move the Table of Contents below Tools.
 * 2. Hide technical links from logged-out players.
 * 3. Keep technical links visible for logged-in administrators.
 * ==========================================================
 */

(function (mw) {
    'use strict';

    var attempts = 0;
    var maximumAttempts = 40;

    /*
     * Move Contents below the Tools section.
     */
    function moveContentsBelowTools() {
        var tools = document.getElementById('p-tb');
        var toc = document.getElementById('toc');
        var tocContainer =
            document.getElementById('gladius-sidebar-toc');

        if (!tools || !tools.parentNode) {
            return false;
        }

        /*
         * If no Gladius container exists yet,
         * create one and move the original TOC into it.
         */
        if (!tocContainer && toc) {
            tocContainer = document.createElement('div');

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

            tocContainer.appendChild(toc);
        }

        if (!tocContainer) {
            return false;
        }

        /*
         * Always place the container immediately after Tools.
         *
         * This also corrects the position when an older script
         * accidentally leaves Contents inside the article.
         */
        tools.parentNode.insertBefore(
            tocContainer,
            tools.nextSibling
        );

        return true;
    }

    /*
     * Hide technical sidebar items from players
     * who are not logged in.
     */
    function hidePlayerTechnicalLinks() {
        var userName = mw.config.get('wgUserName');

        /*
         * A non-null username means the visitor is logged in.
         * Administrators and logged-in users keep the links.
         */
        if (userName !== null) {
            return;
        }

        var elementsToHide = [
            'n-recentchanges',
            'n-randompage',
            'n-help',
            'p-tb'
        ];

        elementsToHide.forEach(function (elementId) {
            var element = document.getElementById(elementId);

            if (element) {
                element.style.setProperty(
                    'display',
                    'none',
                    'important'
                );
            }
        });
    }

    /*
     * Run both functions in the correct order.
     *
     * Contents must be moved first because the script
     * uses the Tools panel as its placement reference.
     * Tools can then safely be hidden from players.
     */
    function initialiseGladiusSidebar() {
        attempts++;

        var moved = moveContentsBelowTools();

        if (moved) {
            hidePlayerTechnicalLinks();
            return;
        }

        /*
         * Retry while MediaWiki finishes creating the sidebar.
         */
        if (attempts < maximumAttempts) {
            window.setTimeout(
                initialiseGladiusSidebar,
                200
            );
        }
    }

    /*
     * Start after the HTML is ready.
     */
    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            initialiseGladiusSidebar
        );
    } else {
        initialiseGladiusSidebar();
    }

    /*
     * Run again after images and skin resources load.
     */
    window.addEventListener(
        'load',
        function () {
            attempts = 0;
            initialiseGladiusSidebar();
        }
    );

    /*
     * Run again if MediaWiki updates article content.
     */
    if (mw.hook) {
        mw.hook('wikipage.content').add(function () {
            attempts = 0;

            window.setTimeout(
                initialiseGladiusSidebar,
                100
            );
        });
    }

}(mediaWiki));