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