1/* 2 SortTable 3 version 2 4 7th April 2007 5 Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ 6 7 Instructions: 8 Download this file 9 Add <script src="sorttable.js"></script> to your HTML 10 Add class="sortable" to any table you'd like to make sortable 11 Click on the headers to sort 12 13 Thanks to many, many people for contributions and suggestions. 14 Licenced as X11: http://www.kryogenix.org/code/browser/licence.html 15 This basically means: do what you want with it. 16*/ 17 18 19var stIsIE = /*@cc_on!@*/false; 20 21sorttable = { 22 init: function () { 23 // quit if this function has already been called 24 if (arguments.callee.done) return; 25 // flag this function so we don't do the same thing twice 26 arguments.callee.done = true; 27 // kill the timer 28 if (_timer) clearInterval(_timer); 29 30 if (!document.createElement || !document.getElementsByTagName) return; 31 32 sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/; 33 34 forEach(document.getElementsByTagName('table'), function (table) { 35 if (table.className.search(/\bsortable\b/) != -1) { 36 sorttable.makeSortable(table); 37 } 38 }); 39 40 }, 41 42 makeSortable: function (table) { 43 if (table.getElementsByTagName('thead').length == 0) { 44 // table doesn't have a tHead. Since it should have, create one and 45 // put the first table row in it. 46 the = document.createElement('thead'); 47 the.appendChild(table.rows[0]); 48 table.insertBefore(the, table.firstChild); 49 } 50 // Safari doesn't support table.tHead, sigh 51 if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0]; 52 53 if (table.tHead.rows.length != 1) return; // can't cope with two header rows 54 55 // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as 56 // "total" rows, for example). This is B&R, since what you're supposed 57 // to do is put them in a tfoot. So, if there are sortbottom rows, 58 // for backwards compatibility, move them to tfoot (creating it if needed). 59 sortbottomrows = []; 60 for (var i = 0; i < table.rows.length; i++) { 61 if (table.rows[i].className.search(/\bsortbottom\b/) != -1) { 62 sortbottomrows[sortbottomrows.length] = table.rows[i]; 63 } 64 } 65 if (sortbottomrows) { 66 if (table.tFoot == null) { 67 // table doesn't have a tfoot. Create one. 68 tfo = document.createElement('tfoot'); 69 table.appendChild(tfo); 70 } 71 for (var i = 0; i < sortbottomrows.length; i++) { 72 tfo.appendChild(sortbottomrows[i]); 73 } 74 delete sortbottomrows; 75 } 76 77 // work through each column and calculate its type 78 headrow = table.tHead.rows[0].cells; 79 for (var i = 0; i < headrow.length; i++) { 80 // manually override the type with a sorttable_type attribute 81 if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col 82 mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/); 83 if (mtch) { 84 override = mtch[1]; 85 } 86 if (mtch && typeof sorttable["sort_" + override] == 'function') { 87 headrow[i].sorttable_sortfunction = sorttable["sort_" + override]; 88 } else { 89 headrow[i].sorttable_sortfunction = sorttable.guessType(table, i); 90 } 91 // make it clickable to sort 92 headrow[i].sorttable_columnindex = i; 93 headrow[i].sorttable_tbody = table.tBodies[0]; 94 dean_addEvent(headrow[i], "click", sorttable.innerSortFunction = function (e) { 95 96 if (this.className.search(/\bsorttable_sorted\b/) != -1) { 97 // if we're already sorted by this column, just 98 // reverse the table, which is quicker 99 sorttable.reverse(this.sorttable_tbody); 100 this.className = this.className.replace('sorttable_sorted', 101 'sorttable_sorted_reverse'); 102 this.removeChild(document.getElementById('sorttable_sortfwdind')); 103 sortrevind = document.createElement('span'); 104 sortrevind.id = "sorttable_sortrevind"; 105 sortrevind.innerHTML = stIsIE ? ' <font face="webdings">5</font>' : ' ▴'; 106 this.appendChild(sortrevind); 107 return; 108 } 109 if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) { 110 // if we're already sorted by this column in reverse, just 111 // re-reverse the table, which is quicker 112 sorttable.reverse(this.sorttable_tbody); 113 this.className = this.className.replace('sorttable_sorted_reverse', 114 'sorttable_sorted'); 115 this.removeChild(document.getElementById('sorttable_sortrevind')); 116 sortfwdind = document.createElement('span'); 117 sortfwdind.id = "sorttable_sortfwdind"; 118 sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾'; 119 this.appendChild(sortfwdind); 120 return; 121 } 122 123 // remove sorttable_sorted classes 124 theadrow = this.parentNode;
125 forEach(theadrow.childNodes, function (cell) { 126 if (cell.nodeType == 1) { // an element 127 cell.className = cell.className.replace('sorttable_sorted_reverse', ''); 128 cell.className = cell.className.replace('sorttable_sorted', ''); 129 } 130 }); 131 sortfwdind = document.getElementById('sorttable_sortfwdind'); 132 if (sortfwdind) { 133 sortfwdind.parentNode.removeChild(sortfwdind); 134 } 135 sortrevind = document.getElementById('sorttable_sortrevind'); 136 if (sortrevind) { 137 sortrevind.parentNode.removeChild(sortrevind); 138 } 139 140 this.className += ' sorttable_sorted'; 141 sortfwdind = document.createElement('span'); 142 sortfwdind.id = "sorttable_sortfwdind"; 143 sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾'; 144 this.appendChild(sortfwdind); 145 146 // build an array to sort. This is a Schwartzian transform thing, 147 // i.e., we "decorate" each row with the actual sort key, 148 // sort based on the sort keys, and then put the rows back in order 149 // which is a lot faster because you only do getInnerText once per row 150 row_array = []; 151 col = this.sorttable_columnindex; 152 rows = this.sorttable_tbody.rows; 153 for (var j = 0; j < rows.length; j++) { 154 row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]]; 155 } 156 /* If you want a stable sort, uncomment the following line */ 157 //sorttable.shaker_sort(row_array, this.sorttable_sortfunction); 158 /* and comment out this one */ 159 row_array.sort(this.sorttable_sortfunction); 160 161 tb = this.sorttable_tbody; 162 for (var j = 0; j < row_array.length; j++) { 163 tb.appendChild(row_array[j][1]); 164 } 165 166 delete row_array; 167 }); 168 } 169 } 170 }, 171 172 guessType: function (table, column) { 173 // guess the type of a column based on its first non-blank row 174 sortfn = sorttable.sort_alpha; 175 for (var i = 0; i < table.tBodies[0].rows.length; i++) { 176 text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]); 177 if (text != '') { 178 if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) { 179 return sorttable.sort_numeric; 180 } 181 // check for a date: dd/mm/yyyy or dd/mm/yy 182 // can have / or . or - as separator 183 // can be mm/dd as well 184 possdate = text.match(sorttable.DATE_RE) 185 if (possdate) { 186 // looks like a date 187 first = parseInt(possdate[1]); 188 second = parseInt(possdate[2]); 189 if (first > 12) { 190 // definitely dd/mm 191 return sorttable.sort_ddmm; 192 } else if (second > 12) { 193 return sorttable.sort_mmdd; 194 } else { 195 // looks like a date, but we can't tell which, so assume 196 // that it's dd/mm (English imperialism!) and keep looking 197 sortfn = sorttable.sort_ddmm; 198 } 199 } 200 } 201 } 202 return sortfn; 203 }, 204 205 getInnerText: function (node) { 206 // gets the text we want to use for sorting for a cell. 207 // strips leading and trailing whitespace. 208 // this is *not* a generic getInnerText function; it's special to sorttable. 209 // for example, you can override the cell text with a customkey attribute. 210 // it also gets .value for <input> fields. 211 212 if (!node) return ""; 213 214 hasInputs = (typeof node.getElementsByTagName == 'function') && 215 node.getElementsByTagName('input').length; 216 217 if (node.getAttribute("sorttable_customkey") != null) { 218 return node.getAttribute("sorttable_customkey"); 219 } else if (typeof node.textContent != 'undefined' && !hasInputs) { 220 return node.textContent.replace(/^\s+|\s+$/g, ''); 221 } else if (typeof node.innerText != 'undefined' && !hasInputs) { 222 return node.innerText.replace(/^\s+|\s+$/g, ''); 223 } else if (typeof node.text != 'undefined' && !hasInputs) { 224 return node.text.replace(/^\s+|\s+$/g, ''); 225 } else { 226 switch (node.nodeType) { 227 case 3: 228 if (node.nodeName.toLowerCase() == 'input') { 229 return node.value.replace(/^\s+|\s+$/g, ''); 230 } 231 case 4: 232 return node.nodeValue.replace(/^\s+|\s+$/g, ''); 233 break; 234 case 1: 235 case 11: 236 var innerText = ''; 237 for (var i = 0; i < node.childNodes.length; i++) { 238 innerText += sorttable.getInnerText(node.childNodes[i]); 239 } 240 return innerText.replace(/^\s+|\s+$/g, ''); 241 break; 242 default: 243 return ''; 244 } 245 } 246 }, 247 248 reverse: function (tbody) { 249 // reverse the rows in a tbody 250 newrows = []; 251 for (var i = 0; i < tbody.rows.length; i++) { 252 newrows[newrows.length] = tbody.rows[i]; 253 } 254 for (var i = newrows.length - 1; i >= 0; i--) { 255 tbody.appendChild(newrows[i]); 256 } 257 delete newrows; 258 }, 259 260 /* sort functions 261 each sort function takes two parameters, a and b 262 you are comparing a[0] and b[0] */ 263 sort_numeric: function (a, b) {
264 aa = parseFloat(a[0].replace(/[^0-9.-]/g, '')); 265 if (isNaN(aa)) aa = 0; 266 bb = parseFloat(b[0].replace(/[^0-9.-]/g, '')); 267 if (isNaN(bb)) bb = 0; 268 return aa - bb; 269 }, 270 sort_alpha: function (a, b) { 271 if (a[0] == b[0]) return 0; 272 if (a[0] < b[0]) return -1; 273 return 1; 274 }, 275 sort_ddmm: function (a, b) { 276 mtch = a[0].match(sorttable.DATE_RE); 277 y = mtch[3]; 278 m = mtch[2]; 279 d = mtch[1]; 280 if (m.length == 1) m = '0' + m; 281 if (d.length == 1) d = '0' + d; 282 dt1 = y + m + d; 283 mtch = b[0].match(sorttable.DATE_RE); 284 y = mtch[3]; 285 m = mtch[2]; 286 d = mtch[1]; 287 if (m.length == 1) m = '0' + m; 288 if (d.length == 1) d = '0' + d; 289 dt2 = y + m + d; 290 if (dt1 == dt2) return 0; 291 if (dt1 < dt2) return -1; 292 return 1; 293 }, 294 sort_mmdd: function (a, b) { 295 mtch = a[0].match(sorttable.DATE_RE); 296 y = mtch[3]; 297 d = mtch[2]; 298 m = mtch[1]; 299 if (m.length == 1) m = '0' + m; 300 if (d.length == 1) d = '0' + d; 301 dt1 = y + m + d; 302 mtch = b[0].match(sorttable.DATE_RE); 303 y = mtch[3]; 304 d = mtch[2]; 305 m = mtch[1]; 306 if (m.length == 1) m = '0' + m; 307 if (d.length == 1) d = '0' + d; 308 dt2 = y + m + d; 309 if (dt1 == dt2) return 0; 310 if (dt1 < dt2) return -1; 311 return 1; 312 }, 313 314 shaker_sort: function (list, comp_func) { 315 // A stable sort function to allow multi-level sorting of data 316 // see: http://en.wikipedia.org/wiki/Cocktail_sort 317 // thanks to Joseph Nahmias 318 var b = 0; 319 var t = list.length - 1; 320 var swap = true; 321 322 while (swap) { 323 swap = false; 324 for (var i = b; i < t; ++i) { 325 if (comp_func(list[i], list[i + 1]) > 0) { 326 var q = list[i]; 327 list[i] = list[i + 1]; 328 list[i + 1] = q; 329 swap = true; 330 } 331 } // for 332 t--; 333 334 if (!swap) break; 335 336 for (var i = t; i > b; --i) { 337 if (comp_func(list[i], list[i - 1]) < 0) { 338 var q = list[i]; 339 list[i] = list[i - 1]; 340 list[i - 1] = q; 341 swap = true; 342 } 343 } // for 344 b++; 345 346 } // while(swap) 347 } 348} 349 350/* ****************************************************************** 351 Supporting functions: bundled here to avoid depending on a library 352 ****************************************************************** */ 353 354// Dean Edwards/Matthias Miller/John Resig 355 356/* for Mozilla/Opera9 */ 357if (document.addEventListener) { 358 document.addEventListener("DOMContentLoaded", sorttable.init, false); 359} 360 361/* for Internet Explorer */ 362/*@cc_on @*/ 363/*@if (@_win32) 364 document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); 365 var script = document.getElementById("__ie_onload"); 366 script.onreadystatechange = function() { 367 if (this.readyState == "complete") { 368 sorttable.init(); // call the onload handler 369 } 370 }; 371/*@end @*/ 372 373/* for Safari */ 374if (/WebKit/i.test(navigator.userAgent)) { // sniff 375 var _timer = setInterval(function () { 376 if (/loaded|complete/.test(document.readyState)) { 377 sorttable.init(); // call the onload handler 378 } 379 }, 10); 380} 381 382/* for other browsers */ 383window.onload = sorttable.init; 384 385// written by Dean Edwards, 2005 386// with input from Tino Zijdel, Matthias Miller, Diego Perini 387 388// http://dean.edwards.name/weblog/2005/10/add-event/ 389 390function dean_addEvent(element, type, handler) { 391 if (element.addEventListener) { 392 element.addEventListener(type, handler, false); 393 } else { 394 // assign each event handler a unique ID 395 if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++; 396 // create a hash table of event types for the element 397 if (!element.events) element.events = {}; 398 // create a hash table of event handlers for each element/event pair 399 var handlers = element.events[type]; 400 if (!handlers) { 401 handlers = element.events[type] = {}; 402 // store the existing event handler (if there is one) 403 if (element["on" + type]) { 404 handlers[0] = element["on" + type]; 405 } 406 } 407 // store the event handler in the hash table 408 handlers[handler.$$guid] = handler; 409 // assign a global event handler to do all the work 410 element["on" + type] = handleEvent; 411 } 412}; 413// a counter used to create unique IDs 414dean_addEvent.guid = 1; 415 416function removeEvent(element, type, handler) { 417 if (element.removeEventListener) { 418 element.removeEventListener(type, handler, false); 419 } else { 420 // delete the event handler from the hash table 421 if (element.events && element.events[type]) { 422 delete element.events[type][handler.$$guid]; 423 } 424 } 425}; 426 427function handleEvent(event) { 428 var returnValue = true; 429 // grab the event object (IE uses a global event object) 430 event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event); 431 // get a reference to the hash table of event handlers 432 var handlers = this.events[event.type]; 433 // execute each event handler 434 for (var i in handlers) { 435 this.$$handleEvent = handlers[i]; 436 if (this.$$handleEvent(event) === false) { 437 returnValue = false;
438 } 439 } 440 return returnValue; 441}; 442 443function fixEvent(event) { 444 // add W3C standard event methods 445 event.preventDefault = fixEvent.preventDefault; 446 event.stopPropagation = fixEvent.stopPropagation; 447 return event; 448}; 449fixEvent.preventDefault = function () { 450 this.returnValue = false; 451}; 452fixEvent.stopPropagation = function () { 453 this.cancelBubble = true; 454} 455 456// Dean's forEach: http://dean.edwards.name/base/forEach.js 457/* 458 forEach, version 1.0 459 Copyright 2006, Dean Edwards 460 License: http://www.opensource.org/licenses/mit-license.php 461*/ 462 463// array-like enumeration 464if (!Array.forEach) { // mozilla already supports this 465 Array.forEach = function (array, block, context) { 466 for (var i = 0; i < array.length; i++) { 467 block.call(context, array[i], i, array); 468 } 469 }; 470} 471 472// generic enumeration 473Function.prototype.forEach = function (object, block, context) { 474 for (var key in object) { 475 if (typeof this.prototype[key] == "undefined") { 476 block.call(context, object[key], key, object); 477 } 478 } 479}; 480 481// character enumeration 482String.forEach = function (string, block, context) { 483 Array.forEach(string.split(""), function (chr, index) { 484 block.call(context, chr, index, string); 485 }); 486}; 487 488// globally resolve forEach enumeration 489var forEach = function (object, block, context) { 490 if (object) { 491 var resolve = Object; // default 492 if (object instanceof Function) { 493 // functions have a "length" property 494 resolve = Function; 495 } else if (object.forEach instanceof Function) { 496 // the object implements a custom forEach method so use that 497 object.forEach(block, context); 498 return; 499 } else if (typeof object == "string") { 500 // the object is a string 501 resolve = String; 502 } else if (typeof object.length == "number") { 503 // the object is array-like 504 resolve = Array; 505 } 506 resolve.forEach(object, block, context); 507 } 508};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.