Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / docs / source / Provider1.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.1
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.state.Provider"></div>/**
16  * @class Ext.state.Provider
17  * Abstract base class for state provider implementations. This class provides methods
18  * for encoding and decoding <b>typed</b> variables including dates and defines the
19  * Provider interface.
20  */
21 Ext.state.Provider = function(){
22     <div id="event-Ext.state.Provider-statechange"></div>/**
23      * @event statechange
24      * Fires when a state change occurs.
25      * @param {Provider} this This state provider
26      * @param {String} key The state key which was changed
27      * @param {String} value The encoded value for the state
28      */
29     this.addEvents("statechange");
30     this.state = {};
31     Ext.state.Provider.superclass.constructor.call(this);
32 };
33 Ext.extend(Ext.state.Provider, Ext.util.Observable, {
34     <div id="method-Ext.state.Provider-get"></div>/**
35      * Returns the current value for a key
36      * @param {String} name The key name
37      * @param {Mixed} defaultValue A default value to return if the key's value is not found
38      * @return {Mixed} The state data
39      */
40     get : function(name, defaultValue){
41         return typeof this.state[name] == "undefined" ?
42             defaultValue : this.state[name];
43     },
44
45     <div id="method-Ext.state.Provider-clear"></div>/**
46      * Clears a value from the state
47      * @param {String} name The key name
48      */
49     clear : function(name){
50         delete this.state[name];
51         this.fireEvent("statechange", this, name, null);
52     },
53
54     <div id="method-Ext.state.Provider-set"></div>/**
55      * Sets the value for a key
56      * @param {String} name The key name
57      * @param {Mixed} value The value to set
58      */
59     set : function(name, value){
60         this.state[name] = value;
61         this.fireEvent("statechange", this, name, value);
62     },
63
64     <div id="method-Ext.state.Provider-decodeValue"></div>/**
65      * Decodes a string previously encoded with {@link #encodeValue}.
66      * @param {String} value The value to decode
67      * @return {Mixed} The decoded value
68      */
69     decodeValue : function(cookie){
70         var re = /^(a|n|d|b|s|o)\:(.*)$/;
71         var matches = re.exec(unescape(cookie));
72         if(!matches || !matches[1]) return; // non state cookie
73         var type = matches[1];
74         var v = matches[2];
75         switch(type){
76             case "n":
77                 return parseFloat(v);
78             case "d":
79                 return new Date(Date.parse(v));
80             case "b":
81                 return (v == "1");
82             case "a":
83                 var all = [];
84                 if(v != ''){
85                     Ext.each(v.split('^'), function(val){
86                         all.push(this.decodeValue(val));
87                     }, this);
88                 }
89                 return all;
90            case "o":
91                 var all = {};
92                 if(v != ''){
93                     Ext.each(v.split('^'), function(val){
94                         var kv = val.split('=');
95                         all[kv[0]] = this.decodeValue(kv[1]);
96                     }, this);
97                 }
98                 return all;
99            default:
100                 return v;
101         }
102     },
103
104     <div id="method-Ext.state.Provider-encodeValue"></div>/**
105      * Encodes a value including type information.  Decode with {@link #decodeValue}.
106      * @param {Mixed} value The value to encode
107      * @return {String} The encoded value
108      */
109     encodeValue : function(v){
110         var enc;
111         if(typeof v == "number"){
112             enc = "n:" + v;
113         }else if(typeof v == "boolean"){
114             enc = "b:" + (v ? "1" : "0");
115         }else if(Ext.isDate(v)){
116             enc = "d:" + v.toGMTString();
117         }else if(Ext.isArray(v)){
118             var flat = "";
119             for(var i = 0, len = v.length; i < len; i++){
120                 flat += this.encodeValue(v[i]);
121                 if(i != len-1) flat += "^";
122             }
123             enc = "a:" + flat;
124         }else if(typeof v == "object"){
125             var flat = "";
126             for(var key in v){
127                 if(typeof v[key] != "function" && v[key] !== undefined){
128                     flat += key + "=" + this.encodeValue(v[key]) + "^";
129                 }
130             }
131             enc = "o:" + flat.substring(0, flat.length-1);
132         }else{
133             enc = "s:" + v;
134         }
135         return escape(enc);
136     }
137 });
138 </pre>    
139 </body>
140 </html>