MediaWiki:Gadget-DocTabs.js

"use strict"; /*globals mw, jQuery */ // var conf = mw.config.get([	'wgPageName',	'wgUserLanguage',	'wgNamespaceNumber',	'wgNamespaceIds',	'wgTitle',	'wgCanonicalNamespace' ]); var tabs = {}; var titlesQueue = []; var msgQueue = (conf.wgUserLanguage !== 'en') ? {} : null;

var msgs = { 'nstab-main': "Entry", 'nstab-talk': "Discussion", 'nstab-citations': "Citations", 'nstab-template': "Template", 'nstab-template_talk': "Discussion", 'nstab-module': "Module", 'nstab-module_talk': "Discussion", 'nstab-documentation': "Documentation", 'tooltip-ca-nstab-main': "View the content page", 'tooltip-ca-nstab-module': "", 'tooltip-ca-nstab-template': "View the template", 'tooltip-ca-nstab-citations': "View the citations page", 'tooltip-ca-nstab-documentation': "View the documentation", 'tooltip-ca-talk': "Discussion about the content page" };

var api = new mw.Api;

if (conf.wgUserLanguage !== 'en') {

try { var msgsCache = JSON.parse(window.sessionStorage.getItem('DocTabs-messages-' + conf.wgUserLanguage)); if (msgsCache) msgs = msgsCache; else throw void('tantrum'); } catch (e) { api.get({		action: 'query',		meta: 'allmessages',		amlang: conf.wgUserLanguage,		ammessages: Object.keys(msgs).join('|')	}).done(function (result) {		for (var i = 0; i < result.query.allmessages.length; ++i) {			var mesg = result.query.allmessages[i];			if ('missing' in mesg)				continue;			msgs[mesg.name] = mesg['*'];			if (msgQueue && msgQueue[mesg.name])				msgQueue[mesg.name].elem[msgQueue[mesg.name].prop] = mesg['*'];		}		msgQueue = null;

try { window.sessionStorage.setItem('DocTabs-messages-' + conf.wgUserLanguage, JSON.stringify(msgs)); } catch (e) { /* swallow */ }	}); }

}

function makeThird(page1, page2, msgroot, msgtalk, msgmine) { msgtalk = msgtalk || (msgroot + '_talk');

var portlet = document.getElementById('p-associated-pages') || document.getElementById('p-namespaces') || document.getElementById('p-cactions'); if (!portlet) { console.info("portlet not found"); return;} var ul = portlet.getElementsByTagName('ul')[0]; var first = ul.firstElementChild; ul.removeChild(first.nextElementSibling); var pl1 = tabs[page1] = mw.util.addPortletLink(portlet.id, mw.util.getUrl(page1), msgs['nstab-' + msgroot], 'ca-nstab-' + msgroot, msgs['tooltip-ca-nstab-' + msgroot], 'c', first); var pl2 = tabs[page2] = mw.util.addPortletLink(portlet.id, mw.util.getUrl(page2), msgs['nstab-' + msgtalk], 'ca-talk', msgs['tooltip-ca-talk'], 't', first); titlesQueue.push(page1, page2); var firstLink = first.getElementsByTagName('a')[0]; if (msgmine) { firstLink.textContent = msgs['nstab-' + msgmine]; firstLink.title = msgs['tooltip-ca-nstab-' + msgmine]; }	firstLink.accessKey = '3'; if (!msgQueue) return; msgQueue['nstab-' + msgroot] = { elem: pl1.getElementsByTagName('a')[0], prop: 'textContent' };	msgQueue['nstab-' + msgtalk] = { elem: pl2.getElementsByTagName('a')[0], prop: 'textContent' };	msgQueue['tooltip-ca-nstab-' + msgroot] = { elem: pl1.getElementsByTagName('a')[0], prop: 'title' };	msgQueue['tooltip-ca-talk'] = { elem: pl2.getElementsByTagName('a')[0], prop: 'title' }; }

function addThird(page, msgroot) { var portlet = document.getElementById('p-associated-pages') || document.getElementById('p-namespaces') || document.getElementById('p-cactions'); if (!portlet) { console.info("portlet not found"); return;} var pl = tabs[page] = mw.util.addPortletLink(portlet.id,		mw.util.getUrl(page),		msgs['nstab-' + msgroot],		'ca-nstab-' + msgroot,		msgs['tooltip-ca-nstab-' + msgroot],		'3',		portlet.id === 'p-cactions' ?			(document.getElementById('ca-edit') || document.getElementById('ca-viewsource')) :			null	); pl.isThird = true; titlesQueue.push(page);

if (!msgQueue) return; msgQueue['nstab-' + msgroot] = { elem: pl.getElementsByTagName('a')[0], prop: 'textContent' };	msgQueue['tooltip-ca-nstab-' + msgroot] = { elem: pl.getElementsByTagName('a')[0], prop: 'title' }; }

jQuery(document).ready(function {

if (conf.wgNamespaceNumber === conf.wgNamespaceIds.citations) { makeThird(conf.wgTitle, 'Talk:' + conf.wgTitle, 'main', 'talk'); } else if ((conf.wgNamespaceNumber === 0) || (conf.wgNamespaceNumber === conf.wgNamespaceIds.talk)) { addThird('Citations:' + conf.wgTitle, 'citations'); } else if (	(conf.wgNamespaceNumber === conf.wgNamespaceIds.module) || (conf.wgNamespaceNumber === conf.wgNamespaceIds.template) ||	(conf.wgNamespaceNumber === conf.wgNamespaceIds.module_talk) || (conf.wgNamespaceNumber === conf.wgNamespaceIds.template_talk) ) { if (/\/documentation$/.test(conf.wgPageName)) { if (!(conf.wgNamespaceNumber & 1)) { // talk namespace var basename = conf.wgTitle.replace(/\/documentation$/, '');

makeThird(				conf.wgCanonicalNamespace + ':' + basename,				conf.wgCanonicalNamespace + ' talk:' + basename,				(conf.wgNamespaceNumber & ~1) == conf.wgNamespaceIds.module ? 'module' : 'template',				null, 'documentation'			); } else return; } else { addThird(conf.wgCanonicalNamespace.replace(/[_ ]talk$/, '') + ':' + conf.wgTitle + '/documentation', 'documentation'); } } else return;

if (titlesQueue.length) { api.get({		action: 'query',		titles: titlesQueue.join('|')	}).done(function (result) {		if (!result.query) {			return;		}

for (var pageid in result.query.pages) { var page = result.query.pages[pageid]; if (parseInt(pageid, 10) < 0) { var link = tabs[page.title].getElementsByTagName('a')[0]; var query = { action: 'edit', redlink: '1' }; if (tabs[page.title].isThird && (conf.wgNamespaceNumber !== 0 && conf.wgNamespaceNumber !== 1)) { if (conf.wgNamespaceNumber === conf.wgNamespaceIds.module) if (/\/testcases$/.test(conf.wgTitle)) query.preload = 'Template:documentation/preloadModuleTestcases'; else query.preload = 'Template:documentation/preloadModule'; else query.preload = 'Template:documentation/preloadTemplate'; }				link.href = mw.util.getUrl(page.title, query); jQuery(tabs[page.title]).addClass('new'); }		}	}); }

});