Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / pkgs / state-debug.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.state.Provider
9  * Abstract base class for state provider implementations. This class provides methods
10  * for encoding and decoding <b>typed</b> variables including dates and defines the
11  * Provider interface.
12  */
13 Ext.state.Provider = function(){
14     /**
15      * @event statechange
16      * Fires when a state change occurs.
17      * @param {Provider} this This state provider
18      * @param {String} key The state key which was changed
19      * @param {String} value The encoded value for the state
20      */
21     this.addEvents("statechange");
22     this.state = {};
23     Ext.state.Provider.superclass.constructor.call(this);
24 };
25 Ext.extend(Ext.state.Provider, Ext.util.Observable, {
26     /**
27      * Returns the current value for a key
28      * @param {String} name The key name
29      * @param {Mixed} defaultValue A default value to return if the key's value is not found
30      * @return {Mixed} The state data
31      */
32     get : function(name, defaultValue){
33         return typeof this.state[name] == "undefined" ?
34             defaultValue : this.state[name];
35     },
36
37     /**
38      * Clears a value from the state
39      * @param {String} name The key name
40      */
41     clear : function(name){
42         delete this.state[name];
43         this.fireEvent("statechange", this, name, null);
44     },
45
46     /**
47      * Sets the value for a key
48      * @param {String} name The key name
49      * @param {Mixed} value The value to set
50      */
51     set : function(name, value){
52         this.state[name] = value;
53         this.fireEvent("statechange", this, name, value);
54     },
55
56     /**
57      * Decodes a string previously encoded with {@link #encodeValue}.
58      * @param {String} value The value to decode
59      * @return {Mixed} The decoded value
60      */
61     decodeValue : function(cookie){
62         var re = /^(a|n|d|b|s|o)\:(.*)$/;
63         var matches = re.exec(unescape(cookie));
64         if(!matches || !matches[1]) return; // non state cookie
65         var type = matches[1];
66         var v = matches[2];
67         switch(type){
68             case "n":
69                 return parseFloat(v);
70             case "d":
71                 return new Date(Date.parse(v));
72             case "b":
73                 return (v == "1");
74             case "a":
75                 var all = [];
76                 if(v != ''){
77                     Ext.each(v.split('^'), function(val){
78                         all.push(this.decodeValue(val));
79                     }, this);
80                 }
81                 return all;
82            case "o":
83                 var all = {};
84                 if(v != ''){
85                     Ext.each(v.split('^'), function(val){
86                         var kv = val.split('=');
87                         all[kv[0]] = this.decodeValue(kv[1]);
88                     }, this);
89                 }
90                 return all;
91            default:
92                 return v;
93         }
94     },
95
96     /**
97      * Encodes a value including type information.  Decode with {@link #decodeValue}.
98      * @param {Mixed} value The value to encode
99      * @return {String} The encoded value
100      */
101     encodeValue : function(v){
102         var enc;
103         if(typeof v == "number"){
104             enc = "n:" + v;
105         }else if(typeof v == "boolean"){
106             enc = "b:" + (v ? "1" : "0");
107         }else if(Ext.isDate(v)){
108             enc = "d:" + v.toGMTString();
109         }else if(Ext.isArray(v)){
110             var flat = "";
111             for(var i = 0, len = v.length; i < len; i++){
112                 flat += this.encodeValue(v[i]);
113                 if(i != len-1) flat += "^";
114             }
115             enc = "a:" + flat;
116         }else if(typeof v == "object"){
117             var flat = "";
118             for(var key in v){
119                 if(typeof v[key] != "function" && v[key] !== undefined){
120                     flat += key + "=" + this.encodeValue(v[key]) + "^";
121                 }
122             }
123             enc = "o:" + flat.substring(0, flat.length-1);
124         }else{
125             enc = "s:" + v;
126         }
127         return escape(enc);
128     }
129 });
130 /**
131  * @class Ext.state.Manager
132  * This is the global state manager. By default all components that are "state aware" check this class
133  * for state information if you don't pass them a custom state provider. In order for this class
134  * to be useful, it must be initialized with a provider when your application initializes. Example usage:
135  <pre><code>
136 // in your initialization function
137 init : function(){
138    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
139    var win = new Window(...);
140    win.restoreState();
141 }
142  </code></pre>
143  * @singleton
144  */
145 Ext.state.Manager = function(){
146     var provider = new Ext.state.Provider();
147
148     return {
149         /**
150          * Configures the default state provider for your application
151          * @param {Provider} stateProvider The state provider to set
152          */
153         setProvider : function(stateProvider){
154             provider = stateProvider;
155         },
156
157         /**
158          * Returns the current value for a key
159          * @param {String} name The key name
160          * @param {Mixed} defaultValue The default value to return if the key lookup does not match
161          * @return {Mixed} The state data
162          */
163         get : function(key, defaultValue){
164             return provider.get(key, defaultValue);
165         },
166
167         /**
168          * Sets the value for a key
169          * @param {String} name The key name
170          * @param {Mixed} value The state data
171          */
172          set : function(key, value){
173             provider.set(key, value);
174         },
175
176         /**
177          * Clears a value from the state
178          * @param {String} name The key name
179          */
180         clear : function(key){
181             provider.clear(key);
182         },
183
184         /**
185          * Gets the currently configured state provider
186          * @return {Provider} The state provider
187          */
188         getProvider : function(){
189             return provider;
190         }
191     };
192 }();
193 /**
194  * @class Ext.state.CookieProvider
195  * @extends Ext.state.Provider
196  * The default Provider implementation which saves state via cookies.
197  * <br />Usage:
198  <pre><code>
199    var cp = new Ext.state.CookieProvider({
200        path: "/cgi-bin/",
201        expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
202        domain: "extjs.com"
203    });
204    Ext.state.Manager.setProvider(cp);
205  </code></pre>
206  * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
207  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
208  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than
209  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include
210  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
211  * domain the page is running on including the 'www' like 'www.extjs.com')
212  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
213  * @constructor
214  * Create a new CookieProvider
215  * @param {Object} config The configuration object
216  */
217 Ext.state.CookieProvider = function(config){
218     Ext.state.CookieProvider.superclass.constructor.call(this);
219     this.path = "/";
220     this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
221     this.domain = null;
222     this.secure = false;
223     Ext.apply(this, config);
224     this.state = this.readCookies();
225 };
226
227 Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, {
228     // private
229     set : function(name, value){
230         if(typeof value == "undefined" || value === null){
231             this.clear(name);
232             return;
233         }
234         this.setCookie(name, value);
235         Ext.state.CookieProvider.superclass.set.call(this, name, value);
236     },
237
238     // private
239     clear : function(name){
240         this.clearCookie(name);
241         Ext.state.CookieProvider.superclass.clear.call(this, name);
242     },
243
244     // private
245     readCookies : function(){
246         var cookies = {};
247         var c = document.cookie + ";";
248         var re = /\s?(.*?)=(.*?);/g;
249         var matches;
250         while((matches = re.exec(c)) != null){
251             var name = matches[1];
252             var value = matches[2];
253             if(name && name.substring(0,3) == "ys-"){
254                 cookies[name.substr(3)] = this.decodeValue(value);
255             }
256         }
257         return cookies;
258     },
259
260     // private
261     setCookie : function(name, value){
262         document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
263            ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
264            ((this.path == null) ? "" : ("; path=" + this.path)) +
265            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
266            ((this.secure == true) ? "; secure" : "");
267     },
268
269     // private
270     clearCookie : function(name){
271         document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
272            ((this.path == null) ? "" : ("; path=" + this.path)) +
273            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
274            ((this.secure == true) ? "; secure" : "");
275     }
276 });