|
|
Line 109: |
Line 109: |
| | | |
| addOnloadHook(createCollapseButtons); | | addOnloadHook(createCollapseButtons); |
− |
| |
− | /**
| |
− | * 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';
| |
− | var numberTransformTable = null;
| |
− | var numberRegex = null;
| |
− |
| |
− | 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="reSortTable(this);return false;">'
| |
− | + '<span class="sortarrow">'
| |
− | + '<img src="' + noneArrow + '" alt="↓"/></span></a>';
| |
− | }
| |
− | }
| |
− | }
| |
− |
| |
− | 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;
| |
− | }
| |
− |
| |
− | if (numberTransformTable == null) {
| |
− | initTransformTable();
| |
− | }
| |
− |
| |
− | 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;
| |
− | }
| |
− | }
| |
− | }
| |
− |
| |
− | var sortfn = sortGeneric;
| |
− | var preprocessor = ts_toLowerCase;
| |
− |
| |
− | if (/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/.test(itm)) {
| |
− | preprocessor = dateToSortKey;
| |
− | } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/.test(itm)) {
| |
− | preprocessor = dateToSortKey;
| |
− | } else if (/^\d\d[\/.-]\d\d[\/.-]\d\d$/.test(itm)) {
| |
− | preprocessor = dateToSortKey;
| |
− | } else if (/(^[\u00a3$\u20ac\u00a4\u00a5]|\u00a2$)/.test(itm)) {
| |
− | preprocessor = currencyToSortKey;
| |
− | } else if (numberRegex.test(itm)) {
| |
− | preprocessor = parseFloat;
| |
− | }
| |
− |
| |
− | var reverse = (span.getAttribute('sortdir') == 'down');
| |
− | var newRows = new Array();
| |
− | var staticRows = new Array();
| |
− |
| |
− | for (var j = rowStart; j < table.rows.length; j++) {
| |
− | var row = table.rows[j];
| |
− |
| |
− | if((' ' + row.className + ' ').indexOf(' unsortable ') < 0) {
| |
− | var keyText = getInnerText(row.cells[column]);
| |
− |
| |
− | if(keyText == undefined) {
| |
− | keyText = '';
| |
− | }
| |
− |
| |
− | var oldIndex = (reverse ? -j : j);
| |
− | var preprocessed = preprocessor( keyText.replace(/^[\s\xa0]+/, '').replace(/[\s\xa0]+$/, '') );
| |
− |
| |
− | newRows[newRows.length] = new Array(row, preprocessed, oldIndex);
| |
− | } else {
| |
− | staticRows[staticRows.length] = new Array(row, false, j - rowStart);
| |
− | }
| |
− | }
| |
− |
| |
− | 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 < staticRows.length; i++) {
| |
− | var row = staticRows[i];
| |
− |
| |
− | newRows.splice(row[2], 0, row);
| |
− | }
| |
− |
| |
− | 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;
| |
− | }
| |
− |
| |
− | function initTransformTable() {
| |
− | if (typeof wgSeparatorTransformTable == 'undefined' || (wgSeparatorTransformTable[0] == '' && wgDigitTransformTable[2] == '')) {
| |
− | digitClass = '[0-9,.]';
| |
− | numberTransformTable = false;
| |
− | } else {
| |
− | numberTransformTable = {};
| |
− | ascii = wgSeparatorTransformTable[0].split('\t');
| |
− | localised = wgSeparatorTransformTable[1].split('\t');
| |
− |
| |
− | for (var i = 0; i < ascii.length; i++) {
| |
− | numberTransformTable[localised[i]] = ascii[i];
| |
− | }
| |
− |
| |
− | ascii = wgDigitTransformTable[0].split('\t');
| |
− | localised = wgDigitTransformTable[1].split('\t');
| |
− |
| |
− | for (var i = 0; i < ascii.length; i++) {
| |
− | numberTransformTable[localised[i]] = ascii[i];
| |
− | }
| |
− |
| |
− | digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\\.'];
| |
− | maxDigitLength = 1;
| |
− |
| |
− | for (var digit in numberTransformTable) {
| |
− | digits.push(
| |
− | digit.replace( /[\\\\$\*\+\?\.\(\)\|\{\}\[\]\-]/,
| |
− | function( s ) { return '\\' + s; } )
| |
− | );
| |
− |
| |
− | if (digit.length > maxDigitLength) {
| |
− | maxDigitLength = digit.length;
| |
− | }
| |
− | }
| |
− |
| |
− | if (maxDigitLength > 1) {
| |
− | digitClass = '[' + digits.join('', digits) + ']';
| |
− | } else {
| |
− | digitClass = '(' + digits.join('|', digits) + ')';
| |
− | }
| |
− | }
| |
− |
| |
− | numberRegex = new RegExp(
| |
− | '^(' +
| |
− | '[-+\u2212]?[0-9][0-9,]*(\\.[0-9,]*)?(E[-+\u2212]?[0-9][0-9,]*)?' +
| |
− | '|' +
| |
− | '[-+\u2212]?' + digitClass + '+%?' +
| |
− | ')$', 'i'
| |
− | );
| |
− | }
| |
− |
| |
− | function ts_toLowerCase(s) {
| |
− | return s.toLowerCase();
| |
− | }
| |
− |
| |
− | 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) {
| |
− | return date.substr(6, 4) + date.substr(0, 2) + date.substr(3, 2);
| |
− | } else if (date.length == 8) {
| |
− | year = date.substr(6, 2);
| |
− |
| |
− | if (parseInt(year) < 50) {
| |
− | year = '20' + year;
| |
− | } else {
| |
− | year = '19' + year;
| |
− | }
| |
− |
| |
− | return year + date.substr(0, 2) + date.substr(3, 2);
| |
− | }
| |
− |
| |
− | return '00000000';
| |
− | }
| |
− |
| |
− | function parseFloat(s) {
| |
− | if (!s) {
| |
− | return 0;
| |
− | }
| |
− |
| |
− | if (numberTransformTable != false) {
| |
− | var newNum = '', c;
| |
− |
| |
− | for ( var p = 0; p < s.length; p++ ) {
| |
− | c = s.charAt(p);
| |
− |
| |
− | if (c in numberTransformTable) {
| |
− | newNum += numberTransformTable[c];
| |
− | } else {
| |
− | newNum += c;
| |
− | }
| |
− | }
| |
− | s = newNum;
| |
− | }
| |
− |
| |
− | num = parseFloat(s.replace(/[, ]/g, '').replace('\u2212', '-'));
| |
− |
| |
− | return (isNaN(num) ? -Infinity : num);
| |
− | }
| |
− |
| |
− | function currencyToSortKey(s) {
| |
− | return ts_parseFloat(s.replace(/[^-\u22120-9.,]/g, ''));
| |
− | }
| |
− |
| |
− | function sortGeneric(a, b) {
| |
− | return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : a[2] - b[2];
| |
− | }
| |
− |
| |
− | addOnloadHook(sortTablesInit);
| |