From 8bcc07326d26ad4f2e9b826d2b05a67e97acd3e5 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 19 Apr 2013 10:22:37 -0700 Subject: [PATCH] avoid global names --- war/src/main/webapp/scripts/sortable.js | 587 ++++++++++++------------ 1 file changed, 302 insertions(+), 285 deletions(-) diff --git a/war/src/main/webapp/scripts/sortable.js b/war/src/main/webapp/scripts/sortable.js index a640d9be0c..d810d23ffd 100644 --- a/war/src/main/webapp/scripts/sortable.js +++ b/war/src/main/webapp/scripts/sortable.js @@ -42,323 +42,340 @@ The script guesses the table data, and try to use the right sorting algorithm. But you can override this behavior by having 'data="..."' attribute on each row, in which case the sort will be done on that field. */ -function ts_makeSortable(table) { - var firstRow; - if (table.rows && table.rows.length > 0) { - firstRow = table.rows[0]; - } - if (!firstRow) return; - // We have a first row: assume it's the header, and make its contents clickable links - for (var i=0;i 0) { + firstRow = table.rows[0]; + } + if (!firstRow) return; - var initialSortDir = arrowTable[cell.getAttribute("initialSortDir")]; - if(initialSortDir==null) initialSortDir=arrowTable.none; - cell.innerHTML = ''+txt+ - ''+initialSortDir.text+''; + // We have a first row: assume it's the header, and make its contents clickable links + for (var i=0;i'+initialSortDir.text+''; + cell.firstChild.onclick = function () { + ts_resortTable(this); + return false; + } - if(initialSortDir!=arrowTable.none) - cell.firstChild.lastChild.sortdir = initialSortDir; + if(initialSortDir!=arrowTable.none) + cell.firstChild.lastChild.sortdir = initialSortDir; + } + ts_loadDirection(table); + ts_refresh(table); } - ts_loadDirection(table); - ts_refresh(table); -} - -function ts_getInnerText(el) { - if (typeof el == "string") return el; - if (typeof el == "undefined") { return el }; - if (el.innerText) return el.innerText; //Not needed but it is faster - var str = ""; - - var cs = el.childNodes; - var l = cs.length; - for (var i = 0; i < l; i++) { - switch (cs[i].nodeType) { - case 1: //ELEMENT_NODE - str += ts_getInnerText(cs[i]); - break; - case 3: //TEXT_NODE - str += cs[i].nodeValue; - break; - } - } - return str; -} - -// extract data for sorting from a cell -function extractData(x) { - if(x==null) return ''; - var data = x.getAttribute("data"); - if(data!=null) - return data; - return ts_getInnerText(x); -} -var arrowTable = { - up: { - id: "up", - text: "  ↑" - }, - down: { - id: "down", - text: "  ↓" - }, - none: { - id: "none", - text: "   " - }, - lnkRef: null -} - -arrowTable.up.next = arrowTable.down; -arrowTable.down.next = arrowTable.up; - -function ts_resortTable(lnk) { - // get the span - var span = lnk.lastChild; - var spantext = ts_getInnerText(span); - var th = lnk.parentNode; - var column = th.cellIndex; - var table = getParent(th,'TABLE'); - - // Work out a type for the column - if (table.rows.length <= 1) return; - var itm = extractData(table.rows[1].cells[column]).trim(); - var sortfn = ts_sort_caseinsensitive; - 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(/^[�$]/)) sortfn = ts_sort_currency; - if (itm.match(/^-?[\d\.]+$/)) sortfn = ts_sort_numeric; - var firstRow = new Array(); - var newRows = new Array(); - for (i=0;i 0) { - firstRow = table.rows[0]; + var sortfn2 = sortfn; + if(dir === arrowTable.up) { + // ascending + sortfn2 = function(a,b){return -sortfn(a,b)}; } - if (!firstRow) { - return; + newRows.sort(function(a,b) { + return sortfn2( + extractData(a.cells[column]), + extractData(b.cells[column])); + }); + + arrowTable.lnkRef = lnk; // make column sort down only if column selected is same as last + span.sortdir = dir; + + // 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 0) { + firstRow = table.rows[0]; + } + if (!firstRow) { + return; + } + for (var i=0;i