MediaWiki:Common.js: Difference between revisions

From Taylor Wiki
No edit summary
No edit summary
Line 1: Line 1:
/* Dynamic session breadcrumb trail with automatic Home anchor */
/* Dynamic session breadcrumb trail with clean spacing */
( function () {
( function () {
     var conf = mw.config.get([
     var conf = mw.config.get([
Line 22: Line 22:
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );


     // If currently on Home, reset history to just Home and don't render a trail
     // If currently on Main Page, reset history and don't render a bar
     if ( isHome ) {
     if ( isHome ) {
         try {
         try {
             sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Home', url: homeUrl } ] ) );
             sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Main Page', url: homeUrl } ] ) );
         } catch ( e ) {}
         } catch ( e ) {}
         return;
         return;
Line 43: Line 43:
     }
     }


     // Always ensure 'Home' is the very first crumb
     // Always ensure 'Main Page' is the first crumb
     if ( !history.length || history[0].title !== 'Home' ) {
     if ( !history.length || history[0].title !== 'Main Page' ) {
         history = [ { title: 'Home', url: homeUrl } ];
         history = [ { title: 'Main Page', url: homeUrl } ];
     }
     }


     // If current page is already in the trail, trim back to it (prevents loops)
     // If current page is already in trail, rewind back to it (prevents loops)
     var existingIndex = -1;
     var existingIndex = -1;
     for ( var i = 0; i < history.length; i++ ) {
     for ( var i = 0; i < history.length; i++ ) {
Line 61: Line 61:
     } else {
     } else {
         history.push( { title: currentTitle, url: currentUrl } );
         history.push( { title: currentTitle, url: currentUrl } );
        // Keep within limit, preserving 'Home' at index 0
         if ( history.length > MAX_CRUMBS ) {
         if ( history.length > MAX_CRUMBS ) {
             history.splice( 1, 1 );
             history.splice( 1, 1 );
Line 71: Line 70:
     } catch ( e ) {}
     } catch ( e ) {}


     // Build the breadcrumb bar
     // Build breadcrumb bar (no "Trail:" label)
     var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb Trail"></div>' );
     var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb navigation"></div>' );
    $nav.append( '<span class="crumb-label">Trail:</span> ' );


     for ( var j = 0; j < history.length; j++ ) {
     for ( var j = 0; j < history.length; j++ ) {
         var isCurrent = ( j === history.length - 1 );
         var isCurrent = ( j === history.length - 1 );
         if ( j > 0 ) {
         if ( j > 0 ) {
             $nav.append( '<span class="crumb-sep">›</span>' );
             $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
         }
         }


Line 92: Line 90:
     }
     }


     // Inject above page title in Timeless
     // Inject directly above page title in Timeless
     $( function () {
     $( function () {
         var $heading = $( '#firstHeading' );
         var $heading = $( '#firstHeading' );

Revision as of 22:30, 19 September 2026

/* Dynamic session breadcrumb trail with clean spacing */
( function () {
    var conf = mw.config.get([
        'wgAction',
        'wgNamespaceNumber',
        'wgPageName',
        'wgTitle',
        'wgIsMainPage',
        'wgMainPageTitle'
    ]);

    // Only run on standard page views (ignore edit forms, history, special pages)
    if ( conf.wgAction !== 'view' || conf.wgNamespaceNumber < 0 ) {
        return;
    }

    var STORAGE_KEY = 'gewame_wiki_crumbs';
    var MAX_CRUMBS = 5;

    var mainPageTitle = conf.wgMainPageTitle || 'Main Page';
    var homeUrl = mw.util.getUrl( mainPageTitle );
    var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );

    // If currently on Main Page, reset history and don't render a bar
    if ( isHome ) {
        try {
            sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Main Page', url: homeUrl } ] ) );
        } catch ( e ) {}
        return;
    }

    var currentTitle = conf.wgTitle.replace( /_/g, ' ' );
    var currentUrl = mw.util.getUrl( conf.wgPageName );

    var history = [];
    try {
        var raw = sessionStorage.getItem( STORAGE_KEY );
        if ( raw ) {
            history = JSON.parse( raw );
        }
    } catch ( e ) {
        history = [];
    }

    // Always ensure 'Main Page' is the first crumb
    if ( !history.length || history[0].title !== 'Main Page' ) {
        history = [ { title: 'Main Page', url: homeUrl } ];
    }

    // If current page is already in trail, rewind back to it (prevents loops)
    var existingIndex = -1;
    for ( var i = 0; i < history.length; i++ ) {
        if ( history[i].url === currentUrl ) {
            existingIndex = i;
            break;
        }
    }

    if ( existingIndex !== -1 ) {
        history = history.slice( 0, existingIndex + 1 );
    } else {
        history.push( { title: currentTitle, url: currentUrl } );
        if ( history.length > MAX_CRUMBS ) {
            history.splice( 1, 1 );
        }
    }

    try {
        sessionStorage.setItem( STORAGE_KEY, JSON.stringify( history ) );
    } catch ( e ) {}

    // Build breadcrumb bar (no "Trail:" label)
    var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb navigation"></div>' );

    for ( var j = 0; j < history.length; j++ ) {
        var isCurrent = ( j === history.length - 1 );
        if ( j > 0 ) {
            $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        }

        if ( isCurrent ) {
            $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( history[j].title ) );
        } else {
            var $link = $( '<a>' )
                .addClass( 'crumb-link' )
                .attr( 'href', history[j].url )
                .text( history[j].title );
            $nav.append( $link );
        }
    }

    // Inject directly above page title in Timeless
    $( function () {
        var $heading = $( '#firstHeading' );
        if ( $heading.length ) {
            $nav.insertBefore( $heading );
        } else {
            $( '#mw-content-text' ).prepend( $nav );
        }
    } );
}() );