User:Dsf/monobook.js

/* monobook.js Copyright (C) 2006, 2007 GrafZahl (en.wikisource.org user) Licence: GPLv2

ToggleText
Displayed text toggle, e.g. for applying errata. Similar to ThomasV's OptionText (which displays or doesn't display a certain text). Uses ideas from ThomasV.

Description
Creates an additional menu (similar to the navigation and toolbox menus) with links which, when pressed, will hide some portion of the text and display another instead of it. When pressed again, the old state is restored. More generally, the style with which both texts are displayed, are swapped, and then restored. The link names are specified by the user through specially crafted tags. For example, will create a link called "toggle errata". The text to be toggled is specified within subtags of the  tag. For example, lorem ipsum lorem istud will replace the text "lorem ipsum" with "lorem istud" and back again when the "toggle errata" link is pressed. function ToggleText {   // find all ToggleText requests var spanElts = document.getElementsByTagName('span'); var usedTitles = new Object; var ttTitles = new Array; var ttOrig = new Array; var ttRepl = new Array; var ttState = new Array; var spanElt; // store title, original style and replacement style, and current state for(var i = 0; spanElt = spanElts[i]; ++i) if(spanElt.className == 'ToggleText') {		// need exactly 2 direct subspans for orig/repl var subSpans = spanElt.getElementsByTagName('span'); var spanOrig = subSpans[0]; var spanRepl = spanOrig; while(spanRepl = spanRepl.nextSibling) if(spanRepl.tagName == 'SPAN') break; if(!spanRepl) continue; // skip already handled elements if(usedTitles[spanElt.title] == true) continue; usedTitles[spanElt.title] = true; ttTitles.push(spanElt.title); ttOrig.push(spanOrig.getAttribute('style')); ttRepl.push(spanRepl.getAttribute('style')); var cookie = GetCookie('Toggle' + spanElt.title); if((cookie) &&		  (cookie == 'true')) ttState.push(true); else ttState.push(false); }   // exit if no requests if(ttTitles.length == 0) return; // generate menu var menu = document.createElement('div'); menu.style.marginTop = '0.7em'; menu.innerHTML = ' display toggle ' + ' '	+ ' '; document.getElementById('p-tb').appendChild(menu); var list = document.getElementById('togglelist'); // add menu entries for(var i = 0; i != ttTitles.length; ++i) {	   var anchor = document.createElement('a'); anchor.appendChild(document.createTextNode(ttTitles[i])); anchor.setAttribute('href', 'javascript:displayToggleText("'				+ ttTitles[i] + '","' + ttOrig[i] + '","'				+ ttRepl[i] + '","' + ttState[i] + '");'); var link = document.createElement('li'); link.appendChild(anchor); link.setAttribute('id', ttTitles[i]); list.appendChild(link); // create well-defined initial state displayToggleText(ttTitles[i], ttOrig[i], ttRepl[i], ttState[i]); } } function displayToggleText(title, origStyle, replStyle, showRepl) {   // prepare variables var link = document.getElementById(title).firstChild; // menu entry var firstStyle; var secondStyle; if(showRepl) {	   link.firstChild.data = 'disable ' + title; link.setAttribute('href', 'javascript:displayToggleText("'			     + title + '","' + origStyle + '","' + replStyle			      + '", false);'); firstStyle = replStyle; secondStyle = origStyle; }   else {	   link.firstChild.data = 'enable ' + title; link.setAttribute('href', 'javascript:displayToggleText("'			     + title + '","' + origStyle + '","' + replStyle			      + '", true);'); firstStyle = origStyle; secondStyle = replStyle; }   // update cookie SetCookie('Toggle' + title, showRepl); // alter styles var spanElt; for(var i = 0; spanElt = document.getElementsByTagName('span')[i]; ++i) {	   // Handle appropriate elements only if((spanElt.className != 'ToggleText') ||	      (spanElt.title != title)) continue; var subSpans = spanElt.getElementsByTagName('span'); if(subSpans.length != 2) continue; subSpans[0].setAttribute('style', firstStyle); subSpans[1].setAttribute('style', secondStyle); } } $(ToggleText); /* Set Footer test */ function SetFooter {   /* check whether not to display the footer based on wikimarkup length * and template/parser function use. *    * The default is to display the footer. */   if(!window.XMLHttpRequest) {	   /* Indiscriminately display footer if AJAX is not available */ DisplayFooter; return; }   footerlimit = 1764; ajax = new XMLHttpRequest; ajax.onreadystatechange = function {	try {		if(ajax.readyState != 4) return; if(ajax.status != 200) /* Trouble, display without checking */ throw "Boo!"; xml = ajax.responseXML; wikitext = xml.getElementsByTagName("rev")[0].firstChild.data; if(wikitext.length < footerlimit) return; /* don't display footer */ throw "Boo!"; }	catch(er) {		DisplayFooter; return; }   }    page = wgScriptPath + '/api.php?action=query&titles=' + wgPageName + '&prop=revisions&rvprop=content&rvlimit=1&format=xml'; ajax.open('GET', page, true); ajax.send(null); } /* addOnloadHook(SetFooter); */ /*

OtherVersions
Allows the user to add links to other versions of the same work (a different translation, for example) below the "in other languages" box on the left. Uses ideas employed by ThomasV in the French Wikisource.

Description
The user places the template in a text. This template takes two parameters, the wikilink and the link text. So, where you would write  if your intention were to produce a normal wikilink, you now write. function OtherVersions {   // find all other versions var spanElts = document.getElementsByTagName('span'); var ov = new Array; var spanElt; for(var i = 0; spanElt = spanElts[i]; ++i) if(spanElt.className == 'OtherVersions') ov.push(spanElt.id); if(ov.length == 0) return; // build link list var listDiv = document.createElement('div'); listDiv.style.marginTop = '0.7em'; listDiv.innerHTML = ' other versions '; var pBody = document.createElement('div'); pBody.className = 'pBody'; var list = document.createElement('ul'); list.id = 'ovlist'; var ovText; var displayBox = false; while(ovText = ov.shift) {	   var brack = document.getElementById(ovText).title; // find link/text separator var pipeIdx = brack.indexOf('|'); if(pipeIdx == -1) continue; displayBox = true; // create anchor var wikilink = brack.slice(0, pipeIdx); var wikitext = brack.slice(pipeIdx + 1); var anchor = document.createElement('a'); anchor.appendChild(document.createTextNode(wikitext)); anchor.setAttribute('href', '/wiki/' + wikilink); // create list element var listElt = document.createElement('li'); listElt.appendChild(anchor); listElt.setAttribute('id', wikilink); // add list element to list list.appendChild(listElt); }   if(displayBox) {	   pBody.appendChild(list); listDiv.appendChild(pBody); document.getElementById('p-tb').appendChild(listDiv); } } $(OtherVersions); /*