Made internal variables more consistent & some code rearrangement

This commit is contained in:
Mottie 2013-03-13 23:36:50 -05:00
parent ae267560fc
commit 0b7ba1024c

View File

@ -489,128 +489,13 @@
return (parsers && parsers[i]) ? parsers[i].type || '' : ''; return (parsers && parsers[i]) ? parsers[i].type || '' : '';
} }
// sort multiple columns function initSort(table, cell, e){
function multisort(table) { /*jshint loopfunc:true */ var a, i, j, o, s,
var dynamicExp, sortWrapper, col, mx = 0, dir = 0, tc = table.config, c = table.config,
sortList = tc.sortList, l = sortList.length, bl = table.tBodies.length, k = !e[c.sortMultiSortKey],
sortTime, i, j, k, c, colMax, cache, lc, s, e, order, orgOrderCol; $this = $(table);
if (tc.serverSideSorting || !tc.cache[0]) { // empty table - fixes #206
return;
}
if (tc.debug) { sortTime = new Date(); }
for (k = 0; k < bl; k++) {
colMax = tc.cache[k].colMax;
cache = tc.cache[k].normalized;
lc = cache.length;
orgOrderCol = (cache && cache[0]) ? cache[0].length - 1 : 0;
cache.sort(function(a, b) {
// cache is undefined here in IE, so don't use it!
for (i = 0; i < l; i++) {
c = sortList[i][0];
order = sortList[i][1];
// fallback to natural sort since it is more robust
s = /n/i.test(getCachedSortType(tc.parsers, c)) ? "Numeric" : "Text";
s += order === 0 ? "" : "Desc";
if (/Numeric/.test(s) && tc.strings[c]) {
// sort strings in numerical columns
if (typeof (tc.string[tc.strings[c]]) === 'boolean') {
dir = (order === 0 ? 1 : -1) * (tc.string[tc.strings[c]] ? -1 : 1);
} else {
dir = (tc.strings[c]) ? tc.string[tc.strings[c]] || 0 : 0;
}
}
var sort = $.tablesorter["sort" + s](table, a[c], b[c], c, colMax[c], dir);
if (sort) { return sort; }
}
return a[orgOrderCol] - b[orgOrderCol];
});
}
if (tc.debug) { benchmark("Sorting on " + sortList.toString() + " and dir " + order + " time", sortTime); }
}
function resortComplete($table, callback){
$table.trigger('updateComplete');
if (typeof callback === "function") {
callback($table[0]);
}
}
function checkResort($table, flag, callback) {
if (flag !== false) {
$table.trigger("sorton", [$table[0].config.sortList, function(){
resortComplete($table, callback);
}]);
} else {
resortComplete($table, callback);
}
}
/* public methods */
ts.construct = function(settings) {
return this.each(function() {
// if no thead or tbody, or tablesorter is already present, quit
if (!this.tHead || this.tBodies.length === 0 || this.hasInitialized === true) {
return (this.config && this.config.debug) ? log('stopping initialization! No thead, tbody or tablesorter has already been initialized') : '';
}
// declare
var $cell, $this = $(this), $t0 = this,
c, i, j, k = '', a, s, o, downTime,
m = $.metadata;
// initialization flag
$t0.hasInitialized = false;
// new blank config object
$t0.config = {};
// merge and extend
c = $.extend(true, $t0.config, ts.defaults, settings);
// save the settings where they read
$.data($t0, "tablesorter", c);
if (c.debug) { $.data( $t0, 'startoveralltimer', new Date()); }
// constants
c.supportsTextContent = $('<span>x</span>')[0].textContent === 'x';
c.supportsDataObject = parseFloat($.fn.jquery) >= 1.4;
// digit sort text location; keeping max+/- for backwards compatibility
c.string = { 'max': 1, 'min': -1, 'max+': 1, 'max-': -1, 'zero': 0, 'none': 0, 'null': 0, 'top': true, 'bottom': false };
// add table theme class only if there isn't already one there
if (!/tablesorter\-/.test($this.attr('class'))) {
k = (c.theme !== '' ? ' tablesorter-' + c.theme : '');
}
c.$table = $this.addClass(c.tableClass + k);
c.$tbodies = $this.children('tbody:not(.' + c.cssInfoBlock + ')');
// build headers
buildHeaders($t0);
// fixate columns if the users supplies the fixedWidth option
// do this after theme has been applied
fixColumnWidth($t0);
// try to auto detect column type, and store in tables config
c.parsers = buildParserCache($t0);
// build the cache for the tbody cells
// delayInit will delay building the cache until the user starts a sort
if (!c.delayInit) { buildCache($t0); }
// apply event handling to headers
// this is to big, perhaps break it out?
c.$headers
// http://stackoverflow.com/questions/5312849/jquery-find-self; andSelf() deprecated in jQuery 1.8
.find('*')[ $.fn.addBack ? 'addBack': 'andSelf' ]().filter(c.selectorSort)
.unbind('mousedown.tablesorter mouseup.tablesorter')
.bind('mousedown.tablesorter mouseup.tablesorter', function(e, external) {
// jQuery v1.2.6 doesn't have closest()
var $cell = this.tagName.match('TH|TD') ? $(this) : $(this).parents('th, td').filter(':last'), cell = $cell[0];
// only recognize left clicks
if ((e.which || e.button) !== 1) { return false; }
// set timer on mousedown
if (e.type === 'mousedown') {
downTime = new Date().getTime();
return e.target.tagName === "INPUT" ? '' : !c.cancelSelection;
}
// ignore long clicks (prevents resizable widget from initializing a sort)
if (external !== true && (new Date().getTime() - downTime > 250)) { return false; }
if (c.delayInit && !c.cache) { buildCache($t0); }
if (!cell.sortDisabled) {
// Only call sortStart if sorting is enabled // Only call sortStart if sorting is enabled
$this.trigger("sortStart", $t0); $this.trigger("sortStart", table);
// store exp, for speed
// $cell = $(this);
k = !e[c.sortMultiSortKey];
// get current column sort order // get current column sort order
cell.count = e[c.sortResetKey] ? 2 : (cell.count + 1) % (c.sortReset ? 3 : 2); cell.count = e[c.sortResetKey] ? 2 : (cell.count + 1) % (c.sortReset ? 3 : 2);
// reset all sorts on non-current column - issue #30 // reset all sorts on non-current column - issue #30
@ -693,15 +578,135 @@
} }
} }
// sortBegin event triggered immediately before the sort // sortBegin event triggered immediately before the sort
$this.trigger("sortBegin", $t0); $this.trigger("sortBegin", table);
// setTimeout needed so the processing icon shows up // setTimeout needed so the processing icon shows up
setTimeout(function(){ setTimeout(function(){
// set css for headers // set css for headers
setHeadersCss($t0); setHeadersCss(table);
multisort($t0); multisort(table);
appendToTable($t0); appendToTable(table);
}, 1); }, 1);
} }
// sort multiple columns
function multisort(table) { /*jshint loopfunc:true */
var dynamicExp, sortWrapper, col, mx = 0, dir = 0, tc = table.config,
sortList = tc.sortList, l = sortList.length, bl = table.tBodies.length,
sortTime, i, j, k, c, colMax, cache, lc, s, e, order, orgOrderCol;
if (tc.serverSideSorting || !tc.cache[0]) { // empty table - fixes #206
return;
}
if (tc.debug) { sortTime = new Date(); }
for (k = 0; k < bl; k++) {
colMax = tc.cache[k].colMax;
cache = tc.cache[k].normalized;
lc = cache.length;
orgOrderCol = (cache && cache[0]) ? cache[0].length - 1 : 0;
cache.sort(function(a, b) {
// cache is undefined here in IE, so don't use it!
for (i = 0; i < l; i++) {
c = sortList[i][0];
order = sortList[i][1];
// fallback to natural sort since it is more robust
s = /n/i.test(getCachedSortType(tc.parsers, c)) ? "Numeric" : "Text";
s += order === 0 ? "" : "Desc";
if (/Numeric/.test(s) && tc.strings[c]) {
// sort strings in numerical columns
if (typeof (tc.string[tc.strings[c]]) === 'boolean') {
dir = (order === 0 ? 1 : -1) * (tc.string[tc.strings[c]] ? -1 : 1);
} else {
dir = (tc.strings[c]) ? tc.string[tc.strings[c]] || 0 : 0;
}
}
var sort = $.tablesorter["sort" + s](table, a[c], b[c], c, colMax[c], dir);
if (sort) { return sort; }
}
return a[orgOrderCol] - b[orgOrderCol];
});
}
if (tc.debug) { benchmark("Sorting on " + sortList.toString() + " and dir " + order + " time", sortTime); }
}
function resortComplete($table, callback){
$table.trigger('updateComplete');
if (typeof callback === "function") {
callback($table[0]);
}
}
function checkResort($table, flag, callback) {
if (flag !== false) {
$table.trigger("sorton", [$table[0].config.sortList, function(){
resortComplete($table, callback);
}]);
} else {
resortComplete($table, callback);
}
}
/* public methods */
ts.construct = function(settings) {
return this.each(function() {
// if no thead or tbody, or tablesorter is already present, quit
if (!this.tHead || this.tBodies.length === 0 || this.hasInitialized === true) {
return (this.config && this.config.debug) ? log('stopping initialization! No thead, tbody or tablesorter has already been initialized') : '';
}
// declare
var $cell, $this = $(this), table = this,
c, i, j, k = '', a, s, o, downTime,
m = $.metadata;
// initialization flag
table.hasInitialized = false;
// new blank config object
table.config = {};
// merge and extend
c = $.extend(true, table.config, ts.defaults, settings);
// save the settings where they read
$.data(table, "tablesorter", c);
if (c.debug) { $.data( table, 'startoveralltimer', new Date()); }
// constants
c.supportsTextContent = $('<span>x</span>')[0].textContent === 'x';
c.supportsDataObject = parseFloat($.fn.jquery) >= 1.4;
// digit sort text location; keeping max+/- for backwards compatibility
c.string = { 'max': 1, 'min': -1, 'max+': 1, 'max-': -1, 'zero': 0, 'none': 0, 'null': 0, 'top': true, 'bottom': false };
// add table theme class only if there isn't already one there
if (!/tablesorter\-/.test($this.attr('class'))) {
k = (c.theme !== '' ? ' tablesorter-' + c.theme : '');
}
c.$table = $this.addClass(c.tableClass + k);
c.$tbodies = $this.children('tbody:not(.' + c.cssInfoBlock + ')');
// build headers
buildHeaders(table);
// fixate columns if the users supplies the fixedWidth option
// do this after theme has been applied
fixColumnWidth(table);
// try to auto detect column type, and store in tables config
c.parsers = buildParserCache(table);
// build the cache for the tbody cells
// delayInit will delay building the cache until the user starts a sort
if (!c.delayInit) { buildCache(table); }
// apply event handling to headers
// this is to big, perhaps break it out?
c.$headers
// http://stackoverflow.com/questions/5312849/jquery-find-self; andSelf() deprecated in jQuery 1.8
.find('*')[ $.fn.addBack ? 'addBack': 'andSelf' ]().filter(c.selectorSort)
.unbind('mousedown.tablesorter mouseup.tablesorter')
.bind('mousedown.tablesorter mouseup.tablesorter', function(e, external) {
// jQuery v1.2.6 doesn't have closest()
var $cell = this.tagName.match('TH|TD') ? $(this) : $(this).parents('th, td').filter(':last'), cell = $cell[0];
// only recognize left clicks
if ((e.which || e.button) !== 1) { return false; }
// set timer on mousedown
if (e.type === 'mousedown') {
downTime = new Date().getTime();
return e.target.tagName === "INPUT" ? '' : !c.cancelSelection;
}
// ignore long clicks (prevents resizable widget from initializing a sort)
if (external !== true && (new Date().getTime() - downTime > 250)) { return false; }
if (c.delayInit && !c.cache) { buildCache(table); }
if (!cell.sortDisabled) {
initSort(table, cell, e);
}
}); });
if (c.cancelSelection) { if (c.cancelSelection) {
// cancel selection // cancel selection
@ -716,19 +721,19 @@
.unbind('sortReset update updateRows updateCell addRows sorton appendCache applyWidgetId applyWidgets refreshWidgets destroy mouseup mouseleave '.split(' ').join('.tablesorter ')) .unbind('sortReset update updateRows updateCell addRows sorton appendCache applyWidgetId applyWidgets refreshWidgets destroy mouseup mouseleave '.split(' ').join('.tablesorter '))
.bind("sortReset.tablesorter", function(){ .bind("sortReset.tablesorter", function(){
c.sortList = []; c.sortList = [];
setHeadersCss($t0); setHeadersCss(table);
multisort($t0); multisort(table);
appendToTable($t0); appendToTable(table);
}) })
.bind("update.tablesorter updateRows.tablesorter", function(e, resort, callback) { .bind("update.tablesorter updateRows.tablesorter", function(e, resort, callback) {
// remove rows/elements before update // remove rows/elements before update
$this.find(c.selectorRemove).remove(); $this.find(c.selectorRemove).remove();
// update sorting // update sorting
updateHeader($t0); updateHeader(table);
// rebuild parsers // rebuild parsers
c.parsers = buildParserCache($t0); c.parsers = buildParserCache(table);
// rebuild the cache map // rebuild the cache map
buildCache($t0); buildCache(table);
checkResort($this, resort, callback); checkResort($this, resort, callback);
}) })
.bind("updateCell.tablesorter", function(e, cell, resort, callback) { .bind("updateCell.tablesorter", function(e, cell, resort, callback) {
@ -746,8 +751,8 @@
row = $tb.eq(tbdy).find('tr').index( $row ); row = $tb.eq(tbdy).find('tr').index( $row );
icell = cell.cellIndex; icell = cell.cellIndex;
l = c.cache[tbdy].normalized[row].length - 1; l = c.cache[tbdy].normalized[row].length - 1;
c.cache[tbdy].row[$t0.config.cache[tbdy].normalized[row][l]] = $row; c.cache[tbdy].row[table.config.cache[tbdy].normalized[row][l]] = $row;
c.cache[tbdy].normalized[row][icell] = c.parsers[icell].format( getElementText($t0, cell, icell), $t0, cell, icell ); c.cache[tbdy].normalized[row][icell] = c.parsers[icell].format( getElementText(table, cell, icell), table, cell, icell );
checkResort($this, resort, callback); checkResort($this, resort, callback);
} }
}) })
@ -757,13 +762,13 @@
tbdy = $this.find('tbody').index( $row.closest('tbody') ); tbdy = $this.find('tbody').index( $row.closest('tbody') );
// fixes adding rows to an empty table - see issue #179 // fixes adding rows to an empty table - see issue #179
if (!c.parsers) { if (!c.parsers) {
c.parsers = buildParserCache($t0); c.parsers = buildParserCache(table);
} }
// add each row // add each row
for (i = 0; i < rows; i++) { for (i = 0; i < rows; i++) {
// add each cell // add each cell
for (j = 0; j < l; j++) { for (j = 0; j < l; j++) {
dat[j] = c.parsers[j].format( getElementText($t0, $row[i].cells[j], j), $t0, $row[i].cells[j], j ); dat[j] = c.parsers[j].format( getElementText(table, $row[i].cells[j], j), table, $row[i].cells[j], j );
} }
// add the row index to the end // add the row index to the end
dat.push(c.cache[tbdy].row.length); dat.push(c.cache[tbdy].row.length);
@ -778,34 +783,34 @@
.bind("sorton.tablesorter", function(e, list, callback, init) { .bind("sorton.tablesorter", function(e, list, callback, init) {
$this.trigger("sortStart", this); $this.trigger("sortStart", this);
// update header count index // update header count index
updateHeaderSortCount($t0, list); updateHeaderSortCount(table, list);
// set css for headers // set css for headers
setHeadersCss($t0); setHeadersCss(table);
// sort the table and append it to the dom // sort the table and append it to the dom
multisort($t0); multisort(table);
appendToTable($t0, init); appendToTable(table, init);
if (typeof callback === "function") { if (typeof callback === "function") {
callback($t0); callback(table);
} }
}) })
.bind("appendCache.tablesorter", function(e, callback, init) { .bind("appendCache.tablesorter", function(e, callback, init) {
appendToTable($t0, init); appendToTable(table, init);
if (typeof callback === "function") { if (typeof callback === "function") {
callback($t0); callback(table);
} }
}) })
.bind("applyWidgetId.tablesorter", function(e, id) { .bind("applyWidgetId.tablesorter", function(e, id) {
ts.getWidgetById(id).format($t0, c, c.widgetOptions); ts.getWidgetById(id).format(table, c, c.widgetOptions);
}) })
.bind("applyWidgets.tablesorter", function(e, init) { .bind("applyWidgets.tablesorter", function(e, init) {
// apply widgets // apply widgets
ts.applyWidget($t0, init); ts.applyWidget(table, init);
}) })
.bind("refreshWidgets.tablesorter", function(e, all, dontapply){ .bind("refreshWidgets.tablesorter", function(e, all, dontapply){
ts.refreshWidgets($t0, all, dontapply); ts.refreshWidgets(table, all, dontapply);
}) })
.bind("destroy.tablesorter", function(e, c, cb){ .bind("destroy.tablesorter", function(e, c, cb){
ts.destroy($t0, c, cb); ts.destroy(table, c, cb);
}); });
// get sort list from jQuery data or metadata // get sort list from jQuery data or metadata
@ -816,13 +821,13 @@
c.sortList = $this.metadata().sortlist; c.sortList = $this.metadata().sortlist;
} }
// apply widget init code // apply widget init code
ts.applyWidget($t0, true); ts.applyWidget(table, true);
// if user has supplied a sort list to constructor // if user has supplied a sort list to constructor
if (c.sortList.length > 0) { if (c.sortList.length > 0) {
$this.trigger("sorton", [c.sortList, {}, !c.initWidgets]); $this.trigger("sorton", [c.sortList, {}, !c.initWidgets]);
} else if (c.initWidgets) { } else if (c.initWidgets) {
// apply widget format // apply widget format
ts.applyWidget($t0); ts.applyWidget(table);
} }
// show processesing icon // show processesing icon
@ -830,17 +835,17 @@
$this $this
.unbind('sortBegin.tablesorter sortEnd.tablesorter') .unbind('sortBegin.tablesorter sortEnd.tablesorter')
.bind('sortBegin.tablesorter sortEnd.tablesorter', function(e) { .bind('sortBegin.tablesorter sortEnd.tablesorter', function(e) {
ts.isProcessing($t0, e.type === 'sortBegin'); ts.isProcessing(table, e.type === 'sortBegin');
}); });
} }
// initialized // initialized
$t0.hasInitialized = true; table.hasInitialized = true;
if (c.debug) { if (c.debug) {
ts.benchmark("Overall initialization time", $.data( $t0, 'startoveralltimer')); ts.benchmark("Overall initialization time", $.data( table, 'startoveralltimer'));
} }
$this.trigger('tablesorter-initialized', $t0); $this.trigger('tablesorter-initialized', table);
if (typeof c.initialized === 'function') { c.initialized($t0); } if (typeof c.initialized === 'function') { c.initialized(table); }
}); });
}; };