MediaWiki:Common.js: Difference between revisions
Content deleted Content added
Pathoschild (talk | contribs) m tweak |
Pathoschild (talk | contribs) m rv (very clumsy implementation, will write a better version) |
||
Line 108: | Line 108: | ||
} |
} |
||
/************ |
|||
** Show/hide boxes (unknown authors, from [[w:MediaWiki:Common.js]]) |
|||
** - useful for hiding extensive technical data on MediaWiki talk pages, etc. |
|||
************/ |
|||
// set up the captions |
|||
var NavigationBarHide = '[hide]'; |
|||
var NavigationBarShow = '[show]'; |
|||
// 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 < div >-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 ); |
|||
/*</pre>*/ |
/*</pre>*/ |
Revision as of 01:26, 20 April 2007
/* Any JavaScript here will be loaded for all users on every page load. */
/*<pre>*/
//Fix bug in http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/skins/common/wikibits.js :
//for the sorting order, take the whole number, not the number before the second comma.
//Also, remove spaces.
function ts_parseFloat(num) {
if (!num) return 0;
num = removeSpaces(num);
num = parseFloat(num.replace(/,/g, ""));
return (isNaN(num) ? 0 : num);
}
function removeSpaces(string) {
var tstring = "";
string = '' + string;
splitstring = string.split(" ");
for(i = 0; i < splitstring.length; i++)
tstring += splitstring[i];
return tstring;
}
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;
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;
itm = removeSpaces(itm);
if (itm.match(/^[\d\.\,\-\+]+\%?$/)) sortfn = ts_sort_numeric;
if (itm.match(/^[\d\.\,\-\+]+[eE][\d\-\+]+\%?$/)) sortfn = ts_sort_numeric;
if (itm.match(/^[\d\.\,\-\+]+e[\d\-\+]+\u00d710[\d\-\+]+\%?$/)) 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 = '<img src="'+ ts_image_path + ts_image_down + '" alt="↓"/>';
newRows.reverse();
span.setAttribute('sortdir','up');
} else {
arrowHTML = '<img src="'+ ts_image_path + ts_image_up + '" alt="↑"/>';
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 = '<img src="'+ ts_image_path + ts_image_none + '" alt="↓"/>';
}
span.innerHTML = arrowHTML;
ts_alternate(table);
}
/*</pre>*/