Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js

MediaWiki interface page
Revision as of 13:00, 4 August 2026 by Admin (talk | contribs) (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))

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.
/**
 * Global Version Switcher — Docusaurus-style version dropdown for CARE docs.
 *
 * Behaviour:
 * 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 () {
	'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 ) );
			}
		} );
	}
} );