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