sorttable.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* $Id: sorttable.js 2912 2007-04-15 20:57:26Z warion $ */
  2. addEvent(window, "load", sortables_init);
  3. var SORT_COLUMN_INDEX;
  4. function sortables_init() {
  5. // Find all tables with class sortable and make them sortable
  6. if (!document.getElementsByTagName) return;
  7. tbls = document.getElementsByTagName("table");
  8. for (ti=0;ti<tbls.length;ti++) {
  9. thisTbl = tbls[ti];
  10. if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
  11. //initTable(thisTbl.id);
  12. ts_makeSortable(thisTbl);
  13. }
  14. }
  15. }
  16. function ts_makeSortable(table) {
  17. if (table.rows && table.rows.length > 0) {
  18. var firstRow = table.rows[0];
  19. }
  20. if (!firstRow) return;
  21. // We have a first row: assume it's the header, and make its contents clickable links
  22. // b4rt :
  23. // not first column.. client cant sort on server-file-date
  24. // not last column.. admin-buttons
  25. //for (var i=0;i<firstRow.cells.length;i++) {
  26. for (var i=1;i<firstRow.cells.length-1;i++) {
  27. var cell = firstRow.cells[i];
  28. var txt = ts_getInnerText(cell);
  29. cell.innerHTML = '<a href="#" class="sortheader" '+
  30. 'onclick="ts_resortTable(this, '+i+');return false;">' +
  31. txt+'<span class="sortarrow">&nbsp;&nbsp;&nbsp;</span></a>';
  32. }
  33. }
  34. function ts_getInnerText(el) {
  35. if (typeof el == "string") return el;
  36. if (typeof el == "undefined") { return el };
  37. if (el.innerText) return el.innerText; //Not needed but it is faster
  38. var str = "";
  39. var cs = el.childNodes;
  40. var l = cs.length;
  41. for (var i = 0; i < l; i++) {
  42. switch (cs[i].nodeType) {
  43. case 1: //ELEMENT_NODE
  44. str += ts_getInnerText(cs[i]);
  45. break;
  46. case 3: //TEXT_NODE
  47. str += cs[i].nodeValue;
  48. break;
  49. }
  50. }
  51. return str;
  52. }
  53. function ts_resortTable(lnk,clid) {
  54. // get the span
  55. var span;
  56. for (var ci=0;ci<lnk.childNodes.length;ci++) {
  57. if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
  58. }
  59. var spantext = ts_getInnerText(span);
  60. var td = lnk.parentNode;
  61. var column = clid || td.cellIndex;
  62. var table = getParent(td,'TABLE');
  63. // Work out a type for the column
  64. if (table.rows.length <= 1) return;
  65. for (var ri=1;ri<table.rows.length;ri++) {
  66. // Find first row with content (some transfers only contain whitespace in some columns).
  67. var itm = ts_getInnerText(table.rows[ri].cells[column]);
  68. if (itm.match(/^\s*$/)) continue;
  69. sortfn = ts_sort_caseinsensitive;
  70. if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
  71. if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
  72. if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
  73. if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
  74. // b4rt : next 3 regexps are copy-pasted from azureus webui
  75. if (itm.match(/^[\d\.]+( | k| M| G| T)B$/)) sortfn = ts_sort_size;
  76. if (itm.match(/^[\d\.]+( | k| M| G| T)B\/s$/)) sortfn = ts_sort_speed;
  77. if (itm.match(/[\d\.]+%/) || itm.match(/N\/A/)) sortfn = ts_sort_percent;
  78. break;
  79. }
  80. SORT_COLUMN_INDEX = column;
  81. var firstRow = new Array();
  82. var newRows = new Array();
  83. for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
  84. for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
  85. newRows.sort(sortfn);
  86. if (span.getAttribute("sortdir") == 'down') {
  87. ARROW = '&nbsp;&nbsp;&uarr;';
  88. newRows.reverse();
  89. span.setAttribute('sortdir','up');
  90. } else {
  91. ARROW = '&nbsp;&nbsp;&darr;';
  92. span.setAttribute('sortdir','down');
  93. }
  94. // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
  95. // don't do sortbottom rows
  96. for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
  97. // do sortbottom rows only
  98. for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
  99. // Delete any other arrows there may be showing
  100. var allspans = document.getElementsByTagName("span");
  101. for (var ci=0;ci<allspans.length;ci++) {
  102. if (allspans[ci].className == 'sortarrow') {
  103. if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
  104. allspans[ci].innerHTML = '&nbsp;&nbsp;&nbsp;';
  105. }
  106. }
  107. }
  108. span.innerHTML = ARROW;
  109. }
  110. function getParent(el, pTagName) {
  111. if (el == null) return null;
  112. else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
  113. return el;
  114. else
  115. return getParent(el.parentNode, pTagName);
  116. }
  117. function ts_sort_date(a,b) {
  118. // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
  119. aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  120. bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  121. if (aa.length == 10) {
  122. dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
  123. } else {
  124. yr = aa.substr(6,2);
  125. if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
  126. dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
  127. }
  128. if (bb.length == 10) {
  129. dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
  130. } else {
  131. yr = bb.substr(6,2);
  132. if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
  133. dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
  134. }
  135. if (dt1==dt2) return 0;
  136. if (dt1<dt2) return -1;
  137. return 1;
  138. }
  139. function ts_sort_currency(a,b) {
  140. aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
  141. bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
  142. return parseFloat(aa) - parseFloat(bb);
  143. }
  144. function ts_sort_numeric(a,b) {
  145. aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
  146. if (isNaN(aa)) aa = 0;
  147. bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
  148. if (isNaN(bb)) bb = 0;
  149. return aa-bb;
  150. }
  151. function ts_sort_caseinsensitive(a,b) {
  152. aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
  153. bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
  154. if (aa==bb) return 0;
  155. if (aa<bb) return -1;
  156. return 1;
  157. }
  158. function ts_sort_default(a,b) {
  159. aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  160. bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  161. if (aa==bb) return 0;
  162. if (aa<bb) return -1;
  163. return 1;
  164. }
  165. // b4rt : function copy-pasted from azureus webui and slightly modified
  166. function ts_sort_size(a,b) {
  167. a_unit = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  168. b_unit = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  169. if (a_unit.match(/B$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''));
  170. if (a_unit.match(/kB$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1024;
  171. if (a_unit.match(/MB$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1048576;
  172. if (a_unit.match(/GB$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1073741824;
  173. if (a_unit.match(/TB$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1099511627776;
  174. if (b_unit.match(/B$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''));
  175. if (b_unit.match(/kB$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1024;
  176. if (b_unit.match(/MB$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1048576;
  177. if (b_unit.match(/GB$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1073741824;
  178. if (b_unit.match(/TB$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1099511627776;
  179. return aa-bb;
  180. }
  181. // b4rt : function copy-pasted from azureus webui and slightly modified
  182. function ts_sort_speed(a,b) {
  183. a_unit = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  184. b_unit = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  185. if (a_unit.match(/B\/s$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''));
  186. if (a_unit.match(/kB\/s$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1024;
  187. if (a_unit.match(/MB\/s$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1048576;
  188. if (a_unit.match(/GB\/s$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1073741824;
  189. if (a_unit.match(/TB\/s$/)) aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1099511627776;
  190. if (b_unit.match(/B\/s$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''));
  191. if (b_unit.match(/kB\/s$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1024;
  192. if (b_unit.match(/MB\/s$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1048576;
  193. if (b_unit.match(/GB\/s$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1073741824;
  194. if (b_unit.match(/TB\/s$/)) bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'')) * 1099511627776;
  195. return aa-bb;
  196. }
  197. // b4rt : function copy-pasted from azureus webui and slightly modified
  198. function ts_sort_percent(a,b) {
  199. a_content = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
  200. b_content = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
  201. if (a_content.match(/N\/A/) && b_content.match(/N\/A/)) return 0;
  202. if (a_content.match(/N\/A/) && !b_content.match(/N\/A/)) return -1;
  203. if (!a_content.match(/N\/A/) && b_content.match(/N\/A/)) return 1;
  204. ap = a_content.indexOf('%');
  205. bp = b_content.indexOf('%');
  206. if (ap == -1 && bp == -1) return 0;
  207. if (ap == -1 && bp != -1) return -1;
  208. if (ap != -1 && bp == -1) return 1;
  209. a1 = a_content.substring(0,ap);
  210. b1 = b_content.substring(0,bp);
  211. aa = parseFloat(a1.replace(/[^0-9.]/g,''));
  212. bb = parseFloat(b1.replace(/[^0-9.]/g,''));
  213. return aa-bb;
  214. }
  215. function addEvent(elm, evType, fn, useCapture)
  216. // addEvent and removeEvent
  217. // cross-browser event handling for IE5+, NS6 and Mozilla
  218. // By Scott Andrew
  219. {
  220. if (elm.addEventListener){
  221. elm.addEventListener(evType, fn, useCapture);
  222. return true;
  223. } else if (elm.attachEvent){
  224. var r = elm.attachEvent("on"+evType, fn);
  225. return r;
  226. } else {
  227. alert("Handler could not be removed");
  228. }
  229. }