mirror of
https://github.com/Mottie/tablesorter.git
synced 2024-11-15 23:54:22 +00:00
40 lines
2.7 KiB
JavaScript
40 lines
2.7 KiB
JavaScript
/*! Widget: storage - updated 3/26/2015 (v2.21.3) */
|
|
/*global JSON:false */
|
|
!function(a,b,c){"use strict";var d=a.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
|
|
*/
|
|
d.storage=function(d,e,f,g){d=a(d)[0];var h,i,j,k=!1,l={},m=d.config,n=m&&m.widgetOptions,o=g&&g.useSessionStorage||n&&n.storage_useSessionStorage?"sessionStorage":"localStorage",p=a(d),
|
|
// id from (1) options ID, (2) table 'data-table-group' attribute, (3) widgetOptions.storage_tableId,
|
|
// (4) table ID, then (5) table index
|
|
q=g&&g.id||p.attr(g&&g.group||n&&n.storage_group||"data-table-group")||n&&n.storage_tableId||d.id||a(".tablesorter").index(p),
|
|
// url from (1) options url, (2) table 'data-table-page' attribute, (3) widgetOptions.storage_fixedUrl,
|
|
// (4) table.config.fixedUrl (deprecated), then (5) window location path
|
|
r=g&&g.url||p.attr(g&&g.page||n&&n.storage_page||"data-table-page")||n&&n.storage_fixedUrl||m&&m.fixedUrl||b.location.pathname;
|
|
// https://gist.github.com/paulirish/5558557
|
|
if(o in b)try{b[o].setItem("_tmptest","temp"),k=!0,b[o].removeItem("_tmptest")}catch(s){m&&m.debug&&console.warn(o+" is not supported in this browser")}
|
|
// allow value to be an empty string too
|
|
// *** get value ***
|
|
// old browser, using cookies
|
|
// add one to get from the key to the value
|
|
// allow value to be an empty string too
|
|
// add unique identifiers = url pathname > table ID/index on page > data
|
|
// *** set value ***
|
|
// 365 days
|
|
return a.parseJSON&&(k?l=a.parseJSON(b[o][e]||"null")||{}:(i=c.cookie.split(/[;\s|=]/),h=a.inArray(e,i)+1,l=0!==h?a.parseJSON(i[h]||"null")||{}:{})),(f||""===f)&&b.JSON&&JSON.hasOwnProperty("stringify")?(l[r]||(l[r]={}),l[r][q]=f,k?b[o][e]=JSON.stringify(l):(j=new Date,j.setTime(j.getTime()+31536e6),c.cookie=e+"="+JSON.stringify(l).replace(/\"/g,'"')+"; expires="+j.toGMTString()+"; path=/"),void 0):l&&l[r]?l[r][q]:""}}(jQuery,window,document); |