MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More languages
More actions
Content deleted Content added
Set version dropdown to released CARE versions only (3.1 latest, 3.0 older) — mirrors docs.ohc.network; no 4.0 (via update-page on MediaWiki MCP Server) |
Bot: careui — version-switcher select no longer hardcodes the old teal; it is styled from the token layer in Common.css - Claude |
||
| (3 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
/** |
/** |
||
* OHC Network Wiki — shared site JS. |
|||
* Global Version Switcher — Docusaurus-style version dropdown for CARE docs. |
|||
* CARE version switcher (Docusaurus-style dropdown on doc pages, {{Doc header}}). |
|||
* |
|||
* Guarded so it never double-injects when the ohc-version-switcher gadget is |
|||
* Behaviour: |
|||
* enabled (see MediaWiki:Gadgets-definition). |
|||
* 1. On any page with the .docs-meta class (i.e. pages using {{Doc header}}), |
|||
* injects a version dropdown into the metadata strip. |
|||
* 2. Selecting an older version checks (via MW API) whether a frozen fork |
|||
* subpage exists (e.g. Concepts/Patient/3.0). If yes → navigate there. |
|||
* If no → stay on the canonical page (it spans that version implicitly). |
|||
* 3. Persists the chosen version in localStorage. On page load, if you have |
|||
* a non-latest version stored and a fork exists, auto-redirects. |
|||
* 4. On frozen forks, the dropdown shows the fork's version as selected and |
|||
* switching to "latest" returns you to the canonical page. |
|||
*/ |
*/ |
||
mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification' ] ).then( function () { |
mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification' ] ).then( function () { |
||
'use strict'; |
'use strict'; |
||
// Only activate on doc pages |
|||
var metaStrip = document.querySelector( '.docs-meta' ); |
var metaStrip = document.querySelector( '.docs-meta' ); |
||
if ( !metaStrip ) { |
if ( !metaStrip || metaStrip.querySelector( '.docs-version-switcher' ) ) { |
||
return; |
return; |
||
} |
} |
||
// --- Configuration (update KNOWN_VERSIONS on each CARE release) --- |
|||
var KNOWN_VERSIONS = [ '3.1', '3.0' ]; |
var KNOWN_VERSIONS = [ '3.1', '3.0' ]; |
||
var LATEST = KNOWN_VERSIONS[ 0 ]; |
var LATEST = KNOWN_VERSIONS[ 0 ]; |
||
var LS_KEY = 'ohcwiki-care-version'; |
var LS_KEY = 'ohcwiki-care-version'; |
||
// --- Detect context --- |
|||
var currentPage = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ); |
var currentPage = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ); |
||
var storedVersion = null; |
var storedVersion = null; |
||
| Line 33: | Line 22: | ||
if ( !storedVersion ) { storedVersion = LATEST; } |
if ( !storedVersion ) { storedVersion = LATEST; } |
||
// Are we on a frozen fork? (subpage like "Concepts/Patient/3.0") |
|||
var forkMatch = currentPage.match( /^(.+)\/(\d+\.\d+)$/ ); |
var forkMatch = currentPage.match( /^(.+)\/(\d+\.\d+)$/ ); |
||
var isOnFork = !!forkMatch; |
var isOnFork = !!forkMatch; |
||
| Line 39: | Line 27: | ||
var viewingVersion = isOnFork ? forkMatch[ 2 ] : LATEST; |
var viewingVersion = isOnFork ? forkMatch[ 2 ] : LATEST; |
||
// --- Build the dropdown --- |
|||
var container = document.createElement( 'span' ); |
var container = document.createElement( 'span' ); |
||
container.className = 'docs-meta__badge docs-version-switcher'; |
container.className = 'docs-meta__badge docs-version-switcher'; |
||
| Line 46: | Line 33: | ||
var select = document.createElement( 'select' ); |
var select = document.createElement( 'select' ); |
||
select.title = 'Switch documentation version'; |
select.title = 'Switch documentation version'; |
||
// Colour comes from the token layer, not a literal -- MediaWiki:Common.css |
|||
| ⚫ | |||
// already styles `.docs-version-switcher select`, so only layout is set here. |
|||
| ⚫ | |||
KNOWN_VERSIONS.forEach( function ( v ) { |
KNOWN_VERSIONS.forEach( function ( v ) { |
||
| Line 65: | Line 54: | ||
metaStrip.insertBefore( container, metaStrip.firstChild ); |
metaStrip.insertBefore( container, metaStrip.firstChild ); |
||
// --- Navigation --- |
|||
function navigateToVersion( version ) { |
function navigateToVersion( version ) { |
||
if ( version === LATEST ) { |
if ( version === LATEST ) { |
||
| Line 78: | Line 66: | ||
window.location.href = mw.util.getUrl( forkTitle ); |
window.location.href = mw.util.getUrl( forkTitle ); |
||
} else if ( isOnFork ) { |
} else if ( isOnFork ) { |
||
// No fork for chosen version but we're on a fork — go canonical |
|||
window.location.href = mw.util.getUrl( canonicalPage ); |
window.location.href = mw.util.getUrl( canonicalPage ); |
||
} else { |
} else { |
||
| Line 96: | Line 83: | ||
} |
} |
||
// --- Auto-redirect on load (respects stored version preference) --- |
|||
if ( storedVersion !== LATEST && !isOnFork && !window.location.search.includes( 'action=' ) ) { |
if ( storedVersion !== LATEST && !isOnFork && !window.location.search.includes( 'action=' ) ) { |
||
var forkTitle = canonicalPage + '/' + storedVersion; |
var forkTitle = canonicalPage + '/' + storedVersion; |
||
Latest revision as of 13:00, 12 August 2026
/**
* OHC Network Wiki — shared site JS.
* CARE version switcher (Docusaurus-style dropdown on doc pages, {{Doc header}}).
* Guarded so it never double-injects when the ohc-version-switcher gadget is
* enabled (see MediaWiki:Gadgets-definition).
*/
mw.loader.using( [ 'mediawiki.util', 'mediawiki.notification' ] ).then( function () {
'use strict';
var metaStrip = document.querySelector( '.docs-meta' );
if ( !metaStrip || metaStrip.querySelector( '.docs-version-switcher' ) ) {
return;
}
var KNOWN_VERSIONS = [ '3.1', '3.0' ];
var LATEST = KNOWN_VERSIONS[ 0 ];
var LS_KEY = 'ohcwiki-care-version';
var currentPage = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
var storedVersion = null;
try { storedVersion = localStorage.getItem( LS_KEY ); } catch ( e ) {}
if ( !storedVersion ) { storedVersion = LATEST; }
var forkMatch = currentPage.match( /^(.+)\/(\d+\.\d+)$/ );
var isOnFork = !!forkMatch;
var canonicalPage = isOnFork ? forkMatch[ 1 ] : currentPage;
var viewingVersion = isOnFork ? forkMatch[ 2 ] : LATEST;
var container = document.createElement( 'span' );
container.className = 'docs-meta__badge docs-version-switcher';
container.style.cssText = 'display:inline-flex;align-items:center;gap:0.3em;padding:0.15em 0.5em;';
var select = document.createElement( 'select' );
select.title = 'Switch documentation version';
// Colour comes from the token layer, not a literal -- MediaWiki:Common.css
// already styles `.docs-version-switcher select`, so only layout is set here.
select.style.cssText = 'font-size:1em;font-weight:700;font-family:inherit;';
KNOWN_VERSIONS.forEach( function ( v ) {
var opt = document.createElement( 'option' );
opt.value = v;
opt.textContent = v + ( v === LATEST ? ' (latest)' : '' );
if ( v === viewingVersion ) { opt.selected = true; }
select.appendChild( opt );
} );
select.addEventListener( 'change', function () {
var chosen = select.value;
try { localStorage.setItem( LS_KEY, chosen ); } catch ( e ) {}
navigateToVersion( chosen );
} );
container.appendChild( select );
metaStrip.insertBefore( container, metaStrip.firstChild );
function navigateToVersion( version ) {
if ( version === LATEST ) {
if ( isOnFork ) {
window.location.href = mw.util.getUrl( canonicalPage );
}
return;
}
var forkTitle = canonicalPage + '/' + version;
checkPageExists( forkTitle ).then( function ( exists ) {
if ( exists ) {
window.location.href = mw.util.getUrl( forkTitle );
} else if ( isOnFork ) {
window.location.href = mw.util.getUrl( canonicalPage );
} else {
mw.notify(
'This page is unchanged in CARE ' + version + ' — you\'re already seeing the right content.',
{ type: 'info', autoHide: true, autoHideSeconds: 4 }
);
}
} );
}
function checkPageExists( title ) {
return new mw.Api().get( { action: 'query', titles: title } ).then( function ( data ) {
var pages = data.query && data.query.pages;
return pages && !pages[ '-1' ];
} ).catch( function () { return false; } );
}
if ( storedVersion !== LATEST && !isOnFork && !window.location.search.includes( 'action=' ) ) {
var forkTitle = canonicalPage + '/' + storedVersion;
checkPageExists( forkTitle ).then( function ( exists ) {
if ( exists ) {
window.location.replace( mw.util.getUrl( forkTitle ) );
}
} );
}
} );