MediaWiki:Common.js

From GladiusRO Wiki
Revision as of 02:29, 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 into the existing left sidebar.
 */

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

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

        /*
         * Stop if:
         * 1. The current page has no table of contents.
         * 2. The TOC has already been moved.
         */
        if (!toc || document.querySelector('#gladius-sidebar-toc')) {
            return;
        }

        /*
         * Find the wiki's existing left sidebar.
         * The first selector is normally used by Vector.
         * The other selectors provide support for custom skins.
         */
        var sidebar =
            document.querySelector('#mw-panel') ||
            document.querySelector('#mw-navigation') ||
            document.querySelector('.sidebar');

        if (!sidebar) {
            return;
        }

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

        /*
         * Insert Contents at the top of the sidebar,
         * before Main page, Recent changes and other links.
         */
        sidebar.insertBefore(tocContainer, sidebar.firstChild);

        document.body.classList.add('gladius-toc-enabled');
    }

    /*
     * Run when the page first loads.
     */
    moveTOCToSidebar();

    /*
     * Run again when MediaWiki dynamically updates page content.
     */
    mw.hook('wikipage.conten*').add(function () {
        moveT*CToSidebar();
    });
});