jquery-cookies-rails 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/jquery.cookies.js +192 -155
- data/lib/jquery-cookies-rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6c4e8764fe2635eda1cf74b53c378f95069c8cc
|
4
|
+
data.tar.gz: d783bd239dcf59a7657fe72e319e4e1d66ae7b29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df020720cc0a59b2bbc59b32ad7de6864180b3a6c6bbae50a3c2f64b8e16d6ea229389e24080d2d036ac01326b557179d8038128f9bf550768fb2b166e9e674c
|
7
|
+
data.tar.gz: bc65946790e76ee0246870214ff9530fb989241313bbba53e0c2aca9466df1eace380c7a48b8b5eb7029914fe76bdb4210ec2f0f3bcdeaebfbf47866e14bd0c4
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/**
|
2
|
-
* Copyright (c) 2005 -
|
2
|
+
* Copyright (c) 2005 - 2010, James Auldridge
|
3
3
|
* All rights reserved.
|
4
4
|
*
|
5
5
|
* Licensed under the BSD, MIT, and GPL (your choice!) Licenses:
|
@@ -10,25 +10,23 @@ var jaaulde = window.jaaulde || {};
|
|
10
10
|
jaaulde.utils = jaaulde.utils || {};
|
11
11
|
jaaulde.utils.cookies = ( function()
|
12
12
|
{
|
13
|
-
var
|
14
|
-
|
15
|
-
var defaultOptions = {
|
16
|
-
hoursToLive: null,
|
13
|
+
var resolveOptions, assembleOptionsString, parseCookies, constructor, defaultOptions = {
|
14
|
+
expiresAt: null,
|
17
15
|
path: '/',
|
18
16
|
domain: null,
|
19
17
|
secure: false
|
20
18
|
};
|
21
19
|
/**
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
20
|
+
* resolveOptions - receive an options object and ensure all options are present and valid, replacing with defaults where necessary
|
21
|
+
*
|
22
|
+
* @access private
|
23
|
+
* @static
|
24
|
+
* @parameter Object options - optional options to start with
|
25
|
+
* @return Object complete and valid options object
|
26
|
+
*/
|
27
|
+
resolveOptions = function( options )
|
30
28
|
{
|
31
|
-
var returnValue;
|
29
|
+
var returnValue, expireDate;
|
32
30
|
|
33
31
|
if( typeof options !== 'object' || options === null )
|
34
32
|
{
|
@@ -37,84 +35,114 @@ jaaulde.utils.cookies = ( function()
|
|
37
35
|
else
|
38
36
|
{
|
39
37
|
returnValue = {
|
40
|
-
|
41
|
-
path:
|
42
|
-
domain:
|
43
|
-
secure:
|
38
|
+
expiresAt: defaultOptions.expiresAt,
|
39
|
+
path: defaultOptions.path,
|
40
|
+
domain: defaultOptions.domain,
|
41
|
+
secure: defaultOptions.secure
|
44
42
|
};
|
43
|
+
|
44
|
+
if( typeof options.expiresAt === 'object' && options.expiresAt instanceof Date )
|
45
|
+
{
|
46
|
+
returnValue.expiresAt = options.expiresAt;
|
47
|
+
}
|
48
|
+
else if( typeof options.hoursToLive === 'number' && options.hoursToLive !== 0 )
|
49
|
+
{
|
50
|
+
expireDate = new Date();
|
51
|
+
expireDate.setTime( expireDate.getTime() + ( options.hoursToLive * 60 * 60 * 1000 ) );
|
52
|
+
returnValue.expiresAt = expireDate;
|
53
|
+
}
|
54
|
+
|
55
|
+
if( typeof options.path === 'string' && options.path !== '' )
|
56
|
+
{
|
57
|
+
returnValue.path = options.path;
|
58
|
+
}
|
59
|
+
|
60
|
+
if( typeof options.domain === 'string' && options.domain !== '' )
|
61
|
+
{
|
62
|
+
returnValue.domain = options.domain;
|
63
|
+
}
|
64
|
+
|
65
|
+
if( options.secure === true )
|
66
|
+
{
|
67
|
+
returnValue.secure = options.secure;
|
68
|
+
}
|
45
69
|
}
|
46
70
|
|
47
71
|
return returnValue;
|
48
|
-
|
72
|
+
};
|
49
73
|
/**
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
{
|
59
|
-
var dateObject = new Date();
|
60
|
-
dateObject.setTime( dateObject.getTime() + ( hoursToLive * 60 * 60 * 1000 ) );
|
61
|
-
|
62
|
-
return dateObject.toGMTString();
|
63
|
-
};
|
64
|
-
/**
|
65
|
-
* assembleOptionsString - analyze options and assemble appropriate string for setting a cookie with those options
|
66
|
-
*
|
67
|
-
* @access private
|
68
|
-
* @static
|
69
|
-
* @parameter Object options - optional options to start with
|
70
|
-
* @return String - complete and valid cookie setting options
|
71
|
-
*/
|
72
|
-
var assembleOptionsString = function( options )
|
74
|
+
* assembleOptionsString - analyze options and assemble appropriate string for setting a cookie with those options
|
75
|
+
*
|
76
|
+
* @access private
|
77
|
+
* @static
|
78
|
+
* @parameter options OBJECT - optional options to start with
|
79
|
+
* @return STRING - complete and valid cookie setting options
|
80
|
+
*/
|
81
|
+
assembleOptionsString = function( options )
|
73
82
|
{
|
74
83
|
options = resolveOptions( options );
|
75
84
|
|
76
85
|
return (
|
77
|
-
( typeof options.
|
86
|
+
( typeof options.expiresAt === 'object' && options.expiresAt instanceof Date ? '; expires=' + options.expiresAt.toGMTString() : '' ) +
|
78
87
|
'; path=' + options.path +
|
79
88
|
( typeof options.domain === 'string' ? '; domain=' + options.domain : '' ) +
|
80
89
|
( options.secure === true ? '; secure' : '' )
|
81
90
|
);
|
82
91
|
};
|
83
92
|
/**
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
93
|
+
* parseCookies - retrieve document.cookie string and break it into a hash with values decoded and unserialized
|
94
|
+
*
|
95
|
+
* @access private
|
96
|
+
* @static
|
97
|
+
* @return OBJECT - hash of cookies from document.cookie
|
98
|
+
*/
|
99
|
+
parseCookies = function()
|
91
100
|
{
|
92
|
-
cookies = {};
|
93
|
-
|
94
|
-
for( var i = 0; i < separated.length; i = i + 1 )
|
101
|
+
var cookies = {}, i, pair, name, value, separated = document.cookie.split( ';' ), unparsedValue;
|
102
|
+
for( i = 0; i < separated.length; i = i + 1 )
|
95
103
|
{
|
96
104
|
pair = separated[i].split( '=' );
|
97
105
|
name = pair[0].replace( /^\s*/, '' ).replace( /\s*$/, '' );
|
98
|
-
|
106
|
+
|
107
|
+
try
|
108
|
+
{
|
109
|
+
value = decodeURIComponent( pair[1] );
|
110
|
+
}
|
111
|
+
catch( e1 )
|
112
|
+
{
|
113
|
+
value = pair[1];
|
114
|
+
}
|
115
|
+
|
116
|
+
if( typeof JSON === 'object' && JSON !== null && typeof JSON.parse === 'function' )
|
117
|
+
{
|
118
|
+
try
|
119
|
+
{
|
120
|
+
unparsedValue = value;
|
121
|
+
value = JSON.parse( value );
|
122
|
+
}
|
123
|
+
catch( e2 )
|
124
|
+
{
|
125
|
+
value = unparsedValue;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
99
129
|
cookies[name] = value;
|
100
130
|
}
|
101
131
|
return cookies;
|
102
132
|
};
|
103
133
|
|
104
|
-
|
105
|
-
|
134
|
+
constructor = function(){};
|
135
|
+
|
106
136
|
/**
|
107
137
|
* get - get one, several, or all cookies
|
108
138
|
*
|
109
139
|
* @access public
|
110
140
|
* @paramater Mixed cookieName - String:name of single cookie; Array:list of multiple cookie names; Void (no param):if you want all cookies
|
111
|
-
* @return Mixed -
|
141
|
+
* @return Mixed - Value of cookie as set; Null:if only one cookie is requested and is not found; Object:hash of multiple or all cookies (if multiple or all requested);
|
112
142
|
*/
|
113
143
|
constructor.prototype.get = function( cookieName )
|
114
144
|
{
|
115
|
-
var returnValue;
|
116
|
-
|
117
|
-
splitCookies();
|
145
|
+
var returnValue, item, cookies = parseCookies();
|
118
146
|
|
119
147
|
if( typeof cookieName === 'string' )
|
120
148
|
{
|
@@ -123,7 +151,7 @@ jaaulde.utils.cookies = ( function()
|
|
123
151
|
else if( typeof cookieName === 'object' && cookieName !== null )
|
124
152
|
{
|
125
153
|
returnValue = {};
|
126
|
-
for(
|
154
|
+
for( item in cookieName )
|
127
155
|
{
|
128
156
|
if( typeof cookies[cookieName[item]] !== 'undefined' )
|
129
157
|
{
|
@@ -151,16 +179,14 @@ jaaulde.utils.cookies = ( function()
|
|
151
179
|
*/
|
152
180
|
constructor.prototype.filter = function( cookieNameRegExp )
|
153
181
|
{
|
154
|
-
var returnValue = {};
|
155
|
-
|
156
|
-
splitCookies();
|
182
|
+
var cookieName, returnValue = {}, cookies = parseCookies();
|
157
183
|
|
158
184
|
if( typeof cookieNameRegExp === 'string' )
|
159
185
|
{
|
160
186
|
cookieNameRegExp = new RegExp( cookieNameRegExp );
|
161
187
|
}
|
162
188
|
|
163
|
-
for(
|
189
|
+
for( cookieName in cookies )
|
164
190
|
{
|
165
191
|
if( cookieName.match( cookieNameRegExp ) )
|
166
192
|
{
|
@@ -175,22 +201,36 @@ jaaulde.utils.cookies = ( function()
|
|
175
201
|
*
|
176
202
|
* @access public
|
177
203
|
* @paramater String cookieName - name of cookie to set
|
178
|
-
* @paramater Mixed value -
|
179
|
-
* @paramater Object options - optional list of cookie options to specify
|
204
|
+
* @paramater Mixed value - Any JS value. If not a string, will be JSON encoded; NULL to delete
|
205
|
+
* @paramater Object options - optional list of cookie options to specify
|
180
206
|
* @return void
|
181
207
|
*/
|
182
208
|
constructor.prototype.set = function( cookieName, value, options )
|
183
209
|
{
|
210
|
+
if( typeof options !== 'object' || options === null )
|
211
|
+
{
|
212
|
+
options = {};
|
213
|
+
}
|
214
|
+
|
184
215
|
if( typeof value === 'undefined' || value === null )
|
185
216
|
{
|
186
|
-
if( typeof options !== 'object' || options === null )
|
187
|
-
{
|
188
|
-
options = {};
|
189
|
-
}
|
190
217
|
value = '';
|
191
218
|
options.hoursToLive = -8760;
|
192
219
|
}
|
193
|
-
|
220
|
+
|
221
|
+
else if( typeof value !== 'string' )
|
222
|
+
{
|
223
|
+
if( typeof JSON === 'object' && JSON !== null && typeof JSON.stringify === 'function' )
|
224
|
+
{
|
225
|
+
value = JSON.stringify( value );
|
226
|
+
}
|
227
|
+
else
|
228
|
+
{
|
229
|
+
throw new Error( 'cookies.set() received non-string value and could not serialize.' );
|
230
|
+
}
|
231
|
+
}
|
232
|
+
|
233
|
+
|
194
234
|
var optionsString = assembleOptionsString( options );
|
195
235
|
|
196
236
|
document.cookie = cookieName + '=' + encodeURIComponent( value ) + optionsString;
|
@@ -205,7 +245,7 @@ jaaulde.utils.cookies = ( function()
|
|
205
245
|
*/
|
206
246
|
constructor.prototype.del = function( cookieName, options )
|
207
247
|
{
|
208
|
-
var allCookies = {};
|
248
|
+
var allCookies = {}, name;
|
209
249
|
|
210
250
|
if( typeof options !== 'object' || options === null )
|
211
251
|
{
|
@@ -221,7 +261,7 @@ jaaulde.utils.cookies = ( function()
|
|
221
261
|
allCookies[cookieName] = true;
|
222
262
|
}
|
223
263
|
|
224
|
-
for(
|
264
|
+
for( name in allCookies )
|
225
265
|
{
|
226
266
|
if( typeof name === 'string' && name !== '' )
|
227
267
|
{
|
@@ -253,7 +293,7 @@ jaaulde.utils.cookies = ( function()
|
|
253
293
|
* setOptions - set default options for calls to cookie methods
|
254
294
|
*
|
255
295
|
* @access public
|
256
|
-
* @param Object options - list of cookie options to specify
|
296
|
+
* @param Object options - list of cookie options to specify
|
257
297
|
* @return void
|
258
298
|
*/
|
259
299
|
constructor.prototype.setOptions = function( options )
|
@@ -279,125 +319,122 @@ jaaulde.utils.cookies = ( function()
|
|
279
319
|
|
280
320
|
var extensions = {
|
281
321
|
/**
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
*/
|
322
|
+
* $( 'selector' ).cookify - set the value of an input field, or the innerHTML of an element, to a cookie by the name or id of the field or element
|
323
|
+
* (field or element MUST have name or id attribute)
|
324
|
+
*
|
325
|
+
* @access public
|
326
|
+
* @param options OBJECT - list of cookie options to specify
|
327
|
+
* @return jQuery
|
328
|
+
*/
|
290
329
|
cookify: function( options )
|
291
330
|
{
|
292
331
|
return this.each( function()
|
293
332
|
{
|
294
|
-
var i,
|
333
|
+
var i, nameAttrs = ['name', 'id'], name, $this = $( this ), value;
|
295
334
|
|
296
335
|
for( i in nameAttrs )
|
297
336
|
{
|
298
337
|
if( ! isNaN( i ) )
|
299
338
|
{
|
300
|
-
name = $
|
339
|
+
name = $this.attr( nameAttrs[ i ] );
|
301
340
|
if( typeof name === 'string' && name !== '' )
|
302
341
|
{
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
342
|
+
if( $this.is( ':checkbox, :radio' ) )
|
343
|
+
{
|
344
|
+
if( $this.attr( 'checked' ) )
|
345
|
+
{
|
346
|
+
value = $this.val();
|
347
|
+
}
|
348
|
+
}
|
349
|
+
else if( $this.is( ':input' ) )
|
350
|
+
{
|
351
|
+
value = $this.val();
|
352
|
+
}
|
353
|
+
else
|
354
|
+
{
|
355
|
+
value = $this.html();
|
356
|
+
}
|
308
357
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
else
|
318
|
-
{
|
319
|
-
inputType = $( this ).attr( 'type' );
|
320
|
-
if( typeof inputType === 'string' && inputType !== '' )
|
321
|
-
{
|
322
|
-
inputType = inputType.toLowerCase();
|
323
|
-
}
|
324
|
-
if( inputType !== 'radio' && inputType !== 'checkbox' )
|
325
|
-
{
|
326
|
-
value = $( this ).val();
|
327
|
-
resolvedValue = true;
|
328
|
-
}
|
329
|
-
}
|
330
|
-
|
331
|
-
if( resolvedValue )
|
332
|
-
{
|
333
|
-
if( typeof value !== 'string' || value === '' )
|
334
|
-
{
|
335
|
-
value = null;
|
358
|
+
if( typeof value !== 'string' || value === '' )
|
359
|
+
{
|
360
|
+
value = null;
|
361
|
+
}
|
362
|
+
|
363
|
+
$.cookies.set( name, value, options );
|
364
|
+
|
365
|
+
break;
|
336
366
|
}
|
337
|
-
$.cookies.set( name, value, options );
|
338
367
|
}
|
339
368
|
}
|
340
369
|
} );
|
341
370
|
},
|
342
371
|
/**
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
372
|
+
* $( 'selector' ).cookieFill - set the value of an input field or the innerHTML of an element from a cookie by the name or id of the field or element
|
373
|
+
*
|
374
|
+
* @access public
|
375
|
+
* @return jQuery
|
376
|
+
*/
|
348
377
|
cookieFill: function()
|
349
378
|
{
|
350
379
|
return this.each( function()
|
351
380
|
{
|
352
|
-
var
|
381
|
+
var n, getN, nameAttrs = ['name', 'id'], name, $this = $( this ), value;
|
353
382
|
|
354
|
-
|
383
|
+
getN = function()
|
355
384
|
{
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
if( typeof name === 'string' && name !== '' )
|
360
|
-
{
|
361
|
-
resolvedName = true;
|
362
|
-
break;
|
363
|
-
}
|
364
|
-
}
|
365
|
-
}
|
385
|
+
n = nameAttrs.pop();
|
386
|
+
return !! n;
|
387
|
+
};
|
366
388
|
|
367
|
-
|
389
|
+
while( getN() )
|
368
390
|
{
|
369
|
-
|
370
|
-
if(
|
391
|
+
name = $this.attr( n );
|
392
|
+
if( typeof name === 'string' && name !== '' )
|
371
393
|
{
|
372
|
-
|
373
|
-
if(
|
394
|
+
value = $.cookies.get( name );
|
395
|
+
if( value !== null )
|
374
396
|
{
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
397
|
+
if( $this.is( ':checkbox, :radio' ) )
|
398
|
+
{
|
399
|
+
if( $this.val() === value )
|
400
|
+
{
|
401
|
+
$this.attr( 'checked', 'checked' );
|
402
|
+
}
|
403
|
+
else
|
404
|
+
{
|
405
|
+
$this.removeAttr( 'checked' );
|
406
|
+
}
|
407
|
+
}
|
408
|
+
else if( $this.is( ':input' ) )
|
409
|
+
{
|
410
|
+
$this.val( value );
|
411
|
+
}
|
412
|
+
else
|
413
|
+
{
|
414
|
+
$this.html( value );
|
415
|
+
}
|
380
416
|
}
|
417
|
+
|
418
|
+
break;
|
381
419
|
}
|
382
420
|
}
|
383
|
-
|
384
|
-
iteration = 0;
|
385
421
|
} );
|
386
422
|
},
|
387
423
|
/**
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
424
|
+
* $( 'selector' ).cookieBind - call cookie fill on matching elements, and bind their change events to cookify()
|
425
|
+
*
|
426
|
+
* @access public
|
427
|
+
* @param options OBJECT - list of cookie options to specify
|
428
|
+
* @return jQuery
|
429
|
+
*/
|
394
430
|
cookieBind: function( options )
|
395
431
|
{
|
396
432
|
return this.each( function()
|
397
433
|
{
|
398
|
-
$( this )
|
434
|
+
var $this = $( this );
|
435
|
+
$this.cookieFill().change( function()
|
399
436
|
{
|
400
|
-
$
|
437
|
+
$this.cookify( options );
|
401
438
|
} );
|
402
439
|
} );
|
403
440
|
}
|