Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / src / data / Types.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.data.Types
9  * <p>This is s static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
10  * <p>The properties in this class are used as type indicators in the {@link Ext.data.Field Field} class, so to
11  * test whether a Field is of a certain type, compare the {@link Ext.data.Field#type type} property against properties
12  * of this class.</p>
13  * <p>Developers may add their own application-specific data types to this class. Definition names must be UPPERCASE.
14  * each type definition must contain three properties:</p>
15  * <div class="mdetail-params"><ul>
16  * <li><code>convert</code> : <i>Function</i><div class="sub-desc">A function to convert raw data values from a data block into the data
17  * to be stored in the Field. The function is passed the collowing parameters:
18  * <div class="mdetail-params"><ul>
19  * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
20  * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
21  * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
22  * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
23  * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
24  * </ul></div></div></li>
25  * <li><code>sortType</code> : <i>Function</i> <div class="sub-desc">A function to convert the stored data into comparable form, as defined by {@link Ext.data.SortTypes}.</div></li>
26  * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
27  * </ul></div>
28  * <p>For example, to create a VELatLong field (See the Microsoft Bing Mapping API) containing the latitude/longitude value of a datapoint on a map from a JsonReader data block
29  * which contained the properties <code>lat</code> and <code>long</code>, you would define a new data type like this:</p>
30  *<pre><code>
31 // Add a new Field data type which stores a VELatLong object in the Record.
32 Ext.data.Types.VELATLONG = {
33     convert: function(v, data) {
34         return new VELatLong(data.lat, data.long);
35     },
36     sortType: function(v) {
37         return v.Latitude;  // When sorting, order by latitude
38     },
39     type: 'VELatLong'
40 };
41 </code></pre>
42  * <p>Then, when declaring a Record, use <pre><code>
43 var types = Ext.data.Types; // allow shorthand type access
44 UnitRecord = Ext.data.Record.create([
45     { name: 'unitName', mapping: 'UnitName' },
46     { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
47     { name: 'latitude', mapping: 'lat', type: types.FLOAT },
48     { name: 'latitude', mapping: 'lat', type: types.FLOAT },
49     { name: 'position', type: types.VELATLONG }
50 ]);
51 </code></pre>
52  * @singleton
53  */
54 Ext.data.Types = new function(){
55     var st = Ext.data.SortTypes;
56     Ext.apply(this, {
57         /**
58          * @type Regexp
59          * @property stripRe
60          * A regular expression for stripping non-numeric characters from a numeric value. Defaults to <tt>/[\$,%]/g</tt>.
61          * This should be overridden for localization.
62          */
63         stripRe: /[\$,%]/g,
64         
65         /**
66          * @type Object.
67          * @property AUTO
68          * This data type means that no conversion is applied to the raw data before it is placed into a Record.
69          */
70         AUTO: {
71             convert: function(v){ return v; },
72             sortType: st.none,
73             type: 'auto'
74         },
75
76         /**
77          * @type Object.
78          * @property STRING
79          * This data type means that the raw data is converted into a String before it is placed into a Record.
80          */
81         STRING: {
82             convert: function(v){ return (v === undefined || v === null) ? '' : String(v); },
83             sortType: st.asUCString,
84             type: 'string'
85         },
86
87         /**
88          * @type Object.
89          * @property INT
90          * This data type means that the raw data is converted into an integer before it is placed into a Record.
91          * <p>The synonym <code>INTEGER</code> is equivalent.</p>
92          */
93         INT: {
94             convert: function(v){
95                 return v !== undefined && v !== null && v !== '' ?
96                     parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : 0;
97             },
98             sortType: st.none,
99             type: 'int'
100         },
101         
102         /**
103          * @type Object.
104          * @property FLOAT
105          * This data type means that the raw data is converted into a number before it is placed into a Record.
106          * <p>The synonym <code>NUMBER</code> is equivalent.</p>
107          */
108         FLOAT: {
109             convert: function(v){
110                 return v !== undefined && v !== null && v !== '' ?
111                     parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : 0;
112             },
113             sortType: st.none,
114             type: 'float'
115         },
116         
117         /**
118          * @type Object.
119          * @property BOOL
120          * <p>This data type means that the raw data is converted into a boolean before it is placed into
121          * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
122          * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
123          */
124         BOOL: {
125             convert: function(v){ return v === true || v === 'true' || v == 1; },
126             sortType: st.none,
127             type: 'bool'
128         },
129         
130         /**
131          * @type Object.
132          * @property DATE
133          * This data type means that the raw data is converted into a Date before it is placed into a Record.
134          * The date format is specified in the constructor of the {@link Ext.data.Field} to which this type is
135          * being applied.
136          */
137         DATE: {
138             convert: function(v){
139                 var df = this.dateFormat;
140                 if(!v){
141                     return null;
142                 }
143                 if(Ext.isDate(v)){
144                     return v;
145                 }
146                 if(df){
147                     if(df == 'timestamp'){
148                         return new Date(v*1000);
149                     }
150                     if(df == 'time'){
151                         return new Date(parseInt(v, 10));
152                     }
153                     return Date.parseDate(v, df);
154                 }
155                 var parsed = Date.parse(v);
156                 return parsed ? new Date(parsed) : null;
157             },
158             sortType: st.asDate,
159             type: 'date'
160         }
161     });
162     
163     Ext.apply(this, {
164         /**
165          * @type Object.
166          * @property BOOLEAN
167          * <p>This data type means that the raw data is converted into a boolean before it is placed into
168          * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
169          * <p>The synonym <code>BOOL</code> is equivalent.</p>
170          */
171         BOOLEAN: this.BOOL,
172         /**
173          * @type Object.
174          * @property INTEGER
175          * This data type means that the raw data is converted into an integer before it is placed into a Record.
176          * <p>The synonym <code>INT</code> is equivalent.</p>
177          */
178         INTEGER: this.INT,
179         /**
180          * @type Object.
181          * @property NUMBER
182          * This data type means that the raw data is converted into a number before it is placed into a Record.
183          * <p>The synonym <code>FLOAT</code> is equivalent.</p>
184          */
185         NUMBER: this.FLOAT    
186     });
187 };