Jump to content

MediaWiki:Common.js: Difference between revisions

From New Bradford
nb>Admin
Show the main menu pinned for logged-out visitors
m 1 revision imported
 
(2 intermediate revisions by the same user not shown)
Line 25: Line 25:
} else {
} else {
pinMainMenu();
pinMainMenu();
}
}() );
/* Section share links: a small link icon after the page title and every section heading.
  Clicking it copies a direct link to that spot and updates the address bar. */
( function () {
var css = '.nb-anchor{margin-left:.4em;font-size:.6em;vertical-align:middle;text-decoration:none!important;opacity:.45;cursor:pointer}' +
'.nb-anchor:hover{opacity:1}' +
'.nb-anchor-tip{margin-left:.5em;padding:.15em .5em;font-size:.5em;font-weight:normal;font-family:sans-serif;vertical-align:middle;background:#333;color:#fff;border-radius:4px;transition:opacity .4s;white-space:nowrap}.nb-anchor-tip.nb-hide{opacity:0}' +
'.nb-anchor svg{width:1em;height:1em;fill:currentColor;vertical-align:middle}';
var icon = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3.9 12a3.1 3.1 0 0 1 3.1-3.1h4V7H7a5 5 0 0 0 0 10h4v-1.9H7A3.1 3.1 0 0 1 3.9 12zM8 13h8v-2H8v2zm9-6h-4v1.9h4a3.1 3.1 0 0 1 0 6.2h-4V17h4a5 5 0 0 0 0-10z"/></svg>';
function fallback( url ) {
var ta = document.createElement( 'textarea' );
ta.value = url; ta.style.position = 'fixed'; ta.style.opacity = '0';
document.body.appendChild( ta ); ta.select();
try { document.execCommand( 'copy' ); } catch ( err ) {}
document.body.removeChild( ta );
}
function showTip( a, text ) {
var old = a.parentNode.querySelector( '.nb-anchor-tip' );
if ( old ) { old.remove(); }
var tip = document.createElement( 'span' );
tip.className = 'nb-anchor-tip';
tip.textContent = text;
a.insertAdjacentElement( 'afterend', tip );
setTimeout( function () { tip.classList.add( 'nb-hide' ); }, 1600 );
setTimeout( function () { tip.remove(); }, 2000 );
}
function makeLink( hash, label ) {
var a = document.createElement( 'a' );
a.className = 'nb-anchor';
a.href = hash || location.pathname + location.search;
a.title = 'Copy link to ' + label;
a.setAttribute( 'aria-label', a.title );
a.innerHTML = icon;
a.addEventListener( 'click', function ( e ) {
e.preventDefault();
e.stopPropagation();
var url = location.origin + location.pathname + location.search + ( hash || '' );
function done() { showTip( a, 'Link copied: ' + label ); }
if ( navigator.clipboard && window.isSecureContext ) {
navigator.clipboard.writeText( url ).then( done, function () { fallback( url ); done(); } );
} else {
fallback( url ); done();
}
} );
return a;
}
function addAnchors() {
if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; }
var st=document.createElement('style');st.textContent=css;document.head.appendChild(st);
var title = document.getElementById( 'firstHeading' );
if ( title && !title.querySelector( '.nb-anchor' ) ) {
title.appendChild( makeLink( '', title.textContent.trim() ) );
}
var heads = document.querySelectorAll( '.mw-parser-output .mw-heading h2[id], .mw-parser-output .mw-heading h3[id], .mw-parser-output .mw-heading h4[id], .mw-parser-output .mw-heading h5[id], .mw-parser-output .mw-heading h6[id]' );
heads.forEach( function ( h ) {
if ( h.querySelector( '.nb-anchor' ) ) { return; }
h.appendChild( makeLink( '#' + h.id, h.textContent.trim() ) );
} );
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', addAnchors );
} else {
addAnchors();
}
}
}() );
}() );



Latest revision as of 01:37, 31 October 2026

/* Logged-out visitors: show the main menu (Bestiary, Tameables, etc.) pinned on the left,
   the same way logged-in users see it. The Vector skin only pins it for logged-in users. */
