3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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
13 Ext.state.Provider = function(){
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
21 this.addEvents("statechange");
23 Ext.state.Provider.superclass.constructor.call(this);
25 Ext.extend(Ext.state.Provider, Ext.util.Observable, {
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
32 get : function(name, defaultValue){
33 return typeof this.state[name] == "undefined" ?
34 defaultValue : this.state[name];
38 * Clears a value from the state
39 * @param {String} name The key name
41 clear : function(name){
42 delete this.state[name];
43 this.fireEvent("statechange", this, name, null);
47 * Sets the value for a key
48 * @param {String} name The key name
49 * @param {Mixed} value The value to set
51 set : function(name, value){
52 this.state[name] = value;
53 this.fireEvent("statechange", this, name, value);
57 * Decodes a string previously encoded with {@link #encodeValue}.
58 * @param {String} value The value to decode
59 * @return {Mixed} The decoded value
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];
71 return new Date(Date.parse(v));
77 Ext.each(v.split('^'), function(val){
78 all.push(this.decodeValue(val));
85 Ext.each(v.split('^'), function(val){
86 var kv = val.split('=');
87 all[kv[0]] = this.decodeValue(kv[1]);
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
101 encodeValue : function(v){
103 if(typeof v == "number"){
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)){
111 for(var i = 0, len = v.length; i < len; i++){
112 flat += this.encodeValue(v[i]);
113 if(i != len-1) flat += "^";
116 }else if(typeof v == "object"){
119 if(typeof v[key] != "function" && v[key] !== undefined){
120 flat += key + "=" + this.encodeValue(v[key]) + "^";
123 enc = "o:" + flat.substring(0, flat.length-1);