Upgrade to ExtJS 4.0.7 - Released 10/19/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  * A Provider implementation which saves and retrieves state via cookies. The CookieProvider supports the usual cookie
17  * options, such as:
18  *
19  * - {@link #path}
20  * - {@link #expires}
21  * - {@link #domain}
22  * - {@link #secure}
23  *
24  * Example:
25  *
26  *     Ext.create('Ext.state.CookieProvider', {
27  *         path: "/cgi-bin/",
28  *         expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
29  *         domain: "sencha.com"
30  *     });
31  *
32  *     Ext.state.Manager.setProvider(cp);
33  *
34  * @constructor
35  * Creates a new CookieProvider.
36  * @param {Object} config (optional) Config object.
37  * @return {Object}
38  */
39 Ext.define('Ext.state.CookieProvider', {
40     extend: 'Ext.state.Provider',
41
42     /**
43      * @cfg {String} path
44      * The path for which the cookie is active. Defaults to root '/' which makes it active for all pages in the site.
45      */
46
47     /**
48      * @cfg {Date} expires
49      * The cookie expiration date. Defaults to 7 days from now.
50      */
51
52     /**
53      * @cfg {String} domain
54      * The domain to save the cookie for. Note that you cannot specify a different domain than your page is on, but you can
55      * specify a sub-domain, or simply the domain itself like 'sencha.com' to include all sub-domains if you need to access
56      * cookies across different sub-domains. Defaults to null which uses the same domain the page is running on including
57      * the 'www' like 'www.sencha.com'.
58      */
59
60     /**
61      * @cfg {Boolean} [secure=false]
62      * True if the site is using SSL
63      */
64
65     /**
66      * Creates a new CookieProvider.
67      * @param {Object} [config] Config object.
68      */
69     constructor : function(config){
70         var me = this;
71         me.path = "/";
72         me.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
73         me.domain = null;
74         me.secure = false;
75         me.callParent(arguments);
76         me.state = me.readCookies();
77     },
78
79     // private
80     set : function(name, value){
81         var me = this;
82
83         if(typeof value == "undefined" || value === null){
84             me.clear(name);
85             return;
86         }
87         me.setCookie(name, value);
88         me.callParent(arguments);
89     },
90
91     // private
92     clear : function(name){
93         this.clearCookie(name);
94         this.callParent(arguments);
95     },
96
97     // private
98     readCookies : function(){
99         var cookies = {},
100             c = document.cookie + ";",
101             re = /\s?(.*?)=(.*?);/g,
102             prefix = this.prefix,
103             len = prefix.length,
104             matches,
105             name,
106             value;
107
108         while((matches = re.exec(c)) != null){
109             name = matches[1];
110             value = matches[2];
111             if (name && name.substring(0, len) == prefix){
112                 cookies[name.substr(len)] = this.decodeValue(value);
113             }
114         }
115         return cookies;
116     },
117
118     // private
119     setCookie : function(name, value){
120         var me = this;
121
122         document.cookie = me.prefix + name + "=" + me.encodeValue(value) +
123            ((me.expires == null) ? "" : ("; expires=" + me.expires.toGMTString())) +
124            ((me.path == null) ? "" : ("; path=" + me.path)) +
125            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
126            ((me.secure == true) ? "; secure" : "");
127     },
128
129     // private
130     clearCookie : function(name){
131         var me = this;
132
133         document.cookie = me.prefix + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
134            ((me.path == null) ? "" : ("; path=" + me.path)) +
135            ((me.domain == null) ? "" : ("; domain=" + me.domain)) +
136            ((me.secure == true) ? "; secure" : "");
137     }
138 });
139