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