2015-10-31 16:06:09 +00:00
|
|
|
/*! Widget: saveSort - updated 10/31/2015 (v2.24.0) *//*
|
|
|
|
* Requires tablesorter v2.16+
|
|
|
|
* by Rob Garrison
|
|
|
|
*/
|
2015-02-13 03:10:38 +00:00
|
|
|
;(function ($) {
|
2015-07-23 04:29:51 +00:00
|
|
|
'use strict';
|
|
|
|
var ts = $.tablesorter || {};
|
2015-02-13 03:10:38 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
// this widget saves the last sort only if the
|
|
|
|
// saveSort widget option is true AND the
|
|
|
|
// $.tablesorter.storage function is included
|
|
|
|
// **************************
|
|
|
|
ts.addWidget({
|
|
|
|
id: 'saveSort',
|
|
|
|
priority: 20,
|
|
|
|
options: {
|
|
|
|
saveSort : true
|
|
|
|
},
|
|
|
|
init: function(table, thisWidget, c, wo) {
|
|
|
|
// run widget format before all other widgets are applied to the table
|
|
|
|
thisWidget.format(table, c, wo, true);
|
|
|
|
},
|
|
|
|
format: function(table, c, wo, init) {
|
|
|
|
var stored, time,
|
|
|
|
$table = c.$table,
|
|
|
|
saveSort = wo.saveSort !== false, // make saveSort active/inactive; default to true
|
|
|
|
sortList = { 'sortList' : c.sortList };
|
|
|
|
if (c.debug) {
|
|
|
|
time = new Date();
|
2015-02-13 03:10:38 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
if ($table.hasClass('hasSaveSort')) {
|
|
|
|
if (saveSort && table.hasInitialized && ts.storage) {
|
|
|
|
ts.storage( table, 'tablesorter-savesort', sortList );
|
|
|
|
if (c.debug) {
|
2015-07-24 23:23:00 +00:00
|
|
|
console.log('saveSort widget: Saving last sort: ' + c.sortList + ts.benchmark(time));
|
2015-07-23 04:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// set table sort on initial run of the widget
|
|
|
|
$table.addClass('hasSaveSort');
|
|
|
|
sortList = '';
|
|
|
|
// get data
|
|
|
|
if (ts.storage) {
|
|
|
|
stored = ts.storage( table, 'tablesorter-savesort' );
|
|
|
|
sortList = (stored && stored.hasOwnProperty('sortList') && $.isArray(stored.sortList)) ? stored.sortList : '';
|
|
|
|
if (c.debug) {
|
2015-07-24 23:23:00 +00:00
|
|
|
console.log('saveSort: Last sort loaded: "' + sortList + '"' + ts.benchmark(time));
|
2015-07-23 04:29:51 +00:00
|
|
|
}
|
|
|
|
$table.bind('saveSortReset', function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
ts.storage( table, 'tablesorter-savesort', '' );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// init is true when widget init is run, this will run this widget before all other widgets have initialized
|
|
|
|
// this method allows using this widget in the original tablesorter plugin; but then it will run all widgets twice.
|
|
|
|
if (init && sortList && sortList.length > 0) {
|
|
|
|
c.sortList = sortList;
|
|
|
|
} else if (table.hasInitialized && sortList && sortList.length > 0) {
|
|
|
|
// update sort change
|
2015-10-22 21:41:34 +00:00
|
|
|
ts.sortOn( c, sortList );
|
2015-02-13 03:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
},
|
|
|
|
remove: function(table, c) {
|
|
|
|
c.$table.removeClass('hasSaveSort');
|
|
|
|
// clear storage
|
|
|
|
if (ts.storage) { ts.storage( table, 'tablesorter-savesort', '' ); }
|
2015-02-13 03:10:38 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
});
|
2015-02-13 03:10:38 +00:00
|
|
|
|
|
|
|
})(jQuery);
|