Upgrade to ExtJS 3.2.0 - Released 03/30/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.0
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 = function(config){
41     Ext.state.CookieProvider.superclass.constructor.call(this);
42     this.path = "/";
43     this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
44     this.domain = null;
45     this.secure = false;
46     Ext.apply(this, config);
47     this.state = this.readCookies();
48 };
49
50 Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, {
51     // private
52     set : function(name, value){
53         if(typeof value == "undefined" || value === null){
54             this.clear(name);
55             return;
56         }
57         this.setCookie(name, value);
58         Ext.state.CookieProvider.superclass.set.call(this, name, value);
59     },
60
61     // private
62     clear : function(name){
63         this.clearCookie(name);
64         Ext.state.CookieProvider.superclass.clear.call(this, name);
65     },
66
67     // private
68     readCookies : function(){
69         var cookies = {};
70         var c = document.cookie + ";";
71         var re = /\s?(.*?)=(.*?);/g;
72         var matches;
73         while((matches = re.exec(c)) != null){
74             var name = matches[1];
75             var value = matches[2];
76             if(name && name.substring(0,3) == "ys-"){
77                 cookies[name.substr(3)] = this.decodeValue(value);
78             }
79         }
80         return cookies;
81     },
82
83     // private
84     setCookie : function(name, value){
85         document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
86            ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
87            ((this.path == null) ? "" : ("; path=" + this.path)) +
88            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
89            ((this.secure == true) ? "; secure" : "");
90     },
91
92     // private
93     clearCookie : function(name){
94         document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
95            ((this.path == null) ? "" : ("; path=" + this.path)) +
96            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
97            ((this.secure == true) ? "; secure" : "");
98     }
99 });</pre>    
100 </body>
101 </html>