tablesorter/js/widgets/widget-storage.js
Mottie 2774abf8d8 Build: Add grunt build process
Rearranged, renamed & broke apart other files
2015-02-12 21:10:38 -06:00

77 lines
2.8 KiB
JavaScript

/*! Widget: storage */
;(function ($, window, document) {
'use strict';
var ts = $.tablesorter = $.tablesorter || {};
// *** Store data in local storage, with a cookie fallback ***
/* IE7 needs JSON library for JSON.stringify - (http://caniuse.com/#search=json)
if you need it, then include https://github.com/douglascrockford/JSON-js
$.parseJSON is not available is jQuery versions older than 1.4.1, using older
versions will only allow storing information for one page at a time
// *** Save data (JSON format only) ***
// val must be valid JSON... use http://jsonlint.com/ to ensure it is valid
var val = { "mywidget" : "data1" }; // valid JSON uses double quotes
// $.tablesorter.storage(table, key, val);
$.tablesorter.storage(table, 'tablesorter-mywidget', val);
// *** Get data: $.tablesorter.storage(table, key); ***
v = $.tablesorter.storage(table, 'tablesorter-mywidget');
// val may be empty, so also check for your data
val = (v && v.hasOwnProperty('mywidget')) ? v.mywidget : '';
alert(val); // "data1" if saved, or "" if not
*/
ts.storage = function(table, key, value, options) {
table = $(table)[0];
var cookieIndex, cookies, date,
hasLocalStorage = false,
values = {},
c = table.config,
$table = $(table),
id = options && options.id || $table.attr(options && options.group ||
'data-table-group') || table.id || $('.tablesorter').index( $table ),
url = options && options.url || $table.attr(options && options.page ||
'data-table-page') || c && c.fixedUrl || window.location.pathname;
// https://gist.github.com/paulirish/5558557
if ('localStorage' in window) {
try {
window.localStorage.setItem('_tmptest', 'temp');
hasLocalStorage = true;
window.localStorage.removeItem('_tmptest');
} catch(error) {}
}
// *** get value ***
if ($.parseJSON) {
if (hasLocalStorage) {
values = $.parseJSON(localStorage[key] || 'null') || {};
} else {
// old browser, using cookies
cookies = document.cookie.split(/[;\s|=]/);
// add one to get from the key to the value
cookieIndex = $.inArray(key, cookies) + 1;
values = (cookieIndex !== 0) ? $.parseJSON(cookies[cookieIndex] || 'null') || {} : {};
}
}
// allow value to be an empty string too
if ((value || value === '') && window.JSON && JSON.hasOwnProperty('stringify')) {
// add unique identifiers = url pathname > table ID/index on page > data
if (!values[url]) {
values[url] = {};
}
values[url][id] = value;
// *** set value ***
if (hasLocalStorage) {
localStorage[key] = JSON.stringify(values);
} else {
date = new Date();
date.setTime(date.getTime() + (31536e+6)); // 365 days
document.cookie = key + '=' + (JSON.stringify(values)).replace(/\"/g,'\"') + '; expires=' + date.toGMTString() + '; path=/';
}
} else {
return values && values[url] ? values[url][id] : '';
}
};
})(jQuery, window, document);