1/** 2 * Copyright (c) 2005 - 2010, James Auldridge 3 * All rights reserved. 4 * 5 * Licensed under the BSD, MIT, and GPL (your choice!) Licenses: 6 * http://code.google.com/p/cookies/wiki/License 7 * 8 */ 9var jaaulde = window.jaaulde || {}; 10jaaulde.utils = jaaulde.utils || {}; 11jaaulde.utils.cookies = ( function() 12{ 13 var resolveOptions, assembleOptionsString, parseCookies, constructor, defaultOptions = { 14 expiresAt: null, 15 path: '/', 16 domain: null, 17 secure: false 18 }; 19 /** 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 ) 28 { 29 var returnValue, expireDate; 30 31 if( typeof options !== 'object' || options === null ) 32 { 33 returnValue = defaultOptions; 34 } 35 else 36 { 37 returnValue = { 38 expiresAt: defaultOptions.expiresAt, 39 path: defaultOptions.path, 40 domain: defaultOptions.domain, 41 secure: defaultOptions.secure 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 } 69 } 70 71 return returnValue; 72 }; 73 /** 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 ) 82 { 83 options = resolveOptions( options ); 84 85 return ( 86 ( typeof options.expiresAt === 'object' && options.expiresAt instanceof Date ? '; expires=' + options.expiresAt.toGMTString() : '' ) + 87 '; path=' + options.path + 88 ( typeof options.domain === 'string' ? '; domain=' + options.domain : '' ) + 89 ( options.secure === true ? '; secure' : '' ) 90 ); 91 }; 92 /** 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() 100 { 101 var cookies = {}, i, pair, name, value, separated = document.cookie.split( ';' ), unparsedValue; 102 for( i = 0; i < separated.length; i = i + 1 ) 103 { 104 pair = separated[i].split( '=' ); 105 name = pair[0].replace( /^\s*/, '' ).replace( /\s*$/, '' ); 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 129 cookies[name] = value; 130 } 131 return cookies; 132 }; 133 134 constructor = function(){}; 135 136 /** 137 * get - get one, several, or all cookies 138 * 139 * @access public 140 * @paramater Mixed cookieName - String:name of single cookie; Array:list of multiple cookie names; Void (no param):if you want 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); 142 */ 143 constructor.prototype.get = function( cookieName ) 144 { 145 var returnValue, item, cookies = parseCookies(); 146 147 if( typeof cookieName === 'string' ) 148 { 149 returnValue = ( typeof cookies[cookieName] !== 'undefined' ) ? cookies[cookieName] : null; 150 } 151 else if( typeof cookieName === 'object' && cookieName !== null ) 152 { 153 returnValue = {}; 154 for( item in cookieName ) 155 { 156 if( typeof cookies[cookieName[item]] !== 'undefined' ) 157 { 158 returnValue[cookieName[item]] = cookies[cookieName[item]]; 159 } 160 else 161 { 162 returnValue[cookieName[item]] = null; 163 } 164 } 165 } 166 else 167 { 168 returnValue = cookies; 169 } 170 171 return returnValue; 172 }; 173 /** 174 * filter - get array of cookies whose names match the provided RegExp 175 * 176 * @access public 177 * @paramater Object RegExp - The regular expression to match against cookie names 178 * @return Mixed - Object:hash of cookies whose names match the RegExp 179 */ 180 constructor.prototype.filter = function( cookieNameRegExp ) 181 { 182 var cookieName, returnValue = {}, cookies = parseCookies(); 183 184 if( typeof cookieNameRegExp === 'string' ) 185 {
186 cookieNameRegExp = new RegExp( cookieNameRegExp ); 187 } 188 189 for( cookieName in cookies ) 190 { 191 if( cookieName.match( cookieNameRegExp ) ) 192 { 193 returnValue[cookieName] = cookies[cookieName]; 194 } 195 } 196 197 return returnValue; 198 }; 199 /** 200 * set - set or delete a cookie with desired options 201 * 202 * @access public 203 * @paramater String cookieName - name of cookie to set 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 206 * @return void 207 */ 208 constructor.prototype.set = function( cookieName, value, options ) 209 { 210 if( typeof options !== 'object' || options === null ) 211 { 212 options = {}; 213 } 214 215 if( typeof value === 'undefined' || value === null ) 216 { 217 value = ''; 218 options.hoursToLive = -8760; 219 } 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 234 var optionsString = assembleOptionsString( options ); 235 236 document.cookie = cookieName + '=' + encodeURIComponent( value ) + optionsString; 237 }; 238 /** 239 * del - delete a cookie (domain and path options must match those with which the cookie was set; this is really an alias for set() with parameters simplified for this use) 240 * 241 * @access public 242 * @paramater MIxed cookieName - String name of cookie to delete, or Bool true to delete all 243 * @paramater Object options - optional list of cookie options to specify ( path, domain ) 244 * @return void 245 */ 246 constructor.prototype.del = function( cookieName, options ) 247 { 248 var allCookies = {}, name; 249 250 if( typeof options !== 'object' || options === null ) 251 { 252 options = {}; 253 } 254 255 if( typeof cookieName === 'boolean' && cookieName === true ) 256 { 257 allCookies = this.get(); 258 } 259 else if( typeof cookieName === 'string' ) 260 { 261 allCookies[cookieName] = true; 262 } 263 264 for( name in allCookies ) 265 { 266 if( typeof name === 'string' && name !== '' ) 267 { 268 this.set( name, null, options ); 269 } 270 } 271 }; 272 /** 273 * test - test whether the browser is accepting cookies 274 * 275 * @access public 276 * @return Boolean 277 */ 278 constructor.prototype.test = function() 279 { 280 var returnValue = false, testName = 'cT', testValue = 'data'; 281 282 this.set( testName, testValue ); 283 284 if( this.get( testName ) === testValue ) 285 { 286 this.del( testName ); 287 returnValue = true; 288 } 289 290 return returnValue; 291 }; 292 /** 293 * setOptions - set default options for calls to cookie methods 294 * 295 * @access public 296 * @param Object options - list of cookie options to specify 297 * @return void 298 */ 299 constructor.prototype.setOptions = function( options ) 300 { 301 if( typeof options !== 'object' ) 302 { 303 options = null; 304 } 305 306 defaultOptions = resolveOptions( options ); 307 }; 308 309 return new constructor(); 310} )(); 311 312( function() 313{ 314 if( window.jQuery ) 315 { 316 ( function( $ ) 317 { 318 $.cookies = jaaulde.utils.cookies; 319 320 var extensions = { 321 /** 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 */ 329 cookify: function( options ) 330 { 331 return this.each( function() 332 { 333 var i, nameAttrs = ['name', 'id'], name, $this = $( this ), value; 334 335 for( i in nameAttrs ) 336 { 337 if( ! isNaN( i ) ) 338 { 339 name = $this.attr( nameAttrs[ i ] ); 340 if( typeof name === 'string' && name !== '' ) 341 { 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 } 357 358 if( typeof value !== 'string' || value === '' ) 359 { 360 value = null; 361 } 362 363 $.cookies.set( name, value, options ); 364 365 break; 366 } 367 } 368 } 369 } ); 370 }, 371 /** 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 */ 377 cookieFill: function() 378 { 379 return this.each( function() 380 {
381 var n, getN, nameAttrs = ['name', 'id'], name, $this = $( this ), value; 382 383 getN = function() 384 { 385 n = nameAttrs.pop(); 386 return !! n; 387 }; 388 389 while( getN() ) 390 { 391 name = $this.attr( n ); 392 if( typeof name === 'string' && name !== '' ) 393 { 394 value = $.cookies.get( name ); 395 if( value !== null ) 396 { 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 } 416 } 417 418 break; 419 } 420 } 421 } ); 422 }, 423 /** 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 */ 430 cookieBind: function( options ) 431 { 432 return this.each( function() 433 { 434 var $this = $( this ); 435 $this.cookieFill().change( function() 436 { 437 $this.cookify( options ); 438 } ); 439 } ); 440 } 441 }; 442 443 $.each( extensions, function( i ) 444 { 445 $.fn[i] = this; 446 } ); 447 448 } )( window.jQuery ); 449 } 450} )();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.