MediaWiki:Common.js
From Taylor Wiki
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 () {
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 );
}
} );
}() );