MediaWiki:Common.js: Difference between revisions
From Taylor Wiki
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* Category-Based Structural Breadcrumbs */ | /* Category-Based Structural Breadcrumbs */ | ||
( 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 12: | Line 17: | ||
// Only run on standard page views (skip 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 || conf.wgAction !== 'view' || conf.wgNamespaceNumber < 0 ) { | ||
return; | return; | ||
} | } | ||
| Line 28: | Line 33: | ||
var categories = conf.wgCategories || []; | var categories = conf.wgCategories || []; | ||
// | // Hub sections defined in your archive | ||
var KNOWN_SECTIONS = [ | var KNOWN_SECTIONS = [ | ||
'Letters', | 'Letters', | ||
| Line 41: | Line 45: | ||
var parentSection = null; | var parentSection = null; | ||
// | // Check if the page belongs to any recognized section category | ||
for ( var i = 0; i < KNOWN_SECTIONS.length; i++ ) { | for ( var i = 0; i < KNOWN_SECTIONS.length; i++ ) { | ||
var sec = KNOWN_SECTIONS[i]; | var sec = KNOWN_SECTIONS[i]; | ||
| Line 55: | Line 59: | ||
} | } | ||
// | // Fallback: if not in KNOWN_SECTIONS, use the first category assigned | ||
if ( !parentSection && categories.length > 0 ) { | if ( !parentSection && categories.length > 0 ) { | ||
parentSection = categories[0].replace( /_/g, ' ' ); | parentSection = categories[0].replace( /_/g, ' ' ); | ||
} | } | ||
// | // Avoid duplicate if viewing the hub page itself (e.g. the "Letters" page) | ||
if ( parentSection && currentTitle.toLowerCase() === parentSection.toLowerCase() ) { | if ( parentSection && currentTitle.toLowerCase() === parentSection.toLowerCase() ) { | ||
parentSection = null; | parentSection = null; | ||
| Line 68: | Line 72: | ||
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. | // 1. Main Page | ||
var $homeLink = $( '<a>' ) | var $homeLink = $( '<a>' ) | ||
.addClass( 'crumb-link' ) | .addClass( 'crumb-link' ) | ||
| Line 75: | Line 79: | ||
$nav.append( $homeLink ); | $nav.append( $homeLink ); | ||
// 2. | // 2. Category Hub (e.g. Letters) | ||
if ( parentSection ) { | if ( parentSection ) { | ||
$nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) ); | $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) ); | ||
| Line 86: | Line 90: | ||
} | } | ||
// 3. Current Page | // 3. Current Page 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 ) ); | ||
// Inject above | // Inject above title in Timeless skin | ||
$( function () { | $( function () { | ||
var $heading = $( '#firstHeading' ); | var $heading = $( '#firstHeading' ); | ||
Revision as of 22:39, 19 September 2026
/* 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 );
}
} );
}() );