MediaWiki:Common.js: Difference between revisions

From Taylor Wiki
No edit summary
No edit summary
Line 1: Line 1:
/* Category-Based Structural Breadcrumbs */
/* Multi-Tier Category Breadcrumbs (Home › Section › Collection/Topic › Page/File) */
( function () {
( 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([
     var conf = mw.config.get([
         'wgAction',
         'wgAction',
Line 25: Line 20:
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );
     var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );


    // If on the Main Page, no breadcrumb needed
     if ( isHome ) {
     if ( isHome ) {
         return;
         return;
Line 33: Line 27:
     var categories = conf.wgCategories || [];
     var categories = conf.wgCategories || [];


     // Hub sections defined in your archive
     // Top-level hub categories
     var KNOWN_SECTIONS = [
     var TOP_SECTIONS = [
         'Letters',
         'Letters',
         'Instruments',
         'Instruments',
Line 43: Line 37:
     ];
     ];


     var parentSection = null;
     var topSection = null;
    var subSection = null;


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


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


     // Avoid duplicate if viewing the hub page itself (e.g. the "Letters" page)
     // Prevent duplicates if viewing the section or collection page itself
     if ( parentSection && currentTitle.toLowerCase() === parentSection.toLowerCase() ) {
    if ( topSection && currentTitle.toLowerCase() === topSection.toLowerCase() ) {
         parentSection = null;
        topSection = null;
    }
     if ( subSection && currentTitle.toLowerCase() === subSection.toLowerCase() ) {
         subSection = null;
     }
     }


Line 72: Line 74:
     var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb navigation"></div>' );
     var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb navigation"></div>' );


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


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


     // 3. Current Page Title
     // 4. Current File or Article Title
     $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
     $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
     $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( currentTitle ) );
     $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( currentTitle ) );

Revision as of 22:45, 19 September 2026

/* 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 );
        }
    } );
}() );