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 - Complete Interface Configuration
*
* 1. Keep Contents permanently in the left sidebar.
* 2. Place Contents below Tools.
* 3. Hide technical navigation from normal players.
* 4. Hide View source and View history from normal players.
* 5. Show administrative controls to administrators.
* ==========================================================
  */
  */


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


     function configureGladiusSidebar() {
    var refreshTimer = null;
         var $toc = $('#toc');
    var observerStarted = false;
         var $tools = $('#p-tb');
 
         var $sidebar = $('#mw-panel');
    /*
    * Check whether the current account is an administrator.
    * MediaWiki administrators belong to the sysop group.
    */
     function isAdministrator() {
         var userGroups = mw.config.get('wgUserGroups') || [];
 
        return userGroups.indexOf('sysop') !== -1;
    }
 
    /*
    * Find the existing left sidebar.
    */
    function getSidebar() {
        return document.getElementById('mw-panel') ||
            document.getElementById('mw-navigation') ||
            document.querySelector('.sidebar');
    }
 
    /*
    * Move Contents permanently into the left sidebar.
    */
    function moveContentsToSidebar() {
        var sidebar = getSidebar();
         var tools = document.getElementById('p-tb');
         var toc = document.getElementById('toc');
        var container =
            document.getElementById('gladius-sidebar-toc');
 
        if (!sidebar) {
            return;
        }
 
        /*
        * Do not create an empty container on pages
        * that do not have a Table of Contents.
        */
        if (!toc && !container) {
            return;
        }


         /*
         /*
         * Move Contents into the left sidebar.
         * Create the sidebar container when required.
         */
         */
         if ($toc.length && $sidebar.length) {
         if (!container) {
             var $container = $('#gladius-sidebar-toc');
             container = document.createElement('div');


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


             $container.append($toc);
             container.setAttribute(
                'role',
                'navigation'
            );


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


         /*
         /*
         * Hide technical links from logged-out players.
         * Move the original article Contents into
         * Logged-in administrators can still see everything.
         * the permanent sidebar container.
         */
         */
         if (mw.config.get('wgUserName') === null) {
         if (toc && toc.parentNode !== container) {
             $('#p-tb').hide();
             container.appendChild(toc);
            $('#n-recentchanges').hide();
        }
            $('#n-randompage').hide();
            $('#n-help').hide();


            /*
        /*
            * Fallback for this older MediaWiki skin.
        * Place Contents immediately below Tools.
            */
        *
            $('#mw-panel a').each(function () {
        * When Tools is hidden from normal players,
                 var $link = $(this);
        * Contents will appear below Main page.
                 var text = $.trim($link.text()).toLowerCase();
        */
        if (tools && tools.parentNode) {
            if (
                container.parentNode !== tools.parentNode ||
                 container.previousElementSibling !== tools
            ) {
                 tools.parentNode.insertBefore(
                    container,
                    tools.nextSibling
                );
            }
        } else if (container.parentNode !== sidebar) {
            sidebar.appendChild(container);
        }


                var hiddenLinks = [
        /*
                    'recent changes',
        * Hide the container on pages without Contents.
                    'random page',
        */
                    'help about mediawiki',
        if (container.querySelector('#toc')) {
                    'what links here',
            container.style.removeProperty('display');
                    'related changes',
        } else {
                    'special pages',
            container.style.setProperty(
                    'printable version',
                'display',
                    'permanent link',
                'none',
                    'page information'
                'important'
                ];
            );
        }
    }


                if ($.inArray(text, hiddenLinks) !== -1) {
    /*
                    var $row = $link.closest('li');
    * Hide an interface element.
    */
    function hideElement(element) {
        if (element) {
            element.style.setProperty(
                'display',
                'none',
                'important'
            );
        }
    }


                    if ($row.length) {
    /*
                        $row.hide();
    * Restore an interface element.
                    } else {
    */
                        $link.hide();
    function showElement(element) {
                    }
        if (element) {
                }
            element.style.removeProperty('display');
            });
         }
         }
     }
     }


     /*
     /*
     * Apply immediately and repeat after the older
     * Hide or show technical navigation.
    * MediaWiki skin has finished loading.
     */
     */
     configureGladiusSidebar();
     function updateTechnicalNavigation() {
        var administrator = isAdministrator();


    window.setTimeout(configureGladiusSidebar, 250);
        var restrictedElements = [
    window.setTimeout(configureGladiusSidebar, 1000);
            'n-recentchanges',
});
            'n-randompage',
            'n-help',
            'n-help-mediawiki',
            'n-helppage',
            'p-tb'
        ];


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


/*
            if (administrator) {
* GladiusRO Wiki
                showElement(element);
* Show View source and View history only to administrators.
            } else {
*/
                hideElement(element);
(function (mw) {
            }
    'use strict';
         });
 
    function updatePageTabs() {
         var userGroups = mw.config.get('wgUserGroups') || [];


         /*
         /*
         * MediaWiki administrators belong to the "sysop" group.
         * Fallback for older or custom skins using
        * different navigation element IDs.
         */
         */
         var isAdministrator =
         var sidebar = getSidebar();
             userGroups.indexOf('sysop') !== -1;
 
        if (!sidebar) {
            return;
        }
 
        var restrictedLinkNames = [
            'recent changes',
            'random page',
            'help about mediawiki',
            'what links here',
            'related changes',
            'upload file',
            'special pages',
            'printable version',
            'permanent link',
            'page information'
        ];
 
        var links = sidebar.querySelectorAll('a');
 
        Array.prototype.forEach.call(
             links,
            function (link) {
                var linkText = (link.textContent || '')
                    .replace(/\s+/g, ' ')
                    .trim()
                    .toLowerCase();
 
                if (
                    restrictedLinkNames.indexOf(
                        linkText
                    ) === -1
                ) {
                    return;
                }
 
                var row =
                    link.closest('li') ||
                    link.parentElement;
 
                if (administrator) {
                    showElement(row);
                    showElement(link);
                } else {
                    hideElement(row || link);
                }
            }
        );
    }
 
    /*
    * Hide View source and View history from
    * everyone except administrators.
    */
    function updatePageTabs() {
        var administrator = isAdministrator();


        /*
        * Page tabs used by MediaWiki and compatible skins.
        */
         var restrictedTabs = [
         var restrictedTabs = [
             'ca-viewsource',
             'ca-viewsource',
Line 115: Line 235:
         ];
         ];


    *  restrictedTabs.forEach(function *tabId) {
        restrictedTabs.forEach(function (tabId) {
             var tab = doc*ment.getElementById(tabId);
             var tab = document.getElementById(tabId);


    *      if (!tab) {
            if (administrator) {
                 *eturn;
                showElement(tab);
            } else {
                 hideElement(tab);
             }
             }
        });
    }


            *f (isAdministrator) {
    /*
            *   tab.style.removeProperty('displ*y');
    * Apply all GladiusRO interface settings.
            } else {
    *
        *      tab.style.setProperty(
    * Contents must be moved before Tools is hidden.
    *              'display',
    */
         *          'none',
    function applyGladiusInterface() {
              *    'important'
        moveContentsToSidebar();
                );*            }
        updateTechnicalNavigation();
        updatePageTabs();
    }
 
    /*
    * Automatically correct Contents if MediaWiki
    * recreates it inside the article.
    */
    function startInterfaceObserver() {
        if (
            observerStarted ||
            !document.body ||
            typeof MutationObserver === 'undefined'
        ) {
            return;
        }
 
        observerStarted = true;
 
        var observer = new MutationObserver(function () {
            window.clearTimeout(refreshTimer);
 
            refreshTimer = window.setTimeout(
                applyGladiusInterface,
                100
            );
         });
 
        observer.observe(document.body, {
            childList: true,
            subtree: true
         });
         });
     }
     }


