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) |
Version switcher moved to ohc-version-switcher gadget; keep Common.js as a shared anchor (via update-page on MediaWiki MCP Server) Tag: Replaced |
||
| Line 1: | Line 1: | ||
/** |
/** |
||
* OHC Network Wiki — shared site JavaScript. |
|||
* Global Version Switcher — Docusaurus-style version dropdown for CARE docs. |
|||
* |
* |
||
* Previously hosted the CARE version switcher; that logic now lives in the |
|||
* Behaviour: |
|||
* "ohc-version-switcher" gadget (see MediaWiki:Gadgets-definition) so it can |
|||
* 1. On any page with the .docs-meta class (i.e. pages using {{Doc header}}), |
|||
* be toggled per-user. Keep generic, site-wide behavior here instead. |
|||
* 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 () { |
|||
'use strict'; |
|||
// Only activate on doc pages |
|||
var metaStrip = document.querySelector( '.docs-meta' ); |
|||
if ( !metaStrip ) { |
|||
return; |
|||
} |
|||
// --- Configuration (update KNOWN_VERSIONS on each CARE release) --- |
|||
var KNOWN_VERSIONS = [ '3.1', '3.0' ]; |
|||
var LATEST = KNOWN_VERSIONS[ 0 ]; |
|||
var LS_KEY = 'ohcwiki-care-version'; |
|||
// --- Detect context --- |
|||
var currentPage = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ); |
|||
var storedVersion = null; |
|||
try { storedVersion = localStorage.getItem( LS_KEY ); } catch ( e ) {} |
|||
if ( !storedVersion ) { storedVersion = LATEST; } |
|||
// Are we on a frozen fork? (subpage like "Concepts/Patient/3.0") |
|||
var forkMatch = currentPage.match( /^(.+)\/(\d+\.\d+)$/ ); |
|||
var isOnFork = !!forkMatch; |
|||
var canonicalPage = isOnFork ? forkMatch[ 1 ] : currentPage; |
|||
var viewingVersion = isOnFork ? forkMatch[ 2 ] : LATEST; |
|||
// --- Build the dropdown --- |
|||
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'; |
|||
select.style.cssText = 'border:none;background:transparent;font-size:1em;font-weight:600;cursor:pointer;color: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 ); |
|||
// --- Navigation --- |
|||
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 ) { |
|||
// No fork for chosen version but we're on a fork — go canonical |
|||
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; } ); |
|||
} |
|||
// --- Auto-redirect on load (respects stored version preference) --- |
|||
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 ) ); |
|||
} |
|||
} ); |
|||
} |
|||
} ); |
|||
Revision as of 14:23, 11 August 2026
/**
* OHC Network Wiki — shared site JavaScript.
*
* Previously hosted the CARE version switcher; that logic now lives in the
* "ohc-version-switcher" gadget (see MediaWiki:Gadgets-definition) so it can
* be toggled per-user. Keep generic, site-wide behavior here instead.
*/