Core: Add widgetClass option & fix debug logs for applying widgets. Fixes #743

This commit is contained in:
Mottie 2014-10-10 20:46:18 -05:00
parent 5cd7b33df7
commit e649b0a63c
4 changed files with 38 additions and 23 deletions

View File

@ -1040,6 +1040,23 @@
<td></td> <td></td>
</tr> </tr>
<tr id="widgetclass">
<td><a href="#" class="permalink">widgetClass</a></td>
<td>String</td>
<td>'widget-{name}'</td>
<td>When the table has a class name that matches the template and a widget id that matches the <code>{name}</code>, the widget will automatically be added to the table (<span class="version">v2.18.0</span>)
<div class="collapsible">
<br>
By default, this option is set to <code>'widget-{name}'</code>. So if the table has a class name of <code>widget-zebra</code> the zebra widget will be automatically added to the <code>config.widgets</code> option and applied to the table.<br>
<br>
Some widget ID's with special characters may not be detected; ID's with letters, numbers, underscores and/or dashes will be correctly detected.<br>
<br>
The template string *must* contain the <code>{name}</code> tag.
</div>
</td>
<td></td>
</tr>
<tr id="onrenderheader"> <tr id="onrenderheader">
<td><a href="#" class="permalink">onRenderHeader</a></td> <td><a href="#" class="permalink">onRenderHeader</a></td>
<td>Function</td> <td>Function</td>

View File

