MediaWiki:Common.js

/* Any JavaScript here will be loaded for all users on every page load. */ /* */

// ============================================================ // BEGIN Dynamic Navigation Bars (experimantal) // This script is from Wikipedia. For author attribution, please see http://en.wikipedia.org/w/index.php?title=MediaWiki:Common.js&action=history

/* Test if an element has a certain class ************************************** * * Description: Uses regular expressions and caching for better performance. * Maintainers: User:Mike Dillon, User:R. Koot, User:SG */

var hasClass = (function {    var reCache = {};    return function (element, className) {        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);   }; });

/** Collapsible tables ********************************************************* * *  Description: Allows tables to be collapsed, showing only the header. See *              NavFrame. * Maintainers: User:R. Koot */ var autoCollapse = 2; var collapseCaption = "hide"; var expandCaption = "show"; function collapseTable( tableIndex ) {    var Button = document.getElementById( "collapseButton" + tableIndex ); var Table = document.getElementById( "collapsibleTable" + tableIndex ); if ( !Table || !Button ) { return false; }    var Rows = Table.getElementsByTagName( "tr" ); if ( Button.firstChild.data == collapseCaption ) { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = "none"; }        Button.firstChild.data = expandCaption; } else { for ( var i = 1; i < Rows.length; i++ ) { Rows[i].style.display = Rows[0].style.display; }        Button.firstChild.data = collapseCaption; } } function createCollapseButtons {    var tableIndex = 0; var NavigationBoxes = new Object; var Tables = document.getElementsByTagName( "table" ); for ( var i = 0; i < Tables.length; i++ ) { if ( hasClass( Tables[i], "collapsible" ) ) { NavigationBoxes[ tableIndex ] = Tables[i]; Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex ); var Button    = document.createElement( "span" ); var ButtonLink = document.createElement( "a" ); var ButtonText = document.createTextNode( collapseCaption ); Button.style.styleFloat = "right"; Button.style.cssFloat = "right"; Button.style.fontWeight = "normal"; Button.style.textAlign = "right"; Button.style.width = "6em"; ButtonLink.setAttribute( "id", "collapseButton" + tableIndex ); ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" ); ButtonLink.appendChild( ButtonText ); Button.appendChild( document.createTextNode( "[" ) ); Button.appendChild( ButtonLink ); Button.appendChild( document.createTextNode( "]" ) ); var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0]; /* only add button and increment count if there is a header row to work with */ if (Header) { Header.insertBefore( Button, Header.childNodes[0] ); tableIndex++; }        }     }     for ( var i = 0;  i < tableIndex; i++ ) { if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) { collapseTable( i ); }    } } addOnloadHook( createCollapseButtons );

/** Dynamic Navigation Bars (experimental) ************************************* * *  Description: See NavFrame. * Maintainers: UNMAINTAINED */ // set up the words in your language var NavigationBarHide = '[' + collapseCaption + ']'; var NavigationBarShow = '[' + expandCaption + ']'; // set up max count of Navigation Bars on page, // if there are more, all will be hidden // NavigationBarShowDefault = 0; // all bars will be hidden // NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden var NavigationBarShowDefault = autoCollapse; // shows and hides content and picture (if available) of navigation bars // Parameters: //    indexNavigationBar: the index of navigation bar to be toggled function toggleNavigationBar(indexNavigationBar) {    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar); var NavFrame = document.getElementById("NavFrame" + indexNavigationBar); if (!NavFrame || !NavToggle) { return false; }    // if shown now if (NavToggle.firstChild.data == NavigationBarHide) { for (                var NavChild = NavFrame.firstChild;                 NavChild != null;                 NavChild = NavChild.nextSibling             ) { if ( hasClass( NavChild, 'NavPic' ) ) { NavChild.style.display = 'none'; }            if ( hasClass( NavChild, 'NavContent') ) { NavChild.style.display = 'none'; }        }     NavToggle.firstChild.data = NavigationBarShow; // if hidden now } else if (NavToggle.firstChild.data == NavigationBarShow) { for (                var NavChild = NavFrame.firstChild;                 NavChild != null;                 NavChild = NavChild.nextSibling             ) { if (hasClass(NavChild, 'NavPic')) { NavChild.style.display = 'block'; }            if (hasClass(NavChild, 'NavContent')) { NavChild.style.display = 'block'; }        }     NavToggle.firstChild.data = NavigationBarHide; } }  // adds show/hide-button to navigation bars function createNavigationBarToggleButton {    var indexNavigationBar = 0; // iterate over all -elements var divs = document.getElementsByTagName("div"); for(            var i=0;              NavFrame = divs[i];              i++         ) { // if found a navigation bar if (hasClass(NavFrame, "NavFrame")) { indexNavigationBar++; var NavToggle = document.createElement("a"); NavToggle.className = 'NavToggle'; NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar); NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');'); var NavToggleText = document.createTextNode(NavigationBarHide); NavToggle.appendChild(NavToggleText); // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) for(              var j=0;                j < NavFrame.childNodes.length;                j++             ) { if (hasClass(NavFrame.childNodes[j], "NavHead")) { NavFrame.childNodes[j].appendChild(NavToggle); }            }             NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar); }    }     // if more Navigation Bars found than Default: hide all if (NavigationBarShowDefault < indexNavigationBar) { for(                var i=1;                  i<=indexNavigationBar;                  i++         ) { toggleNavigationBar(i); }    }  }

addOnloadHook( createNavigationBarToggleButton );

function getElementsByClassName(oElm, strTagName, oClassNames){ var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName); var arrReturnElements = new Array; var arrRegExpClassNames = new Array; if(typeof oClassNames == "object"){ for(var i=0; i<oClassNames.length; i++){ arrRegExpClassNames[arrRegExpClassNames.length] = new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"); }	}	else{ arrRegExpClassNames[arrRegExpClassNames.length] = new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"); }	var oElement; var bMatchesAll; for(var j=0; j<arrElements.length; j++){ oElement = arrElements[j]; bMatchesAll = true; for(var k=0; k" + items[ti].innerHTML; } }

addOnloadHook( createCheckboxes );

/* Any JavaScript here will be loaded for all users on every page load. */

/* */ //Overrides the function with the same name in http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js //Fixes a bug: for the sorting order, takes the whole number, not the number before the second comma. //Also, removes spaces. See Help:Sorting. function ts_parseFloat(num) { if (!num) return 0; num = removeSpaces(num); num = parseFloat(num.replace(/,/g, "")); return (isNaN(num) ? 0 : num); }

//Keep spaces in "currency" mode, to sort a range "70 to 80" at 70, not 7080 function ts_parseFloat_ks(num) { if (!num) return 0; num = parseFloat(num.replace(/,/g, "")); return (isNaN(num) ? 0 : num); }

//Auxiliary function for function ts_resortTable(lnk) function removeSpaces(string) { var tstring = ""; string = '' + string; splitstring = string.split(" "); for(i = 0; i < splitstring.length; i++) tstring += splitstring[i]; return tstring; }

//Overrides the function with the same name in http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js //Changes the criteria for various sorting modes, see Help:Sorting. //For easy maintenance the difference with wikibits.js is kept limited to a few lines. function ts_resortTable(lnk) { // get the span var span = lnk.getElementsByTagName('span')[0];

var td = lnk.parentNode; var tr = td.parentNode; var column = td.cellIndex;

var table = tr.parentNode; while (table && !(table.tagName && table.tagName.toLowerCase == 'table')) table = table.parentNode; if (!table) return;

// Work out a type for the column if (table.rows.length <= 1) return;

// Skip the first row if that's where the headings are var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1);

var itm = ""; for (var i = rowStart; i < table.rows.length; i++) { if (table.rows[i].cells.length > column) { itm = ts_getInnerText(table.rows[i].cells[column]); itm = itm.replace(/^[\s\xa0]+/, "").replace(/[\s\xa0]+$/, ""); if (itm != "") break; }	}

sortfn = ts_sort_caseinsensitive; itmns = removeSpaces(itm); if (itmns.match(/^[\d\.\,\-\+]+\%?$/)) sortfn = ts_sort_numeric; if (itmns.match(/^[\d\.\,\-\+]+[eE][\d\-\+]+\%?$/)) sortfn = ts_sort_numeric; if (itmns.match(/^[\d\.\,\-\+]+e[\d\-\+]+\u00d710[\d\-\+]+\%?$/)) sortfn = ts_sort_numeric; if (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/)) sortfn = ts_sort_date; if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/)) sortfn = ts_sort_date; if (itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/)) sortfn = ts_sort_date; if (itm.match(/^[\u00a3$\u20ac\u00a5]/)) // pound dollar euro yen sortfn = ts_sort_currency; if (itm.match(/sm=c$/)) sortfn = ts_sort_currency; if (itm.match(/sm=d$/)) sortfn = ts_sort_date; if (itm.match(/sm=n$/)) sortfn = ts_sort_numeric;

