MediaWiki:Timeless.js: Difference between revisions

From Exuvia Wiki
No edit summary
No edit summary
Line 1: Line 1:
/*
===============================================================================
EXUVIA WIKI SIDEBAR DROPDOWNS
===============================================================================
The sidebar contains:
    Locations
    Religions
    People
    World Building
JavaScript turns selected entries into expandable menus.
The top-level entries DO NOT need to be real wiki pages.
===============================================================================
*/
$( function () {
$( function () {


     /*
     /*
    ===========================================================================
    * ============================================================
    DROPDOWN MENUS
    * DROPDOWN MENU CONTENT
    ===========================================================================
    * ============================================================
 
    *
    The name on the left must exactly match the text in MediaWiki:Sidebar.
    * The names here must match the names in MediaWiki:Sidebar.
 
    *
    The names inside [ ] are the wiki pages that appear when the menu opens.
    * To add another dropdown, simply add another entry here.
 
    */
    ===========================================================================
    */


     var menus = {
     var menus = {
         'Locations': [
         'Locations': [
             'Continents',
             'Continents',
Line 55: Line 33:
             'War'
             'War'
         ]
         ]
     };
     };




     /*
     /*
    ===========================================================================
    * ============================================================
    FIND THE TIMeless SIDEBAR
    * FIND THE SIDEBAR
    ===========================================================================
    * ============================================================
    */
    */


     var sidebar = $( '#mw-site-navigation' );
     var sidebar = document.getElementById( 'mw-site-navigation' );


     if ( !sidebar.length ) {
     if ( !sidebar ) {
         return;
         return;
     }
     }
Line 73: Line 50:


     /*
     /*
    ===========================================================================
    * ============================================================
    FIND EVERY SIDEBAR LINK
    * FIND ALL LINKS IN THE SIDEBAR
    ===========================================================================
    * ============================================================
    */
    */


     sidebar.find( 'a' ).each( function () {
     var links = sidebar.getElementsByTagName( 'a' );
 
 
    /*
    * Convert the HTMLCollection into a normal array.
    */
 
    var sidebarLinks = Array.prototype.slice.call( links );
 
 
    /*
    * ============================================================
    * PROCESS EACH DROPDOWN
    * ============================================================
    */
 
    Object.keys( menus ).forEach( function ( menuName ) {
 
        var mainLink = null;


        var link = $( this );


         /*
         /*
        Get only the visible text.
        * Find the sidebar link whose text matches the menu name.
        */


         trim() removes extra spaces.
         sidebarLinks.some( function ( link ) {
        */


        var name = $.trim( link.clone().children().remove().end().text() );
            var text = link.textContent.trim();
 
            if ( text === menuName ) {
                mainLink = link;
                return true;
            }
 
            return false;
 
        });




         /*
         /*
        Check whether this sidebar item is one of our dropdowns.
        * If the link wasn't found, don't do anything.
        */
        */


         if ( !Object.prototype.hasOwnProperty.call( menus, name ) ) {
         if ( !mainLink ) {
             return;
             return;
         }
         }
Line 101: Line 104:


         /*
         /*
        =======================================================================
        * ========================================================
        FIND THE CONTAINER
        * FIND THE SIDEBAR ITEM
        =======================================================================
        * ========================================================
        */
        *
        * Walk upward until we find the list item containing
        * the link.
        */


         var parent = link.closest( 'li' );
         var parent = mainLink.closest( 'li' );
 
        if ( !parent ) {
            return;
        }




         /*
         /*
        =======================================================================
        * ========================================================
        CREATE THE DROPDOWN
        * CREATE THE DROPDOWN
        =======================================================================
        * ========================================================
        */
        */


         var dropdown = $( '<ul>' )
         var dropdown = document.createElement( 'ul' );
            .addClass( 'exuvia-dropdown-menu' );
 
        dropdown.className = 'exuvia-dropdown-menu';




         /*
         /*
        =======================================================================
        * ========================================================
        CREATE EACH LINK
        * CREATE THE CHILD LINKS
        =======================================================================
        * ========================================================
        */
        */
 
        menus[ menuName ].forEach( function ( pageName ) {
 
            var item = document.createElement( 'li' );
 
            var link = document.createElement( 'a' );
 
            link.href = mw.util.getUrl( pageName );
 
            link.textContent = pageName;
 
            item.appendChild( link );


        menus[ name ].forEach( function ( page ) {
            dropdown.appendChild( item );


            var childLink = $( '<a>' )
        });
                .attr( 'href', mw.util.getUrl( page ) )
                .text( page );


            var childItem = $( '<li>' )
                .append( childLink );


            dropdown.append( childItem );
        /*
        * ========================================================
        * ADD THE DROPDOWN TO THE SIDEBAR
        * ========================================================
        */


         } );
         parent.appendChild( dropdown );




         /*
         /*
        =======================================================================
        * ========================================================
        ADD DROPDOWN TO SIDEBAR
        * HIDE IT INITIALLY
        =======================================================================
        * ========================================================
        */
        */


         parent.append( dropdown );
         dropdown.style.display = 'none';




         /*
         /*
         Start closed.
        * ========================================================
         */
        * ADD THE ARROW
        * ========================================================
        */
 
         var arrow = document.createElement( 'span' );
 
         arrow.className = 'exuvia-dropdown-arrow';


         dropdown.hide();
         arrow.textContent = ' ▼';
 
        mainLink.appendChild( arrow );




         /*
         /*
        =======================================================================
        * ========================================================
        MAKE THE TITLE A DROPDOWN BUTTON
        * MAKE THE TITLE A BUTTON
        =======================================================================
        * ========================================================
        */
        *
        * Clicking the title does NOT open a page.
        */


         link.on( 'click', function ( event ) {
         mainLink.addEventListener( 'click', function (event) {


             event.preventDefault();
             event.preventDefault();


             dropdown.stop( true, true ).slideToggle( 150 );
             if ( dropdown.style.display === 'none' ) {


        });
                dropdown.style.display = 'block';
 
                arrow.textContent = ' ▲';
 
            } else {
 
                dropdown.style.display = 'none';


                arrow.textContent = ' ▼';


        /*
            }
        =======================================================================
        ADD ARROW
        =======================================================================
        */


         link.append(
         });
            $( '<span>' )
                .addClass( 'exuvia-dropdown-arrow' )
                .text( ' ▼' )
        );


     });
     });


});
});

Revision as of 01:53, 26 August 2026

$( function () {

    /*
     * ============================================================
     * DROPDOWN MENU CONTENT
     * ============================================================
     *
     * The names here must match the names in MediaWiki:Sidebar.
     *
     * To add another dropdown, simply add another entry here.
     */

    var menus = {
        'Locations': [
            'Continents',
            'Kingdoms',
            'Towns'
        ],

        'Religions': [
            'The Valor',
            'The Brumal'
        ],

        'People': [
            'High Lord Kip',
            'Queen Melena'
        ],

        'World Building': [
            'History',
            'Magic',
            'War'
        ]
    };


    /*
     * ============================================================
     * FIND THE SIDEBAR
     * ============================================================
     */

    var sidebar = document.getElementById( 'mw-site-navigation' );

    if ( !sidebar ) {
        return;
    }


    /*
     * ============================================================
     * FIND ALL LINKS IN THE SIDEBAR
     * ============================================================
     */

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


    /*
     * Convert the HTMLCollection into a normal array.
     */

    var sidebarLinks = Array.prototype.slice.call( links );


    /*
     * ============================================================
     * PROCESS EACH DROPDOWN
     * ============================================================
     */

    Object.keys( menus ).forEach( function ( menuName ) {

        var mainLink = null;


        /*
         * Find the sidebar link whose text matches the menu name.
         */

        sidebarLinks.some( function ( link ) {

            var text = link.textContent.trim();

            if ( text === menuName ) {
                mainLink = link;
                return true;
            }

            return false;

        });


        /*
         * If the link wasn't found, don't do anything.
         */

        if ( !mainLink ) {
            return;
        }


        /*
         * ========================================================
         * FIND THE SIDEBAR ITEM
         * ========================================================
         *
         * Walk upward until we find the list item containing
         * the link.
         */

        var parent = mainLink.closest( 'li' );

        if ( !parent ) {
            return;
        }


        /*
         * ========================================================
         * CREATE THE DROPDOWN
         * ========================================================
         */

        var dropdown = document.createElement( 'ul' );

        dropdown.className = 'exuvia-dropdown-menu';


        /*
         * ========================================================
         * CREATE THE CHILD LINKS
         * ========================================================
         */

        menus[ menuName ].forEach( function ( pageName ) {

            var item = document.createElement( 'li' );

            var link = document.createElement( 'a' );

            link.href = mw.util.getUrl( pageName );

            link.textContent = pageName;

            item.appendChild( link );

            dropdown.appendChild( item );

        });


        /*
         * ========================================================
         * ADD THE DROPDOWN TO THE SIDEBAR
         * ========================================================
         */

        parent.appendChild( dropdown );


        /*
         * ========================================================
         * HIDE IT INITIALLY
         * ========================================================
         */

        dropdown.style.display = 'none';


        /*
         * ========================================================
         * ADD THE ARROW
         * ========================================================
         */

        var arrow = document.createElement( 'span' );

        arrow.className = 'exuvia-dropdown-arrow';

        arrow.textContent = ' ▼';

        mainLink.appendChild( arrow );


        /*
         * ========================================================
         * MAKE THE TITLE A BUTTON
         * ========================================================
         *
         * Clicking the title does NOT open a page.
         */

        mainLink.addEventListener( 'click', function (event) {

            event.preventDefault();

            if ( dropdown.style.display === 'none' ) {

                dropdown.style.display = 'block';

                arrow.textContent = ' ▲';

            } else {

                dropdown.style.display = 'none';

                arrow.textContent = ' ▼';

            }

        });

    });

});