@ -75,6 +75,7 @@
zebra : [ 'even', 'odd' ] // zebra widget alternating row class names zebra : [ 'even', 'odd' ] // zebra widget alternating row class names
}, },
initWidgets : true, // apply widgets on tablesorter initialization initWidgets : true, // apply widgets on tablesorter initialization
widgetClass : 'widget-{name}', // table class name template to match to include a widget
// *** callbacks // *** callbacks
initialized : null, // function(table){}, initialized : null, // function(table){},
@ -1561,10 +1562,20 @@
var c = table.config, var c = table.config,
wo = c.widgetOptions, wo = c.widgetOptions,
widgets = [], widgets = [],
time, w, wd; time, time2, w, wd;
// prevent numerous consecutive widget applications // prevent numerous consecutive widget applications
if (init !== false && table.hasInitialized && (table.isApplyingWidgets || table.isUpdating)) { return; } if (init !== false && table.hasInitialized && (table.isApplyingWidgets || table.isUpdating)) { return; }
if (c.debug) { time = new Date(); } if (c.debug) { time = new Date(); }
wd = new RegExp( '\\b' + c.widgetClass.replace( /\{name\}/i, '([\\w-]+)' )+ '\\b', 'g' );
if ( c.table.className.match( wd ) ) {
// extract out the widget id from the table class (widget id's can include dashes)
w = c.table.className.match( wd );
if ( w ) {
$.each( w, function( i,n ){
c.widgets.push( n.replace( wd, '$1' ) );
});
}
}
if (c.widgets.length) { if (c.widgets.length) {
table.isApplyingWidgets = true; table.isApplyingWidgets = true;
// ensure unique widget ids // ensure unique widget ids
@ -1594,17 +1605,22 @@
wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo ); wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo );
} }
if (w.hasOwnProperty('init')) { if (w.hasOwnProperty('init')) {
if (c.debug) { time2 = new Date(); }
w.init(table, w, c, wo); w.init(table, w, c, wo);
if (c.debug) { ts.benchmark('Initializing ' + w.id + ' widget', time2); }
} }
} }
if (!init && w.hasOwnProperty('format')) { if (!init && w.hasOwnProperty('format')) {
if (c.debug) { time2 = new Date(); }
w.format(table, c, wo, false); w.format(table, c, wo, false);
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + w.id + ' widget', time2); }
} }
} }
}); });
} }
setTimeout(function(){ setTimeout(function(){
table.isApplyingWidgets = false; table.isApplyingWidgets = false;
$.data(table, 'lastWidgetApplication', new Date());
}, 0); }, 0);
if (c.debug) { if (c.debug) {
w = c.widgets.length; w = c.widgets.length;
@ -1888,9 +1904,6 @@
$tr.removeClass(wo.zebra[even ? 1 : 0]).addClass(wo.zebra[even ? 0 : 1]); $tr.removeClass(wo.zebra[even ? 1 : 0]).addClass(wo.zebra[even ? 0 : 1]);
}); });
} }
if (c.debug) {
ts.benchmark("Applying Zebra widget", time);
}
}, },
remove: function(table, c, wo){ remove: function(table, c, wo){
var k, $tb, var k, $tb,

View File

@ -274,7 +274,7 @@ ts.addWidget({
columns : [ "primary", "secondary", "tertiary" ] columns : [ "primary", "secondary", "tertiary" ]
}, },
format: function(table, c, wo) { format: function(table, c, wo) {
var time, $tbody, tbodyIndex, $rows, rows, $row, $cells, remove, indx, var $tbody, tbodyIndex, $rows, rows, $row, $cells, remove, indx,
$table = c.$table, $table = c.$table,
$tbodies = c.$tbodies, $tbodies = c.$tbodies,
sortList = c.sortList, sortList = c.sortList,
@ -283,9 +283,6 @@ ts.addWidget({
css = wo && wo.columns || [ "primary", "secondary", "tertiary" ], css = wo && wo.columns || [ "primary", "secondary", "tertiary" ],
last = css.length - 1; last = css.length - 1;
remove = css.join(' '); remove = css.join(' ');
if (c.debug) {
time = new Date();
}
// check if there is a sort (on initialization there may not be one) // check if there is a sort (on initialization there may not be one)
for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) { for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) {
$tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // detach tbody $tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // detach tbody
@ -325,9 +322,6 @@ ts.addWidget({
} }
} }
} }
if (c.debug) {
ts.benchmark("Applying Columns widget", time);
}
}, },
remove: function(table, c, wo) { remove: function(table, c, wo) {
var tbodyIndex, $tbody, var tbodyIndex, $tbody,
@ -581,11 +575,8 @@ ts.filter = {
and : 'and' and : 'and'
}, ts.language); }, ts.language);
var options, string, txt, $header, column, filters, val, time, fxn, noSelect, var options, string, txt, $header, column, filters, val, fxn, noSelect,
regex = ts.filter.regex; regex = ts.filter.regex;
if (c.debug) {
time = new Date();
}
c.$table.addClass('hasFilters'); c.$table.addClass('hasFilters');
// define timers so using clearTimeout won't cause an undefined error // define timers so using clearTimeout won't cause an undefined error
@ -717,9 +708,6 @@ ts.filter = {
// set filtered rows count (intially unfiltered) // set filtered rows count (intially unfiltered)
c.filteredRows = c.totalRows; c.filteredRows = c.totalRows;
if (c.debug) {
ts.benchmark("Applying Filter widget", time);
}
// add default values // add default values
c.$table.bind('tablesorter-initialized pagerInitialized', function() { c.$table.bind('tablesorter-initialized pagerInitialized', function() {
// redefine "wo" as it does not update properly inside this callback // redefine "wo" as it does not update properly inside this callback
@ -728,6 +716,7 @@ ts.filter = {
if (filters.length) { if (filters.length) {
// prevent delayInit from triggering a cache build if filters are empty // prevent delayInit from triggering a cache build if filters are empty
if ( !(c.delayInit && filters.join('') === '') ) { if ( !(c.delayInit && filters.join('') === '') ) {
ts.setFilters(table, filters, true); ts.setFilters(table, filters, true);
} }
} }

View File

@ -54,7 +54,7 @@ ts.grouping = {
update : function(table, c, wo){ update : function(table, c, wo){
if ($.isEmptyObject(c.cache)) { return; } if ($.isEmptyObject(c.cache)) { return; }
var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, time, cache, saveName, direction, var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, cache, saveName, direction,
lang = wo.grouping_language, lang = wo.grouping_language,
group = '', group = '',
savedGroup = false, savedGroup = false,
@ -67,7 +67,6 @@ ts.grouping = {
c.$table.data('pagerSavedHeight', 0); c.$table.data('pagerSavedHeight', 0);
} }
if (column >= 0 && !c.$headers.filter('[data-column="' + column + '"]:last').hasClass('group-false')) { if (column >= 0 && !c.$headers.filter('[data-column="' + column + '"]:last').hasClass('group-false')) {
if (c.debug){ time = new Date(); }
wo.group_currentGroup = ''; // save current groups wo.group_currentGroup = ''; // save current groups
wo.group_currentGroups = {}; wo.group_currentGroups = {};
@ -151,9 +150,6 @@ ts.grouping = {
} }
}); });
c.$table.trigger(wo.group_complete); c.$table.trigger(wo.group_complete);
if (c.debug) {
$.tablesorter.benchmark("Applying groups widget: ", time);
}
} }
}, },