1 | /*! |
---|
2 | * Ext JS Library 3.0.0 |
---|
3 | * Copyright(c) 2006-2009 Ext JS, LLC |
---|
4 | * licensing@extjs.com |
---|
5 | * http://www.extjs.com/license |
---|
6 | */ |
---|
7 | /** |
---|
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 |
---|
11 | * Provider interface. |
---|
12 | */ |
---|
13 | Ext.state.Provider = function(){ |
---|
14 | /** |
---|
15 | * @event statechange |
---|
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 |
---|
20 | */ |
---|
21 | this.addEvents("statechange"); |
---|
22 | this.state = {}; |
---|
23 | Ext.state.Provider.superclass.constructor.call(this); |
---|
24 | }; |
---|
25 | Ext.extend(Ext.state.Provider, Ext.util.Observable, { |
---|
26 | /** |
---|
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 |
---|
31 | */ |
---|
32 | get : function(name, defaultValue){ |
---|
33 | return typeof this.state[name] == "undefined" ? |
---|
34 | defaultValue : this.state[name]; |
---|
35 | }, |
---|
36 | |
---|
37 | /** |
---|
38 | * Clears a value from the state |
---|
39 | * @param {String} name The key name |
---|
40 | */ |
---|
41 | clear : function(name){ |
---|
42 | delete this.state[name]; |
---|
43 | this.fireEvent("statechange", this, name, null); |
---|
44 | }, |
---|
45 | |
---|
46 | /** |
---|
47 | * Sets the value for a key |
---|
48 | * @param {String} name The key name |
---|
49 | * @param {Mixed} value The value to set |
---|
50 | */ |
---|
51 | set : function(name, value){ |
---|
52 | this.state[name] = value; |
---|
53 | this.fireEvent("statechange", this, name, value); |
---|
54 | }, |
---|
55 | |
---|
56 | /** |
---|
57 | * Decodes a string previously encoded with {@link #encodeValue}. |
---|
58 | * @param {String} value The value to decode |
---|
59 | * @return {Mixed} The decoded value |
---|
60 | */ |
---|
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]; |
---|
66 | var v = matches[2]; |
---|
67 | switch(type){ |
---|
68 | case "n": |
---|
69 | return parseFloat(v); |
---|
70 | case "d": |
---|
71 | return new Date(Date.parse(v)); |
---|
72 | case "b": |
---|
73 | return (v == "1"); |
---|
74 | case "a": |
---|
75 | var all = []; |
---|
76 | var values = v.split("^"); |
---|
77 | for(var i = 0, len = values.length; i < len; i++){ |
---|
78 | all.push(this.decodeValue(values[i])); |
---|
79 | } |
---|
80 | return all; |
---|
81 | case "o": |
---|
82 | var all = {}; |
---|
83 | var values = v.split("^"); |
---|
84 | for(var i = 0, len = values.length; i < len; i++){ |
---|
85 | var kv = values[i].split("="); |
---|
86 | all[kv[0]] = this.decodeValue(kv[1]); |
---|
87 | } |
---|
88 | return all; |
---|
89 | default: |
---|
90 | return v; |
---|
91 | } |
---|
92 | }, |
---|
93 | |
---|
94 | /** |
---|
95 | * Encodes a value including type information. Decode with {@link #decodeValue}. |
---|
96 | * @param {Mixed} value The value to encode |
---|
97 | * @return {String} The encoded value |
---|
98 | */ |
---|
99 | encodeValue : function(v){ |
---|
100 | var enc; |
---|
101 | if(typeof v == "number"){ |
---|
102 | enc = "n:" + v; |
---|
103 | }else if(typeof v == "boolean"){ |
---|
104 | enc = "b:" + (v ? "1" : "0"); |
---|
105 | }else if(Ext.isDate(v)){ |
---|
106 | enc = "d:" + v.toGMTString(); |
---|
107 | }else if(Ext.isArray(v)){ |
---|
108 | var flat = ""; |
---|
109 | for(var i = 0, len = v.length; i < len; i++){ |
---|
110 | flat += this.encodeValue(v[i]); |
---|
111 | if(i != len-1) flat += "^"; |
---|
112 | } |
---|
113 | enc = "a:" + flat; |
---|
114 | }else if(typeof v == "object"){ |
---|
115 | var flat = ""; |
---|
116 | for(var key in v){ |
---|
117 | if(typeof v[key] != "function" && v[key] !== undefined){ |
---|
118 | flat += key + "=" + this.encodeValue(v[key]) + "^"; |
---|
119 | } |
---|
120 | } |
---|
121 | enc = "o:" + flat.substring(0, flat.length-1); |
---|
122 | }else{ |
---|
123 | enc = "s:" + v; |
---|
124 | } |
---|
125 | return escape(enc); |
---|
126 | } |
---|
127 | }); |
---|
128 | /** |
---|
129 | * @class Ext.state.Manager |
---|
130 | * This is the global state manager. By default all components that are "state aware" check this class |
---|
131 | * for state information if you don't pass them a custom state provider. In order for this class |
---|
132 | * to be useful, it must be initialized with a provider when your application initializes. Example usage: |
---|
133 | <pre><code> |
---|
134 | // in your initialization function |
---|
135 | init : function(){ |
---|
136 | Ext.state.Manager.setProvider(new Ext.state.CookieProvider()); |
---|
137 | var win = new Window(...); |
---|
138 | win.restoreState(); |
---|
139 | } |
---|
140 | </code></pre> |
---|
141 | * @singleton |
---|
142 | */ |
---|
143 | Ext.state.Manager = function(){ |
---|
144 | var provider = new Ext.state.Provider(); |
---|
145 | |
---|
146 | return { |
---|
147 | /** |
---|
148 | * Configures the default state provider for your application |
---|
149 | * @param {Provider} stateProvider The state provider to set |
---|
150 | */ |
---|
151 | setProvider : function(stateProvider){ |
---|
152 | provider = stateProvider; |
---|
153 | }, |
---|
154 | |
---|
155 | /** |
---|
156 | * Returns the current value for a key |
---|
157 | * @param {String} name The key name |
---|
158 | * @param {Mixed} defaultValue The default value to return if the key lookup does not match |
---|
159 | * @return {Mixed} The state data |
---|
160 | */ |
---|
161 | get : function(key, defaultValue){ |
---|
162 | return provider.get(key, defaultValue); |
---|
163 | }, |
---|
164 | |
---|
165 | /** |
---|
166 | * Sets the value for a key |
---|
167 | * @param {String} name The key name |
---|
168 | * @param {Mixed} value The state data |
---|
169 | */ |
---|
170 | set : function(key, value){ |
---|
171 | provider.set(key, value); |
---|
172 | }, |
---|
173 | |
---|
174 | /** |
---|
175 | * Clears a value from the state |
---|
176 | * @param {String} name The key name |
---|
177 | */ |
---|
178 | clear : function(key){ |
---|
179 | provider.clear(key); |
---|
180 | }, |
---|
181 | |
---|
182 | /** |
---|
183 | * Gets the currently configured state provider |
---|
184 | * @return {Provider} The state provider |
---|
185 | */ |
---|
186 | getProvider : function(){ |
---|
187 | return provider; |
---|
188 | } |
---|
189 | }; |
---|
190 | }(); |
---|
191 | /** |
---|
192 | * @class Ext.state.CookieProvider |
---|
193 | * @extends Ext.state.Provider |
---|
194 | * The default Provider implementation which saves state via cookies. |
---|
195 | * <br />Usage: |
---|
196 | <pre><code> |
---|
197 | var cp = new Ext.state.CookieProvider({ |
---|
198 | path: "/cgi-bin/", |
---|
199 | expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days |
---|
200 | domain: "extjs.com" |
---|
201 | }); |
---|
202 | Ext.state.Manager.setProvider(cp); |
---|
203 | </code></pre> |
---|
204 | * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site) |
---|
205 | * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now) |
---|
206 | * @cfg {String} domain The domain to save the cookie for. Note that you cannot specify a different domain than |
---|
207 | * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include |
---|
208 | * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same |
---|
209 | * domain the page is running on including the 'www' like 'www.extjs.com') |
---|
210 | * @cfg {Boolean} secure True if the site is using SSL (defaults to false) |
---|
211 | * @constructor |
---|
212 | * Create a new CookieProvider |
---|
213 | * @param {Object} config The configuration object |
---|
214 | */ |
---|
215 | Ext.state.CookieProvider = function(config){ |
---|
216 | Ext.state.CookieProvider.superclass.constructor.call(this); |
---|
217 | this.path = "/"; |
---|
218 | this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days |
---|
219 | this.domain = null; |
---|
220 | this.secure = false; |
---|
221 | Ext.apply(this, config); |
---|
222 | this.state = this.readCookies(); |
---|
223 | }; |
---|
224 | |
---|
225 | Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, { |
---|
226 | // private |
---|
227 | set : function(name, value){ |
---|
228 | if(typeof value == "undefined" || value === null){ |
---|
229 | this.clear(name); |
---|
230 | return; |
---|
231 | } |
---|
232 | this.setCookie(name, value); |
---|
233 | Ext.state.CookieProvider.superclass.set.call(this, name, value); |
---|
234 | }, |
---|
235 | |
---|
236 | // private |
---|
237 | clear : function(name){ |
---|
238 | this.clearCookie(name); |
---|
239 | Ext.state.CookieProvider.superclass.clear.call(this, name); |
---|
240 | }, |
---|
241 | |
---|
242 | // private |
---|
243 | readCookies : function(){ |
---|
244 | var cookies = {}; |
---|
245 | var c = document.cookie + ";"; |
---|
246 | var re = /\s?(.*?)=(.*?);/g; |
---|
247 | var matches; |
---|
248 | while((matches = re.exec(c)) != null){ |
---|
249 | var name = matches[1]; |
---|
250 | var value = matches[2]; |
---|
251 | if(name && name.substring(0,3) == "ys-"){ |
---|
252 | cookies[name.substr(3)] = this.decodeValue(value); |
---|
253 | } |
---|
254 | } |
---|
255 | return cookies; |
---|
256 | }, |
---|
257 | |
---|
258 | // private |
---|
259 | setCookie : function(name, value){ |
---|
260 | document.cookie = "ys-"+ name + "=" + this.encodeValue(value) + |
---|
261 | ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) + |
---|
262 | ((this.path == null) ? "" : ("; path=" + this.path)) + |
---|
263 | ((this.domain == null) ? "" : ("; domain=" + this.domain)) + |
---|
264 | ((this.secure == true) ? "; secure" : ""); |
---|
265 | }, |
---|
266 | |
---|
267 | // private |
---|
268 | clearCookie : function(name){ |
---|
269 | document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" + |
---|
270 | ((this.path == null) ? "" : ("; path=" + this.path)) + |
---|
271 | ((this.domain == null) ? "" : ("; domain=" + this.domain)) + |
---|
272 | ((this.secure == true) ? "; secure" : ""); |
---|
273 | } |
---|
274 | }); |
---|