MediaWiki:Vector-2022.js

From LinnDocs
Revision as of 10:51, 27 October 2026 by Yoda (talk | contribs)
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.
/* All JavaScript here will be loaded for users of the Vector skin */

/* Second logo in the header, linking to www.linn.co.uk */
mw.loader.using( 'mediawiki.util' ).then( function () {
	var start = document.querySelector( '.vector-header-start' );
	if ( !start || document.getElementById( 'linn-site-logo' ) ) {
		return;
	}
	var link = document.createElement( 'a' );
	link.id = 'linn-site-logo';
	link.href = 'https://www.linn.co.uk/';
	link.title = 'Linn – www.linn.co.uk';
	link.target = '_blank';
	link.rel = 'noopener';

	var img = document.createElement( 'img' );
	img.src = mw.config.get( 'wgScriptPath' ) + '/images/Linn_Logo.png';
	img.alt = 'Linn';

	link.appendChild( img );
	start.appendChild( link );
} );

/* In-page table of contents for Vector 2022 (in addition to the sidebar) */
$( function () {
	var content = document.querySelector( '#mw-content-text .mw-parser-output' );
	
// Pages can opt out of the in-text TOC with <span class="no-inline-toc"></span>
	if ( content.querySelector( '.no-inline-toc' ) ) {
		return;
	}
	// MediaWiki leaves this marker where the TOC belongs; none means no TOC
	// (e.g. __NOTOC__, or fewer than four headings without __FORCETOC__)
	var marker = content.querySelector( 'meta[property="mw:PageProp/toc"]' );
	if ( !marker ) {
		return;
	}
	var headings = content.querySelectorAll( 'h2[id], h3[id], h4[id], h5[id], h6[id]' );
	if ( !headings.length ) {
		return;
	}

	var levels = Array.prototype.map.call( headings, function ( h ) {
		return Number( h.tagName.charAt( 1 ) );
	} );
	var base = Math.min.apply( null, levels );
	var root = document.createElement( 'ul' );
	var stack = [ root ];
	var nums = [];

	Array.prototype.forEach.call( headings, function ( h, i ) {
		var depth = levels[ i ] - base;
		while ( stack.length - 1 > depth ) {
			stack.pop();
		}
		while ( stack.length - 1 < depth ) {
			var parent = stack[ stack.length - 1 ];
			var lastLi = parent.lastElementChild || parent.appendChild( document.createElement( 'li' ) );
			stack.push( lastLi.appendChild( document.createElement( 'ul' ) ) );
		}
		nums.length = depth + 1;
		nums[ depth ] = ( nums[ depth ] || 0 ) + 1;

		var li = document.createElement( 'li' );
		li.className = 'toclevel-' + ( depth + 1 );
		var a = document.createElement( 'a' );
		a.href = '#' + h.id;
		var num = document.createElement( 'span' );
		num.className = 'tocnumber';
		num.textContent = nums.map( function ( n ) { return n || 1; } ).join( '.' );
		var text = document.createElement( 'span' );
		text.className = 'toctext';
		text.textContent = h.textContent.trim();
		a.appendChild( num );
		a.appendChild( document.createTextNode( ' ' ) );
		a.appendChild( text );
		li.appendChild( a );
		stack[ stack.length - 1 ].appendChild( li );
	} );

	var toc = document.createElement( 'div' );
	toc.id = 'toc';
	toc.className = 'toc';
	toc.setAttribute( 'role', 'navigation' );
	var title = document.createElement( 'div' );
	title.className = 'toctitle';
	title.textContent = 'Contents';
	toc.appendChild( title );
	toc.appendChild( root );
	marker.parentNode.replaceChild( toc, marker );
} );