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