Skip to content

Commit

Permalink
Optimizations: replace arrays using $.each with for loops. See #827
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed Feb 26, 2015
1 parent 87652f2 commit 873f6d2
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 233 deletions.
46 changes: 27 additions & 19 deletions addons/pager/jquery.tablesorter.pager.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,20 @@
},

calcFilters = function(table, p) {
var c = table.config,
var normalized, indx, len,
c = table.config,
hasFilters = c.$table.hasClass('hasFilters');
if (hasFilters && !p.ajaxUrl) {
if ($.isEmptyObject(c.cache)) {
// delayInit: true so nothing is in the cache
p.filteredRows = p.totalRows = c.$tbodies.eq(0).children('tr').not( p.countChildRows ? '' : '.' + c.cssChildRow ).length;
} else {
p.filteredRows = 0;
$.each(c.cache[0].normalized, function(i, el) {
p.filteredRows += p.regexRows.test(el[c.columns].$row[0].className) ? 0 : 1;
});
normalized = c.cache[0].normalized;
len = normalized.length;
for (indx = 0; indx < len; indx++) {
p.filteredRows += p.regexRows.test(normalized[indx][c.columns].$row[0].className) ? 0 : 1;
}
}
} else if (!hasFilters) {
p.filteredRows = p.totalRows;
Expand All @@ -154,7 +157,7 @@

updatePageDisplay = function(table, p, completed) {
if ( p.initializing ) { return; }
var s, t, $out,
var s, t, $out, indx, len, options,
c = table.config,
sz = p.size || p.settings.size || 10; // don't allow dividing by zero
if (p.countChildRows) { t.push(c.cssChildRow); }
Expand Down Expand Up @@ -192,9 +195,11 @@
});
if ( p.$goto.length ) {
t = '';
$.each(buildPageSelect(p), function(i, opt){
t += '<option value="' + opt + '">' + opt + '</option>';
});
options = buildPageSelect(p);
len = options.length;
for (indx = 0; indx < len; indx++) {
t += '<option value="' + options[indx] + '">' + options[indx] + '</option>';
}
// innerHTML doesn't work in IE9 - http://support2.microsoft.com/kb/276228
p.$goto.html(t).val( p.page + 1 );
}
Expand Down Expand Up @@ -532,35 +537,38 @@
},

getAjaxUrl = function(table, p) {
var c = table.config,
var indx, len,
c = table.config,
url = (p.ajaxUrl) ? p.ajaxUrl
// allow using "{page+1}" in the url string to switch to a non-zero based index
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
.replace(/\{size\}/g, p.size) : '',
sl = c.sortList,
fl = p.currentFilters || $(table).data('lastSearch') || [],
sortList = c.sortList,
filterList = p.currentFilters || $(table).data('lastSearch') || [],
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
arry = [];
if (sortCol) {
sortCol = sortCol[1];
$.each(sl, function(i,v){
arry.push(sortCol + '[' + v[0] + ']=' + v[1]);
});
len = sortList.length;
for (indx = 0; indx < len; indx++) {
arry.push(sortCol + '[' + sortList[indx][0] + ']=' + sortList[indx][1]);
}
// if the arry is empty, just add the col parameter... "&{sortList:col}" becomes "&col"
url = url.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : sortCol );
arry = [];
}
if (filterCol) {
filterCol = filterCol[1];
$.each(fl, function(i,v){
if (v) {
arry.push(filterCol + '[' + i + ']=' + encodeURIComponent(v));
len = filterList.length;
for (indx = 0; indx < len; indx++) {
if (filterList[indx]) {
arry.push(filterCol + '[' + indx + ']=' + encodeURIComponent(filterList[indx]));
}
});
}
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
p.currentFilters = fl;
p.currentFilters = filterList;
}
if ( typeof(p.customAjaxUrl) === "function" ) {
url = p.customAjaxUrl(table, url);
Expand Down
4 changes: 2 additions & 2 deletions dist/js/extras/jquery.tablesorter.pager.min.js

Large diffs are not rendered by default.

127 changes: 69 additions & 58 deletions dist/js/jquery.tablesorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,49 +592,50 @@
});
}

