blob: 143a9e61c8bcb9136b38cf73191755963f75226f [file] [log] [blame]
Carsten Ziegelerc77d3b52009-12-11 10:56:15 +00001/**
2 * Copyright (c) 2005 - 2009, 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||{};jaaulde.utils=jaaulde.utils||{};jaaulde.utils.cookies=(function()
10{var cookies=[];var defaultOptions={hoursToLive:null,path:'/',domain:null,secure:false};var resolveOptions=function(options)
11{var returnValue;if(typeof options!=='object'||options===null)
12{returnValue=defaultOptions;}
13else
14{returnValue={hoursToLive:(typeof options.hoursToLive==='number'&&options.hoursToLive!==0?options.hoursToLive:defaultOptions.hoursToLive),path:(typeof options.path==='string'&&options.path!==''?options.path:defaultOptions.path),domain:(typeof options.domain==='string'&&options.domain!==''?options.domain:defaultOptions.domain),secure:(typeof options.secure==='boolean'&&options.secure?options.secure:defaultOptions.secure)};}
15return returnValue;};var expiresGMTString=function(hoursToLive)
16{var dateObject=new Date();dateObject.setTime(dateObject.getTime()+(hoursToLive*60*60*1000));return dateObject.toGMTString();};var assembleOptionsString=function(options)
17{options=resolveOptions(options);return((typeof options.hoursToLive==='number'?'; expires='+expiresGMTString(options.hoursToLive):'')+'; path='+options.path+
18(typeof options.domain==='string'?'; domain='+options.domain:'')+
19(options.secure===true?'; secure':''));};var splitCookies=function()
20{cookies={};var pair,name,value,separated=document.cookie.split(';');for(var i=0;i<separated.length;i=i+1)
21{pair=separated[i].split('=');name=pair[0].replace(/^\s*/,'').replace(/\s*$/,'');value=decodeURIComponent(pair[1]);cookies[name]=value;}
22return cookies;};var constructor=function(){};constructor.prototype.get=function(cookieName)
23{var returnValue;splitCookies();if(typeof cookieName==='string')
24{returnValue=(typeof cookies[cookieName]!=='undefined')?cookies[cookieName]:null;}
25else if(typeof cookieName==='object'&&cookieName!==null)
26{returnValue={};for(var item in cookieName)
27{if(typeof cookies[cookieName[item]]!=='undefined')
28{returnValue[cookieName[item]]=cookies[cookieName[item]];}
29else
30{returnValue[cookieName[item]]=null;}}}
31else
32{returnValue=cookies;}
33return returnValue;};constructor.prototype.filter=function(cookieNameRegExp)
34{var returnValue={};splitCookies();if(typeof cookieNameRegExp==='string')
35{cookieNameRegExp=new RegExp(cookieNameRegExp);}
36for(var cookieName in cookies)
37{if(cookieName.match(cookieNameRegExp))
38{returnValue[cookieName]=cookies[cookieName];}}
39return returnValue;};constructor.prototype.set=function(cookieName,value,options)
40{if(typeof value==='undefined'||value===null)
41{if(typeof options!=='object'||options===null)
42{options={};}
43value='';options.hoursToLive=-8760;}
44var optionsString=assembleOptionsString(options);document.cookie=cookieName+'='+encodeURIComponent(value)+optionsString;};constructor.prototype.del=function(cookieName,options)
45{var allCookies={};if(typeof options!=='object'||options===null)
46{options={};}
47if(typeof cookieName==='boolean'&&cookieName===true)
48{allCookies=this.get();}
49else if(typeof cookieName==='string')
50{allCookies[cookieName]=true;}
51for(var name in allCookies)
52{if(typeof name==='string'&&name!=='')
53{this.set(name,null,options);}}};constructor.prototype.test=function()
54{var returnValue=false,testName='cT',testValue='data';this.set(testName,testValue);if(this.get(testName)===testValue)
55{this.del(testName);returnValue=true;}
56return returnValue;};constructor.prototype.setOptions=function(options)
57{if(typeof options!=='object')
58{options=null;}
59defaultOptions=resolveOptions(options);};return new constructor();})();(function()
60{if(window.jQuery)
61{(function($)
62{$.cookies=jaaulde.utils.cookies;var extensions={cookify:function(options)
63{return this.each(function()
64{var i,resolvedName=false,resolvedValue=false,name='',value='',nameAttrs=['name','id'],nodeName,inputType;for(i in nameAttrs)
65{if(!isNaN(i))
66{name=$(this).attr(nameAttrs[i]);if(typeof name==='string'&&name!=='')
67{resolvedName=true;break;}}}
68if(resolvedName)
69{nodeName=this.nodeName.toLowerCase();if(nodeName!=='input'&&nodeName!=='textarea'&&nodeName!=='select'&&nodeName!=='img')
70{value=$(this).html();resolvedValue=true;}
71else
72{inputType=$(this).attr('type');if(typeof inputType==='string'&&inputType!=='')
73{inputType=inputType.toLowerCase();}
74if(inputType!=='radio'&&inputType!=='checkbox')
75{value=$(this).val();resolvedValue=true;}}
76if(resolvedValue)
77{if(typeof value!=='string'||value==='')
78{value=null;}
79$.cookies.set(name,value,options);}}});},cookieFill:function()
80{return this.each(function()
81{var i,resolvedName=false,name='',value,nameAttrs=['name','id'],iteration=0,nodeName;for(i in nameAttrs)
82{if(!isNaN(i))
83{name=$(this).attr(nameAttrs[i]);if(typeof name==='string'&&name!=='')
84{resolvedName=true;break;}}}
85if(resolvedName)
86{value=$.cookies.get(name);if(value!==null)
87{nodeName=this.nodeName.toLowerCase();if(nodeName==='input'||nodeName==='textarea'||nodeName==='select')
88{$(this).val(value);}
89else
90{$(this).html(value);}}}
91iteration=0;});},cookieBind:function(options)
92{return this.each(function()
93{$(this).cookieFill().change(function()
94{$(this).cookify(options);});});}};$.each(extensions,function(i)
95{$.fn[i]=this;});})(window.jQuery);}})();