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