Difference between revisions of "MediaWiki:Common.js"
m |
m |
||
Line 11: | Line 11: | ||
})(); | })(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
/** | /** | ||
* Code for sortable tables. | * Code for sortable tables. | ||
*/ | */ | ||
− | var upArrow = | + | var upArrow = "images/5/5b/Sort_up.gif"; |
− | var downArrow = | + | var downArrow = "images/7/7a/Sort_down.gif"; |
− | var noneArrow = | + | var noneArrow = "images/e/e8/Sort_none.gif"; |
function sortTablesInit() { | function sortTablesInit() { | ||
− | + | var idnum = 0; | |
− | + | var tables = getElementsByClassName(document, "table", "sorts"); | |
− | + | ||
− | + | for (var i = 0; i < tables.length; i++) { | |
− | + | if (!tables[i].id) { | |
− | + | tables[i].setAttribute("id", "sortable_table_id_" + idnum); | |
− | + | ++idnum; | |
− | + | } | |
− | + | makeSortable(tables[i]); | |
+ | } | ||
} | } | ||
function makeSortable(table) { | function makeSortable(table) { | ||
− | + | var firstRow; | |
− | + | if (table.rows && table.rows.length > 0) { | |
− | + | if (table.tHead && table.tHead.rows.length > 0) { | |
− | + | firstRow = table.tHead.rows[table.tHead.rows.length - 1]; | |
− | + | } else { | |
− | + | firstRow = table.rows[0]; | |
− | + | } | |
− | + | } | |
− | + | if (!firstRow) { | |
− | + | return; | |
− | + | } | |
− | + | for (var i = 0; i < firstRow.cells.length; i++) { | |
− | + | var cell = firstRow.cells[i]; | |
− | + | if ((" "+cell.className+" ").indexOf(" unsortable ") == -1) { | |
− | + | cell.innerHTML += ' <a href="#" class="sortheader" onclick="ts_resortTable(this);return false;"><span class="sortarrow"><img src="'+ ts_image_path + ts_image_none + '" alt="↓"/></span></a>'; | |
− | + | } | |
− | + | } | |
− | + | alternate(table); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
function getInnerText(el) { | function getInnerText(el) { | ||
− | + | if (typeof el == "string" || typeof el = "undefined") { | |
− | + | return el; | |
− | + | } | |
− | + | if (el.textContent) { | |
− | + | return el.textContent; | |
− | + | } | |
− | + | if (el.innerText) { | |
− | + | return el.innerText; | |
− | + | } | |
− | + | var innerText = ""; | |
− | + | var cs = el.childNodes; | |
− | + | var l = cs.length; | |
− | + | for (var i = 0; i < l; i++) { | |
− | + | switch (cs[i].nodeType) { | |
− | + | case 1: innerText += getInnerText(cs[i]); break; | |
− | + | case 3: innerText += cs[i].nodeValue; break; | |
− | + | } | |
− | + | } | |
− | + | return innerText; | |
− | + | ||
} | } | ||
function reSortTable(lnk) { | function reSortTable(lnk) { | ||
− | + | 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; | |
− | + | } | |
− | + | if (table.rows.length <= 1) { | |
− | + | return; | |
− | + | } | |
− | + | 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 = 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]/)) { | |
− | + | sortfn = ts_sort_currency; | |
− | + | } | |
− | + | if (itm.match(/^[\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 = 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="'+ downArrow + '" alt="↓" />'; | |
− | + | newRows.reverse(); | |
− | + | span.setAttribute('sortdir','up'); | |
− | + | } else { | |
+ | arrowHTML = '<img src="'+ upArrow + '" alt="↑" />'; | ||
+ | span.setAttribute('sortdir','down'); | ||
+ | } | ||
− | + | for (var i = 0; i < newRows.length; i++) { | |
− | + | if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") == -1) { | |
− | + | table.tBodies[0].appendChild(newRows[i][0]); | |
− | + | } | |
− | + | } | |
− | + | for (var i = 0; i < newRows.length; i++) { | |
+ | if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") != -1) { | ||
+ | table.tBodies[0].appendChild(newRows[i][0]); | ||
+ | } | ||
+ | } | ||
− | + | var spans = getElementsByClassName(tr, "span", "sortarrow"); | |
− | + | ||
− | + | ||
− | + | for (var i = 0; i < spans.length; i++) { | |
− | + | spans[i].innerHTML = '<img src="'+ noneArrow + '" alt="↓"/>'; | |
+ | } | ||
+ | |||
+ | span.innerHTML = arrowHTML; | ||
+ | alternate(table); | ||
} | } | ||
− | function dateToSortKey(date) { | + | function dateToSortKey(date) { |
− | + | if (date.length == 11) { | |
− | + | 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; | |
− | + | } | |
− | + | return date.substr(7, 4) + month + date.substr(0, 2); | |
− | + | } else if (date.length == 10) { | |
− | + | if (ts_europeandate == false) { | |
− | + | return date.substr(6, 4) + date.substr(0, 2) + date.substr(3, 2); | |
− | + | } else { | |
− | + | 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(0, 2) + date.substr(3, 2); | ||
+ | } | ||
+ | return "00000000"; | ||
} | } | ||
function parseFloat(num) { | function parseFloat(num) { | ||
− | + | if (!num) { | |
− | + | return 0; | |
− | + | } | |
− | + | num = parseFloat(num.replace(/,/g, "")); | |
− | + | return (isNaN(num) ? 0 : num); | |
− | + | ||
− | + | ||
} | } | ||
function sortDate(a, b) { | function sortDate(a, b) { | ||
− | + | var aa = ts_dateToSortKey(a[1]); | |
− | + | var bb = ts_dateToSortKey(b[1]); | |
− | + | return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]); | |
} | } | ||
function sortCurrency(a, b) { | function sortCurrency(a, b) { | ||
− | + | var aa = ts_parseFloat(a[1].replace(/[^0-9.]/g, "")); | |
− | + | var bb = ts_parseFloat(b[1].replace(/[^0-9.]/g, "")); | |
− | + | return (aa != bb ? aa - bb : a[2] - b[2]); | |
} | } | ||
function sortNumeric(a, b) { | function sortNumeric(a, b) { | ||
− | + | var aa = ts_parseFloat(a[1]); | |
− | + | var bb = ts_parseFloat(b[1]); | |
− | + | return (aa != bb ? aa - bb : a[2] - b[2]); | |
} | } | ||
function sortCaseInsensitive(a, b) { | function sortCaseInsensitive(a, b) { | ||
− | + | var aa = a[1].toLowerCase(); | |
− | + | var bb = b[1].toLowerCase(); | |
− | + | return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]); | |
} | } | ||
function sortDefault(a, b) { | function sortDefault(a, b) { | ||
− | + | return (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2]); | |
} | } | ||
function alternate(table) { | function alternate(table) { | ||
− | + | var tableBodies = table.getElementsByTagName("tbody"); | |
− | + | for (var i = 0; i < tableBodies.length; i++) { | |
− | + | var tableRows = tableBodies[i].getElementsByTagName("tr"); | |
− | + | for (var j = 0; j < tableRows.length; j++) { | |
− | + | var oldClasses = tableRows[j].className.split(" "); | |
− | + | var newClassName = ""; | |
− | + | for (var k = 0; k < oldClasses.length; k++) { | |
− | + | if (oldClasses[k] != "" && oldClasses[k] != "even" && oldClasses[k] != "odd") | |
− | + | newClassName += oldClasses[k] + " "; | |
− | + | } | |
− | + | tableRows[j].className = newClassName + (j % 2 == 0 ? "even" : "odd"); | |
− | + | } | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
addOnloadHook(sortTablesInit); | addOnloadHook(sortTablesInit); |
Revision as of 07:58, 30 December 2009
/* Any JavaScript here will be loaded for all users on every page load. */ /** * Tests if a particular element possesses a class. */ var hasClass = (function () { var reCache = {}; return function (element, className) { return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className); }; })(); /** * Code for sortable tables. */ var upArrow = "images/5/5b/Sort_up.gif"; var downArrow = "images/7/7a/Sort_down.gif"; var noneArrow = "images/e/e8/Sort_none.gif"; function sortTablesInit() { var idnum = 0; var tables = getElementsByClassName(document, "table", "sorts"); for (var i = 0; i < tables.length; i++) { if (!tables[i].id) { tables[i].setAttribute("id", "sortable_table_id_" + idnum); ++idnum; } makeSortable(tables[i]); } } function makeSortable(table) { var firstRow; if (table.rows && table.rows.length > 0) { if (table.tHead && table.tHead.rows.length > 0) { firstRow = table.tHead.rows[table.tHead.rows.length - 1]; } else { firstRow = table.rows[0]; } } if (!firstRow) { return; } for (var i = 0; i < firstRow.cells.length; i++) { var cell = firstRow.cells[i]; if ((" "+cell.className+" ").indexOf(" unsortable ") == -1) { cell.innerHTML += ' <a href="#" class="sortheader" onclick="ts_resortTable(this);return false;"><span class="sortarrow"><img src="'+ ts_image_path + ts_image_none + '" alt="↓"/></span></a>'; } } alternate(table); } function getInnerText(el) { if (typeof el == "string" || typeof el = "undefined") { return el; } if (el.textContent) { return el.textContent; } if (el.innerText) { return el.innerText; } var innerText = ""; var cs = el.childNodes; var l = cs.length; for (var i = 0; i < l; i++) { switch (cs[i].nodeType) { case 1: innerText += getInnerText(cs[i]); break; case 3: innerText += cs[i].nodeValue; break; } } return innerText; } function reSortTable(lnk) { 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; } if (table.rows.length <= 1) { return; } 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 = 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]/)) { sortfn = ts_sort_currency; } if (itm.match(/^[\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 = 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="'+ downArrow + '" alt="↓" />'; newRows.reverse(); span.setAttribute('sortdir','up'); } else { arrowHTML = '<img src="'+ upArrow + '" alt="↑" />'; span.setAttribute('sortdir','down'); } for (var i = 0; i < newRows.length; i++) { if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") == -1) { table.tBodies[0].appendChild(newRows[i][0]); } } for (var i = 0; i < newRows.length; i++) { if ((" "+newRows[i][0].className+" ").indexOf(" sortbottom ") != -1) { table.tBodies[0].appendChild(newRows[i][0]); } } var spans = getElementsByClassName(tr, "span", "sortarrow"); for (var i = 0; i < spans.length; i++) { spans[i].innerHTML = '<img src="'+ noneArrow + '" alt="↓"/>'; } span.innerHTML = arrowHTML; alternate(table); } function dateToSortKey(date) { if (date.length == 11) { 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; } return date.substr(7, 4) + month + date.substr(0, 2); } else if (date.length == 10) { if (ts_europeandate == false) { return date.substr(6, 4) + date.substr(0, 2) + date.substr(3, 2); } else { 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(0, 2) + date.substr(3, 2); } return "00000000"; } function parseFloat(num) { if (!num) { return 0; } num = parseFloat(num.replace(/,/g, "")); return (isNaN(num) ? 0 : num); } function sortDate(a, b) { var aa = ts_dateToSortKey(a[1]); var bb = ts_dateToSortKey(b[1]); return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]); } function sortCurrency(a, b) { var aa = ts_parseFloat(a[1].replace(/[^0-9.]/g, "")); var bb = ts_parseFloat(b[1].replace(/[^0-9.]/g, "")); return (aa != bb ? aa - bb : a[2] - b[2]); } function sortNumeric(a, b) { var aa = ts_parseFloat(a[1]); var bb = ts_parseFloat(b[1]); return (aa != bb ? aa - bb : a[2] - b[2]); } function sortCaseInsensitive(a, b) { var aa = a[1].toLowerCase(); var bb = b[1].toLowerCase(); return (aa < bb ? -1 : aa > bb ? 1 : a[2] - b[2]); } function sortDefault(a, b) { return (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2]); } function alternate(table) { var tableBodies = table.getElementsByTagName("tbody"); for (var i = 0; i < tableBodies.length; i++) { var tableRows = tableBodies[i].getElementsByTagName("tr"); for (var j = 0; j < tableRows.length; j++) { var oldClasses = tableRows[j].className.split(" "); var newClassName = ""; for (var k = 0; k < oldClasses.length; k++) { if (oldClasses[k] != "" && oldClasses[k] != "even" && oldClasses[k] != "odd") newClassName += oldClasses[k] + " "; } tableRows[j].className = newClassName + (j % 2 == 0 ? "even" : "odd"); } } } addOnloadHook(sortTablesInit);