jquery-cookies-rails 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72ce6daf269f8eb31dad2f90aadf91606578c539
4
- data.tar.gz: d4d8b626670092617879341c95529a88b15f24e7
3
+ metadata.gz: c6c4e8764fe2635eda1cf74b53c378f95069c8cc
4
+ data.tar.gz: d783bd239dcf59a7657fe72e319e4e1d66ae7b29
5
5
  SHA512:
6
- metadata.gz: 80668e04d546d595e9a7f6aea4df23e64861814029fb5acbab9a512713417294960c9f9bb7d950907b3edb4af69b9ce3efc5a362bf417567a8c653c4eae32371
7
- data.tar.gz: 72aa66a623eebe44c733ba272d27945883810e7be0bd4f48a55c8e91500b222451f6d73adc54b5d99a557a6b5d29edc3a76dfb98d96dbfd92ff3ac5c66c2a094
6
+ metadata.gz: df020720cc0a59b2bbc59b32ad7de6864180b3a6c6bbae50a3c2f64b8e16d6ea229389e24080d2d036ac01326b557179d8038128f9bf550768fb2b166e9e674c
7
+ data.tar.gz: bc65946790e76ee0246870214ff9530fb989241313bbba53e0c2aca9466df1eace380c7a48b8b5eb7029914fe76bdb4210ec2f0f3bcdeaebfbf47866e14bd0c4
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) 2005 - 2009, James Auldridge
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 cookies = [];
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
- * resolveOptions - receive an options object and ensure all options are present and valid, replacing with defaults where necessary
23
- *
24
- * @access private
25
- * @static
26
- * @parameter Object options - optional options to start with
27
- * @return Object complete and valid options object
28
- */
29
- var resolveOptions = function( options )
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
- hoursToLive: ( typeof options.hoursToLive === 'number' && options.hoursToLive !== 0 ? options.hoursToLive : defaultOptions.hoursToLive ),
41
- path: ( typeof options.path === 'string' && options.path !== '' ? options.path : defaultOptions.path ),
42
- domain: ( typeof options.domain === 'string' && options.domain !== '' ? options.domain : defaultOptions.domain ),
43
- secure: ( typeof options.secure === 'boolean' && options.secure ? options.secure : defaultOptions.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
- * expiresGMTString - add given number of hours to current date/time and convert to GMT string
51
- *
52
- * @access private
53
- * @static
54
- * @parameter Integer hoursToLive - number of hours for which cookie should be valid
55
- * @return String - GMT time representing current date/time plus number of hours given
56
- */
57
- var expiresGMTString = function( hoursToLive )
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.hoursToLive === 'number' ? '; expires=' + expiresGMTString( options.hoursToLive ) : '' ) +
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
- * splitCookies - retrieve document.cookie string and break it into a hash
85
- *
86
- * @access private
87
- * @static
88
- * @return Object - hash of cookies from document.cookie
89
- */
90
- var splitCookies = function()
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
- var pair, name, value, separated = document.cookie.split( ';' );
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
- value = decodeURIComponent( pair[1] );
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
- var constructor = function(){};
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 - String:if single cookie requested and found; Null:if single cookie requested and not found; Object:hash of multiple or all cookies
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( var item in cookieName )
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( var cookieName in cookies )
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 - Null:if deleting, String:value to assign cookie if setting
179
- * @paramater Object options - optional list of cookie options to specify (hoursToLive, path, domain, secure)
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( var name in allCookies )
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 (hoursToLive, path, domain, secure)
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
- * $( '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
283
- * (radio and checkbox not yet supported)
284
- * (field or element MUST have name or id attribute)
285
- *
286
- * @access public
287
- * @param Object options - list of cookie options to specify
288
- * @return Object jQuery
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, resolvedName = false, resolvedValue = false, name = '', value = '', nameAttrs = ['name', 'id'], nodeName, inputType;
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 = $( this ).attr( nameAttrs[ i ] );
339
+ name = $this.attr( nameAttrs[ i ] );
301
340
  if( typeof name === 'string' && name !== '' )
302
341
  {
303
- resolvedName = true;
304
- break;
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
- if( resolvedName )
310
- {
311
- nodeName = this.nodeName.toLowerCase();
312
- if( nodeName !== 'input' && nodeName !== 'textarea' && nodeName !== 'select' && nodeName !== 'img' )
313
- {
314
- value = $( this ).html();
315
- resolvedValue = true;
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
- * $( '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
344
- *
345
- * @access public
346
- * @return Object jQuery
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 i, resolvedName = false, name = '', value, nameAttrs = ['name', 'id'], iteration = 0, nodeName;
381
+ var n, getN, nameAttrs = ['name', 'id'], name, $this = $( this ), value;
353
382
 
354
- for( i in nameAttrs )
383
+ getN = function()
355
384
  {
356
- if( ! isNaN( i ) )
357
- {
358
- name = $( this ).attr( nameAttrs[ i ] );
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
- if( resolvedName )
389
+ while( getN() )
368
390
  {
369
- value = $.cookies.get( name );
370
- if( value !== null )
391
+ name = $this.attr( n );
392
+ if( typeof name === 'string' && name !== '' )
371
393
  {
372
- nodeName = this.nodeName.toLowerCase();
373
- if( nodeName === 'input' || nodeName === 'textarea' || nodeName === 'select' )
394
+ value = $.cookies.get( name );
395
+ if( value !== null )
374
396
  {
375
- $( this ).val( value );
376
- }
377
- else
378
- {
379
- $( this ).html( value );
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
- * $( 'selector' ).cookieBind - call cookie fill on matching elements, and bind their change events to cookify()
389
- *
390
- * @access public
391
- * @param Object options - list of cookie options to specify
392
- * @return Object jQuery
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 ).cookieFill().change( function()
434
+ var $this = $( this );
435
+ $this.cookieFill().change( function()
399
436
  {
400
- $( this ).cookify( options );
437
+ $this.cookify( options );
401
438
  } );
402
439
  } );
403
440
  }
@@ -4,7 +4,7 @@ module JQueryCookies
4
4
  class Version
5
5
  class << self
6
6
  def to_s
7
- "2.1.0"
7
+ "2.2.0"
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-cookies-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Auldridge