Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / CookieProvider.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">\r
9 <div id="cls-Ext.state.CookieProvider"></div>/**\r
10  * @class Ext.state.CookieProvider\r
11  * @extends Ext.state.Provider\r
12  * The default Provider implementation which saves state via cookies.\r
13  * <br />Usage:\r
14  <pre><code>\r
15    var cp = new Ext.state.CookieProvider({\r
16        path: "/cgi-bin/",\r
17        expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days\r
18        domain: "extjs.com"\r
19    });\r
20    Ext.state.Manager.setProvider(cp);\r
21  </code></pre>\r
22  * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)\r
23  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)\r
24  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than\r
25  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include\r
26  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same\r
27  * domain the page is running on including the 'www' like 'www.extjs.com')\r
28  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)\r
29  * @constructor\r
30  * Create a new CookieProvider\r
31  * @param {Object} config The configuration object\r
32  */\r
33 Ext.state.CookieProvider = function(config){\r
34     Ext.state.CookieProvider.superclass.constructor.call(this);\r
35     this.path = "/";\r
36     this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days\r
37     this.domain = null;\r
38     this.secure = false;\r
39     Ext.apply(this, config);\r
40     this.state = this.readCookies();\r
41 };\r
42 \r
43 Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, {\r
44     // private\r
45     set : function(name, value){\r
46         if(typeof value == "undefined" || value === null){\r
47             this.clear(name);\r
48             return;\r
49         }\r
50         this.setCookie(name, value);\r
51         Ext.state.CookieProvider.superclass.set.call(this, name, value);\r
52     },\r
53 \r
54     // private\r
55     clear : function(name){\r
56         this.clearCookie(name);\r
57         Ext.state.CookieProvider.superclass.clear.call(this, name);\r
58     },\r
59 \r
60     // private\r
61     readCookies : function(){\r
62         var cookies = {};\r
63         var c = document.cookie + ";";\r
64         var re = /\s?(.*?)=(.*?);/g;\r
65         var matches;\r
66         while((matches = re.exec(c)) != null){\r
67             var name = matches[1];\r
68             var value = matches[2];\r
69             if(name && name.substring(0,3) == "ys-"){\r
70                 cookies[name.substr(3)] = this.decodeValue(value);\r
71             }\r
72         }\r
73         return cookies;\r
74     },\r
75 \r
76     // private\r
77     setCookie : function(name, value){\r
78         document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +\r
79            ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +\r
80            ((this.path == null) ? "" : ("; path=" + this.path)) +\r
81            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +\r
82            ((this.secure == true) ? "; secure" : "");\r
83     },\r
84 \r
85     // private\r
86     clearCookie : function(name){\r
87         document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +\r
88            ((this.path == null) ? "" : ("; path=" + this.path)) +\r
89            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +\r
90            ((this.secure == true) ? "; secure" : "");\r
91     }\r
92 });</pre>    \r
93 </body>\r
94 </html>