MediaWiki:Common.js

From Doc Moates Wiki
Jump to navigation Jump to search

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.
/* Source of https://wiki.docmoates.com/index.php/MediaWiki:Common.js
   Kept in the repo at wiki/onwiki/MediaWiki-Common.js; see docs/runbooks/wiki-toc-collapsed.md. */

/* TOC collapsed by default (legacy Vector, MonoBook, Timeless): every page opens with the
   "Contents [show]" box closed until the reader clicks "show". Runs after core's
   mediawiki.toc so the click is toggled through core (keeps its state and aria-label right).
   MediaWiki:Common.css hides the list until this has run, so it never flashes open. */
( function () {
	function collapseTocs( $content ) {
		$content.find( '.toc' ).addBack( '.toc' ).each( function () {
			var box = this.querySelector( '.toctogglecheckbox' );
			if ( box && !box.checked ) {
				box.checked = true;
				box.dispatchEvent( new Event( 'change' ) );
			}
			this.classList.add( 'dm-toc-ready' );
		} );
	}
	if ( document.querySelector( '.toc' ) || mw.config.get( 'wgAction' ) !== 'view' ) {
		mw.loader.using( 'mediawiki.toc' ).then( function () {
			mw.hook( 'wikipage.content' ).add( collapseTocs );
		} );
	}
}() );

/* User pages: drop "User:" from the browser tab title too (the heading is done in
   MediaWiki:Common.css). Only the tab text changes; URL and page name stay "User:Name".
   See docs/runbooks/wiki-user-page-title.md. */
( function () {
	var prefix = ( mw.config.get( 'wgFormattedNamespaces' ) || {} )[ 2 ] + ':';
	if ( mw.config.get( 'wgNamespaceNumber' ) === 2 && document.title.indexOf( prefix ) === 0 ) {
		document.title = document.title.slice( prefix.length );
	}
}() );

/* Herzing guide: inline PDF viewer. Template:Herzing syllabus outputs
   <div class="hz-pdf" data-file="URL">; this adds a frame that shows the file with the
   browser's own PDF viewer. Only PDFs uploaded to this wiki are accepted.
   See docs/wiki/herzing-pages.md. */
( function () {
	var ok = /^(https?:)?(\/\/wiki\.docmoates\.com)?\/images\/[A-Za-z0-9\/._%-]+\.pdf$/;
	mw.hook( 'wikipage.content' ).add( function ( $content ) {
		$content.find( '.hz-pdf[data-file]' ).each( function () {
			var url = this.getAttribute( 'data-file' ) || '';
			if ( this.querySelector( 'iframe' ) || !ok.test( url ) ) {
				return;
			}
			var frame = document.createElement( 'iframe' );
			frame.className = 'hz-pdf-frame';
			frame.loading = 'lazy';
			frame.title = this.getAttribute( 'data-title' ) || 'PDF document';
			frame.src = url + '#view=FitH';
			this.appendChild( frame );
		} );
	} );
}() );