initial import
[roojs1] / Roo / state / Provider.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
13  * @class Roo.state.Provider
14  * Abstract base class for state provider implementations. This class provides methods
15  * for encoding and decoding <b>typed</b> variables including dates and defines the 
16  * Provider interface.
17  */
18 Roo.state.Provider = function(){
19     /**
20      * @event statechange
21      * Fires when a state change occurs.
22      * @param {Provider} this This state provider
23      * @param {String} key The state key which was changed
24      * @param {String} value The encoded value for the state
25      */
26     this.addEvents({
27         "statechange": true
28     });
29     this.state = {};
30     Roo.state.Provider.superclass.constructor.call(this);
31 };
32 Roo.extend(Roo.state.Provider, Roo.util.Observable, {
33     /**
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     /**
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     /**
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     /**
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                 var values = v.split("^");
84                 for(var i = 0, len = values.length; i < len; i++){
85                     all.push(this.decodeValue(values[i]));
86                 }
87                 return all;
88            case "o":
89                 var all = {};
90                 var values = v.split("^");
91                 for(var i = 0, len = values.length; i < len; i++){
92                     var kv = values[i].split("=");
93                     all[kv[0]] = this.decodeValue(kv[1]);
94                 }
95                 return all;
96            default:
97                 return v;
98         }
99     },
100     
101     /**
102      * Encodes a value including type information.  Decode with {@link #decodeValue}.
103      * @param {Mixed} value The value to encode
104      * @return {String} The encoded value
105      */
106     encodeValue : function(v){
107         var enc;
108         if(typeof v == "number"){
109             enc = "n:" + v;
110         }else if(typeof v == "boolean"){
111             enc = "b:" + (v ? "1" : "0");
112         }else if(v instanceof Date){
113             enc = "d:" + v.toGMTString();
114         }else if(v instanceof Array){
115             var flat = "";
116             for(var i = 0, len = v.length; i < len; i++){
117                 flat += this.encodeValue(v[i]);
118                 if(i != len-1) flat += "^";
119             }
120             enc = "a:" + flat;
121         }else if(typeof v == "object"){
122             var flat = "";
123             for(var key in v){
124                 if(typeof v[key] != "function"){
125                     flat += key + "=" + this.encodeValue(v[key]) + "^";
126                 }
127             }
128             enc = "o:" + flat.substring(0, flat.length-1);
129         }else{
130             enc = "s:" + v;
131         }
132         return escape(enc);        
133     }
134 });
135