MediaWiki:Common.js

From GladiusRO Wiki
Revision as of 02:34, 2 August 2026 by Ikhwan (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
 * GladiusRO Wiki
 * Move the article Table of Contents below the sidebar logo.
 */

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

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

        /*
         * Stop if the page has no TOC
         * or the TOC has already been moved.
         */
        if (!toc || document.querySelector('#gladius-sidebar-toc')) {
            return;
        }

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

        if (!sidebar) {
            return;
        }

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

        /*
         * Try to find the GladiusRO sidebar logo.
         */
        var logoImage = sidebar.querySelector(
            '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');
    }

    /*
     * Run after the HTML page is ready.
     */
    function initialiseGladiusTOC() {
        window.setTimeout(moveTOCBelowLogo, 100);
    }

    initialiseGladiusTOC();

    /*
     * Run when MediaWiki updates article content.
     */
    mw.hook('wikipage.content').add(function () {
        initialiseGladiusTOC();
    });
});