MediaWiki:Common.js

From Taylor Wiki
Revision as of 22:39, 19 September 2026 by Admin (talk | contribs)

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.
/* Category-Based Structural Breadcrumbs */
( function () {
    // 1. Wipe out any leftover click-trail history from the previous script
    try {
        sessionStorage.removeItem( 'gewame_wiki_crumbs' );
    } catch ( e ) {}

    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 || 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 || [];

    // Hub sections defined in your archive
    var KNOWN_SECTIONS = [
        'Letters',
        'Instruments',
        'Photographs',
        'Documents',
        'Biographies',
        'Family'
    ];

    var parentSection = null;

    // Check if the page belongs to any recognized 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;
        }
    }

    // Fallback: if not in KNOWN_SECTIONS, use the first category assigned
    if ( !parentSection && categories.length > 0 ) {
        parentSection = categories[0].replace( /_/g, ' ' );
    }

    // Avoid duplicate if viewing the hub page itself (e.g. 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. Main Page
    var $homeLink = $( '<a>' )
        .addClass( 'crumb-link' )
        .attr( 'href', homeUrl )
        .text( 'Main Page' );
    $nav.append( $homeLink );

    // 2. Category Hub (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 Title
    $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
    $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( currentTitle ) );

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