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