MediaWiki:Common.js: Difference between revisions

From Taylor Wiki
No edit summary
No edit summary
Line 1: Line 1:
/* Dynamic session breadcrumb trail with clean spacing */
/* Category-Based Structural Breadcrumbs */
( function () {
( function () {
     var conf = mw.config.get([
     var conf = mw.config.get([
Line 7: Line 7:
         'wgTitle',
         'wgTitle',
         'wgIsMainPage',
         'wgIsMainPage',
         'wgMainPageTitle'
         'wgMainPageTitle',
        'wgCategories'
     ]);
     ]);


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


     var mainPageTitle = conf.wgMainPageTitle || 'Main Page';
     var mainPageTitle = conf.wgMainPageTitle || 'Main Page';
Line 22: Line 20:
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );


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


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


     var history = [];
    // Main section hub pages you use in your archive
    try {
    // If a page has any of these categories, it links back to that hub page
         var raw = sessionStorage.getItem( STORAGE_KEY );
     var KNOWN_SECTIONS = [
         if ( raw ) {
        'Letters',
            history = JSON.parse( raw );
        'Instruments',
         }
         'Photographs',
     } catch ( e ) {
         'Documents',
        history = [];
        'Biographies',
    }
         'Family'
     ];


     // Always ensure 'Main Page' is the first crumb
     var parentSection = null;
    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)
     // 1. Check if the page belongs to any recognized archive section category
    var existingIndex = -1;
    for ( var i = 0; i < KNOWN_SECTIONS.length; i++ ) {
    for ( var i = 0; i < history.length; i++ ) {
        var sec = KNOWN_SECTIONS[i];
        if ( history[i].url === currentUrl ) {
        for ( var c = 0; c < categories.length; c++ ) {
            existingIndex = i;
            if ( categories[c].toLowerCase() === sec.toLowerCase() ) {
                parentSection = sec;
                break;
            }
        }
        if ( parentSection ) {
             break;
             break;
         }
         }
     }
     }


     if ( existingIndex !== -1 ) {
     // 2. If no known section matched but it has categories, use the first category
        history = history.slice( 0, existingIndex + 1 );
     if ( !parentSection && categories.length > 0 ) {
     } else {
        parentSection = categories[0].replace( /_/g, ' ' );
        history.push( { title: currentTitle, url: currentUrl } );
        if ( history.length > MAX_CRUMBS ) {
            history.splice( 1, 1 );
        }
     }
     }


     try {
     // Don't duplicate if the current page is itself the section page (e.g. on the "Letters" page)
        sessionStorage.setItem( STORAGE_KEY, JSON.stringify( history ) );
    if ( parentSection && currentTitle.toLowerCase() === parentSection.toLowerCase() ) {
     } catch ( e ) {}
        parentSection = null;
     }


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


     for ( var j = 0; j < history.length; j++ ) {
     // 1. Home / Main Page Link
        var isCurrent = ( j === history.length - 1 );
    var $homeLink = $( '<a>' )
        if ( j > 0 ) {
        .addClass( 'crumb-link' )
            $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        .attr( 'href', homeUrl )
        }
        .text( 'Main Page' );
    $nav.append( $homeLink );


        if ( isCurrent ) {
    // 2. Parent Section Link (e.g. Letters)
            $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( history[j].title ) );
    if ( parentSection ) {
         } else {
        $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
            var $link = $( '<a>' )
         var sectionUrl = mw.util.getUrl( parentSection );
                .addClass( 'crumb-link' )
        var $sectionLink = $( '<a>' )
                .attr( 'href', history[j].url )
            .addClass( 'crumb-link' )
                .text( history[j].title );
            .attr( 'href', sectionUrl )
            $nav.append( $link );
            .text( parentSection );
        }
        $nav.append( $sectionLink );
     }
     }


     // Inject directly above page title in Timeless
    // 3. Current Page (active)
    $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
    $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( currentTitle ) );
 
     // Inject above page title in Timeless skin
     $( function () {
     $( function () {
         var $heading = $( '#firstHeading' );
         var $heading = $( '#firstHeading' );

Revision as of 22:36, 19 September 2026

/* Category-Based Structural Breadcrumbs */
( function () {
    var conf = mw.config.get([
        'wgAction',
        'wgNamespaceNumber',
        'wgPageName',
        'wgTitle',
        'wgIsMainPage',
        'wgMainPageTitle',
        'wgCategories'
    ]);

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

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

    // If on the Main Page, no breadcrumb needed
    if ( isHome ) {
        return;
    }

    var currentTitle = conf.wgTitle.replace( /_/g, ' ' );
    var categories = conf.wgCategories || [];

    // Main section hub pages you use in your archive
    // If a page has any of these categories, it links back to that hub page
    var KNOWN_SECTIONS = [
        'Letters',
        'Instruments',
        'Photographs',
        'Documents',
        'Biographies',
        'Family'
    ];

    var parentSection = null;

    // 1. Check if the page belongs to any recognized archive section category
    for ( var i = 0; i < KNOWN_SECTIONS.length; i++ ) {
        var sec = KNOWN_SECTIONS[i];
        for ( var c = 0; c < categories.length; c++ ) {
            if ( categories[c].toLowerCase() === sec.toLowerCase() ) {
                parentSection = sec;
                break;
            }
        }
        if ( parentSection ) {
            break;
        }
    }

    // 2. If no known section matched but it has categories, use the first category
    if ( !parentSection && categories.length > 0 ) {
        parentSection = categories[0].replace( /_/g, ' ' );
    }

    // Don't duplicate if the current page is itself the section page (e.g. on the "Letters" page)
    if ( parentSection && currentTitle.toLowerCase() === parentSection.toLowerCase() ) {
        parentSection = null;
    }

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

    // 1. Home / Main Page Link
    var $homeLink = $( '<a>' )
        .addClass( 'crumb-link' )
        .attr( 'href', homeUrl )
        .text( 'Main Page' );
    $nav.append( $homeLink );

    // 2. Parent Section Link (e.g. Letters)
    if ( parentSection ) {
        $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        var sectionUrl = mw.util.getUrl( parentSection );
        var $sectionLink = $( '<a>' )
            .addClass( 'crumb-link' )
            .attr( 'href', sectionUrl )
            .text( parentSection );
        $nav.append( $sectionLink );
    }

    // 3. Current Page (active)
    $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
    $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( currentTitle ) );

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