Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Provider.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-state-Provider'>/**
19 </span> * @class Ext.state.Provider
20  * &lt;p&gt;Abstract base class for state provider implementations. The provider is responsible
21  * for setting values  and extracting values to/from the underlying storage source. The 
22  * storage source can vary and the details should be implemented in a subclass. For example
23  * a provider could use a server side database or the browser localstorage where supported.&lt;/p&gt;
24  *
25  * &lt;p&gt;This class provides methods for encoding and decoding &lt;b&gt;typed&lt;/b&gt; variables including 
26  * dates and defines the Provider interface. By default these methods put the value and the
27  * type information into a delimited string that can be stored. These should be overridden in 
28  * a subclass if you want to change the format of the encoded value and subsequent decoding.&lt;/p&gt;
29  */
30 Ext.define('Ext.state.Provider', {
31     mixins: {
32         observable: 'Ext.util.Observable'
33     },
34     
35 <span id='Ext-state-Provider-cfg-prefix'>    /**
36 </span>     * @cfg {String} prefix A string to prefix to items stored in the underlying state store. 
37      * Defaults to &lt;tt&gt;'ext-'&lt;/tt&gt;
38      */
39     prefix: 'ext-',
40     
41     constructor : function(config){
42         config = config || {};
43         var me = this;
44         Ext.apply(me, config);
45 <span id='Ext-state-Provider-event-statechange'>        /**
46 </span>         * @event statechange
47          * Fires when a state change occurs.
48          * @param {Provider} this This state provider
49          * @param {String} key The state key which was changed
50          * @param {String} value The encoded value for the state
51          */
52         me.addEvents(&quot;statechange&quot;);
53         me.state = {};
54         me.mixins.observable.constructor.call(me);
55     },
56     
57 <span id='Ext-state-Provider-method-get'>    /**
58 </span>     * Returns the current value for a key
59      * @param {String} name The key name
60      * @param {Mixed} defaultValue A default value to return if the key's value is not found
61      * @return {Mixed} The state data
62      */
63     get : function(name, defaultValue){
64         return typeof this.state[name] == &quot;undefined&quot; ?
65             defaultValue : this.state[name];
66     },
67
68 <span id='Ext-state-Provider-method-clear'>    /**
69 </span>     * Clears a value from the state
70      * @param {String} name The key name
71      */
72     clear : function(name){
73         var me = this;
74         delete me.state[name];
75         me.fireEvent(&quot;statechange&quot;, me, name, null);
76     },
77
78 <span id='Ext-state-Provider-method-set'>    /**
79 </span>     * Sets the value for a key
80      * @param {String} name The key name
81      * @param {Mixed} value The value to set
82      */
83     set : function(name, value){
84         var me = this;
85         me.state[name] = value;
86         me.fireEvent(&quot;statechange&quot;, me, name, value);
87     },
88
89 <span id='Ext-state-Provider-method-decodeValue'>    /**
90 </span>     * Decodes a string previously encoded with {@link #encodeValue}.
91      * @param {String} value The value to decode
92      * @return {Mixed} The decoded value
93      */
94     decodeValue : function(value){
95
96         // a -&gt; Array
97         // n -&gt; Number
98         // d -&gt; Date
99         // b -&gt; Boolean
100         // s -&gt; String
101         // o -&gt; Object
102         // -&gt; Empty (null)
103
104         var me = this,
105             re = /^(a|n|d|b|s|o|e)\:(.*)$/,
106             matches = re.exec(unescape(value)),
107             all,
108             type,
109             value,
110             keyValue;
111             
112         if(!matches || !matches[1]){
113             return; // non state
114         }
115         
116         type = matches[1];
117         value = matches[2];
118         switch (type) {
119             case 'e':
120                 return null;
121             case 'n':
122                 return parseFloat(value);
123             case 'd':
124                 return new Date(Date.parse(value));
125             case 'b':
126                 return (value == '1');
127             case 'a':
128                 all = [];
129                 if(value != ''){
130                     Ext.each(value.split('^'), function(val){
131                         all.push(me.decodeValue(val));
132                     }, me);
133                 }
134                 return all;
135            case 'o':
136                 all = {};
137                 if(value != ''){
138                     Ext.each(value.split('^'), function(val){
139                         keyValue = val.split('=');
140                         all[keyValue[0]] = me.decodeValue(keyValue[1]);
141                     }, me);
142                 }
143                 return all;
144            default:
145                 return value;
146         }
147     },
148
149 <span id='Ext-state-Provider-method-encodeValue'>    /**
150 </span>     * Encodes a value including type information.  Decode with {@link #decodeValue}.
151      * @param {Mixed} value The value to encode
152      * @return {String} The encoded value
153      */
154     encodeValue : function(value){
155         var flat = '',
156             i = 0,
157             enc,
158             len,
159             key;
160             
161         if (value == null) {
162             return 'e:1';    
163         } else if(typeof value == 'number') {
164             enc = 'n:' + value;
165         } else if(typeof value == 'boolean') {
166             enc = 'b:' + (value ? '1' : '0');
167         } else if(Ext.isDate(value)) {
168             enc = 'd:' + value.toGMTString();
169         } else if(Ext.isArray(value)) {
170             for (len = value.length; i &lt; len; i++) {
171                 flat += this.encodeValue(value[i]);
172                 if (i != len - 1) {
173                     flat += '^';
174                 }
175             }
176             enc = 'a:' + flat;
177         } else if (typeof value == 'object') {
178             for (key in value) {
179                 if (typeof value[key] != 'function' &amp;&amp; value[key] !== undefined) {
180                     flat += key + '=' + this.encodeValue(value[key]) + '^';
181                 }
182             }
183             enc = 'o:' + flat.substring(0, flat.length-1);
184         } else {
185             enc = 's:' + value;
186         }
187         return escape(enc);
188     }
189 });</pre>
190 </body>
191 </html>