~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/extjs/source/widgets/grid/PropertyGrid.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.grid.PropertyRecord
 
11
 * A specific {@link Ext.data.Record} type that represents a name/value pair and is made to work with the
 
12
 * {@link Ext.grid.PropertyGrid}.  Typically, PropertyRecords do not need to be created directly as they can be
 
13
 * created implicitly by simply using the appropriate data configs either via the {@link Ext.grid.PropertyGrid#source}
 
14
 * config property or by calling {@link Ext.grid.PropertyGrid#setSource}.  However, if the need arises, these records
 
15
 * can also be created explicitly as shwon below.  Example usage:
 
16
 * <pre><code>
 
17
var rec = new Ext.grid.PropertyRecord({
 
18
    name: 'Birthday',
 
19
    value: new Date(Date.parse('05/26/1972'))
 
20
});
 
21
// Add record to an already populated grid
 
22
grid.store.addSorted(rec);
 
23
</code></pre>
 
24
 * @constructor
 
25
 * @param {Object} config A data object in the format: {name: [name], value: [value]}.  The specified value's type
 
26
 * will be read automatically by the grid to determine the type of editor to use when displaying it.
 
27
 */
 
28
Ext.grid.PropertyRecord = Ext.data.Record.create([
 
29
    {name:'name',type:'string'}, 'value'
 
30
]);
 
31
 
 
32
/**
 
33
 * @class Ext.grid.PropertyStore
 
34
 * @extends Ext.util.Observable
 
35
 * A custom wrapper for the {@link Ext.grid.PropertyGrid}'s {@link Ext.data.Store}. This class handles the mapping
 
36
 * between the custom data source objects supported by the grid and the {@link Ext.grid.PropertyRecord} format
 
37
 * required for compatibility with the underlying store. Generally this class should not need to be used directly --
 
38
 * the grid's data should be accessed from the underlying store via the {@link #store} property.
 
39
 * @constructor
 
40
 * @param {Ext.grid.Grid} grid The grid this store will be bound to
 
41
 * @param {Object} source The source data config object
 
42
 */
 
43
Ext.grid.PropertyStore = function(grid, source){
 
44
    this.grid = grid;
 
45
    this.store = new Ext.data.Store({
 
46
        recordType : Ext.grid.PropertyRecord
 
47
    });
 
48
    this.store.on('update', this.onUpdate,  this);
 
49
    if(source){
 
50
        this.setSource(source);
 
51
    }
 
52
    Ext.grid.PropertyStore.superclass.constructor.call(this);
 
53
};
 
54
Ext.extend(Ext.grid.PropertyStore, Ext.util.Observable, {
 
55
    // protected - should only be called by the grid.  Use grid.setSource instead.
 
56
    setSource : function(o){
 
57
        this.source = o;
 
58
        this.store.removeAll();
 
59
        var data = [];
 
60
        for(var k in o){
 
61
            if(this.isEditableValue(o[k])){
 
62
                data.push(new Ext.grid.PropertyRecord({name: k, value: o[k]}, k));
 
63
            }
 
64
        }
 
65
        this.store.loadRecords({records: data}, {}, true);
 
66
    },
 
67
 
 
68
    // private
 
69
    onUpdate : function(ds, record, type){
 
70
        if(type == Ext.data.Record.EDIT){
 
71
            var v = record.data['value'];
 
72
            var oldValue = record.modified['value'];
 
73
            if(this.grid.fireEvent('beforepropertychange', this.source, record.id, v, oldValue) !== false){
 
74
                this.source[record.id] = v;
 
75
                record.commit();
 
76
                this.grid.fireEvent('propertychange', this.source, record.id, v, oldValue);
 
77
            }else{
 
78
                record.reject();
 
79
            }
 
80
        }
 
81
    },
 
82
 
 
83
    // private
 
84
    getProperty : function(row){
 
85
       return this.store.getAt(row);
 
86
    },
 
87
 
 
88
    // private
 
89
    isEditableValue: function(val){
 
90
        if(Ext.isDate(val)){
 
91
            return true;
 
92
        }else if(typeof val == 'object' || typeof val == 'function'){
 
93
            return false;
 
94
        }
 
95
        return true;
 
96
    },
 
97
 
 
98
    // private
 
99
    setValue : function(prop, value){
 
100
        this.source[prop] = value;
 
101
        this.store.getById(prop).set('value', value);
 
102
    },
 
103
 
 
104
    // protected - should only be called by the grid.  Use grid.getSource instead.
 
105
    getSource : function(){
 
106
        return this.source;
 
107
    }
 
108
});
 
109
 
 
110
/**
 
111
 * @class Ext.grid.PropertyColumnModel
 
112
 * @extends Ext.grid.ColumnModel
 
113
 * A custom column model for the {@link Ext.grid.PropertyGrid}.  Generally it should not need to be used directly.
 
114
 * @constructor
 
115
 * @param {Ext.grid.Grid} grid The grid this store will be bound to
 
116
 * @param {Object} source The source data config object
 
117
 */
 
118
Ext.grid.PropertyColumnModel = function(grid, store){
 
119
    this.grid = grid;
 
120
    var g = Ext.grid;
 
121
    g.PropertyColumnModel.superclass.constructor.call(this, [
 
122
        {header: this.nameText, width:50, sortable: true, dataIndex:'name', id: 'name', menuDisabled:true},
 
123
        {header: this.valueText, width:50, resizable:false, dataIndex: 'value', id: 'value', menuDisabled:true}
 
124
    ]);
 
125
    this.store = store;
 
126
    this.bselect = Ext.DomHelper.append(document.body, {
 
127
        tag: 'select', cls: 'x-grid-editor x-hide-display', children: [
 
128
            {tag: 'option', value: 'true', html: 'true'},
 
129
            {tag: 'option', value: 'false', html: 'false'}
 
130
        ]
 
131
    });
 
132
    var f = Ext.form;
 
133
 
 
134
    var bfield = new f.Field({
 
135
        el:this.bselect,
 
136
        bselect : this.bselect,
 
137
        autoShow: true,
 
138
        getValue : function(){
 
139
            return this.bselect.value == 'true';
 
140
        }
 
141
    });
 
142
    this.editors = {
 
143
        'date' : new g.GridEditor(new f.DateField({selectOnFocus:true})),
 
144
        'string' : new g.GridEditor(new f.TextField({selectOnFocus:true})),
 
145
        'number' : new g.GridEditor(new f.NumberField({selectOnFocus:true, style:'text-align:left;'})),
 
146
        'boolean' : new g.GridEditor(bfield)
 
147
    };
 
148
    this.renderCellDelegate = this.renderCell.createDelegate(this);
 
149
    this.renderPropDelegate = this.renderProp.createDelegate(this);
 
150
};
 
151
 
 
152
Ext.extend(Ext.grid.PropertyColumnModel, Ext.grid.ColumnModel, {
 
153
    // private - strings used for locale support
 
154
    nameText : 'Name',
 
155
    valueText : 'Value',
 
156
    dateFormat : 'm/j/Y',
 
157
 
 
158
    // private
 
159
    renderDate : function(dateVal){
 
160
        return dateVal.dateFormat(this.dateFormat);
 
161
    },
 
162
 
 
163
    // private
 
164
    renderBool : function(bVal){
 
165
        return bVal ? 'true' : 'false';
 
166
    },
 
167
 
 
168
    // private
 
169
    isCellEditable : function(colIndex, rowIndex){
 
170
        return colIndex == 1;
 
171
    },
 
172
 
 
173
    // private
 
174
    getRenderer : function(col){
 
175
        return col == 1 ?
 
176
            this.renderCellDelegate : this.renderPropDelegate;
 
177
    },
 
178
 
 
179
    // private
 
180
    renderProp : function(v){
 
181
        return this.getPropertyName(v);
 
182
    },
 
183
 
 
184
    // private
 
185
    renderCell : function(val){
 
186
        var rv = val;
 
187
        if(Ext.isDate(val)){
 
188
            rv = this.renderDate(val);
 
189
        }else if(typeof val == 'boolean'){
 
190
            rv = this.renderBool(val);
 
191
        }
 
192
        return Ext.util.Format.htmlEncode(rv);
 
193
    },
 
194
 
 
195
    // private
 
196
    getPropertyName : function(name){
 
197
        var pn = this.grid.propertyNames;
 
198
        return pn && pn[name] ? pn[name] : name;
 
199
    },
 
200
 
 
201
    // private
 
202
    getCellEditor : function(colIndex, rowIndex){
 
203
        var p = this.store.getProperty(rowIndex);
 
204
        var n = p.data['name'], val = p.data['value'];
 
205
        if(this.grid.customEditors[n]){
 
206
            return this.grid.customEditors[n];
 
207
        }
 
208
        if(Ext.isDate(val)){
 
209
            return this.editors['date'];
 
210
        }else if(typeof val == 'number'){
 
211
            return this.editors['number'];
 
212
        }else if(typeof val == 'boolean'){
 
213
            return this.editors['boolean'];
 
214
        }else{
 
215
            return this.editors['string'];
 
216
        }
 
217
    },
 
218
    
 
219
    // inherit docs
 
220
    destroy : function(){
 
221
        Ext.grid.PropertyColumnModel.superclass.destroy.call(this);
 
222
        for(var ed in this.editors){
 
223
            Ext.destroy(ed);
 
224
        }
 
225
    }
 
226
});
 
227
 
 
228
/**
 
229
 * @class Ext.grid.PropertyGrid
 
230
 * @extends Ext.grid.EditorGridPanel
 
231
 * A specialized grid implementation intended to mimic the traditional property grid as typically seen in
 
232
 * development IDEs.  Each row in the grid represents a property of some object, and the data is stored
 
233
 * as a set of name/value pairs in {@link Ext.grid.PropertyRecord}s.  Example usage:
 
234
 * <pre><code>
 
235
var grid = new Ext.grid.PropertyGrid({
 
236
    title: 'Properties Grid',
 
237
    autoHeight: true,
 
238
    width: 300,
 
239
    renderTo: 'grid-ct',
 
240
    source: {
 
241
        "(name)": "My Object",
 
242
        "Created": new Date(Date.parse('10/15/2006')),
 
243
        "Available": false,
 
244
        "Version": .01,
 
245
        "Description": "A test object"
 
246
    }
 
247
});
 
248
</pre></code>
 
249
 * @constructor
 
250
 * @param {Object} config The grid config object
 
251
 */
 
252
Ext.grid.PropertyGrid = Ext.extend(Ext.grid.EditorGridPanel, {
 
253
    /**
 
254
    * @cfg {Object} propertyNames An object containing property name/display name pairs.
 
255
    * If specified, the display name will be shown in the name column instead of the property name.
 
256
    */
 
257
    /**
 
258
    * @cfg {Object} source A data object to use as the data source of the grid (see {@link #setSource} for details).
 
259
    */
 
260
    /**
 
261
    * @cfg {Object} customEditors An object containing name/value pairs of custom editor type definitions that allow
 
262
    * the grid to support additional types of editable fields.  By default, the grid supports strongly-typed editing
 
263
    * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
 
264
    * associated with a custom input control by specifying a custom editor.  The name of the editor
 
265
    * type should correspond with the name of the property that will use the editor.  Example usage:
 
266
    * <pre><code>
 
267
var grid = new Ext.grid.PropertyGrid({
 
268
    ...
 
269
    customEditors: {
 
270
        'Start Time': new Ext.grid.GridEditor(new Ext.form.TimeField({selectOnFocus:true}))
 
271
    },
 
272
    source: {
 
273
        'Start Time': '10:00 AM'
 
274
    }
 
275
});
 
276
</code></pre>
 
277
    */
 
278
 
 
279
    // private config overrides
 
280
    enableColumnMove:false,
 
281
    stripeRows:false,
 
282
    trackMouseOver: false,
 
283
    clicksToEdit:1,
 
284
    enableHdMenu : false,
 
285
    viewConfig : {
 
286
        forceFit:true
 
287
    },
 
288
 
 
289
    // private
 
290
    initComponent : function(){
 
291
        this.customEditors = this.customEditors || {};
 
292
        this.lastEditRow = null;
 
293
        var store = new Ext.grid.PropertyStore(this);
 
294
        this.propStore = store;
 
295
        var cm = new Ext.grid.PropertyColumnModel(this, store);
 
296
        store.store.sort('name', 'ASC');
 
297
        this.addEvents(
 
298
            /**
 
299
             * @event beforepropertychange
 
300
             * Fires before a property value changes.  Handlers can return false to cancel the property change
 
301
             * (this will internally call {@link Ext.data.Record#reject} on the property's record).
 
302
             * @param {Object} source The source data object for the grid (corresponds to the same object passed in
 
303
             * as the {@link #source} config property).
 
304
             * @param {String} recordId The record's id in the data store
 
305
             * @param {Mixed} value The current edited property value
 
306
             * @param {Mixed} oldValue The original property value prior to editing
 
307
             */
 
308
            'beforepropertychange',
 
309
            /**
 
310
             * @event propertychange
 
311
             * Fires after a property value has changed.
 
312
             * @param {Object} source The source data object for the grid (corresponds to the same object passed in
 
313
             * as the {@link #source} config property).
 
314
             * @param {String} recordId The record's id in the data store
 
315
             * @param {Mixed} value The current edited property value
 
316
             * @param {Mixed} oldValue The original property value prior to editing
 
317
             */
 
318
            'propertychange'
 
319
        );
 
320
        this.cm = cm;
 
321
        this.ds = store.store;
 
322
        Ext.grid.PropertyGrid.superclass.initComponent.call(this);
 
323
 
 
324
                this.mon(this.selModel, 'beforecellselect', function(sm, rowIndex, colIndex){
 
325
            if(colIndex === 0){
 
326
                this.startEditing.defer(200, this, [rowIndex, 1]);
 
327
                return false;
 
328
            }
 
329
        }, this);
 
330
    },
 
331
 
 
332
    // private
 
333
    onRender : function(){
 
334
        Ext.grid.PropertyGrid.superclass.onRender.apply(this, arguments);
 
335
 
 
336
        this.getGridEl().addClass('x-props-grid');
 
337
    },
 
338
 
 
339
    // private
 
340
    afterRender: function(){
 
341
        Ext.grid.PropertyGrid.superclass.afterRender.apply(this, arguments);
 
342
        if(this.source){
 
343
            this.setSource(this.source);
 
344
        }
 
345
    },
 
346
 
 
347
    /**
 
348
     * Sets the source data object containing the property data.  The data object can contain one or more name/value
 
349
     * pairs representing all of the properties of an object to display in the grid, and this data will automatically
 
350
     * be loaded into the grid's {@link #store}.  The values should be supplied in the proper data type if needed,
 
351
     * otherwise string type will be assumed.  If the grid already contains data, this method will replace any
 
352
     * existing data.  See also the {@link #source} config value.  Example usage:
 
353
     * <pre><code>
 
354
grid.setSource({
 
355
    "(name)": "My Object",
 
356
    "Created": new Date(Date.parse('10/15/2006')),  // date type
 
357
    "Available": false,  // boolean type
 
358
    "Version": .01,      // decimal type
 
359
    "Description": "A test object"
 
360
});
 
361
</code></pre>
 
362
     * @param {Object} source The data object
 
363
     */
 
364
    setSource : function(source){
 
365
        this.propStore.setSource(source);
 
366
    },
 
367
 
 
368
    /**
 
369
     * Gets the source data object containing the property data.  See {@link #setSource} for details regarding the
 
370
     * format of the data object.
 
371
     * @return {Object} The data object
 
372
     */
 
373
    getSource : function(){
 
374
        return this.propStore.getSource();
 
375
    }
 
376
});
 
377
Ext.reg("propertygrid", Ext.grid.PropertyGrid);