forked from carhartl/jquery-cookie
/
jquery.remember.js
220 lines (197 loc) · 8.11 KB
/
jquery.remember.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*!
*
* jQuery Remember Plugin
* Version 0.1.1
*
* Copyright Nick Dreckshage, licensed GPL & MIT
* https://github.com/ndreckshage/jquery-remember
*
* A fork of jQuery Cookie Plugin
* https://github.com/carhartl/jquery-cookie
* Copyright Klaus Hartl
* Released under the MIT licensed
*
*/
(function($){
// recommend changing to return function(options) { if using require.js...
$.remember = function(options){
var settings,
remember,
controller;
settings = $.extend({
name: null, // name/key of the cookie/localstorage item
value: undefined, // value pair of cookie/localstorage
getSet: false, // if true, will get if available, set if not. default is to just get OR set
remove: false, // if true, will remove based on name/key
use: 'default', // whether to use localstorage or cookies. default localstorage with cookie fallback.
expires: null, // forces cookie (invalid localstorage attribue).
path: null, // forces cookie.
domain: null, // forces cookie.
secure: null, // forces cookie.
json: false, // will convert to json when set. parse with get.
fallback: true, // whether to fallback to cookies if localstorage not available.
raw: false, // if true, will skip uri encoding/decoding
modernizr: false // set true if youd rather handle localstorage detection through modernizr
}, options);
remember = {
init: function(){
var controller;
// controls what to do with the input. set - get - set/get - erase
// set if name exists, value exists, and not set to remove cookie.
// get if name exists, value does not exist, and not set to remove
// remove if remove set to true
if (settings.name !== null && settings.value !== undefined && settings.remove !== true){
if (settings.getSet === true){
var get = this.get();
// if the value of get exists, return it. otherwise, set the value as specified.
if (get === null){
this.set();
} else {
controller = get;
}
} else {
this.set();
}
} else if (settings.name !== null && settings.value === undefined && settings.remove !== true){
controller = this.get();
} else if (settings.name !== null && settings.remove === true){
this.erase();
}
// will return result of everything to calling js
return controller;
},
get: function(){
var use = this._use(),
value = null,
cookies,
parts,
name;
// grab the key value pair from localstorage
if (use === 'localstorage') {
value = localStorage.getItem(settings.name);
}
// hit if cookie requested, and when double checking a get on an empty localstorage item
if ((use === 'cookie') || (use === 'localstorage' && value === null && settings.fallback !== false)) {
// grab all the cookies from the browser, check if set, and loop through
cookies = document.cookie ? document.cookie : null;
if (cookies !== null){
cookies = cookies.split(';');
for (var i = 0; i < cookies.length; i++){
// separate the key value pair
parts = cookies[i].split('=');
// set name and value to split parts, cleaning up whitespace
name = parts.shift();
name = settings.raw === false ? this._trim(this._decode(name)) : this._trim(name);
value = parts[0];
// break when we hit a match, or cookie is empty
if (settings.name === name) {
break;
} else if (settings.fallback !== false) {
value = localStorage.getItem(settings.name) || null;
} else {
value = null;
}
}
}
}
// decode uri and if json is requested, parse the cookie/localstorage and return to controller
value = (value && settings.raw === false) ? this._decode(value) : value;
value = (value && settings.json === true) ? JSON.parse(value) : value;
return value;
},
set: function(){
var use = this._use();
// if set is hit, user has intentionally tried to set (get/set not hit)
// clear the storage alternative, so the same value isnt stored in both
this.erase();
// convert the value to store in json if requested
settings.value = settings.json === true ? JSON.stringify(settings.value) : settings.value;
// encode
settings.name = settings.raw === false ? encodeURIComponent(settings.name) : settings.name;
settings.value = settings.raw === false ? encodeURIComponent(settings.value) : settings.value;
// store the key value pair in appropriate storage. set unless storage requirements failed
if (use === 'localstorage'){
localStorage.setItem(settings.name, settings.value);
} else if (use !== false){
// convert values that cant be stored and set
settings.value = settings.value === null ? 'null' : settings.value;
this._setCookie();
}
},
erase: function(){
var use = this._use();
// clear localstorage and cookies by setting expiration to negative
if (use !== 'cookie' || settings.fallback !== false){
localStorage.removeItem(settings.name);
}
if (use !== 'localstorage' || settings.fallback !== false){
this._setCookie('', -1);
}
},
_use: function(){
var use,
localStorageSupport = this._localStorage();
// if cookie requested, or any options set that only apply to cookies
if (settings.use === 'cookie' || settings.expires !== null || settings.path !== null || settings.domain !== null || settings.secure !== null){
use = 'cookie';
} else {
// use local storage if available
if (localStorageSupport){
use = 'localstorage';
} else if (settings.fallback !== false) {
// default to cookie, unless fallback banned
use = 'cookie';
} else {
// if all this fails, nothing can be set
use = false;
}
}
return use;
},
_setCookie: function(){
// allow for varying parameters with defaults. value then expires as optional params
var value = arguments.length > 0 ? arguments[0] : settings.value,
expires = arguments.length > 1 ? arguments[1] : settings.expires,
expire;
// set a date in the future (or negative to delete) based on expires date offset
if (typeof expires === 'number') {
expire = new Date();
expire.setDate(expire.getDate() + expires);
}
// set the cookies with all the varying settings
document.cookie = [
settings.name,
'=',
value,
expire ? '; expires=' + expire.toUTCString() : '',
settings.path ? '; path=' + settings.path : '',
settings.domain ? '; domain=' + settings.domain : '',
settings.secure ? '; secure' : ''
].join('');
},
_localStorage: function(){
if (settings.modernizr === true && typeof Modernizr !== 'undefined'){
return Modernizr.localstorage;
} else {
// check if a browser supports localstorage with simple try catch
try {
localStorage.setItem('jquery-remember-test','jquery-remember-test');
localStorage.removeItem('jquery-remember-test');
return true;
} catch(e){
return false;
}
}
},
_trim: function(s){
// trail a strings leading/ending whitespace
return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
},
_decode: function(s){
return decodeURIComponent(s.replace(/\+/g, ' '));
}
};
return remember.init();
};
return $.remember;
}(jQuery));