User:Alex Smotrov/mw/sortable

From Wikipedia, the free encyclopedia

Contents

Possible improvements to Sortable Tables Javascript code used by MediaWiki.


[edit] Fix: remove alternate rows code

This code marks table rows with classes "odd" and "even":

  • It is apparently not used by anyone
    it could be used by introducing another class into site CSS, something like table.alternate tr.even {background-color: #F5F5F5}
  • It executes on the page load, slowing loading time

The code in question is:

  • var ts_alternate_row_colors = true;
  • function ts_alternate(table) { ...
  • and two calls to this function


[edit] Simplification: remove THEAD check

As MediaWiki does not support THEAD, nor through wiki-syntax, neither as HTML tag.

function ts_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;

can be replaced with

function ts_makeSortable(table) {
        if (!table.rows || table.rows.length == 0) return
        var firstRow = table.rows[0]

and the line

var rowStart = (table.tHead && table.tHead.rows.length > 0 ? 0 : 1);

in function ts_resortTable(lnk) can be replaced with

var rowStart = 1


[edit] Simplification: month conversion

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 == 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;
                        // default: var month = "00";
                }

can be replaced with

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 == 11) {
                var month = date.substr(3,3).toLowerCase()
                month = "janfebmaraprmayjunjulaugsepoctnovdec".indexOf(month)/3 + 1
                if (month == -1) month = "00"
                else if (month<10) month = "0" + month


[edit] Proposal: input date format localization

Sortable Tables code determines the sorting mode by analyzing the top non-empty cell. The first RegExp used for possible dates is

if (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/))

Proposal: accept arbitrary months names, something like

if (itm.match(/^\d\d[\/. -][^\d ]{1,12}[\/. -]\d\d\d\d$/))

and then introduce another parameter ts_months_names, which is set in local Common.js and is equivalent to English "janfebmaraprmayjunjulaugsepoctnovdec"

Then the function ts_dateToSortKey (see above section) is modified to use ts_months_names if month name was not found in the English string of month names.


[edit] Proposal: comma/dot localization

The JavaScript hack

//fix for sortable tables: comma as decimal dot
function ts_parseFloat(num){
 if (!num) return 0
 num = parseFloat(num.replace(/\./g, '').replace(/,/, '.'))
 return (isNaN(num) ? 0 : num)
}

which can be found in de:MediaWiki:Common.js or ru:MediaWiki:Common.js, should be replaced with another configuration variable set in local Common.js.


Of course, if MediaWiki can automatically set this parameter depending on wgContentLanguage, this would be even better (this applies to the previous proposal as well).