MediaWiki:Common.js

From Taylor Wiki
Revision as of 22:45, 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.
/* Multi-Tier Category Breadcrumbs (Home › Section › Collection/Topic › Page/File) */
( 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 || 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 ( isHome ) {
        return;
    }

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

    // Top-level hub categories
    var TOP_SECTIONS = [
        'Letters',
        'Instruments',
        'Photographs',
        'Documents',
        'Biographies',
        'Family'
    ];

    var topSection = null;
    var subSection = null;

    // 1. Find the top-level section if present
    for ( var i = 0; i < TOP_SECTIONS.length; i++ ) {
        var sec = TOP_SECTIONS[i];
        for ( var c = 0; c < categories.length; c++ ) {
            if ( categories[c].toLowerCase() === sec.toLowerCase() ) {
                topSection = sec;
                break;
            }
        }
        if ( topSection ) {
            break;
        }
    }

    // 2. Find any secondary category (like "Lamson") that isn't the top section
    for ( var k = 0; k < categories.length; k++ ) {
        var catName = categories[k].replace( /_/g, ' ' );
        if ( !topSection || catName.toLowerCase() !== topSection.toLowerCase() ) {
            subSection = catName;
            break;
        }
    }

    // Prevent duplicates if viewing the section or collection page itself
    if ( topSection && currentTitle.toLowerCase() === topSection.toLowerCase() ) {
        topSection = null;
    }
    if ( subSection && currentTitle.toLowerCase() === subSection.toLowerCase() ) {
        subSection = null;
    }

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

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

    // 2. Top-level Section (e.g. Letters)
    if ( topSection ) {
        $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        $nav.append( $( '<a>' ).addClass( 'crumb-link' ).attr( 'href', mw.util.getUrl( topSection ) ).text( topSection ) );
    }

    // 3. Sub-level Collection / Person (e.g. Lamson)
    if ( subSection ) {
        $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        $nav.append( $( '<a>' ).addClass( 'crumb-link' ).attr( 'href', mw.util.getUrl( subSection ) ).text( subSection ) );
    }

    // 4. Current File or Article 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 );
        }
    } );
}() );