| 1 |
|
| 2 |
|
| 3 |
;(function ($, window, document) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
var ts = $.tablesorter || {}; |
| 7 |
|
| 8 |
|
| 9 |
$.extend(true, ts.defaults, { |
| 10 |
fixedUrl: '', |
| 11 |
widgetOptions: { |
| 12 |
storage_fixedUrl: '', |
| 13 |
storage_group: '', |
| 14 |
storage_page: '', |
| 15 |
storage_storageType: '', |
| 16 |
storage_tableId: '', |
| 17 |
storage_useSessionStorage: '' |
| 18 |
} |
| 19 |
}); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
ts.storage = function(table, key, value, options) { |
| 41 |
table = $(table)[0]; |
| 42 |
var cookieIndex, cookies, date, |
| 43 |
hasStorage = false, |
| 44 |
values = {}, |
| 45 |
c = table.config, |
| 46 |
wo = c && c.widgetOptions, |
| 47 |
debug = ts.debug(c, 'storage'), |
| 48 |
storageType = ( |
| 49 |
( options && options.storageType ) || ( wo && wo.storage_storageType ) |
| 50 |
).toString().charAt(0).toLowerCase(), |
| 51 |
|
| 52 |
session = storageType ? '' : |
| 53 |
( options && options.useSessionStorage ) || ( wo && wo.storage_useSessionStorage ), |
| 54 |
$table = $(table), |
| 55 |
|
| 56 |
|
| 57 |
id = options && options.id || |
| 58 |
$table.attr( options && options.group || wo && wo.storage_group || 'data-table-group') || |
| 59 |
wo && wo.storage_tableId || table.id || $('.tablesorter').index( $table ), |
| 60 |
|
| 61 |
|
| 62 |
url = options && options.url || |
| 63 |
$table.attr(options && options.page || wo && wo.storage_page || 'data-table-page') || |
| 64 |
wo && wo.storage_fixedUrl || c && c.fixedUrl || window.location.pathname; |
| 65 |
|
| 66 |
|
| 67 |
if (storageType !== 'c') { |
| 68 |
storageType = (storageType === 's' || session) ? 'sessionStorage' : 'localStorage'; |
| 69 |
|
| 70 |
if (storageType in window) { |
| 71 |
try { |
| 72 |
window[storageType].setItem('_tmptest', 'temp'); |
| 73 |
hasStorage = true; |
| 74 |
window[storageType].removeItem('_tmptest'); |
| 75 |
} catch (error) { |
| 76 |
console.warn( storageType + ' is not supported in this browser' ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
if (debug) { |
| 81 |
console.log('Storage >> Using', hasStorage ? storageType : 'cookies'); |
| 82 |
} |
| 83 |
|
| 84 |
if ($.parseJSON) { |
| 85 |
if (hasStorage) { |
| 86 |
values = $.parseJSON( window[storageType][key] || 'null' ) || {}; |
| 87 |
} else { |
| 88 |
|
| 89 |
cookies = document.cookie.split(/[;\s|=]/); |
| 90 |
|
| 91 |
cookieIndex = $.inArray(key, cookies) + 1; |
| 92 |
values = (cookieIndex !== 0) ? $.parseJSON(cookies[cookieIndex] || 'null') || {} : {}; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if (typeof value !== 'undefined' && window.JSON && JSON.hasOwnProperty('stringify')) { |
| 97 |
|
| 98 |
if (!values[url]) { |
| 99 |
values[url] = {}; |
| 100 |
} |
| 101 |
values[url][id] = value; |
| 102 |
|
| 103 |
if (hasStorage) { |
| 104 |
window[storageType][key] = JSON.stringify(values); |
| 105 |
} else { |
| 106 |
date = new Date(); |
| 107 |
date.setTime(date.getTime() + (31536e+6)); |
| 108 |
document.cookie = key + '=' + (JSON.stringify(values)).replace(/\"/g, '\"') + '; expires=' + date.toGMTString() + '; path=/'; |
| 109 |
} |
| 110 |
} else { |
| 111 |
return values && values[url] ? values[url][id] : ''; |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
})(jQuery, window, document); |