1/* 2 A simple, lightweight jQuery plugin for creating sortable tables. 3 https://github.com/kylefox/jquery-tablesort 4 Version 0.0.11 5*/ 6 7(function($) { 8 $.tablesort = function ($table, settings) { 9 var self = this; 10 this.$table = $table; 11 this.$thead = this.$table.find('thead'); 12 this.settings = $.extend({}, $.tablesort.defaults, settings); 13 this.$sortCells = this.$thead.length > 0 ? this.$thead.find('th:not(.no-sort)') : this.$table.find('th:not(.no-sort)'); 14 this.$sortCells.on('click.tablesort', function() { 15 self.sort($(this)); 16 }); 17 this.index = null; 18 this.$th = null; 19 this.direction = null; 20 }; 21 22 $.tablesort.prototype = { 23 24 sort: function(th, direction) { 25 var start = new Date(), 26 self = this, 27 table = this.$table, 28 rowsContainer = table.find('tbody').length > 0 ? table.find('tbody') : table, 29 rows = rowsContainer.find('tr').has('td, th'), 30 cells = rows.find(':nth-child(' + (th.index() + 1) + ')').filter('td, th'), 31 sortBy = th.data().sortBy, 32 sortedMap = []; 33 34 var unsortedValues = cells.map(function(idx, cell) { 35 if (sortBy) 36 return (typeof sortBy === 'function') ? sortBy($(th), $(cell), self) : sortBy; 37 return ($(this).data().sortValue != null ? $(this).data().sortValue : $(this).text()); 38 }); 39 if (unsortedValues.length === 0) return; 40 41 //click on a different column 42 if (this.index !== th.index()) { 43 this.direction = 'asc'; 44 this.index = th.index(); 45 } 46 else if (direction !== 'asc' && direction !== 'desc') 47 this.direction = this.direction === 'asc' ? 'desc' : 'asc'; 48 else 49 this.direction = direction; 50 51 direction = this.direction == 'asc' ? 1 : -1; 52 53 self.$table.trigger('tablesort:start', [self]); 54 self.log("Sorting by " + this.index + ' ' + this.direction); 55 56 // Try to force a browser redraw 57 self.$table.css("display"); 58 // Run sorting asynchronously on a timeout to force browser redraw after 59 // `tablesort:start` callback. Also avoids locking up the browser too much. 60 setTimeout(function() { 61 self.$sortCells.removeClass(self.settings.asc + ' ' + self.settings.desc); 62 for (var i = 0, length = unsortedValues.length; i < length; i++) 63 { 64 sortedMap.push({ 65 index: i, 66 cell: cells[i], 67 row: rows[i], 68 value: unsortedValues[i] 69 }); 70 } 71 72 sortedMap.sort(function(a, b) { 73 return self.settings.compare(a.value, b.value) * direction; 74 }); 75 76 $.each(sortedMap, function(i, entry) { 77 rowsContainer.append(entry.row); 78 }); 79 80 th.addClass(self.settings[self.direction]); 81 82 self.log('Sort finished in ' + ((new Date()).getTime() - start.getTime()) + 'ms'); 83 self.$table.trigger('tablesort:complete', [self]); 84 //Try to force a browser redraw 85 self.$table.css("display"); 86 }, unsortedValues.length > 2000 ? 200 : 10); 87 }, 88 89 log: function(msg) { 90 if(($.tablesort.DEBUG || this.settings.debug) && console && console.log) { 91 console.log('[tablesort] ' + msg); 92 } 93 }, 94 95 destroy: function() { 96 this.$sortCells.off('click.tablesort'); 97 this.$table.data('tablesort', null); 98 return null; 99 } 100 101 }; 102 103 $.tablesort.DEBUG = false; 104 105 $.tablesort.defaults = { 106 debug: $.tablesort.DEBUG, 107 asc: 'sorted ascending', 108 desc: 'sorted descending', 109 compare: function(a, b) { 110 if (a > b) { 111 return 1; 112 } else if (a < b) { 113 return -1; 114 } else { 115 return 0; 116 } 117 } 118 }; 119 120 $.fn.tablesort = function(settings) { 121 var table, sortable, previous; 122 return this.each(function() { 123 table = $(this); 124 previous = table.data('tablesort'); 125 if(previous) { 126 previous.destroy(); 127 } 128 table.data('tablesort', new $.tablesort(table, settings)); 129 }); 130 }; 131 132})(window.Zepto || window.jQuery);
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.