function updateHeaderSortCount(table, list) {
var s, t, o, col, primary,
function updateHeaderSortCount( table, list ) {
var col, dir, group, header, indx, primary, temp, val,
c = table.config,
sl = list || c.sortList;
sortList = list || c.sortList,
len = sortList.length;
c.sortList = [];
$.each(sl, function(i,v){
for (indx = 0; indx < len; indx++) {
val = sortList[indx];
// ensure all sortList values are numeric - fixes #127
col = parseInt(v[0], 10);
col = parseInt(val[0], 10);
// make sure header exists
o = c.$headers.filter('[data-column="' + col + '"]:last')[0];
if (o) { // prevents error if sorton array is wrong
// o.count = o.count + 1;
t = ('' + v[1]).match(/^(1|d|s|o|n)/);
t = t ? t[0] : '';
header = c.$headers.filter('[data-column="' + col + '"]:last')[0];
if (header) { // prevents error if sorton array is wrong
dir = ('' + val[1]).match(/^(1|d|s|o|n)/);
dir = dir ? dir[0] : '';
// 0/(a)sc (default), 1/(d)esc, (s)ame, (o)pposite, (n)ext
switch(t) {
switch(dir) {
case '1': case 'd': // descending
t = 1;
dir = 1;
break;
case 's': // same direction (as primary column)
// if primary sort is set to 's', make it ascending
t = primary || 0;
dir = primary || 0;
break;
case 'o':
s = o.order[(primary || 0) % (c.sortReset ? 3 : 2)];
temp = header.order[(primary || 0) % (c.sortReset ? 3 : 2)];
// opposite of primary column; but resets if primary resets
t = s === 0 ? 1 : s === 1 ? 0 : 2;
dir = temp === 0 ? 1 : temp === 1 ? 0 : 2;
break;
case 'n':
o.count = o.count + 1;
t = o.order[(o.count) % (c.sortReset ? 3 : 2)];
header.count = header.count + 1;
dir = header.order[(header.count) % (c.sortReset ? 3 : 2)];
break;
default: // ascending
t = 0;
dir = 0;
break;
}
primary = i === 0 ? t : primary;
s = [ col, parseInt(t, 10) || 0 ];
c.sortList.push(s);
t = $.inArray(s[1], o.order); // fixes issue #167
o.count = t >= 0 ? t : s[1] % (c.sortReset ? 3 : 2);
primary = indx === 0 ? dir : primary;
group = [ col, parseInt(dir, 10) || 0 ];
c.sortList.push(group);
dir = $.inArray(group[1], header.order); // fixes issue #167
header.count = dir >= 0 ? dir : group[1] % (c.sortReset ? 3 : 2);
}
});
}
}

function getCachedSortType(parsers, i) {
Expand Down Expand Up @@ -1633,7 +1634,8 @@

ts.applyWidget = function(table, init, callback) {
table = $(table)[0]; // in case this is called externally
var c = table.config,
var indx, len, name,
c = table.config,
wo = c.widgetOptions,
tableClass = ' ' + c.table.className + ' ',
widgets = [],
Expand All @@ -1648,9 +1650,10 @@
// extract out the widget id from the table class (widget id's can include dashes)
w = tableClass.match( wd );
if ( w ) {
$.each( w, function( i,n ){
c.widgets.push( n.replace( wd, '$1' ) );
});
len = w.length;
for (indx = 0; indx < len; indx++) {
c.widgets.push( w[indx].replace( wd, '$1' ) );
}
}
}
if (c.widgets.length) {
Expand All @@ -1659,41 +1662,44 @@
c.widgets = $.grep(c.widgets, function(v, k){
return $.inArray(v, c.widgets) === k;
});
name = c.widgets || [];
len = name.length;
// build widget array & add priority as needed
$.each(c.widgets || [], function(i,n){
wd = ts.getWidgetById(n);
for (indx = 0; indx < len; indx++) {
wd = ts.getWidgetById(name[indx]);
if (wd && wd.id) {
// set priority to 10 if not defined
if (!wd.priority) { wd.priority = 10; }
widgets[i] = wd;
widgets[indx] = wd;
}
});
}
// sort widgets by priority
widgets.sort(function(a, b){
return a.priority < b.priority ? -1 : a.priority === b.priority ? 0 : 1;
});
// add/update selected widgets
$.each(widgets, function(i,w){
if (w) {
if (init || !(c.widgetInit[w.id])) {
len = widgets.length;
for (indx = 0; indx < len; indx++) {
if (widgets[indx]) {
if ( init || !( c.widgetInit[ widgets[indx].id ] ) ) {
// set init flag first to prevent calling init more than once (e.g. pager)
c.widgetInit[w.id] = true;
if (w.hasOwnProperty('options')) {
wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo );
c.widgetInit[ widgets[indx].id ] = true;
if ( 'options' in widgets[indx] ) {
wo = table.config.widgetOptions = $.extend( true, {}, widgets[indx].options, wo );
}
if (w.hasOwnProperty('init')) {
if ( 'init' in widgets[indx] ) {
if (c.debug) { time2 = new Date(); }
w.init(table, w, c, wo);
if (c.debug) { ts.benchmark('Initializing ' + w.id + ' widget', time2); }
widgets[indx].init(table, widgets[indx], c, wo);
if (c.debug) { ts.benchmark('Initializing ' + widgets[indx].id + ' widget', time2); }
}
}
if (!init && w.hasOwnProperty('format')) {
if ( !init && 'format' in widgets[indx] ) {
if (c.debug) { time2 = new Date(); }
w.format(table, c, wo, false);
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + w.id + ' widget', time2); }
widgets[indx].format(table, c, wo, false);
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + widgets[indx].id + ' widget', time2); }
}
}
});
}
// callback executed on init only
if (!init && typeof callback === 'function') {
callback(table);
Expand All @@ -1711,29 +1717,31 @@

ts.removeWidget = function(table, name, refreshing){
table = $(table)[0];
var i, widget, indx, len,
c = table.config;
// if name === true, add all widgets from $.tablesorter.widgets
if (name === true) {
name = [];
$.each( ts.widgets, function(i, w){
if (w && w.id) {
name.push( w.id );
len = ts.widgets.length;
for (indx = 0; indx < len; indx++) {
widget = ts.widgets[indx];
if (widget && widget.id) {
name.push( widget.id );
}
});
}
} else {
// name can be either an array of widgets names,
// or a space/comma separated list of widget names
name = ( $.isArray(name) ? name.join(',') : name || '' ).toLowerCase().split( /[\s,]+/ );
}
var i, widget, indx,
c = table.config,
len = name.length;
len = name.length;
for (i = 0; i < len; i++) {
widget = ts.getWidgetById(name[i]);
indx = $.inArray( name[i], c.widgets );
if ( widget && 'remove' in widget ) {
if (c.debug && indx >= 0) { log( 'Removing "' + name[i] + '" widget' ); }
widget.remove(table, c, c.widgetOptions, refreshing);
c.widgetInit[name[i]] = false;
c.widgetInit[ name[i] ] = false;
}
// don't remove the widget from config.widget if refreshing
if (indx >= 0 && refreshing !== true) {
Expand All @@ -1744,18 +1752,21 @@

ts.refreshWidgets = function(table, doAll, dontapply) {
table = $(table)[0]; // see issue #243
var c = table.config,
var indx,
c = table.config,
cw = c.widgets,
widgets = ts.widgets,
len = widgets.length,
list = [],
callback = function(table){
$(table).trigger('refreshComplete');
};
// remove widgets not defined in config.widgets, unless doAll is true
$.each( ts.widgets, function(i, w){
if (w && w.id && (doAll || $.inArray( w.id, cw ) < 0)) {
list.push( w.id );
for (indx = 0; indx < len; indx++) {
if (widgets[indx] && widgets[indx].id && (doAll || $.inArray( widgets[indx].id, cw ) < 0)) {
list.push( widgets[indx].id );
}
});
}
ts.removeWidget( table, list.join(','), true );
if (dontapply !== true) {
// call widget init if
Expand Down
4 changes: 2 additions & 2 deletions dist/js/jquery.tablesorter.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 873f6d2

Please sign in to comment.