Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / Provider1.html
1 <html>
2 <head>
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>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
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
18  * Provider interface.
19  */
20 Ext.state.Provider = function(){
21     <div id="event-Ext.state.Provider-statechange"></div>/**
22      * @event statechange
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
27      */
28     this.addEvents("statechange");
29     this.state = {};
30     Ext.state.Provider.superclass.constructor.call(this);
31 };
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
38      */
39     get : function(name, defaultValue){
40         return typeof this.state[name] == "undefined" ?
41             defaultValue : this.state[name];
42     },
43
44     <div id="method-Ext.state.Provider-clear"></div>/**
45      * Clears a value from the state
46      * @param {String} name The key name
47      */
48     clear : function(name){
49         delete this.state[name];
50         this.fireEvent("statechange", this, name, null);
51     },
52
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
57      */
58     set : function(name, value){
59         this.state[name] = value;
60         this.fireEvent("statechange", this, name, value);
61     },
62
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
67      */
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];
73         var v = matches[2];
74         switch(type){
75             case "n":
76                 return parseFloat(v);
77             case "d":
78                 return new Date(Date.parse(v));
79             case "b":
80                 return (v == "1");
81             case "a":
82                 var all = [];
83                 if(v != ''){
84                     Ext.each(v.split('^'), function(val){
85                         all.push(this.decodeValue(val));
86                     }, this);
87                 }
88                 return all;
89            case "o":
90                 var all = {};
91                 if(v != ''){
92                     Ext.each(v.split('^'), function(val){
93                         var kv = val.split('=');
94                         all[kv[0]] = this.decodeValue(kv[1]);
95                     }, this);
96                 }
97                 return all;
98            default:
99                 return v;
100         }
101     },
102
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
107      */
108     encodeValue : function(v){
109         var enc;
110         if(typeof v == "number"){
111             enc = "n:" + v;
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)){
117             var flat = "";
118             for(var i = 0, len = v.length; i < len; i++){
119                 flat += this.encodeValue(v[i]);
120                 if(i != len-1) flat += "^";
121             }
122             enc = "a:" + flat;
123         }else if(typeof v == "object"){
124             var flat = "";
125             for(var key in v){
126                 if(typeof v[key] != "function" && v[key] !== undefined){
127                     flat += key + "=" + this.encodeValue(v[key]) + "^";
128                 }
129             }
130             enc = "o:" + flat.substring(0, flat.length-1);
131         }else{
132             enc = "s:" + v;
133         }
134         return escape(enc);
135     }
136 });
137 </pre>
138 </body>
139 </html>