279 lines
No EOL
12 KiB
HTML
279 lines
No EOL
12 KiB
HTML
<html><head><title>State.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>State.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Ext.state.Provider
|
|
* Abstract base class <b>for</b> state provider implementations. This class provides methods
|
|
* <b>for</b> encoding and decoding <b>typed</b> variables including dates and defines the
|
|
* Provider interface.
|
|
*/</i>
|
|
Ext.state.Provider = <b>function</b>(){
|
|
Ext.state.Provider.superclass.constructor.call(<b>this</b>);
|
|
<i>/**
|
|
* @event statechange
|
|
* Fires when a state change occurs.
|
|
* @param {Provider} <b>this</b> This state provider
|
|
* @param {String} key The state key which was changed
|
|
* @param {String} value The encoded value <b>for</b> the state
|
|
*/</i>
|
|
<b>this</b>.addEvents({
|
|
"statechange": true
|
|
});
|
|
<b>this</b>.state = {};
|
|
Ext.state.Provider.superclass.constructor.call(<b>this</b>);
|
|
};
|
|
Ext.extend(Ext.state.Provider, Ext.util.Observable, {
|
|
<i>/**
|
|
* Returns the current value <b>for</b> a key
|
|
* @param {String} name The key name
|
|
* @param {Mixed} defaultValue A <b>default</b> value to <b>return</b> if the key's value is not found
|
|
* @<b>return</b> {Mixed} The state data
|
|
*/</i>
|
|
get : <b>function</b>(name, defaultValue){
|
|
<b>return</b> typeof <b>this</b>.state[name] == "undefined" ?
|
|
defaultValue : <b>this</b>.state[name];
|
|
},
|
|
|
|
<i>/**
|
|
* Clears a value from the state
|
|
* @param {String} name The key name
|
|
*/</i>
|
|
clear : <b>function</b>(name){
|
|
<b>delete</b> this.state[name];
|
|
<b>this</b>.fireEvent("statechange", <b>this</b>, name, null);
|
|
},
|
|
|
|
<i>/**
|
|
* Sets the value <b>for</b> a key
|
|
* @param {String} name The key name
|
|
* @param {Mixed} value The value to set
|
|
*/</i>
|
|
set : <b>function</b>(name, value){
|
|
<b>this</b>.state[name] = value;
|
|
<b>this</b>.fireEvent("statechange", <b>this</b>, name, value);
|
|
},
|
|
|
|
<i>/**
|
|
* Decodes a string previously encoded <b>with</b> {@link #encodeValue}.
|
|
* @param {String} value The value to decode
|
|
* @<b>return</b> {Mixed} The decoded value
|
|
*/</i>
|
|
decodeValue : <b>function</b>(cookie){
|
|
<b>var</b> re = /^(a|n|d|b|s|o)\:(.*)$/;
|
|
<b>var</b> matches = re.exec(unescape(cookie));
|
|
<b>if</b>(!matches || !matches[1]) <b>return</b>; <i>// non state cookie</i>
|
|
<b>var</b> type = matches[1];
|
|
<b>var</b> v = matches[2];
|
|
<b>switch</b>(type){
|
|
<b>case</b> "n":
|
|
<b>return</b> parseFloat(v);
|
|
<b>case</b> "d":
|
|
<b>return</b> new Date(Date.parse(v));
|
|
<b>case</b> "b":
|
|
<b>return</b> (v == "1");
|
|
<b>case</b> "a":
|
|
<b>var</b> all = [];
|
|
<b>var</b> values = v.split("^");
|
|
<b>for</b>(var i = 0, len = values.length; i < len; i++){
|
|
all.push(<b>this</b>.decodeValue(values[i]));
|
|
}
|
|
<b>return</b> all;
|
|
<b>case</b> "o":
|
|
<b>var</b> all = {};
|
|
<b>var</b> values = v.split("^");
|
|
<b>for</b>(var i = 0, len = values.length; i < len; i++){
|
|
<b>var</b> kv = values[i].split("=");
|
|
all[kv[0]] = <b>this</b>.decodeValue(kv[1]);
|
|
}
|
|
<b>return</b> all;
|
|
<b>default</b>:
|
|
<b>return</b> v;
|
|
}
|
|
},
|
|
|
|
<i>/**
|
|
* Encodes a value including type information. Decode <b>with</b> {@link #decodeValue}.
|
|
* @param {Mixed} value The value to encode
|
|
* @<b>return</b> {String} The encoded value
|
|
*/</i>
|
|
encodeValue : <b>function</b>(v){
|
|
<b>var</b> enc;
|
|
<b>if</b>(typeof v == "number"){
|
|
enc = "n:" + v;
|
|
}<b>else</b> if(<b>typeof</b> v == "boolean"){
|
|
enc = "b:" + (v ? "1" : "0");
|
|
}<b>else</b> if(v instanceof Date){
|
|
enc = "d:" + v.toGMTString();
|
|
}<b>else</b> if(v instanceof Array){
|
|
<b>var</b> flat = "";
|
|
<b>for</b>(var i = 0, len = v.length; i < len; i++){
|
|
flat += <b>this</b>.encodeValue(v[i]);
|
|
<b>if</b>(i != len-1) flat += "^";
|
|
}
|
|
enc = "a:" + flat;
|
|
}<b>else</b> if(<b>typeof</b> v == "object"){
|
|
<b>var</b> flat = "";
|
|
<b>for</b>(var key <b>in</b> v){
|
|
<b>if</b>(typeof v[key] != "<b>function</b>"){
|
|
flat += key + "=" + <b>this</b>.encodeValue(v[key]) + "^";
|
|
}
|
|
}
|
|
enc = "o:" + flat.substring(0, flat.length-1);
|
|
}<b>else</b>{
|
|
enc = "s:" + v;
|
|
}
|
|
<b>return</b> escape(enc);
|
|
}
|
|
});
|
|
|
|
<i>/**
|
|
* @class Ext.state.Manager
|
|
* This is the global state manager. By <b>default</b> all components that are "state aware" check <b>this</b> class
|
|
* <b>for</b> state information <b>if</b> you don't pass them a custom state provider. In order <b>for</b> this class
|
|
* to be useful, it must be initialized <b>with</b> a provider when your application initializes.
|
|
<pre><code>
|
|
<i>// <b>in</b> your initialization <b>function</b></i>
|
|
init : <b>function</b>(){
|
|
Ext.state.Manager.setProvider(<b>new</b> Ext.state.CookieProvider());
|
|
...
|
|
<i>// supposed you have a {@link Ext.BorderLayout}</i>
|
|
<b>var</b> layout = <b>new</b> Ext.BorderLayout(...);
|
|
layout.restoreState();
|
|
<i>// or a {Ext.BasicDialog}</i>
|
|
<b>var</b> dialog = <b>new</b> Ext.BasicDialog(...);
|
|
dialog.restoreState();
|
|
</code></pre>
|
|
* @singleton
|
|
*/</i>
|
|
Ext.state.Manager = <b>function</b>(){
|
|
<b>var</b> provider = <b>new</b> Ext.state.Provider();
|
|
|
|
<b>return</b> {
|
|
<i>/**
|
|
* Configures the <b>default</b> state provider <b>for</b> your application
|
|
* @param {Provider} stateProvider The state provider to set
|
|
*/</i>
|
|
setProvider : <b>function</b>(stateProvider){
|
|
provider = stateProvider;
|
|
},
|
|
|
|
<i>/**
|
|
* Returns the current value <b>for</b> a key
|
|
* @param {String} name The key name
|
|
* @param {Mixed} defaultValue The <b>default</b> value to <b>return</b> if the key lookup does not match
|
|
* @<b>return</b> {Mixed} The state data
|
|
*/</i>
|
|
get : <b>function</b>(key, defaultValue){
|
|
<b>return</b> provider.get(key, defaultValue);
|
|
},
|
|
|
|
<i>/**
|
|
* Sets the value <b>for</b> a key
|
|
* @param {String} name The key name
|
|
* @param {Mixed} value The state data
|
|
*/</i>
|
|
set : <b>function</b>(key, value){
|
|
provider.set(key, value);
|
|
},
|
|
|
|
<i>/**
|
|
* Clears a value from the state
|
|
* @param {String} name The key name
|
|
*/</i>
|
|
clear : <b>function</b>(key){
|
|
provider.clear(key);
|
|
},
|
|
|
|
<i>/**
|
|
* Gets the currently configured state provider
|
|
* @<b>return</b> {Provider} The state provider
|
|
*/</i>
|
|
getProvider : <b>function</b>(){
|
|
<b>return</b> provider;
|
|
}
|
|
};
|
|
}();
|
|
|
|
<i>/**
|
|
* @class Ext.state.CookieProvider
|
|
* @extends Ext.state.Provider
|
|
* The <b>default</b> Provider implementation which saves state via cookies.
|
|
* <br />Usage:
|
|
<pre><code>
|
|
<b>var</b> cp = <b>new</b> Ext.state.CookieProvider({
|
|
path: "/cgi-bin/",
|
|
expires: <b>new</b> Date(<b>new</b> Date().getTime()+(1000*60*60*24*30)); <i>//30 days</i>
|
|
domain: "extjs.com"
|
|
})
|
|
Ext.state.Manager.setProvider(cp);
|
|
</code></pre>
|
|
* @cfg {String} path The path <b>for</b> which the cookie is active (defaults to root '/' which makes it active <b>for</b> all pages <b>in</b> the site)
|
|
* @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
|
|
* @cfg {String} domain The domain to save the cookie <b>for</b>. Note that you cannot specify a different domain than
|
|
* your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include
|
|
* all sub-domains <b>if</b> you need to access cookies across different sub-domains (defaults to null which uses the same
|
|
* domain the page is running on including the 'www' like 'www.extjs.com')
|
|
* @cfg {Boolean} secure True <b>if</b> the site is using SSL (defaults to false)
|
|
* @constructor
|
|
* Create a <b>new</b> CookieProvider
|
|
* @param {Object} config The configuration object
|
|
*/</i>
|
|
Ext.state.CookieProvider = <b>function</b>(config){
|
|
Ext.state.CookieProvider.superclass.constructor.call(<b>this</b>);
|
|
<b>this</b>.path = "/";
|
|
<b>this</b>.expires = <b>new</b> Date(<b>new</b> Date().getTime()+(1000*60*60*24*7)); <i>//7 days</i>
|
|
<b>this</b>.domain = null;
|
|
<b>this</b>.secure = false;
|
|
Ext.apply(<b>this</b>, config);
|
|
<b>this</b>.state = <b>this</b>.readCookies();
|
|
};
|
|
|
|
Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, {
|
|
<i>// private</i>
|
|
set : <b>function</b>(name, value){
|
|
<b>if</b>(typeof value == "undefined" || value === null){
|
|
<b>this</b>.clear(name);
|
|
<b>return</b>;
|
|
}
|
|
<b>this</b>.setCookie(name, value);
|
|
Ext.state.CookieProvider.superclass.set.call(<b>this</b>, name, value);
|
|
},
|
|
|
|
<i>// private</i>
|
|
clear : <b>function</b>(name){
|
|
<b>this</b>.clearCookie(name);
|
|
Ext.state.CookieProvider.superclass.clear.call(<b>this</b>, name);
|
|
},
|
|
|
|
<i>// private</i>
|
|
readCookies : <b>function</b>(){
|
|
<b>var</b> cookies = {};
|
|
<b>var</b> c = document.cookie + ";";
|
|
<b>var</b> re = /\s?(.*?)=(.*?);/g;
|
|
<b>var</b> matches;
|
|
<b>while</b>((matches = re.exec(c)) != null){
|
|
<b>var</b> name = matches[1];
|
|
<b>var</b> value = matches[2];
|
|
<b>if</b>(name && name.substring(0,3) == "ys-"){
|
|
cookies[name.substr(3)] = <b>this</b>.decodeValue(value);
|
|
}
|
|
}
|
|
<b>return</b> cookies;
|
|
},
|
|
|
|
<i>// private</i>
|
|
setCookie : <b>function</b>(name, value){
|
|
document.cookie = "ys-"+ name + "=" + <b>this</b>.encodeValue(value) +
|
|
((<b>this</b>.expires == null) ? "" : ("; expires=" + <b>this</b>.expires.toGMTString())) +
|
|
((<b>this</b>.path == null) ? "" : ("; path=" + <b>this</b>.path)) +
|
|
((<b>this</b>.domain == null) ? "" : ("; domain=" + <b>this</b>.domain)) +
|
|
((<b>this</b>.secure == true) ? "; secure" : "");
|
|
},
|
|
|
|
<i>// private</i>
|
|
clearCookie : <b>function</b>(name){
|
|
document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
|
|
((<b>this</b>.path == null) ? "" : ("; path=" + <b>this</b>.path)) +
|
|
((<b>this</b>.domain == null) ? "" : ("; domain=" + <b>this</b>.domain)) +
|
|
((<b>this</b>.secure == true) ? "; secure" : "");
|
|
}
|
|
});
|
|
</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |