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