Debian Codegraph @

js/widgets/widget-storage.js

raw symbols SCIP
1 /*! Widget: storage - updated 2018-03-18 (v2.30.0) */
2 /*global JSON:false */
3 ;(function ($, window, document) {
4 'use strict';
5
6 var ts = $.tablesorter || {};
7
8 // update defaults for validator; these values must be falsy!
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 // *** Store data in local storage, with a cookie fallback ***
22 /* IE7 needs JSON library for JSON.stringify - (http://caniuse.com/#search=json)
23 if you need it, then include https://github.com/douglascrockford/JSON-js
24
25 $.parseJSON is not available is jQuery versions older than 1.4.1, using older
26 versions will only allow storing information for one page at a time
27
28 // *** Save data (JSON format only) ***
29 // val must be valid JSON... use http://jsonlint.com/ to ensure it is valid
30 var val = { "mywidget" : "data1" }; // valid JSON uses double quotes
31 // $.tablesorter.storage(table, key, val);
32 $.tablesorter.storage(table, 'tablesorter-mywidget', val);
33
34 // *** Get data: $.tablesorter.storage(table, key); ***
35 v = $.tablesorter.storage(table, 'tablesorter-mywidget');
36 // val may be empty, so also check for your data
37 val = (v && v.hasOwnProperty('mywidget')) ? v.mywidget : '';
38 alert(val); // 'data1' if saved, or '' if not
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 // deprecating "useSessionStorage"; any storageType setting overrides it
52 session = storageType ? '' :
53 ( options && options.useSessionStorage ) || ( wo && wo.storage_useSessionStorage ),
54 $table = $(table),
55 // id from (1) options ID, (2) table 'data-table-group' attribute, (3) widgetOptions.storage_tableId,
56 // (4) table ID, then (5) table index
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 // url from (1) options url, (2) table 'data-table-page' attribute, (3) widgetOptions.storage_fixedUrl,
61 // (4) table.config.fixedUrl (deprecated), then (5) window location path
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 // skip if using cookies
67 if (storageType !== 'c') {
68 storageType = (storageType === 's' || session) ? 'sessionStorage' : 'localStorage';
69 // https://gist.github.com/paulirish/5558557
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 // *** get value ***
84 if ($.parseJSON) {
85 if (hasStorage) {
86 values = $.parseJSON( window[storageType][key] || 'null' ) || {};
87 } else {
88 // old browser, using cookies
89 cookies = document.cookie.split(/[;\s|=]/);
90 // add one to get from the key to the value
91 cookieIndex = $.inArray(key, cookies) + 1;
92 values = (cookieIndex !== 0) ? $.parseJSON(cookies[cookieIndex] || 'null') || {} : {};
93 }
94 }
95 // allow value to be an empty string too
96 if (typeof value !== 'undefined' && window.JSON && JSON.hasOwnProperty('stringify')) {
97 // add unique identifiers = url pathname > table ID/index on page > data
98 if (!values[url]) {
99 values[url] = {};
100 }
101 values[url][id] = value;
102 // *** set value ***
103 if (hasStorage) {
104 window[storageType][key] = JSON.stringify(values);
105 } else {
106 date = new Date();
107 date.setTime(date.getTime() + (31536e+6)); // 365 days
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);