3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.state.Provider
17 * <p>Abstract base class for state provider implementations. The provider is responsible
18 * for setting values and extracting values to/from the underlying storage source. The
19 * storage source can vary and the details should be implemented in a subclass. For example
20 * a provider could use a server side database or the browser localstorage where supported.</p>
22 * <p>This class provides methods for encoding and decoding <b>typed</b> variables including
23 * dates and defines the Provider interface. By default these methods put the value and the
24 * type information into a delimited string that can be stored. These should be overridden in
25 * a subclass if you want to change the format of the encoded value and subsequent decoding.</p>
27 Ext.define('Ext.state.Provider', {
29 observable: 'Ext.util.Observable'
33 * @cfg {String} prefix A string to prefix to items stored in the underlying state store.
34 * Defaults to <tt>'ext-'</tt>
38 constructor : function(config){
39 config = config || {};
41 Ext.apply(me, config);
44 * Fires when a state change occurs.
45 * @param {Ext.state.Provider} this This state provider
46 * @param {String} key The state key which was changed
47 * @param {String} value The encoded value for the state
49 me.addEvents("statechange");
51 me.mixins.observable.constructor.call(me);
55 * Returns the current value for a key
56 * @param {String} name The key name
57 * @param {Object} defaultValue A default value to return if the key's value is not found
58 * @return {Object} The state data
60 get : function(name, defaultValue){
61 return typeof this.state[name] == "undefined" ?
62 defaultValue : this.state[name];
66 * Clears a value from the state
67 * @param {String} name The key name
69 clear : function(name){
71 delete me.state[name];
72 me.fireEvent("statechange", me, name, null);
76 * Sets the value for a key
77 * @param {String} name The key name
78 * @param {Object} value The value to set
80 set : function(name, value){
82 me.state[name] = value;
83 me.fireEvent("statechange", me, name, value);
87 * Decodes a string previously encoded with {@link #encodeValue}.
88 * @param {String} value The value to decode
89 * @return {Object} The decoded value
91 decodeValue : function(value){
102 re = /^(a|n|d|b|s|o|e)\:(.*)$/,
103 matches = re.exec(unescape(value)),
109 if(!matches || !matches[1]){
119 return parseFloat(value);
121 return new Date(Date.parse(value));
123 return (value == '1');
127 Ext.each(value.split('^'), function(val){
128 all.push(me.decodeValue(val));
135 Ext.each(value.split('^'), function(val){
136 keyValue = val.split('=');
137 all[keyValue[0]] = me.decodeValue(keyValue[1]);
147 * Encodes a value including type information. Decode with {@link #decodeValue}.
148 * @param {Object} value The value to encode
149 * @return {String} The encoded value
151 encodeValue : function(value){
160 } else if(typeof value == 'number') {
162 } else if(typeof value == 'boolean') {
163 enc = 'b:' + (value ? '1' : '0');
164 } else if(Ext.isDate(value)) {
165 enc = 'd:' + value.toGMTString();
166 } else if(Ext.isArray(value)) {
167 for (len = value.length; i < len; i++) {
168 flat += this.encodeValue(value[i]);
174 } else if (typeof value == 'object') {
176 if (typeof value[key] != 'function' && value[key] !== undefined) {
177 flat += key + '=' + this.encodeValue(value[key]) + '^';
180 enc = 'o:' + flat.substring(0, flat.length-1);