commit extjs-2.2.1
[extjs.git] / source / data / DataField.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.data.Field\r
11  * <p>This class encpasulates the field definition information specified in the field definition objects\r
12  * passed to {@link Ext.data.Record#create}.</p>\r
13  * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}\r
14  * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>\r
15 */\r
16 Ext.data.Field = function(config){\r
17     if(typeof config == "string"){\r
18         config = {name: config};\r
19     }\r
20     Ext.apply(this, config);\r
21     \r
22     if(!this.type){\r
23         this.type = "auto";\r
24     }\r
25     \r
26     var st = Ext.data.SortTypes;\r
27     // named sortTypes are supported, here we look them up\r
28     if(typeof this.sortType == "string"){\r
29         this.sortType = st[this.sortType];\r
30     }\r
31     \r
32     // set default sortType for strings and dates\r
33     if(!this.sortType){\r
34         switch(this.type){\r
35             case "string":\r
36                 this.sortType = st.asUCString;\r
37                 break;\r
38             case "date":\r
39                 this.sortType = st.asDate;\r
40                 break;\r
41             default:\r
42                 this.sortType = st.none;\r
43         }\r
44     }\r
45 \r
46     // define once\r
47     var stripRe = /[\$,%]/g;\r
48 \r
49     // prebuilt conversion function for this field, instead of\r
50     // switching every time we're reading a value\r
51     if(!this.convert){\r
52         var cv, dateFormat = this.dateFormat;\r
53         switch(this.type){\r
54             case "":\r
55             case "auto":\r
56             case undefined:\r
57                 cv = function(v){ return v; };\r
58                 break;\r
59             case "string":\r
60                 cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };\r
61                 break;\r
62             case "int":\r
63                 cv = function(v){\r
64                     return v !== undefined && v !== null && v !== '' ?\r
65                            parseInt(String(v).replace(stripRe, ""), 10) : '';\r
66                     };\r
67                 break;\r
68             case "float":\r
69                 cv = function(v){\r
70                     return v !== undefined && v !== null && v !== '' ?\r
71                            parseFloat(String(v).replace(stripRe, ""), 10) : ''; \r
72                     };\r
73                 break;\r
74             case "bool":\r
75             case "boolean":\r
76                 cv = function(v){ return v === true || v === "true" || v == 1; };\r
77                 break;\r
78             case "date":\r
79                 cv = function(v){\r
80                     if(!v){\r
81                         return '';\r
82                     }\r
83                     if(Ext.isDate(v)){\r
84                         return v;\r
85                     }\r
86                     if(dateFormat){\r
87                         if(dateFormat == "timestamp"){\r
88                             return new Date(v*1000);\r
89                         }\r
90                         if(dateFormat == "time"){\r
91                             return new Date(parseInt(v, 10));\r
92                         }\r
93                         return Date.parseDate(v, dateFormat);\r
94                     }\r
95                     var parsed = Date.parse(v);\r
96                     return parsed ? new Date(parsed) : null;\r
97                 };\r
98              break;\r
99             \r
100         }\r
101         this.convert = cv;\r
102     }\r
103 };\r
104 \r
105 Ext.data.Field.prototype = {\r
106     /**\r
107      * @cfg {String} name\r
108      * The name by which the field is referenced within the Record. This is referenced by,\r
109      * for example, the <em>dataIndex</em> property in column definition objects passed to {@link Ext.grid.ColumnModel}\r
110      */\r
111     /**\r
112      * @cfg {String} type\r
113      * (Optional) The data type for conversion to displayable value. Possible values are\r
114      * <ul><li>auto (Default, implies no conversion)</li>\r
115      * <li>string</li>\r
116      * <li>int</li>\r
117      * <li>float</li>\r
118      * <li>boolean</li>\r
119      * <li>date</li></ul>\r
120      */\r
121     /**\r
122      * @cfg {Function} convert\r
123      * (Optional) A function which converts the value provided by the Reader into an object that will be stored\r
124      * in the Record. It is passed the following parameters:<ul>\r
125      * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader.</div></li>\r
126      * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.\r
127      * Depending on Reader type, this could be an Array, an object, or an XML element.</div></li>\r
128      * </ul>\r
129      */\r
130     /**\r
131      * @cfg {String} dateFormat\r
132      * (Optional) A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the\r
133      * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a \r
134      * javascript millisecond timestamp.\r
135      */\r
136     dateFormat: null,\r
137     /**\r
138      * @cfg {Mixed} defaultValue\r
139      * (Optional) The default value used <b>when a Record is being created by a\r
140      * {@link Ext.data.Reader Reader}</b> when the item referenced by the <b><tt>mapping</tt></b> does not exist in the data object\r
141      * (i.e. undefined). (defaults to "")\r
142      */\r
143     defaultValue: "",\r
144     /**\r
145      * @cfg {String} mapping\r
146      * (Optional) A path specification for use by the {@link Ext.data.Reader} implementation\r
147      * that is creating the Record to access the data value from the data object. If an {@link Ext.data.JsonReader}\r
148      * is being used, then this is a string containing the javascript expression to reference the data relative to\r
149      * the Record item's root. If an {@link Ext.data.XmlReader} is being used, this is an {@link Ext.DomQuery} path\r
150      * to the data item relative to the Record element. If the mapping expression is the same as the field name,\r
151      * this may be omitted.\r
152      */\r
153     mapping: null,\r
154     /**\r
155      * @cfg {Function} sortType\r
156      * (Optional) A function which converts a Field's value to a comparable value in order to ensure correct\r
157      * sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}\r
158      */\r
159     sortType : null,\r
160     /**\r
161      * @cfg {String} sortDir\r
162      * (Optional) Initial direction to sort. "ASC" or "DESC"\r
163      */\r
164     sortDir : "ASC"\r
165 };