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 permanent sidebar configuration.
  * GladiusRO Wiki - Permanent Sidebar Configuration
*
* Functions:
* 1. Keep Contents permanently in the left sidebar.
* 2. Hide technical links from logged-out players.
* 3. Show technical links to logged-in administrators/editors.
* ==========================================================
  */
  */


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


    var observerRunning = false;
     function configureGladiusSidebar() {
    var refreshTimer = null;
         var $toc = $('#toc');
 
         var $tools = $('#p-tb');
    /*
         var $sidebar = $('#mw-panel');
    * Find the left sidebar used by the current wiki skin.
    */
     function getGladiusSidebar() {
         return document.getElementById('mw-panel') ||
            document.getElementById('mw-navigation') ||
            document.querySelector('.sidebar');
    }
 
    /*
    * Check whether the current visitor is logged in.
    *
    * Logged-out player:
    * wgUserName is null.
    *
    * Logged-in administrator/editor:
    * wgUserName contains the account name.
    */
    function isLoggedIn() {
        return mw.config.get('wgUserName') !== null;
    }
 
    /*
    * Create or retrieve the permanent TOC container.
    */
    function getTOCContainer() {*        var container =
          * document.getElementById('gladius-sidebar-toc');
 
         if (!container) {
            container = document.createElement('div');
 
            container.id = 'gladius-sidebar-toc';
            container.className = 'portal mw-portlet';
            container.setAttribute(
                'role',
                'navigation'
            );
            container.setAttribute(
                'aria-label',
                'Page contents'
            );
        }
 
        return container;
    }
 
    /*
    * Keep Contents permanently in the left sidebar.
    */
    function keepContentsInSidebar() {
         var sidebar = getGladiusSidebar();
        var toc = document.getElementById('toc');
 
        if (!sidebar) {
            return false;
        }


         /*
         /*
         * Do not create an empty Contents panel on pages
         * Move Contents into the left sidebar.
        * without a TOC.
         */
         */
         var existingContainer =
         if ($toc.length && $sidebar.length) {
            document.getElementById('gladius-sidebar-toc');
            var $container = $('#gladius-sidebar-toc');


        if (!toc && !existingContainer) {
            if (!$container.length) {
            return false;
                $container = $('<div>', {
        }
                    id: 'gladius-sidebar-toc',
 
                    class: 'portal mw-portlet'
        var container =
                 });
            existingContainer || getTOCContainer();
             }
 
        /*
        * Attach the Contents container directly to
        * the left sidebar.
        *
        * This makes placement independent of whether
        * Tools is visible or hidden.
        */
        if (container.parentNode !== sidebar) {
            sidebar.appendChild(container);
        }
 
        /*
        * Move the original article TOC into the
        * permanent sidebar container.
        */
        if (toc && toc.parentNode !== container) {
            container.appendChild(toc);
        }
 
        /*
        * Show the container when it contains a TOC.
        * Hide it on pages without a TOC.
        */
        if (container.querySelector('#toc')) {
            container.style.removeProperty('display');
        } else {
            container.style.setProperty(
                'display',
                'none',
                 'important'
            );
        }
 
        return true;
    }
 
    /*
    * Hide one element completely.
    */
    function hideElement(element) {
        if (!element) {
            return;
        }
 
        element.style.setProperty(
            'display',
            'none',
            'important'
        );
    }
 
    /*
    * Restore an element when an administrator/editor
    * is logged in.
    */
    function restoreElement(element) {
        if (!element) {
             return;
        }
 
        element.style.removeProperty('display');
    }


    /*
            $container.append($toc);
    * Hide or show technical navigation links.
    */
    function updatePlayerNavigation() {
        var loggedIn = isLoggedIn();


        /*
            /*
        * IDs commonly used by MediaWiki sidebar items.
            * Place Contents below Tools.
        */
            * If Tools cannot be found, place Contents
        var technicalElementIds * [
            * at the bottom of the sidebar.
            'n-recentchanges',
            */
            'n-randompage',
             if ($tools.length) {
            'n-help',
                 $container.insertAfter($tools);
            'n-help-mediawiki',
            'n-helppage',
            'p-tb'
        ];
 
        technicalElementIds.forEach(function (id) {
            var element = document.getElementById(id);
 
             if (loggedIn) {
                 restoreElement(element);
             } else {
             } else {
                 hideElement(element);
                 $container.appendTo($sidebar);
             }
             }
        });
        var sidebar = getGladiusSidebar();
        if (!sidebar) {
            return;
         }
         }


         /*
         /*
         * Fallback for older/custom skins where the
         * Hide technical links from logged-out players.
         * navigation items use different IDs.
         * Logged-in administrators can still see everything.
         */
         */
         var hiddenTexts = [
         if (mw.config.get('wgUserName') === null) {
            'recent changes',
             $('#p-tb').hide();
             'random page',
             $('#n-recentchanges').hide();
            'help about mediawiki',
            $('#n-randompage').hide();
            'what links here',
            $('#n-help').hide();
            'related changes',
            'special pages',
            'printable version',
            'permanent link',
            'page information'
        ];
 
        var links = sidebar.q*erySelectorAll('a');
 
        Arra*.prototype.forEach.call(
        *  links,
             function (lin*) {
                var text = (li*k.textContent || '')
                    .replace(/\s+/g, ' ')
                    .trim()
                    .toLowerCase();
 
                var href =
                    link.getAttribute('href') || '';


                 var hideThisLink =
            /*
                    hiddenTexts.indexOf(text) !== -1 ||
            * Fallback for this older MediaWiki skin.
                    href.indexOf(
            */
                        'mediawiki.org/wiki/Help'
            $('#mw-panel a').each(function () {
                    ) !== -1;
                 var $link = $(this);
                var text = $.trim($link.text()).toLowerCase();


                 if (!hideThisLink) {
                 var hiddenLinks = [
                     return;
                     'recent changes',
                 }
                    'random page',
                    'help about mediawiki',
                    'what links here',
                    'related changes',
                    'special pages',
                    'printable version',
                    'permanent link',
                    'page information'
                 ];


                 /*
                 if ($.inArray(text, hiddenLinks) !== -1) {
                * Hide or restore the complete list row,
                    var $row = $link.closest('li');
                * avoiding empty spaces in the sidebar.
                */
                var row =
                    link.closest('li') ||
                    link.parentElement;


                if (loggedIn) {
                    if ($row.length) {
                    restoreElement(row);
                         $row.hide();
                    restoreElement(link);
                     } else {
                } else {
                         $link.hide();
                    hideElement(row || link);
                }
            }
        );
 
        /*
        * Fallback: find a section with the visible
        * heading "Tools" if #p-tb is unavailable.
        */
        var sidebarElements =
            sidebar.querySelectorAll('div, h3, h4');
 
        Array.prototype.forEach.call(
            sidebarElements,
            function (element) {
                var headingText =
                    (element.textContent || '')
                        .replace(/\s+/g, ' ')
                        .trim()
                         .toLowerCase();
 
                /*
                * Avoid matching the whole sidebar, whose
                * text may include the word "Tools".
                */
                if (
                     headingText === 'tools' &&
                    !loggedIn
                ) {
                    var toolsSection =
                         element.closest(
                            '.portal, .mw-portlet'
                        );
 
                    if (toolsSection) {
                        hideElement(toolsSection);
                     }
                     }
                 }
                 }
             }
             });
        );
    }
 
    /*
    * Apply all sidebar corrections.
    *
    * Contents is moved first. Technical links are hidden
    * afterward, ensuring that hiding Tools does not prevent
    * the TOC from being moved.
    */
    function applyGladiusSidebar() {
        keepContentsInSidebar();
        updatePlayerNavigation();
    }
 
    /*
    * Watch for MediaWiki or skin changes.
    *
    * If MediaWiki places Contents inside the article again,
    * the observer moves Contents back to the sidebar.
    */
    function startGladiusObserve*() {
        if (
            obse*verRunning ||
            !document.body ||
            typeof MutationObserver === 'undefined'
        ) {
            return;
         }
         }
        observerRunning = true;
        var observer = new MutationObserver(function () {
            window.clearTimeout(refreshTimer);
            refreshTimer = window.setTimeout(
                applyGladiusSidebar,
                100
            );
        });
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
     }
     }


     /*
     /*
     * Initial setup.
     * Apply immediately and repeat after the older
    * MediaWiki skin has finished loading.
     */
     */
     function initialiseGladiusSidebar() {
     configureGladiusSidebar();
        applyGladiusSidebar();
        startGladiusObserver();
 
        /*
        * Additional checks for older skins and
        * delayed ResourceLoader components.
        */
        window.setTimeout(
            applyGladiusSidebar,
            250
        );
 
        window.setTimeout(
            applyGladiusSidebar,
            800
        );
 
        window.setTimeout(
            applyGladiusSidebar,
            1600
        );
    }
 
    /*
    * Start when the document is ready.
    */
    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            initialiseGladiusSidebar
        );
    } else {
        initialiseGladiusSidebar();
    }
 
    /*
    * Run again after images and skin assets finish loading.
    */
    window.addEventListener(
        'load',
        applyGladiusSidebar
    );
 
    /*
    * Run after MediaWiki dynamically updates article content.
    */
    if (mw.hook) {
        mw.ho*k('wikipage.content').add(function*() {
            window.setTimeout*
                applyGladiusSideb*r,
                100
          *);
        });
    }


}(mediaWiki)*;
    window.setTimeout(configureGladiusSidebar, 250);
    window.setTimeout(configureGladiusSidebar, 1000);
});

