1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.state.Provider |
---|
11 | * Abstract base class for state provider implementations. This class provides methods |
---|
12 | * for encoding and decoding <b>typed</b> variables including dates and defines the |
---|
13 | * Provider interface. |
---|
14 | */ |
---|
15 | Ext.state.Provider = function(){ |
---|
16 | /** |
---|
17 | * @event statechange |
---|
18 | * Fires when a state change occurs. |
---|
19 | * @param {Provider} this This state provider |
---|
20 | * @param {String} key The state key which was changed |
---|
21 | * @param {String} value The encoded value for the state |
---|
22 | */ |
---|
23 | this.addEvents("statechange"); |
---|
24 | this.state = {}; |
---|
25 | Ext.state.Provider.superclass.constructor.call(this); |
---|
26 | }; |
---|
27 | Ext.extend(Ext.state.Provider, Ext.util.Observable, { |
---|
28 | /** |
---|
29 | * Returns the current value for a key |
---|
30 | * @param {String} name The key name |
---|
31 | * @param {Mixed} defaultValue A default value to return if the key's value is not found |
---|
32 | * @return {Mixed} The state data |
---|
33 | */ |
---|
34 | get : function(name, defaultValue){ |
---|
35 | return typeof this.state[name] == "undefined" ? |
---|
36 | defaultValue : this.state[name]; |
---|
37 | }, |
---|
38 | |
---|
39 | /** |
---|
40 | * Clears a value from the state |
---|
41 | * @param {String} name The key name |
---|
42 | */ |
---|
43 | clear : function(name){ |
---|
44 | delete this.state[name]; |
---|
45 | this.fireEvent("statechange", this, name, null); |
---|
46 | }, |
---|
47 | |
---|
48 | /** |
---|
49 | * Sets the value for a key |
---|
50 | * @param {String} name The key name |
---|
51 | * @param {Mixed} value The value to set |
---|
52 | */ |
---|
53 | set : function(name, value){ |
---|
54 | this.state[name] = value; |
---|
55 | this.fireEvent("statechange", this, name, value); |
---|
56 | }, |
---|
57 | |
---|
58 | /** |
---|
59 | * Decodes a string previously encoded with {@link #encodeValue}. |
---|
60 | * @param {String} value The value to decode |
---|
61 | * @return {Mixed} The decoded value |
---|
62 | */ |
---|
63 | decodeValue : function(cookie){ |
---|
64 | var re = /^(a|n|d|b|s|o)\:(.*)$/; |
---|
65 | var matches = re.exec(unescape(cookie)); |
---|
66 | if(!matches || !matches[1]) return; // non state cookie |
---|
67 | var type = matches[1]; |
---|
68 | var v = matches[2]; |
---|
69 | switch(type){ |
---|
70 | case "n": |
---|
71 | return parseFloat(v); |
---|
72 | case "d": |
---|
73 | return new Date(Date.parse(v)); |
---|
74 | case "b": |
---|
75 | return (v == "1"); |
---|
76 | case "a": |
---|
77 | var all = []; |
---|
78 | var values = v.split("^"); |
---|
79 | for(var i = 0, len = values.length; i < len; i++){ |
---|
80 | all.push(this.decodeValue(values[i])); |
---|
81 | } |
---|
82 | return all; |
---|
83 | case "o": |
---|
84 | var all = {}; |
---|
85 | var values = v.split("^"); |
---|
86 | for(var i = 0, len = values.length; i < len; i++){ |
---|
87 | var kv = values[i].split("="); |
---|
88 | all[kv[0]] = this.decodeValue(kv[1]); |
---|
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 | }); |
---|