Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / state / CookieProvider.js
1 /**
2  * @class Ext.state.CookieProvider
3  * @extends Ext.state.Provider
4  * A Provider implementation which saves and retrieves state via cookies.
5  * The CookieProvider supports the usual cookie options, such as:
6  * <ul>
7  * <li>{@link #path}</li>
8  * <li>{@link #expires}</li>
9  * <li>{@link #domain}</li>
10  * <li>{@link #secure}</li>
11  * </ul>
12  <pre><code>
13    var cp = new Ext.state.CookieProvider({
14        path: "/cgi-bin/",
15        expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
16        domain: "sencha.com"
17    });
18    Ext.state.Manager.setProvider(cp);
19  </code></pre>
20  
21  
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)
23  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
24  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than
25  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'sencha.com' to include
26  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
27  * domain the page is running on including the 'www' like 'www.sencha.com')
28  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
29  * @constructor
30  * Create a new CookieProvider
31  * @param {Object} config The configuration object
32  */
33 Ext.define('Ext.state.CookieProvider', {
34     extend: 'Ext.state.Provider',
35
36     constructor : function(config){
37         var me = this;
38         me.path = "/";
39         me.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
40         me.domain = null;
41         me.secure = false;
42         me.callParent(arguments);
43         me.state = me.readCookies();
44     },
45     
46     // private
47     set : function(name, value){
48         var me = this;
49         
50         if(typeof value == "undefined" || value === null){
51             me.clear(name);
52             return;
53         }
54         me.setCookie(name, value);
55         me.callParent(arguments);
56     },
57
58     // private
59     clear : function(name){
60         this.clearCookie(name);
61         this.callParent(arguments);
62     },
63
64     // private
65     readCookies : function(){
66         var cookies = {},
67             c = document.cookie + ";",
68             re = /\s?(.*?)=(.*?);/g,
69             prefix = this.prefix,
70             len = prefix.length,
71             matches,
72             name,
73             value;
74             
75         while((matches = re.exec(c)) != null){
76             name = matches[1];
77             value = matches[2];
78             if (name && name.substring(0, len) == prefix){
79                 cookies[name.substr(len)] = this.decodeValue(value);
80             }
81         }
82         return cookies;
83     },
84
85     // private
86     setCookie : function(name, value){
87         var me = this;
88         
89         document.cookie = me.prefix + name + "=" + me.encodeValue(value) +
90            ((me.expires == null) ? "" : ("; expires=" + me.expires.toGMTString())) +
91            ((me.path == null) ? "" : ("; path=" + me.path)) +
92            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
93            ((me.secure == true) ? "; secure" : "");
94     },
95
96     // private
97     clearCookie : function(name){
98         var me = this;
99         
100         document.cookie = me.prefix + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
101            ((me.path == null) ? "" : ("; path=" + me.path)) +
102            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
103            ((me.secure == true) ? "; secure" : "");
104     }
105 });