var reverse = (span.getAttribute("sortdir") == 'down');

var newRows = new Array; for (var j = rowStart; j < table.rows.length; j++) { var row = table.rows[j]; var keyText = ts_getInnerText(row.cells[column]); var oldIndex = (reverse ? -j : j);

newRows[newRows.length] = new Array(row, keyText, oldIndex); }

newRows.sort(sortfn);

var arrowHTML; if (reverse) { arrowHTML = ''; newRows.reverse; span.setAttribute('sortdir','up'); } else { arrowHTML = ''; span.setAttribute('sortdir','down'); }

// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones // don't do sortbottom rows for (var i = 0; i < newRows.length; i++) { if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") == -1) table.tBodies[0].appendChild(newRows[i][0]); }	// do sortbottom rows only for (var i = 0; i < newRows.length; i++) { if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") != -1) table.tBodies[0].appendChild(newRows[i][0]); }

// Delete any other arrows there may be showing var spans = getElementsByClassName(tr, "span", "sortarrow"); for (var i = 0; i < spans.length; i++) { spans[i].innerHTML = ''; }	span.innerHTML = arrowHTML;

ts_alternate(table); }

function ts_dateToSortKey(date) { // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX if (date.length > 10) { switch (date.substr(3,3).toLowerCase) { case "jan": var month = "01"; break; case "feb": var month = "02"; break; case "mar": var month = "03"; break; case "apr": var month = "04"; break; case "may": var month = "05"; break; case "jun": var month = "06"; break; case "jul": var month = "07"; break; case "aug": var month = "08"; break; case "sep": var month = "09"; break; case "oct": var month = "10"; break; case "nov": var month = "11"; break; case "dec": var month = "12"; break; // default: var month = "00"; }		return date.substr(7,4)+month+date.substr(0,2); } else if (date.length == 10) { return date.substr(6,4)+date.substr(3,2)+date.substr(0,2); } else if (date.length == 8) { yr = date.substr(6,2); if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }			return yr+date.substr(3,2)+date.substr(0,2); }	return "00000000"; }

function ts_sort_currency(a,b) { var aa = ts_parseFloat_ks(a[1].replace(/[\u00a3$\u20ac\u00a5a-zA-Z]/g,'')); var bb = ts_parseFloat_ks(b[1].replace(/[\u00a3$\u20ac\u00a5a-zA-Z]/g,'')); return (aa != bb ? aa - bb : a[2] - b[2]); }

/** Title rewrite ******************************************************** * Rewrites the page's title, used by Template:Title * By Sikon */

function rewriteTitle {  if(typeof(SKIP_TITLE_REWRITE) != 'undefined' && SKIP_TITLE_REWRITE) return;

var titleDiv = document.getElementById('title-meta');

if(titleDiv == null || titleDiv == undefined) return;

var cloneNode = titleDiv.cloneNode(true); var firstHeading = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0]; var node = firstHeading.childNodes[0];

// new, then old! firstHeading.replaceChild(cloneNode, node); cloneNode.style.display = "inline";

var titleAlign = document.getElementById('title-align'); firstHeading.style.textAlign = titleAlign.childNodes[0].nodeValue; }

