Debian Codegraph @

js/widgets/widget-saveSort.js

raw symbols SCIP
1 /*! Widget: saveSort - updated 2018-03-19 (v2.30.1) *//*
2 * Requires tablesorter v2.16+
3 * by Rob Garrison
4 */
5 ;(function ($) {
6 'use strict';
7 var ts = $.tablesorter || {};
8
9 function getStoredSortList(c) {
10 var stored = ts.storage( c.table, 'tablesorter-savesort' );
11 return (stored && stored.hasOwnProperty('sortList') && $.isArray(stored.sortList)) ? stored.sortList : [];
12 }
13
14 function sortListChanged(c, sortList) {
15 return (sortList || getStoredSortList(c)).join(',') !== c.sortList.join(',');
16 }
17
18 // this widget saves the last sort only if the
19 // saveSort widget option is true AND the
20 // $.tablesorter.storage function is included
21 // **************************
22 ts.addWidget({
23 id: 'saveSort',
24 priority: 20,
25 options: {
26 saveSort : true
27 },
28 init: function(table, thisWidget, c, wo) {
29 // run widget format before all other widgets are applied to the table
30 thisWidget.format(table, c, wo, true);
31 },
32 format: function(table, c, wo, init) {
33 var time,
34 $table = c.$table,
35 saveSort = wo.saveSort !== false, // make saveSort active/inactive; default to true
36 sortList = { 'sortList' : c.sortList },
37 debug = ts.debug(c, 'saveSort');
38 if (debug) {
39 time = new Date();
40 }
41 if ($table.hasClass('hasSaveSort')) {
42 if (saveSort && table.hasInitialized && ts.storage && sortListChanged(c)) {
43 ts.storage( table, 'tablesorter-savesort', sortList );
44 if (debug) {
45 console.log('saveSort >> Saving last sort: ' + c.sortList + ts.benchmark(time));
46 }
47 }
48 } else {
49 // set table sort on initial run of the widget
50 $table.addClass('hasSaveSort');
51 sortList = '';
52 // get data
53 if (ts.storage) {
54 sortList = getStoredSortList(c);
55 if (debug) {
56 console.log('saveSort >> Last sort loaded: "' + sortList + '"' + ts.benchmark(time));
57 }
58 $table.bind('saveSortReset', function(event) {
59 event.stopPropagation();
60 ts.storage( table, 'tablesorter-savesort', '' );
61 });
62 }
63 // init is true when widget init is run, this will run this widget before all other widgets have initialized
64 // this method allows using this widget in the original tablesorter plugin; but then it will run all widgets twice.
65 if (init && sortList && sortList.length > 0) {
66 c.sortList = sortList;
67 } else if (table.hasInitialized && sortList && sortList.length > 0) {
68 // update sort change
69 if (sortListChanged(c, sortList)) {
70 ts.sortOn(c, sortList);
71 }
72 }
73 }
74 },
75 remove: function(table, c) {
76 c.$table.removeClass('hasSaveSort');
77 // clear storage
78 if (ts.storage) { ts.storage( table, 'tablesorter-savesort', '' ); }
79 }
80 });
81
82 })(jQuery);