~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/extjs/source/data/DataField.js

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 3.0 RC2
 
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 encapsulates 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, for example,
 
109
     * the <tt>dataIndex</tt> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
 
110
     * <p>Note: In the simplest case, if no properties other than <tt>name</tt> are required, a field
 
111
     * definition may consist of just a String for the field name.</p>
 
112
     */
 
113
    /**
 
114
     * @cfg {String} type
 
115
     * (Optional) The data type for conversion to displayable value if <tt>{@link Ext.data.Field#convert convert}</tt>
 
116
     * has not been specified. Possible values are
 
117
     * <div class="mdetail-params"><ul>
 
118
     * <li>auto (Default, implies no conversion)</li>
 
119
     * <li>string</li>
 
120
     * <li>int</li>
 
121
     * <li>float</li>
 
122
     * <li>boolean</li>
 
123
     * <li>date</li></ul></div>
 
124
     */
 
125
    /**
 
126
     * @cfg {Function} convert
 
127
     * (Optional) A function which converts the value provided by the Reader into an object that will be stored
 
128
     * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
 
129
     * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
 
130
     * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
 
131
     * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
 
132
     * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
 
133
     *  ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
 
134
     * </ul></div>
 
135
     * <pre><code>
 
136
// example of convert function
 
137
function fullName(v, record){
 
138
    return record.name.last + ', ' + record.name.first;
 
139
}
 
140
 
 
141
function location(v, record){
 
142
    return !record.city ? '' : (record.city + ', ' + record.state);
 
143
}
 
144
 
 
145
var Dude = Ext.data.Record.create([
 
146
    {name: 'fullname',  convert: fullName},
 
147
    {name: 'firstname', mapping: 'name.first'},
 
148
    {name: 'lastname',  mapping: 'name.last'},
 
149
    {name: 'city', defaultValue: 'homeless'},
 
150
    'state',
 
151
    {name: 'location',  convert: location}
 
152
]);
 
153
 
 
154
// create the data store
 
155
var store = new Ext.data.Store({
 
156
    reader: new Ext.data.JsonReader(
 
157
        {
 
158
            idProperty: 'key',
 
159
            root: 'daRoot',  
 
160
            totalProperty: 'total'
 
161
        },
 
162
        Dude  // recordType
 
163
    )
 
164
});
 
165
 
 
166
var myData = [
 
167
    { key: 1,
 
168
      name: { first: 'Fat',    last:  'Albert' }
 
169
      // notice no city, state provided in data object
 
170
    },
 
171
    { key: 2,
 
172
      name: { first: 'Barney', last:  'Rubble' },
 
173
      city: 'Bedrock', state: 'Stoneridge'
 
174
    },
 
175
    { key: 3,
 
176
      name: { first: 'Cliff',  last:  'Claven' },
 
177
      city: 'Boston',  state: 'MA'
 
178
    }
 
179
];
 
180
     * </code></pre>
 
181
     */
 
182
    /**
 
183
     * @cfg {String} dateFormat
 
184
     * (Optional) A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
 
185
     * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
 
186
     * javascript millisecond timestamp.
 
187
     */
 
188
    dateFormat: null,
 
189
    /**
 
190
     * @cfg {Mixed} defaultValue
 
191
     * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
 
192
     * when the item referenced by the <tt>{@link Ext.data.Field#mapping mapping}</tt> does not exist in the data
 
193
     * object (i.e. undefined). (defaults to "")
 
194
     */
 
195
    defaultValue: "",
 
196
    /**
 
197
     * @cfg {String/Number} mapping
 
198
     * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
 
199
     * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
 
200
     * If the path expression is the same as the field name, the mapping may be omitted.</p>
 
201
     * <p>The form of the mapping expression depends on the Reader being used.</p>
 
202
     * <div class="mdetail-params"><ul>
 
203
     * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
 
204
     * expression to reference the data from an element of the data item's {@link Ext.data.JsonReader#root root} Array. Defaults to the field name.</div></li>
 
205
     * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
 
206
     * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
 
207
     * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
 
208
     * of the field's value. Defaults to the field specification's Array position.</div></li>
 
209
     * </ul></div>
 
210
     * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
 
211
     * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
 
212
     * return the desired data.</p>
 
213
     */
 
214
    mapping: null,
 
215
    /**
 
216
     * @cfg {Function} sortType
 
217
     * (Optional) A function which converts a Field's value to a comparable value in order to ensure
 
218
     * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
 
219
     * sort example:<pre><code>
 
220
// current sort     after sort we want
 
221
// +-+------+          +-+------+
 
222
// |1|First |          |1|First |
 
223
// |2|Last  |          |3|Second|
 
224
// |3|Second|          |2|Last  |
 
225
// +-+------+          +-+------+
 
226
 
 
227
sortType: function(value) {
 
228
   switch (value.toLowerCase()) // native toLowerCase():
 
229
   {
 
230
      case 'first': return 1;
 
231
      case 'second': return 2;
 
232
      default: return 3;
 
233
   }
 
234
}
 
235
     * </code></pre>
 
236
     */
 
237
    sortType : null,
 
238
    /**
 
239
     * @cfg {String} sortDir
 
240
     * (Optional) Initial direction to sort (<tt>"ASC"</tt> or  <tt>"DESC"</tt>).  Defaults to
 
241
     * <tt>"ASC"</tt>.
 
242
     */
 
243
    sortDir : "ASC",
 
244
        /**
 
245
         * @cfg {Boolean} allowBlank 
 
246
         * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <tt>true</tt>.
 
247
         * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
 
248
         * to evaluate to <tt>false</tt>.
 
249
         */
 
250
        allowBlank : true
 
251
};
 
 
b'\\ No newline at end of file'