Widgets: modify remove functions for refreshing widgets

renamed parameter from "temp" to "refreshing" & updated docs
This commit is contained in:
Mottie 2015-01-25 11:01:07 -06:00
parent b9e028d8ed
commit f1961efe8f
4 changed files with 44 additions and 28 deletions

View File

@ -139,7 +139,7 @@
</li> </li>
<li>The <code>remove</code> block (added v2.4): <li>The <code>remove</code> block (added v2.4):
<ul> <ul>
<li>In <span class="version">v2.18.5</span> the <code>temp</code> parameter was added: <li>In <span class="version">v2.18.5</span> the <code>refreshing</code> parameter was added:
<ul> <ul>
<li>It is a parameter used to indicate that the <a href="index.html#refreshwidgets"><code>refreshWidgets</code></a> method was triggered.</li> <li>It is a parameter used to indicate that the <a href="index.html#refreshwidgets"><code>refreshWidgets</code></a> method was triggered.</li>
<li>When widgets are refreshed, the <code>remove</code> method is called, then the widget <code>init</code> function is immediately called to reapply the widget.</li> <li>When widgets are refreshed, the <code>remove</code> method is called, then the widget <code>init</code> function is immediately called to reapply the widget.</li>
@ -187,10 +187,10 @@ $.tablesorter.addWidget({
// function above otherwise initFlag is undefined // function above otherwise initFlag is undefined
// * see the saveSort widget for a full example * // * see the saveSort widget for a full example *
}, },
remove: function(table, config, widgetOptions, temp){ remove: function(table, config, widgetOptions, refreshing){
// do what ever needs to be done to remove stuff added by your widget // do what ever needs to be done to remove stuff added by your widget
// unbind events, restore hidden content, etc. // unbind events, restore hidden content, etc.
// temp flag is true when the refreshWidgets method is triggered, meaning // refreshing flag is true when the refreshWidgets method is triggered, meaning
// the widget will be removed, then immediately reapplied // the widget will be removed, then immediately reapplied
} }
});</pre> });</pre>

View File

