1(function ($, w, undefined) { 2 if (w.footable === undefined || w.footable === null) 3 throw new Error('Please check and make sure footable.js is included in the page and is loaded prior to this script.'); 4 5 var defaults = { 6 sort: true, 7 sorters: { 8 alpha: function (a, b) { 9 if (typeof(a) === 'string') { a = a.toLowerCase(); } 10 if (typeof(b) === 'string') { b = b.toLowerCase(); } 11 if (a === b) return 0; 12 if (a < b) return -1; 13 return 1; 14 }, 15 numeric: function (a, b) { 16 return a - b; 17 } 18 }, 19 classes: { 20 sort: { 21 sortable: 'footable-sortable', 22 sorted: 'footable-sorted', 23 descending: 'footable-sorted-desc', 24 indicator: 'footable-sort-indicator' 25 } 26 }, 27 events: { 28 sort: { 29 sorting: 'footable_sorting', 30 sorted: 'footable_sorted' 31 } 32 } 33 }; 34 35 function Sort() { 36 var p = this; 37 p.name = 'Footable Sortable'; 38 p.init = function (ft) { 39 p.footable = ft; 40 if (ft.options.sort === true) { 41 $(ft.table) 42 .unbind('.sorting') 43 .bind({ 44 'footable_initialized.sorting': function (e) { 45 var $table = $(ft.table), 46 $tbody = $table.find('> tbody'), 47 cls = ft.options.classes.sort, 48 column, $th; 49
50 if ($table.data('sort') === false) return; 51 52 $table.find('> thead > tr:last-child > th, > thead > tr:last-child > td').each(function (ec) { 53 $th = $(this), column = ft.columns[$th.index()]; 54 if (column.sort.ignore !== true && !$th.hasClass(cls.sortable)) { 55 $th.addClass(cls.sortable); 56 $('<span />').addClass(cls.indicator).appendTo($th); 57 } 58 }); 59 60 $table.find('> thead > tr:last-child > th.' + cls.sortable + ', > thead > tr:last-child > td.' + cls.sortable).unbind('click.footable').bind('click.footable', function (ec) { 61 ec.preventDefault(); 62 $th = $(this); 63 var ascending = !$th.hasClass(cls.sorted); 64 p.doSort($th.index(), ascending); 65 return false; 66 }); 67 68 var didSomeSorting = false; 69 for (var c in ft.columns) { 70 column = ft.columns[c]; 71 if (column.sort.initial) { 72 var ascending = (column.sort.initial !== 'descending'); 73 p.doSort(column.index, ascending); 74 break; 75 } 76 } 77 if (didSomeSorting) { 78 ft.bindToggleSelectors(); 79 } 80 }, 81 'footable_redrawn.sorting': function(e) { 82 var $table = $(ft.table), 83 cls = ft.options.classes.sort; 84 if ($table.data('sorted') >= 0) { 85 $table.find('> thead > tr:last-child > th').each(function(i){ 86 var $th = $(this); 87 if ($th.hasClass(cls.sorted) || $th.hasClass(cls.descending)) { 88 p.doSort(i); 89 return; 90 } 91 }); 92 } 93 }, 94 'footable_column_data.sorting': function (e) { 95 var $th = $(e.column.th); 96 e.column.data.sort = e.column.data.sort || {}; 97 e.column.data.sort.initial = $th.data('sort-initial') || false; 98 e.column.data.sort.ignore = $th.data('sort-ignore') || false; 99 e.column.data.sort.selector = $th.data('sort-selector') || null; 100 101 var match = $th.data('sort-match') || 0; 102 if (match >= e.column.data.matches.length) match = 0; 103 e.column.data.sort.match = e.column.data.matches[match]; 104 } 105 }) 106 //save the sort object onto the table so we can access it later 107 .data('footable-sort', p); 108 } 109 }; 110 111 p.doSort = function(columnIndex, ascending) { 112 var ft = p.footable;
113 if ($(ft.table).data('sort') === false) return; 114 115 var $table = $(ft.table), 116 $tbody = $table.find('> tbody'), 117 column = ft.columns[columnIndex], 118 $th = $table.find('> thead > tr:last-child > th:eq(' + columnIndex + ')'), 119 cls = ft.options.classes.sort, 120 evt = ft.options.events.sort; 121 122 ascending = (ascending === undefined) ? $th.hasClass(cls.sorted) : 123 (ascending === 'toggle') ? !$th.hasClass(cls.sorted) : ascending; 124 125 if (column.sort.ignore === true) return true; 126 127 //raise a pre-sorting event so that we can cancel the sorting if needed 128 var event = ft.raise(evt.sorting, { column: column, direction: ascending ? 'ASC' : 'DESC' }); 129 if (event && event.result === false) return; 130 131 $table.data('sorted', column.index); 132 133 $table.find('> thead > tr:last-child > th, > thead > tr:last-child > td').not($th).removeClass(cls.sorted + ' ' + cls.descending); 134 135 if (ascending === undefined) { 136 ascending = $th.hasClass(cls.sorted); 137 } 138 139 if (ascending) { 140 $th.removeClass(cls.descending).addClass(cls.sorted); 141 } else { 142 $th.removeClass(cls.sorted).addClass(cls.descending); 143 } 144 145 p.sort(ft, $tbody, column, ascending); 146 147 ft.bindToggleSelectors(); 148 ft.raise(evt.sorted, { column: column, direction: ascending ? 'ASC' : 'DESC' }); 149 }; 150 151 p.rows = function (ft, tbody, column) { 152 var rows = []; 153 tbody.find('> tr').each(function () { 154 var $row = $(this), $next = null; 155 if ($row.hasClass(ft.options.classes.detail)) return true; 156 if ($row.next().hasClass(ft.options.classes.detail)) { 157 $next = $row.next().get(0); 158 } 159 var row = { 'row': $row, 'detail': $next }; 160 if (column !== undefined) { 161 row.value = ft.parse(this.cells[column.sort.match], column); 162 } 163 rows.push(row); 164 return true; 165 }).detach(); 166 return rows; 167 }; 168 169 p.sort = function (ft, tbody, column, ascending) { 170 var rows = p.rows(ft, tbody, column); 171 var sorter = ft.options.sorters[column.type] || ft.options.sorters.alpha; 172 rows.sort(function (a, b) { 173 if (ascending) { 174 return sorter(a.value, b.value); 175 } else { 176 return sorter(b.value, a.value); 177 } 178 }); 179 for (var j = 0; j < rows.length; j++) { 180 tbody.append(rows[j].row); 181 if (rows[j].detail !== null) { 182 tbody.append(rows[j].detail); 183 } 184 } 185 }; 186 } 187 188 w.footable.plugins.register(Sort, defaults); 189 190})(jQuery, window);
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.