Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / state / CookieProvider.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.state.CookieProvider
17  * @extends Ext.state.Provider
18  * A Provider implementation which saves and retrieves state via cookies.
19  * The CookieProvider supports the usual cookie options, such as:
20  * <ul>
21  * <li>{@link #path}</li>
22  * <li>{@link #expires}</li>
23  * <li>{@link #domain}</li>
24  * <li>{@link #secure}</li>
25  * </ul>
26  <pre><code>
27    var cp = new Ext.state.CookieProvider({
28        path: "/cgi-bin/",
29        expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
30        domain: "sencha.com"
31    });
32    Ext.state.Manager.setProvider(cp);
33  </code></pre>
34  
35  
36  * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
37  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
38  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than
39  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'sencha.com' to include
40  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
41  * domain the page is running on including the 'www' like 'www.sencha.com')
42  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
43  */
44 Ext.define('Ext.state.CookieProvider', {
45     extend: 'Ext.state.Provider',
46
47     /**
48      * Creates a new CookieProvider.
49      * @param {Object} config (optional) Config object.
50      */
51     constructor : function(config){
52         var me = this;
53         me.path = "/";
54         me.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
55         me.domain = null;
56         me.secure = false;
57         me.callParent(arguments);
58         me.state = me.readCookies();
59     },
60     
61     // private
62     set : function(name, value){
63         var me = this;
64         
65         if(typeof value == "undefined" || value === null){
66             me.clear(name);
67             return;
68         }
69         me.setCookie(name, value);
70         me.callParent(arguments);
71     },
72
73     // private
74     clear : function(name){
75         this.clearCookie(name);
76         this.callParent(arguments);
77     },
78
79     // private
80     readCookies : function(){
81         var cookies = {},
82             c = document.cookie + ";",
83             re = /\s?(.*?)=(.*?);/g,
84             prefix = this.prefix,
85             len = prefix.length,
86             matches,
87             name,
88             value;
89             
90         while((matches = re.exec(c)) != null){
91             name = matches[1];
92             value = matches[2];
93             if (name && name.substring(0, len) == prefix){
94                 cookies[name.substr(len)] = this.decodeValue(value);
95             }
96         }
97         return cookies;
98     },
99
100     // private
101     setCookie : function(name, value){
102         var me = this;
103         
104         document.cookie = me.prefix + name + "=" + me.encodeValue(value) +
105            ((me.expires == null) ? "" : ("; expires=" + me.expires.toGMTString())) +
106            ((me.path == null) ? "" : ("; path=" + me.path)) +
107            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
108            ((me.secure == true) ? "; secure" : "");
109     },
110
111     // private
112     clearCookie : function(name){
113         var me = this;
114         
115         document.cookie = me.prefix + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
116            ((me.path == null) ? "" : ("; path=" + me.path)) +
117            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
118            ((me.secure == true) ? "; secure" : "");
119     }
120 });
121