3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.state.Provider"></div>/**
15 * @class Ext.state.Provider
16 * Abstract base class for state provider implementations. This class provides methods
17 * for encoding and decoding <b>typed</b> variables including dates and defines the
20 Ext.state.Provider = function(){
21 <div id="event-Ext.state.Provider-statechange"></div>/**
23 * Fires when a state change occurs.
24 * @param {Provider} this This state provider
25 * @param {String} key The state key which was changed
26 * @param {String} value The encoded value for the state
28 this.addEvents("statechange");
30 Ext.state.Provider.superclass.constructor.call(this);
32 Ext.extend(Ext.state.Provider, Ext.util.Observable, {
33 <div id="method-Ext.state.Provider-get"></div>/**
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
39 get : function(name, defaultValue){
40 return typeof this.state[name] == "undefined" ?
41 defaultValue : this.state[name];
44 <div id="method-Ext.state.Provider-clear"></div>/**
45 * Clears a value from the state
46 * @param {String} name The key name
48 clear : function(name){
49 delete this.state[name];
50 this.fireEvent("statechange", this, name, null);
53 <div id="method-Ext.state.Provider-set"></div>/**
54 * Sets the value for a key
55 * @param {String} name The key name
56 * @param {Mixed} value The value to set
58 set : function(name, value){
59 this.state[name] = value;
60 this.fireEvent("statechange", this, name, value);
63 <div id="method-Ext.state.Provider-decodeValue"></div>/**
64 * Decodes a string previously encoded with {@link #encodeValue}.
65 * @param {String} value The value to decode
66 * @return {Mixed} The decoded value
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]) return; // non state cookie
72 var type = matches[1];
78 return new Date(Date.parse(v));
84 Ext.each(v.split('^'), function(val){
85 all.push(this.decodeValue(val));
92 Ext.each(v.split('^'), function(val){
93 var kv = val.split('=');
94 all[kv[0]] = this.decodeValue(kv[1]);
103 <div id="method-Ext.state.Provider-encodeValue"></div>/**
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
108 encodeValue : function(v){
110 if(typeof v == "number"){
112 }else if(typeof v == "boolean"){
113 enc = "b:" + (v ? "1" : "0");
114 }else if(Ext.isDate(v)){
115 enc = "d:" + v.toGMTString();
116 }else if(Ext.isArray(v)){
118 for(var i = 0, len = v.length; i < len; i++){
119 flat += this.encodeValue(v[i]);
120 if(i != len-1) flat += "^";
123 }else if(typeof v == "object"){
126 if(typeof v[key] != "function" && v[key] !== undefined){
127 flat += key + "=" + this.encodeValue(v[key]) + "^";
130 enc = "o:" + flat.substring(0, flat.length-1);