@ -4568,7 +4568,7 @@ $("table").trigger("destroy", [false, callback]);</pre></div>
<tr id="refreshwidgets"> <tr id="refreshwidgets">
<td><a href="#" class="permalink">refreshWidgets</a></td> <td><a href="#" class="permalink">refreshWidgets</a></td>
<td>Refresh the currently applied widgets. Depending on the options, it will completely remove all widgets, then re-initialize the current widgets or just remove all non-current widgets (v2.4). <td>Refresh the currently applied widgets. Depending on the options, it will completely remove all widgets, then re-initialize the current widgets or just remove all non-current widgets (v2.4; <span class="verison updated">v2.18.5</span>).
<div class="collapsible"><br> <div class="collapsible"><br>
Trigger this method using either of the following methods (they are equivalent): Trigger this method using either of the following methods (they are equivalent):
<pre class="prettyprint lang-js">// trigger a refresh widget event <pre class="prettyprint lang-js">// trigger a refresh widget event
@ -5946,7 +5946,7 @@ $.tablesorter.isValueInArray(2, sortList);</pre>
<td>This function allows the adding of custom widget scripts to the tablesorter core (<span class="version updated">v2.18.5</span>). <td>This function allows the adding of custom widget scripts to the tablesorter core (<span class="version updated">v2.18.5</span>).
<div class="collapsible"> <div class="collapsible">
<br> <br>
In <span class="version updated">v2.18.5</span>, a <code>"temp"</code> parameter was added to the <code>remove</code> widget function to indicate that the widget will be refreshed so it will only be temporarily removed (see <a href="example-widgets.html">this demo</a> for more details).<br> In <span class="version updated">v2.18.5</span>, a <code>"refreshing"</code> parameter was added to the <code>remove</code> widget function to indicate that the widget will be refreshed so it will only be temporarily removed (see <a href="example-widgets.html">this demo</a> for more details).<br>
<br> <br>
Access it as follows: Access it as follows:
<pre class="prettyprint lang-js">$.tablesorter.addWidget(myWidget);</pre> <pre class="prettyprint lang-js">$.tablesorter.addWidget(myWidget);</pre>
@ -6002,7 +6002,7 @@ widget.format( table, table.config, table.config.widgetOptions );</pre>
<td>This function removes, then reapplies all currently selected widgets on a table (v2.4; <span class="version updated">v2.18.5</span>). <td>This function removes, then reapplies all currently selected widgets on a table (v2.4; <span class="version updated">v2.18.5</span>).
<div class="collapsible"> <div class="collapsible">
<br> <br>
In <span class="version updated">v2.18.5</span>, this function was modified to internally use the <a href="#function-removewidget">removeWidget</a> function &amp; a <a href="#refreshcomplete"><code>"refreshComplete"</code></a> event is now triggered after the refresh is complete.<br> In <span class="version updated">v2.18.5</span>, this function was modified to internally use the <a href="#function-removewidget">removeWidget</a> function &amp; a <a href="#refreshcomplete"><code>"refreshComplete"</code></a> event is now triggered upon completion.<br>
<br> <br>
Use it as follows: Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.refreshWidgets( table, doAll, dontapply );</pre> <pre class="prettyprint lang-js">$.tablesorter.refreshWidgets( table, doAll, dontapply );</pre>
@ -6012,6 +6012,7 @@ widget.format( table, table.config, table.config.widgetOptions );</pre>
<li><code>dontapply</code> - optional, boolean flag.</li> <li><code>dontapply</code> - optional, boolean flag.</li>
</ul> </ul>
The <code>doAll</code> flag is set to <code>true</code> if all widgets contained with the global <a href="#variable-widgets"><code>$.tablesorter.widgets</code></a> array are to be removed.<br> The <code>doAll</code> flag is set to <code>true</code> if all widgets contained with the global <a href="#variable-widgets"><code>$.tablesorter.widgets</code></a> array are to be removed.<br>
When <code>doAll</code> is <code>false</code>, only widgets <em>not contained</em> within the <code>config.widget</code> option are removed; then if the <code>dontapply</code> flag is <code>false</code>, the widgets named in that option are reapplied (without removing them).<br>
<br> <br>
This function is called when the <a href="#refreshwidgets">refreshWidgets</a> method is triggered. This function is called when the <a href="#refreshwidgets">refreshWidgets</a> method is triggered.
</div> </div>
@ -6023,12 +6024,13 @@ widget.format( table, table.config, table.config.widgetOptions );</pre>
<td>This function removes selected widgets (<span class="version">v2.18.5</span>). <td>This function removes selected widgets (<span class="version">v2.18.5</span>).
<div class="collapsible"><br> <div class="collapsible"><br>
Use it as follows: Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.removeWidget( table, names );</pre> <pre class="prettyprint lang-js">$.tablesorter.removeWidget( table, names, refreshing );</pre>
<ul> <ul>
<li><code>table</code> - table DOM element (or jQuery object) of table.</li> <li><code>table</code> - table DOM element (or jQuery object) of table.</li>
<li><code>names</code> - string (space or comma separated widget names) or an array of widget names.</li> <li><code>names</code> - string (space or comma separated widget names), an array of widget names, or if <code>true</code> all installed widgets are removed.</li>
<li><code>refreshing</code> - if <code>true</code>, the widget name will not be removed from the <a href="#widgets"><code>widgets</code></a> option; any other setting and the name will be removed.</li>
</ul> </ul>
This function is used by the <a href="#function-refreshwidgets">refreshWidgets</a> function. This function is used by the <a href="#function-refreshwidgets">refreshWidgets</a> function (<span class="version">v2.18.5</span>).
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -862,7 +862,7 @@
.bind("updateAll" + c.namespace, function(e, resort, callback){ .bind("updateAll" + c.namespace, function(e, resort, callback){
e.stopPropagation(); e.stopPropagation();
table.isUpdating = true; table.isUpdating = true;
ts.refreshWidgets(table, true, true, true); ts.refreshWidgets(table, true, true);
ts.restoreHeaders(table); ts.restoreHeaders(table);
buildHeaders(table); buildHeaders(table);
ts.bindEvents(table, c.$headers, true); ts.bindEvents(table, c.$headers, true);
@ -1008,9 +1008,9 @@
// apply widgets // apply widgets
ts.applyWidget(table, init); ts.applyWidget(table, init);
}) })
.bind("refreshWidgets" + c.namespace, function(e, all, dontapply, temp){ .bind("refreshWidgets" + c.namespace, function(e, all, dontapply){
e.stopPropagation(); e.stopPropagation();
ts.refreshWidgets(table, all, dontapply, temp); ts.refreshWidgets(table, all, dontapply);
}) })
.bind("destroy" + c.namespace, function(e, c, cb){ .bind("destroy" + c.namespace, function(e, c, cb){
e.stopPropagation(); e.stopPropagation();
@ -1018,7 +1018,7 @@
}) })
.bind("resetToLoadState" + c.namespace, function(){ .bind("resetToLoadState" + c.namespace, function(){
// remove all widgets // remove all widgets
ts.refreshWidgets(table, true, true); ts.removeWidget(table, true, false);
// restore original settings; this clears out current settings, but does not clear // restore original settings; this clears out current settings, but does not clear
// values saved to storage. // values saved to storage.
c = $.extend(true, ts.defaults, c.originalSettings); c = $.extend(true, ts.defaults, c.originalSettings);
@ -1366,7 +1366,7 @@
table = $(table)[0]; table = $(table)[0];
if (!table.hasInitialized) { return; } if (!table.hasInitialized) { return; }
// remove all widgets // remove all widgets
ts.refreshWidgets(table, true, true); ts.removeWidget(table, true, false);
var $t = $(table), c = table.config, var $t = $(table), c = table.config,
$h = $t.find('thead:first'), $h = $t.find('thead:first'),
$r = $h.find('tr.' + ts.css.headerRow).removeClass(ts.css.headerRow + ' ' + c.cssHeaderRow), $r = $h.find('tr.' + ts.css.headerRow).removeClass(ts.css.headerRow + ' ' + c.cssHeaderRow),
@ -1675,11 +1675,21 @@
} }
}; };
ts.removeWidget = function(table, name, refresh, temp){ ts.removeWidget = function(table, name, refreshing){
table = $(table)[0]; table = $(table)[0];
// name can be either an array of widgets names, // if name === true, add all widgets from $.tablesorter.widgets
// or a space/comma separated list of widget names if (name === true) {
name = ( $.isArray(name) ? name.join(',') : name || '' ).toLowerCase().split( /[\s,]+/ ); name = [];
$.each( ts.widgets, function(i, w){
if (w && w.id) {
name.push( w.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, var i, widget, indx,
c = table.config, c = table.config,
len = name.length; len = name.length;
@ -1688,17 +1698,17 @@
indx = $.inArray( name[i], c.widgets ); indx = $.inArray( name[i], c.widgets );
if ( widget && 'remove' in widget ) { if ( widget && 'remove' in widget ) {
if (c.debug && indx >= 0) { log( 'Removing "' + name[i] + '" widget' ); } if (c.debug && indx >= 0) { log( 'Removing "' + name[i] + '" widget' ); }
widget.remove(table, c, c.widgetOptions, temp); 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 // don't remove the widget from config.widget if refreshing
if (indx >= 0 && refresh !== true) { if (indx >= 0 && refreshing !== true) {
c.widgets.splice( indx, 1 ); c.widgets.splice( indx, 1 );
} }
} }
}; };
ts.refreshWidgets = function(table, doAll, dontapply, temp) { ts.refreshWidgets = function(table, doAll, dontapply) {
table = $(table)[0]; // see issue #243 table = $(table)[0]; // see issue #243
var c = table.config, var c = table.config,
cw = c.widgets, cw = c.widgets,
@ -1712,7 +1722,7 @@
list.push( w.id ); list.push( w.id );
} }
}); });
ts.removeWidget( table, list.join(','), true, temp ); ts.removeWidget( table, list.join(','), true );
if (dontapply !== true) { if (dontapply !== true) {
// call widget init if // call widget init if
ts.applyWidget(table, doAll || false, callback ); ts.applyWidget(table, doAll || false, callback );
@ -1981,7 +1991,8 @@
}); });
} }
}, },
remove: function(table, c, wo){ remove: function(table, c, wo, refreshing){
if (refreshing) { return; }
var k, $tb, var k, $tb,
b = c.$tbodies, b = c.$tbodies,
rmv = (wo.zebra || [ "even", "odd" ]).join(' '); rmv = (wo.zebra || [ "even", "odd" ]).join(' ');

View File

@ -288,15 +288,17 @@ ts.addWidget({
ts.benchmark("Applying " + theme + " theme", time); ts.benchmark("Applying " + theme + " theme", time);
} }
}, },
remove: function(table, c, wo, temp) { remove: function(table, c, wo, refreshing) {
if (!wo.uitheme_applied) { return; }
var $table = c.$table, var $table = c.$table,
theme = c.theme || 'jui', theme = c.appliedTheme || 'jui',
themes = ts.themes[ theme ] || ts.themes.jui, themes = ts.themes[ theme ] || ts.themes.jui,
$headers = $table.children('thead').children(), $headers = $table.children('thead').children(),
remove = themes.sortNone + ' ' + themes.sortDesc + ' ' + themes.sortAsc, remove = themes.sortNone + ' ' + themes.sortDesc + ' ' + themes.sortAsc,
iconRmv = themes.iconSortNone + ' ' + themes.iconSortDesc + ' ' + themes.iconSortAsc; iconRmv = themes.iconSortNone + ' ' + themes.iconSortDesc + ' ' + themes.iconSortAsc;
$table.removeClass('tablesorter-' + theme + ' ' + themes.table); $table.removeClass('tablesorter-' + theme + ' ' + themes.table);
if (temp) { return; } wo.uitheme_applied = false;
if (refreshing) { return; }
$table.find(ts.css.header).removeClass(themes.header); $table.find(ts.css.header).removeClass(themes.header);
$headers $headers
.unbind('mouseenter.tsuitheme mouseleave.tsuitheme') // remove hover .unbind('mouseenter.tsuitheme mouseleave.tsuitheme') // remove hover
@ -421,7 +423,7 @@ ts.addWidget({
ts.filter.init(table, c, wo); ts.filter.init(table, c, wo);
} }
}, },
remove: function(table, c, wo, temp) { remove: function(table, c, wo, refreshing) {
var tbodyIndex, $tbody, var tbodyIndex, $tbody,
$table = c.$table, $table = c.$table,
$tbodies = c.$tbodies; $tbodies = c.$tbodies;
@ -429,8 +431,9 @@ ts.addWidget({
.removeClass('hasFilters') .removeClass('hasFilters')
// add .tsfilter namespace to all BUT search // add .tsfilter namespace to all BUT search
.unbind('addRows updateCell update updateRows updateComplete appendCache filterReset filterEnd search '.split(' ').join(c.namespace + 'filter ')) .unbind('addRows updateCell update updateRows updateComplete appendCache filterReset filterEnd search '.split(' ').join(c.namespace + 'filter '))
// remove the filter row even if refreshing, because the column might have been moved
.find('.' + ts.css.filterRow).remove(); .find('.' + ts.css.filterRow).remove();
if (temp) { return; } if (refreshing) { return; }
for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) { for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) {
$tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // remove tbody $tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // remove tbody
$tbody.children().removeClass(wo.filter_filteredRow).show(); $tbody.children().removeClass(wo.filter_filteredRow).show();