4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.state.Provider
14 * Abstract base class for state provider implementations. This class provides methods
15 * for encoding and decoding <b>typed</b> variables including dates and defines the
18 Roo.state.Provider = function(){
21 * Fires when a state change occurs.
22 * @param {Provider} this This state provider
23 * @param {String} key The state key which was changed
24 * @param {String} value The encoded value for the state
30 Roo.state.Provider.superclass.constructor.call(this);
32 Roo.extend(Roo.state.Provider, Roo.util.Observable, {
34 * Returns the current value for a key
35 * @param {String} name The key name
36 * @param {Mixed} defaultValue A default value to return if the key's value is not found
37 * @return {Mixed} The state data
39 get : function(name, defaultValue){
40 return typeof this.state[name] == "undefined" ?
41 defaultValue : this.state[name];
45 * Clears a value from the state
46 * @param {String} name The key name
48 clear : function(name){
49 delete this.state[name];
50 this.fireEvent("statechange", this, name, null);
54 * Sets the value for a key
55 * @param {String} name The key name
56 * @param {Mixed} value The value to set
58 set : function(name, value){
59 this.state[name] = value;
60 this.fireEvent("statechange", this, name, value);
64 * Decodes a string previously encoded with {@link #encodeValue}.
65 * @param {String} value The value to decode
66 * @return {Mixed} The decoded value
68 decodeValue : function(cookie){
69 var re = /^(a|n|d|b|s|o)\:(.*)$/;
70 var matches = re.exec(unescape(cookie));
71 if(!matches || !matches[1]) {
72 return; // non state cookie
74 var type = matches[1];
80 return new Date(Date.parse(v));
85 var values = v.split("^");
86 for(var i = 0, len = values.length; i < len; i++){
87 all.push(this.decodeValue(values[i]));
92 var values = v.split("^");
93 for(var i = 0, len = values.length; i < len; i++){
94 var kv = values[i].split("=");
95 all[kv[0]] = this.decodeValue(kv[1]);
104 * Encodes a value including type information. Decode with {@link #decodeValue}.
105 * @param {Mixed} value The value to encode
106 * @return {String} The encoded value
108 encodeValue : function(v){
110 if(typeof v == "number"){
112 }else if(typeof v == "boolean"){
113 enc = "b:" + (v ? "1" : "0");
114 }else if(v instanceof Date){
115 enc = "d:" + v.toGMTString();
116 }else if(v instanceof Array){
118 for(var i = 0, len = v.length; i < len; i++){
119 flat += this.encodeValue(v[i]);
125 }else if(typeof v == "object"){
128 if(typeof v[key] != "function"){
129 flat += key + "=" + this.encodeValue(v[key]) + "^";
132 enc = "o:" + flat.substring(0, flat.length-1);