2018-03-19 11:24:05 +00:00
|
|
|
/*! Widget: saveSort - updated 2018-03-19 (v2.30.1) *//*
|
2015-10-31 16:06:09 +00:00
|
|
|
* 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
|
|
|
|
2018-03-18 05:43:37 +00:00
|
|
|
function getStoredSortList(c) {
|
|
|
|
var stored = ts.storage( c.table, 'tablesorter-savesort' );
|
2018-03-19 11:24:05 +00:00
|
|
|
return (stored && stored.hasOwnProperty('sortList') && $.isArray(stored.sortList)) ? stored.sortList : [];
|
2018-03-18 05:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sortListChanged(c, sortList) {
|
|
|
|
return (sortList || getStoredSortList(c)).join(',') !== c.sortList.join(',');
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-03-18 05:43:37 +00:00
|
|
|
var time,
|
2015-07-23 04:29:51 +00:00
|
|
|
$table = c.$table,
|
|
|
|
saveSort = wo.saveSort !== false, // make saveSort active/inactive; default to true
|
2018-03-18 05:43:37 +00:00
|
|
|
sortList = { 'sortList' : c.sortList },
|
|
|
|
debug = ts.debug(c, 'saveSort');
|
|
|
|
if (debug) {
|
2015-07-23 04:29:51 +00:00
|
|
|
time = new Date();
|
2015-02-13 03:10:38 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
if ($table.hasClass('hasSaveSort')) {
|
2018-03-18 05:43:37 +00:00
|
|
|
if (saveSort && table.hasInitialized && ts.storage && sortListChanged(c)) {
|
2015-07-23 04:29:51 +00:00
|
|
|
ts.storage( table, 'tablesorter-savesort', sortList );
|
2018-03-18 05:43:37 +00:00
|
|
|
if (debug) {
|
|
|
|
console.log('saveSort >> 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) {
|
2018-03-18 05:43:37 +00:00
|
|
|
sortList = getStoredSortList(c);
|
|
|
|
if (debug) {
|
|
|
|
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
|
2018-03-18 05:43:37 +00:00
|
|
|
if (sortListChanged(c, sortList)) {
|
|
|
|
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);
|