Revision as of 03:35, 2 August 2026

/*
 * GladiusRO permanent sidebar configuration.
 */

jQuery(function ($) {
    'use strict';

    function configureGladiusSidebar() {
        var $toc = $('#toc');
        var $tools = $('#p-tb');
        var $sidebar = $('#mw-panel');

        /*
         * Move Contents into the left sidebar.
         */
        if ($toc.length && $sidebar.length) {
            var $container = $('#gladius-sidebar-toc');

            if (!$container.length) {
                $container = $('<div>', {
                    id: 'gladius-sidebar-toc',
                    class: 'portal mw-portlet'
                });
            }

            $container.append($toc);

            /*
             * Place Contents below Tools.
             * If Tools cannot be found, place Contents
             * at the bottom of the sidebar.
             */
            if ($tools.length) {
                $container.insertAfter($tools);
            } else {
                $container.appendTo($sidebar);
            }
        }

        /*
         * Hide technical links from logged-out players.
         * Logged-in administrators can still see everything.
         */
        if (mw.config.get('wgUserName') === null) {
            $('#p-tb').hide();
            $('#n-recentchanges').hide();
            $('#n-randompage').hide();
            $('#n-help').hide();

            /*
             * Fallback for this older MediaWiki skin.
             */
            $('#mw-panel a').each(function () {
                var $link = $(this);
                var text = $.trim($link.text()).toLowerCase();

                var hiddenLinks = [
                    'recent changes',
                    'random page',
                    'help about mediawiki',
                    'what links here',
                    'related changes',
                    'special pages',
                    'printable version',
                    'permanent link',
                    'page information'
                ];

                if ($.inArray(text, hiddenLinks) !== -1) {
                    var $row = $link.closest('li');

                    if ($row.length) {
                        $row.hide();
                    } else {
                        $link.hide();
                    }
                }
            });
        }
    }

    /*
     * Apply immediately and repeat after the older
     * MediaWiki skin has finished loading.
     */
    configureGladiusSidebar();

    window.setTimeout(configureGladiusSidebar, 250);
    window.setTimeout(configureGladiusSidebar, 1000);
});