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