* if (document.readyState === 'loa*ing') {
    /*
         document.addEventL*stener(
    * Initial startup.
             'DOMContentLoa*ed',
    */
             updatePageTabs
    function initialiseGladiusInterface() {
  *    );
        applyGladiusInterface();
        startInterfaceObserver();
 
        /*
        * Additional checks for delayed skin components.
        */
        window.setTimeout(
            applyGladiusInterface,
            250
        );
 
        window.setTimeout(
            applyGladiusInterface,
            800
        );
 
        window.setTimeout(
            applyGladiusInterface,
            1600
        );
    }
 
    if (document.readyState === 'loading') {
         document.addEventListener(
             'DOMContentLoaded',
             initialiseGladiusInterface
        );
     } else {
     } else {
         updat*PageTabs();
         initialiseGladiusInterface();
     }
     }


     window.addE*entListener(
     window.addEventListener(
         'load',
         'load',
    *  updatePageTabs
        applyGladiusInterface
     );
     );


     if (m*.hook) {
    /*
         mw.hook('wikipage*content').add(function () {
    * Reapply after MediaWiki updates article content.
      *    window.setTimeout(
    */
          *    updatePageTabs,
     if (mw.hook) {
            *  50
         mw.hook('wikipage.content').add(function () {
            window.setTimeout(
                applyGladiusInterface,
                100
             );
             );
         });
         });
  * }
    }


}(mediaWiki));
}(mediaWiki, jQuery));

Latest revision as of 06:42, 2 August 2026