( function () {
	if ( mw.config.get( 'wgUserName' ) !== null ) {
		return;
	}
	function pinMainMenu() {
		var menu = document.getElementById( 'vector-main-menu' );
		var pinned = document.getElementById( 'vector-main-menu-pinned-container' );
		if ( !menu || !pinned || pinned.contains( menu ) ) {
			return;
		}
		pinned.appendChild( menu );
		var header = menu.querySelector( '.vector-pinnable-header' );
		if ( header ) {
			header.classList.remove( 'vector-pinnable-header-unpinned' );
			header.classList.add( 'vector-pinnable-header-pinned' );
		}
		var html = document.documentElement.classList;
		html.remove( 'vector-feature-main-menu-pinned-disabled' );
		html.add( 'vector-feature-main-menu-pinned-enabled' );
	}
	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', pinMainMenu );
	} else {
		pinMainMenu();
	}
}() );

/* Section share links: a small link icon after the page title and every section heading.
   Clicking it copies a direct link to that spot and updates the address bar. */
( function () {
	var css = '.nb-anchor{margin-left:.4em;font-size:.6em;vertical-align:middle;text-decoration:none!important;opacity:.45;cursor:pointer}' +
		'.nb-anchor:hover{opacity:1}' +
		'.nb-anchor-tip{margin-left:.5em;padding:.15em .5em;font-size:.5em;font-weight:normal;font-family:sans-serif;vertical-align:middle;background:#333;color:#fff;border-radius:4px;transition:opacity .4s;white-space:nowrap}.nb-anchor-tip.nb-hide{opacity:0}' +
		'.nb-anchor svg{width:1em;height:1em;fill:currentColor;vertical-align:middle}';
	var icon = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M3.9 12a3.1 3.1 0 0 1 3.1-3.1h4V7H7a5 5 0 0 0 0 10h4v-1.9H7A3.1 3.1 0 0 1 3.9 12zM8 13h8v-2H8v2zm9-6h-4v1.9h4a3.1 3.1 0 0 1 0 6.2h-4V17h4a5 5 0 0 0 0-10z"/></svg>';
	function fallback( url ) {
		var ta = document.createElement( 'textarea' );
		ta.value = url; ta.style.position = 'fixed'; ta.style.opacity = '0';
		document.body.appendChild( ta ); ta.select();
		try { document.execCommand( 'copy' ); } catch ( err ) {}
		document.body.removeChild( ta );
	}
	function showTip( a, text ) {
		var old = a.parentNode.querySelector( '.nb-anchor-tip' );
		if ( old ) { old.remove(); }
		var tip = document.createElement( 'span' );
		tip.className = 'nb-anchor-tip';
		tip.textContent = text;
		a.insertAdjacentElement( 'afterend', tip );
		setTimeout( function () { tip.classList.add( 'nb-hide' ); }, 1600 );
		setTimeout( function () { tip.remove(); }, 2000 );
	}
	function makeLink( hash, label ) {
		var a = document.createElement( 'a' );
		a.className = 'nb-anchor';
		a.href = hash || location.pathname + location.search;
		a.title = 'Copy link to ' + label;
		a.setAttribute( 'aria-label', a.title );
		a.innerHTML = icon;
		a.addEventListener( 'click', function ( e ) {
			e.preventDefault();
			e.stopPropagation();
			var url = location.origin + location.pathname + location.search + ( hash || '' );
			function done() { showTip( a, 'Link copied: ' + label ); }
			if ( navigator.clipboard && window.isSecureContext ) {
				navigator.clipboard.writeText( url ).then( done, function () { fallback( url ); done(); } );
			} else {
				fallback( url ); done();
			}
		} );
		return a;
	}
	function addAnchors() {
		if ( mw.config.get( 'wgAction' ) !== 'view' ) { return; }
		var st=document.createElement('style');st.textContent=css;document.head.appendChild(st);
		var title = document.getElementById( 'firstHeading' );
		if ( title && !title.querySelector( '.nb-anchor' ) ) {
			title.appendChild( makeLink( '', title.textContent.trim() ) );
		}
		var heads = document.querySelectorAll( '.mw-parser-output .mw-heading h2[id], .mw-parser-output .mw-heading h3[id], .mw-parser-output .mw-heading h4[id], .mw-parser-output .mw-heading h5[id], .mw-parser-output .mw-heading h6[id]' );
		heads.forEach( function ( h ) {
			if ( h.querySelector( '.nb-anchor' ) ) { return; }
			h.appendChild( makeLink( '#' + h.id, h.textContent.trim() ) );
		} );
	}
	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', addAnchors );
	} else {
		addAnchors();
	}
}() );