addOnloadHook(rewriteTitle, false);

/* Preloaded stuff */ document.write(' ');

function fillPreloads {   var div = document.getElementById("lf-preload");

if(div == null) return;

div.style.display = 'block'; var span = document.getElementById('lf-preload-cbox');

var comboString = ""; comboString += " "; span.innerHTML = comboString; span = document.getElementById('lf-preload-pagename'); span.innerHTML = ''; span = document.getElementById('lf-preload-button'); span.innerHTML = '';

requestComboFill('stdPreloads', "Template:Stdpreloads"); }

function doCustomPreload {   doPreload(document.getElementById('lf-preload-pagename').getElementsByTagName('input')[0].value); }

function onPreloadChange {   var combo = document.getElementById("stdPreloads"); var value = combo.options[combo.selectedIndex].value;

if(value == "") return;

value = "Template:" + value + "/preload"; value = value.replace(" ", "_"); doPreload(value); }

addOnloadHook(fillPreloads);

requestComboFill('combo-id', 'Template:Stdsummaries');

if(document.getElementById('editform')) {   var div = document.createElement('div'); div.appendChild(document.createTextNode('Standard summaries: ')); var select = document.createElement('select'); select.id = 'std-summaries'; div.appendChild(select); document.getElementById('editform').insertBefore(div, document.getElementById('wpSummaryLabel')); requestComboFill('std-summaries', 'Template:Stdsummaries'); } /* */