remove debugging code
[roojs1] / Roo / state / Provider.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
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 
16  * Provider interface.
17  */
18 Roo.state.Provider = function(){
19     /**
20      * @event statechange
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
25      */
26     this.addEvents({
27         "statechange": true
28     });
29     this.state = {};
30     Roo.state.Provider.superclass.constructor.call(this);
31 };
32 Roo.extend(Roo.state.Provider, Roo.util.Observable, {
33     /**
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
38      */
39     get : function(name, defaultValue){
40         return typeof this.state[name] == "undefined" ?
41             defaultValue : this.state[name];
42     },
43     
44     /**
45      * Clears a value from the state
46      * @param {String} name The key name
47      */
48     clear : function(name){
49         delete this.state[name];
50         this.fireEvent("statechange", this, name, null);
51     },
52     
53     /**
54      * Sets the value for a key
55      * @param {String} name The key name
56      * @param {Mixed} value The value to set
57      */
58     set : function(name, value){
59         this.state[name] = value;
60         this.fireEvent("statechange", this, name, value);
61     },
62     
63     /**
64      * Decodes a string previously encoded with {@link #encodeValue}.
65      * @param {String} value The value to decode
66      * @return {Mixed} The decoded value
67      */
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
73         }
74         var type = matches[1];
75         var v = matches[2];
76         switch(type){
77             case "n":
78                 return parseFloat(v);
79             case "d":
80                 return new Date(Date.parse(v));
81             case "b":
82                 return (v == "1");
83             case "a":
84                 var all = [];
85                 var values = v.split("^");
86                 for(var i = 0, len = values.length; i < len; i++){
87                     all.push(this.decodeValue(values[i]));
88                 }
89                 return all;
90            case "o":
91                 var all = {};
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]);
96                 }
97                 return all;
98            default:
99                 return v;
100         }
101     },
102     
103     /**
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
107      */
108     encodeValue : function(v){
109         var enc;
110         if(typeof v == "number"){
111             enc = "n:" + v;
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){
117             var flat = "";
118             for(var i = 0, len = v.length; i < len; i++){
119                 flat += this.encodeValue(v[i]);
120                 if(i != len-1) {
121                     flat += "^";
122                 }
123             }
124             enc = "a:" + flat;
125         }else if(typeof v == "object"){
126             var flat = "";
127             for(var key in v){
128                 if(typeof v[key] != "function"){
129                     flat += key + "=" + this.encodeValue(v[key]) + "^";
130                 }
131             }
132             enc = "o:" + flat.substring(0, flat.length-1);
133         }else{
134             enc = "s:" + v;
135         }
136         return escape(enc);        
137     }
138 });
139