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