commit extjs-2.2.1
[extjs.git] / source / state / Provider.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.state.Provider\r
11  * Abstract base class for state provider implementations. This class provides methods\r
12  * for encoding and decoding <b>typed</b> variables including dates and defines the \r
13  * Provider interface.\r
14  */\r
15 Ext.state.Provider = function(){\r
16     /**\r
17      * @event statechange\r
18      * Fires when a state change occurs.\r
19      * @param {Provider} this This state provider\r
20      * @param {String} key The state key which was changed\r
21      * @param {String} value The encoded value for the state\r
22      */\r
23     this.addEvents("statechange");\r
24     this.state = {};\r
25     Ext.state.Provider.superclass.constructor.call(this);\r
26 };\r
27 Ext.extend(Ext.state.Provider, Ext.util.Observable, {\r
28     /**\r
29      * Returns the current value for a key\r
30      * @param {String} name The key name\r
31      * @param {Mixed} defaultValue A default value to return if the key's value is not found\r
32      * @return {Mixed} The state data\r
33      */\r
34     get : function(name, defaultValue){\r
35         return typeof this.state[name] == "undefined" ?\r
36             defaultValue : this.state[name];\r
37     },\r
38     \r
39     /**\r
40      * Clears a value from the state\r
41      * @param {String} name The key name\r
42      */\r
43     clear : function(name){\r
44         delete this.state[name];\r
45         this.fireEvent("statechange", this, name, null);\r
46     },\r
47     \r
48     /**\r
49      * Sets the value for a key\r
50      * @param {String} name The key name\r
51      * @param {Mixed} value The value to set\r
52      */\r
53     set : function(name, value){\r
54         this.state[name] = value;\r
55         this.fireEvent("statechange", this, name, value);\r
56     },\r
57     \r
58     /**\r
59      * Decodes a string previously encoded with {@link #encodeValue}.\r
60      * @param {String} value The value to decode\r
61      * @return {Mixed} The decoded value\r
62      */\r
63     decodeValue : function(cookie){\r
64         var re = /^(a|n|d|b|s|o)\:(.*)$/;\r
65         var matches = re.exec(unescape(cookie));\r
66         if(!matches || !matches[1]) return; // non state cookie\r
67         var type = matches[1];\r
68         var v = matches[2];\r
69         switch(type){\r
70             case "n":\r
71                 return parseFloat(v);\r
72             case "d":\r
73                 return new Date(Date.parse(v));\r
74             case "b":\r
75                 return (v == "1");\r
76             case "a":\r
77                 var all = [];\r
78                 var values = v.split("^");\r
79                 for(var i = 0, len = values.length; i < len; i++){\r
80                     all.push(this.decodeValue(values[i]));\r
81                 }\r
82                 return all;\r
83            case "o":\r
84                 var all = {};\r
85                 var values = v.split("^");\r
86                 for(var i = 0, len = values.length; i < len; i++){\r
87                     var kv = values[i].split("=");\r
88                     all[kv[0]] = this.decodeValue(kv[1]);\r
89                 }\r
90                 return all;\r
91            default:\r
92                 return v;\r
93         }\r
94     },\r
95     \r
96     /**\r
97      * Encodes a value including type information.  Decode with {@link #decodeValue}.\r
98      * @param {Mixed} value The value to encode\r
99      * @return {String} The encoded value\r
100      */\r
101     encodeValue : function(v){\r
102         var enc;\r
103         if(typeof v == "number"){\r
104             enc = "n:" + v;\r
105         }else if(typeof v == "boolean"){\r
106             enc = "b:" + (v ? "1" : "0");\r
107         }else if(Ext.isDate(v)){\r
108             enc = "d:" + v.toGMTString();\r
109         }else if(Ext.isArray(v)){\r
110             var flat = "";\r
111             for(var i = 0, len = v.length; i < len; i++){\r
112                 flat += this.encodeValue(v[i]);\r
113                 if(i != len-1) flat += "^";\r
114             }\r
115             enc = "a:" + flat;\r
116         }else if(typeof v == "object"){\r
117             var flat = "";\r
118             for(var key in v){\r
119                 if(typeof v[key] != "function" && v[key] !== undefined){\r
120                     flat += key + "=" + this.encodeValue(v[key]) + "^";\r
121                 }\r
122             }\r
123             enc = "o:" + flat.substring(0, flat.length-1);\r
124         }else{\r
125             enc = "s:" + v;\r
126         }\r
127         return escape(enc);        \r
128     }\r
129 });\r