MediaWiki:Common.js
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.
/* Any JavaScript here will be loaded for all users on every page load. */
/*
* Reformed Texts — Page Navigation
*
* Add future works to the registry.
* Navigation logic is shared across all works.
*/
(function () {
'use strict';
const works = [
{
id: 'calvin-romans',
author: 'calvin',
title: 'romans',
firstPage: 13,
lastPage: 472,
extension: 'png',
legacyPrefixes: ['P-'],
directory: 'John Calvin/Commentary on Romans/Page Directory'
}
];
function pageNumber(number) {
return String(number).padStart(3, '0');
}
function canonicalPrefix(work) {
return work.author + '-' + work.title + '-';
}
function findCurrentPage(title) {
for (const work of works) {
const prefixes = [
canonicalPrefix(work),
...(work.legacyPrefixes || [])
];
for (const prefix of prefixes) {
const escaped = prefix.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
);
const pattern = new RegExp(
'^Page:' + escaped + '(\\d+)\\.' +
work.extension + '$',
'i'
);
const match = title.match(pattern);
if (match) {
const number = Number(match[1]);
if (
number >= work.firstPage &&
number <= work.lastPage
) {
return { work, prefix, number };
}
}
}
}
return null;
}
function pageTitle(context, number) {
return 'Page:' +
context.prefix +
pageNumber(number) +
'.' +
context.work.extension;
}
function makeLink(title, label) {
const link = document.createElement('a');
link.href = mw.util.getUrl(title);
link.textContent = label;
return link;
}
function buildNavigation(context) {
const nav = document.createElement('nav');
nav.className = 'rt-page-navigation';
nav.setAttribute(
'aria-label',
'Transcription navigation'
);
if (context.number > context.work.firstPage) {
nav.appendChild(
makeLink(
pageTitle(context, context.number - 1),
'← Previous'
)
);
}
nav.appendChild(
makeLink(context.work.directory, 'Page Directory')
);
if (context.number < context.work.lastPage) {
nav.appendChild(
makeLink(
pageTitle(context, context.number + 1),
'Next →'
)
);
}
return nav;
}
function initialize() {
if (mw.config.get('wgAction') !== 'view') {
return;
}
const title = mw.config.get('wgPageName')
.replace(/_/g, ' ');
const context = findCurrentPage(title);
if (!context) {
return;
}
const container = document.querySelector(
'.prp-page-container'
);
if (!container || document.querySelector(
'.rt-page-navigation'
)) {
return;
}
container.parentNode.insertBefore(
buildNavigation(context),
container
);
}
mw.loader.using('mediawiki.util').then(function () {
if (document.readyState === 'loading') {
document.addEventListener(
'DOMContentLoaded',
initialize,
{ once: true }
);
} else {
initialize();
}
});
}());