/*
 * ==========================================================
 * GladiusRO Wiki - Complete Interface Configuration
 *
 * 1. Keep Contents permanently in the left sidebar.
 * 2. Place Contents below Tools.
 * 3. Hide technical navigation from normal players.
 * 4. Hide View source and View history from normal players.
 * 5. Show administrative controls to administrators.
 * ==========================================================
 */

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

    var refreshTimer = null;
    var observerStarted = false;

    /*
     * Check whether the current account is an administrator.
     * MediaWiki administrators belong to the sysop group.
     */
    function isAdministrator() {
        var userGroups = mw.config.get('wgUserGroups') || [];

        return userGroups.indexOf('sysop') !== -1;
    }

    /*
     * Find the existing left sidebar.
     */
    function getSidebar() {
        return document.getElementById('mw-panel') ||
            document.getElementById('mw-navigation') ||
            document.querySelector('.sidebar');
    }

    /*
     * Move Contents permanently into the left sidebar.
     */
    function moveContentsToSidebar() {
        var sidebar = getSidebar();
        var tools = document.getElementById('p-tb');
        var toc = document.getElementById('toc');
        var container =
            document.getElementById('gladius-sidebar-toc');

        if (!sidebar) {
            return;
        }

        /*
         * Do not create an empty container on pages
         * that do not have a Table of Contents.
         */
        if (!toc && !container) {
            return;
        }

        /*
         * Create the sidebar container when required.
         */
        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'
            );
        }

        /*
         * Move the original article Contents into
         * the permanent sidebar container.
         */
        if (toc && toc.parentNode !== container) {
            container.appendChild(toc);
        }

        /*
         * Place Contents immediately below Tools.
         *
         * When Tools is hidden from normal players,
         * Contents will appear below Main page.
         */
        if (tools && tools.parentNode) {
            if (
                container.parentNode !== tools.parentNode ||
                container.previousElementSibling !== tools
            ) {
                tools.parentNode.insertBefore(
                    container,
                    tools.nextSibling
                );
            }
        } else if (container.parentNode !== sidebar) {
            sidebar.appendChild(container);
        }

        /*
         * Hide the container on pages without Contents.
         */
        if (container.querySelector('#toc')) {
            container.style.removeProperty('display');
        } else {
            container.style.setProperty(
                'display',
                'none',
                'important'
            );
        }
    }

    /*
     * Hide an interface element.
     */
    function hideElement(element) {
        if (element) {
            element.style.setProperty(
                'display',
                'none',
                'important'
            );
        }
    }

    /*
     * Restore an interface element.
     */
    function showElement(element) {
        if (element) {
            element.style.removeProperty('display');
        }
    }

    /*
     * Hide or show technical navigation.
     */
    function updateTechnicalNavigation() {
        var administrator = isAdministrator();

        var restrictedElements = [
            'n-recentchanges',
            'n-randompage',
            'n-help',
            'n-help-mediawiki',
            'n-helppage',
            'p-tb'
        ];

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

            if (administrator) {
                showElement(element);
            } else {
                hideElement(element);
            }
        });

        /*
         * Fallback for older or custom skins using
         * different navigation element IDs.
         */
        var sidebar = getSidebar();

        if (!sidebar) {
            return;
        }

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

        var links = sidebar.querySelectorAll('a');

        Array.prototype.forEach.call(
            links,
            function (link) {
                var linkText = (link.textContent || '')
                    .replace(/\s+/g, ' ')
                    .trim()
                    .toLowerCase();

                if (
                    restrictedLinkNames.indexOf(
                        linkText
                    ) === -1
                ) {
                    return;
                }

                var row =
                    link.closest('li') ||
                    link.parentElement;

                if (administrator) {
                    showElement(row);
                    showElement(link);
                } else {
                    hideElement(row || link);
                }
            }
        );
    }

    /*
     * Hide View source and View history from
     * everyone except administrators.
     */
    function updatePageTabs() {
        var administrator = isAdministrator();

        var restrictedTabs = [
            'ca-viewsource',
            'ca-history'
        ];

        restrictedTabs.forEach(function (tabId) {
            var tab = document.getElementById(tabId);

            if (administrator) {
                showElement(tab);
            } else {
                hideElement(tab);
            }
        });
    }

    /*
     * Apply all GladiusRO interface settings.
     *
     * Contents must be moved before Tools is hidden.
     */
    function applyGladiusInterface() {
        moveContentsToSidebar();
        updateTechnicalNavigation();
        updatePageTabs();
    }

    /*
     * Automatically correct Contents if MediaWiki
     * recreates it inside the article.
     */
    function startInterfaceObserver() {
        if (
            observerStarted ||
            !document.body ||
            typeof MutationObserver === 'undefined'
        ) {
            return;
        }

        observerStarted = true;

        var observer = new MutationObserver(function () {
            window.clearTimeout(refreshTimer);

            refreshTimer = window.setTimeout(
                applyGladiusInterface,
                100
            );
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    /*
     * Initial startup.
     */
    function initialiseGladiusInterface() {
        applyGladiusInterface();
        startInterfaceObserver();

        /*
         * Additional checks for delayed skin components.
         */
        window.setTimeout(
            applyGladiusInterface,
            250
        );

        window.setTimeout(
            applyGladiusInterface,
            800
        );

        window.setTimeout(
            applyGladiusInterface,
            1600
        );
    }

    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            initialiseGladiusInterface
        );
    } else {
        initialiseGladiusInterface();
    }

    window.addEventListener(
        'load',
        applyGladiusInterface
    );

    /*
     * Reapply after MediaWiki updates article content.
     */
    if (mw.hook) {
        mw.hook('wikipage.content').add(function () {
            window.setTimeout(
                applyGladiusInterface,
                100
            );
        });
    }

}(mediaWiki, jQuery));