MediaWiki:Common.js
Appearance
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.
/* MindAttic Cyberspace backdrop for MediaWiki, mounted the way mindattic.com mounts it: MindAttic.Components@4234f880055d3365651fcda2ca15ef6307c0d68a's
circuit-board parallax, scan-lines and effects behind the page, a tap outside the page sparking one effect where
you tapped. The engine is told to keep its effects out of the whole page column (header, side menus, article,
footer); the side columns and footer are see-through, so the circuit board and scan-lines show there, and the
effect layer is clipped out of the article itself, so nothing it draws can ever reach the text, wanderers included.
Off for reduced motion and while editing.
Book chapters (prose2wiki's .prose-book) get zen mode: the chapter alone on a dark or light page, remembered between
chapters, with the arrow keys turning pages. */
( function () {
var base = 'https://cdn.jsdelivr.net/gh/mindattic/MindAttic.Components@4234f880055d3365651fcda2ca15ef6307c0d68a/web/components/';
var root = document.documentElement;
function store( key, value ) {
try {
if ( value === undefined ) {
return window.localStorage.getItem( key );
}
window.localStorage.setItem( key, value );
} catch ( e ) {}
return null;
}
// ---- zen mode ---------------------------------------------------------------------------------------------------
function backdropPaused( paused ) {
var cb = window.consoleBg;
if ( cb && cb.setAutoSpawn ) {
try { cb.setAutoSpawn( !paused ); } catch ( e ) {}
}
}
function setZen( on ) {
root.classList.toggle( 'prose-zen', on );
store( 'prose-zen', on ? '1' : '0' );
backdropPaused( on );
var b = document.querySelector( '.prose-zen-toggle .prose-zen-on' );
if ( b ) {
b.textContent = on ? 'Leave zen' : 'Zen mode';
}
}
function setShade( light ) {
root.classList.toggle( 'prose-zen-light', light );
store( 'prose-zen-shade', light ? 'light' : 'dark' );
var b = document.querySelector( '.prose-zen-toggle .prose-zen-shade' );
if ( b ) {
b.textContent = light ? 'Dark' : 'Light';
}
}
function zenControls() {
if ( !document.querySelector( '.prose-book' ) ) {
return;
}
var bar = document.createElement( 'div' );
bar.className = 'prose-zen-toggle';
bar.innerHTML = '<button type="button" class="prose-zen-shade"></button><button type="button" class="prose-zen-on"></button>';
document.body.appendChild( bar );
bar.querySelector( '.prose-zen-on' ).addEventListener( 'click', function () {
setZen( !root.classList.contains( 'prose-zen' ) );
} );
bar.querySelector( '.prose-zen-shade' ).addEventListener( 'click', function () {
setShade( !root.classList.contains( 'prose-zen-light' ) );
} );
setShade( store( 'prose-zen-shade' ) === 'light' );
setZen( store( 'prose-zen' ) === '1' );
document.addEventListener( 'keydown', function ( e ) {
if ( e.altKey || e.ctrlKey || e.metaKey || /^(INPUT|TEXTAREA|SELECT)$/.test( ( e.target && e.target.tagName ) || '' ) ) {
return;
}
var go = e.key === 'ArrowLeft' ? '.prose-prev a' : e.key === 'ArrowRight' ? '.prose-next a' : null;
if ( go && document.querySelector( go ) ) {
window.location.href = document.querySelector( go ).href;
} else if ( e.key === 'Escape' && root.classList.contains( 'prose-zen' ) ) {
setZen( false );
}
} );
}
// A link can open a chapter in zen: ?zen=dark or ?zen=light (?zen=off leaves it).
var asked = ( /[?&]zen=(dark|light|off)\b/.exec( window.location.search ) || [] )[ 1 ];
if ( asked ) {
store( 'prose-zen', asked === 'off' ? '0' : '1' );
if ( asked !== 'off' ) {
store( 'prose-zen-shade', asked );
}
}
// Zen applies before the backdrop or anything else paints, so a reader in zen never sees the page flash.
if ( store( 'prose-zen' ) === '1' && /\/\d+$/.test( mw.config.get( 'wgPageName' ) || '' ) && mw.config.get( 'wgNamespaceNumber' ) === 3000 ) {
root.classList.add( 'prose-zen' );
if ( store( 'prose-zen-shade' ) === 'light' ) {
root.classList.add( 'prose-zen-light' );
}
}
$( zenControls );
// ---- spoilers (PRS-T1068) ---------------------------------------------------------------------------------------
// Text that spoils a book (.prose-spoiler[data-book]) stays hidden until the reader marks that book read in
// "Books I've read". Visitors keep the list in localStorage (JSON); logged-in users in the user option
// userjs-prose-read (JSON), so it follows them between devices. A click on one spoiler reveals just that one.
var READ_KEY = 'prose-read', READ_OPTION = 'userjs-prose-read';
function readBooks() {
var raw = mw.user.isAnon() ? store( READ_KEY ) : mw.user.options.get( READ_OPTION );
try {
var list = JSON.parse( raw || '[]' );
return Array.isArray( list ) ? list : [];
} catch ( e ) {
return [];
}
}
function saveReadBooks( list ) {
var json = JSON.stringify( list );
if ( mw.user.isAnon() ) {
store( READ_KEY, json );
return $.Deferred().resolve().promise();
}
mw.user.options.set( READ_OPTION, json );
return new mw.Api().saveOption( READ_OPTION, json );
}
function applySpoilers() {
var read = readBooks();
document.querySelectorAll( '.prose-spoiler' ).forEach( function ( el ) {
var open = read.indexOf( el.getAttribute( 'data-book' ) ) !== -1;
el.classList.toggle( 'prose-spoiler-read', open );
if ( !open && !el.title ) {
el.title = 'Spoiler for ' + ( el.getAttribute( 'data-title' ) || 'a book' ) + ': click to show, or mark the book read in "Books I\'ve read"';
}
} );
}
function bookList() {
return $.ajax( {
url: mw.util.getUrl( 'MediaWiki:Prose-books.json', { action: 'raw', ctype: 'application/json' } ),
dataType: 'json', cache: true
} ).then( function ( books ) {
return Array.isArray( books ) ? books : [];
}, function () {
return [];
} );
}
function readingPanel() {
var existing = document.querySelector( '.prose-reading-panel' );
if ( existing ) {
existing.remove();
return;
}
bookList().then( function ( books ) {
var read = readBooks();
var panel = document.createElement( 'div' );
panel.className = 'prose-reading-panel';
panel.setAttribute( 'role', 'dialog' );
panel.setAttribute( 'aria-label', 'Books I\'ve read' );
var html = '<div class="prose-reading-head"><b>Books I\'ve read</b><button type="button" class="prose-reading-close" aria-label="Close">×</button></div>' +
'<p class="prose-reading-note">Spoilers for a book stay hidden until you tick it. ' +
( mw.user.isAnon() ? 'Saved in this browser.' : 'Saved to your account.' ) + '</p><div class="prose-reading-list">';
var series = null;
books.slice().sort( function ( a, b ) { return a.order - b.order; } ).forEach( function ( b ) {
html += '<label><input type="checkbox" value="' + mw.html.escape( b.slug ) + '"' +
( read.indexOf( b.slug ) !== -1 ? ' checked' : '' ) + '> <i>' + mw.html.escape( b.title ) + '</i>' +
( b.series ? ' <small>' + mw.html.escape( b.series ) + '</small>' : '' ) + '</label>';
series = b.series;
} );
html += '</div><div class="prose-reading-actions"><button type="button" data-all="1">All</button>' +
'<button type="button" data-all="0">None</button></div>';
panel.innerHTML = html;
document.body.appendChild( panel );
function changed() {
var list = [];
panel.querySelectorAll( 'input:checked' ).forEach( function ( i ) { list.push( i.value ); } );
saveReadBooks( list ).always( applySpoilers );
applySpoilers();
}
panel.addEventListener( 'change', changed );
panel.querySelectorAll( '[data-all]' ).forEach( function ( btn ) {
btn.addEventListener( 'click', function () {
var on = btn.getAttribute( 'data-all' ) === '1';
panel.querySelectorAll( 'input' ).forEach( function ( i ) { i.checked = on; } );
changed();
} );
} );
panel.querySelector( '.prose-reading-close' ).addEventListener( 'click', function () { panel.remove(); } );
} );
}
function readThisBookToggle() {
// On a book's own page: one button to mark the book read (or unread).
if ( mw.config.get( 'wgNamespaceNumber' ) !== 3000 || /\//.test( mw.config.get( 'wgTitle' ) ) ) {
return;
}
var actions = document.querySelector( '.prose-actions' );
var slug = ( document.querySelector( '.prose-book-slug' ) || {} ).textContent;
if ( !actions || !slug ) {
return;
}
var btn = document.createElement( 'button' );
btn.type = 'button';
btn.className = 'prose-read-toggle';
function label() {
btn.textContent = readBooks().indexOf( slug ) !== -1 ? '✓ Read' : 'Mark as read';
}
label();
btn.addEventListener( 'click', function () {
var list = readBooks(), i = list.indexOf( slug );
if ( i === -1 ) { list.push( slug ); } else { list.splice( i, 1 ); }
saveReadBooks( list ).always( function () { label(); applySpoilers(); } );
label();
applySpoilers();
} );
actions.appendChild( btn );
}
$.when( mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'mediawiki.user' ] ), $.ready ).then( function () {
applySpoilers();
document.addEventListener( 'click', function ( e ) {
var el = e.target.closest && e.target.closest( '.prose-spoiler:not(.prose-spoiler-read):not(.prose-spoiler-shown)' );
if ( el ) {
e.preventDefault();
el.classList.add( 'prose-spoiler-shown' );
}
}, true );
var link = mw.util.addPortletLink( 'p-vector-user-menu-overflow', '#', 'Books I\'ve read', 'pt-prose-read', 'Choose the books you have read: their spoilers show' ) ||
mw.util.addPortletLink( 'p-personal', '#', 'Books I\'ve read', 'pt-prose-read' );
if ( link ) {
link.addEventListener( 'click', function ( e ) { e.preventDefault(); readingPanel(); } );
}
readThisBookToggle();
} );
// ---- the timeline (PRS-T1069) -------------------------------------------------------------------------------------
// The Timeline page's #prose-timeline: every chapter of every book and the records' dated backstory, oldest on
// top. A checkbox dropdown picks the books shown (remembered in localStorage); events of books the reader hasn't
// marked read are spoilers like any other.
var TL_KEY = 'prose-timeline-books';
function drawTimeline( root, data ) {
var shown;
try { shown = JSON.parse( store( TL_KEY ) || 'null' ); } catch ( e ) { shown = null; }
if ( !Array.isArray( shown ) ) {
shown = data.books.map( function ( b ) { return b.slug; } ).concat( [ 'backstory' ] );
}
var bySlug = {};
data.books.forEach( function ( b ) { bySlug[ b.slug ] = b; } );
var bar = document.createElement( 'div' );
bar.className = 'prose-tl-bar';
var pick = '<details class="prose-tl-pick"><summary>Books shown <span class="prose-tl-count"></span></summary><div class="prose-tl-menu">' +
'<div class="prose-tl-menu-actions"><button type="button" data-tl-all="1">All</button><button type="button" data-tl-all="0">None</button></div>' +
'<label><input type="checkbox" value="backstory"> <b>Backstory</b> <small>dated events from the records</small></label>';
data.books.slice().sort( function ( a, b ) { return a.order - b.order; } ).forEach( function ( b ) {
pick += '<label><input type="checkbox" value="' + mw.html.escape( b.slug ) + '"> <i>' + mw.html.escape( b.title ) + '</i>' +
( b.series ? ' <small>' + mw.html.escape( b.series ) + '</small>' : '' ) + '</label>';
} );
bar.innerHTML = pick + '</div></details>';
var list = document.createElement( 'ol' );
list.className = 'prose-tl';
root.innerHTML = '';
root.appendChild( bar );
root.appendChild( list );
function fmt( iso, approx ) {
var d = new Date( iso + ( iso.length <= 16 ? ':00Z' : '' ) );
if ( isNaN( d ) ) { return iso; }
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
return approx ? ( 'c. ' + d.getUTCFullYear() ) : ( d.getUTCDate() + ' ' + months[ d.getUTCMonth() ] + ' ' + d.getUTCFullYear() );
}
function render() {
var items = [];
if ( shown.indexOf( 'backstory' ) !== -1 ) {
data.backstory.forEach( function ( b ) {
items.push( { key: b.year + '-01-01T00:00', year: b.year, html: '<span class="prose-tl-when">' + b.year + '</span>' +
'<span class="prose-tl-what"><a href="' + mw.util.getUrl( b.entity ) + '">' + mw.html.escape( b.entity ) + '</a>: ' +
mw.html.escape( b.text ) + '</span>', kind: 'backstory', book: ( b.books || [] )[ 0 ] || '' } );
} );
}
data.events.forEach( function ( e ) {
if ( shown.indexOf( e.book ) === -1 ) { return; }
items.push( { key: e.start, year: parseInt( e.start, 10 ), kind: 'chapter', book: e.book,
html: '<span class="prose-tl-when">' + fmt( e.start, e.approx ) + '</span>' +
'<span class="prose-tl-what"><span class="prose-spoiler" data-book="' + mw.html.escape( e.book ) + '" data-title="' +
mw.html.escape( e.bookTitle ) + '"><a href="' + mw.util.getUrl( e.page ) + '">' + mw.html.escape( e.title ) + '</a></span>' +
' <small><i>' + mw.html.escape( e.bookTitle ) + '</i>' + ( e.approx ? ', approximate' : '' ) + '</small></span>' } );
} );
items.sort( function ( a, b ) { return a.key < b.key ? -1 : a.key > b.key ? 1 : 0; } );
var html = '', year = null;
items.forEach( function ( it ) {
if ( it.year !== year ) {
year = it.year;
html += '<li class="prose-tl-year">' + year + '</li>';
}
html += '<li class="prose-tl-item prose-tl-' + it.kind + '" data-book="' + mw.html.escape( it.book ) + '">' + it.html + '</li>';
} );
list.innerHTML = html || '<li class="prose-tl-empty">Pick a book from the list above.</li>';
bar.querySelector( '.prose-tl-count' ).textContent = '(' + shown.filter( function ( s ) { return s === 'backstory' || bySlug[ s ]; } ).length +
' of ' + ( data.books.length + 1 ) + ')';
applySpoilers();
}
bar.querySelectorAll( 'input' ).forEach( function ( i ) { i.checked = shown.indexOf( i.value ) !== -1; } );
bar.addEventListener( 'change', function () {
shown = [];
bar.querySelectorAll( 'input:checked' ).forEach( function ( i ) { shown.push( i.value ); } );
store( TL_KEY, JSON.stringify( shown ) );
render();
} );
bar.querySelectorAll( '[data-tl-all]' ).forEach( function ( btn ) {
btn.addEventListener( 'click', function () {
var on = btn.getAttribute( 'data-tl-all' ) === '1';
bar.querySelectorAll( 'input' ).forEach( function ( i ) { i.checked = on; } );
bar.dispatchEvent( new Event( 'change' ) );
} );
} );
render();
}
$.when( mw.loader.using( [ 'mediawiki.util' ] ), $.ready ).then( function () {
var root = document.getElementById( 'prose-timeline' );
if ( !root ) { return; }
$.ajax( { url: mw.util.getUrl( 'MediaWiki:Prose-timeline.json', { action: 'raw', ctype: 'application/json' } ), dataType: 'json' } )
.then( function ( data ) { drawTimeline( root, data ); },
function () { root.textContent = 'The timeline could not be loaded.'; } );
} );
// ---- the backdrop -----------------------------------------------------------------------------------------------
var reduced = window.matchMedia && window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches;
var editing = /^(edit|submit)$/.test( mw.config.get( 'wgAction' ) );
if ( reduced || editing || mw.config.get( 'skin' ) !== 'vector-2022' ) {
return;
}
// Host settings console-bg.js reads when it loads (same as mindattic.com).
window.__cyberspaceCircuitboardSrcs = [ 0, 1, 2 ].map( function ( n ) {
return base + 'Cyberspace/assets/circuitboard.0' + n + '.png';
} );
var column = '.mw-page-container';
var article = '.mw-body';
window.__cyberspaceKeepoutSelectors = [
column, '#content', '.vector-header-container', '.vector-column-start .vector-pinned-container',
'.vector-column-end .vector-pinned-container', '.vector-sticky-header', '.mw-footer-container'
].join( ', ' );
mw.loader.load( base + 'Cyberspace/frontpage.css', 'text/css' );
// The effect layer is clipped out of the article, so no effect is ever drawn over the text.
function clipToMargins() {
var host = document.querySelector( '.console-bg-host' );
var page = document.querySelector( article ) || document.querySelector( column );
if ( !host || !page ) {
return;
}
var h = host.getBoundingClientRect(), r = page.getBoundingClientRect();
var w = Math.ceil( h.width ), ht = Math.ceil( h.height );
var l = Math.floor( r.left - h.left ), t = Math.floor( r.top - h.top );
var rt = Math.ceil( r.right - h.left ), b = Math.ceil( r.bottom - h.top );
host.style.clipPath = 'path(evenodd, "M0 0H' + w + 'V' + ht + 'H0Z M' + l + ' ' + t + 'H' + rt + 'V' + b + 'H' + l + 'Z")';
}
var clipQueued = false;
function queueClip() {
if ( !clipQueued ) {
clipQueued = true;
window.requestAnimationFrame( function () {
clipQueued = false;
clipToMargins();
} );
}
}
function inColumn( x, y ) {
var page = document.querySelector( column );
if ( !page ) {
return false;
}
var r = page.getBoundingClientRect();
return x >= r.left && x <= r.right && y >= r.top && y <= r.bottom;
}
function tapToSpawn() {
var cb = window.consoleBg;
var fx = cb && cb._demo;
if ( !fx ) {
return;
}
var effects = [ 'spawnError', 'spawnWarning', 'spawnWindow', 'spawnMemo', 'spawnFrag', 'spawnArtifact',
'spawnGeoWindow', 'spawnNetConnect', 'spawnMorseDot', 'spawnFolderRip', 'spawnCascade',
'spawnArtifactPredator', 'spawnHyperspaceReader' ];
document.addEventListener( 'pointerdown', function ( e ) {
if ( e.button > 0 || root.classList.contains( 'prose-zen' ) || inColumn( e.clientX, e.clientY ) ||
( e.target.closest && e.target.closest( 'a, button, input, select, textarea, label, [role="button"]' ) ) ) {
return;
}
var o = { x: e.clientX / window.innerWidth * 100, y: e.clientY / window.innerHeight * 100 };
if ( cb.inKeepout && cb.inKeepout( o.x, o.y ) ) {
return;
}
try {
if ( cb.spawnSparkBurst ) {
cb.spawnSparkBurst( o.x, o.y );
}
} catch ( err ) {}
var order = effects.slice().sort( function () { return Math.random() - 0.5; } );
for ( var i = 0; i < order.length; i++ ) {
try {
if ( typeof fx[ order[ i ] ] === 'function' && fx[ order[ i ] ]( o ) !== false ) {
break;
}
} catch ( err ) {}
}
} );
}
$( function () {
[ 'cyberspace-sl-fine', 'cyberspace-sl-coarse', 'console-bg-host' ].forEach( function ( cls ) {
var layer = document.createElement( 'div' );
layer.className = cls;
layer.setAttribute( 'aria-hidden', 'true' );
document.body.appendChild( layer );
} );
clipToMargins();
window.addEventListener( 'resize', queueClip );
window.addEventListener( 'scroll', queueClip, { passive: true } );
if ( window.ResizeObserver ) {
new window.ResizeObserver( queueClip ).observe( document.querySelector( article ) || document.body );
}
[ 'SacredGeometry/sacred-geometry.js', 'Hyperspace/hyperspace.js', 'Hyperspace/hyperspace-reader.js',
'Cyberspace/console-bg.js' ].reduce( function ( chain, path ) {
return chain.then( function () {
return mw.loader.getScript( base + path );
} );
}, $.Deferred().resolve().promise() ).then( function () {
clipToMargins();
backdropPaused( root.classList.contains( 'prose-zen' ) );
tapToSpawn();
} );
} );
}() );