13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-grid-property-Grid'>/**
</span> * A specialized grid implementation intended to mimic the traditional property grid as typically seen in
 * development IDEs.  Each row in the grid represents a property of some object, and the data is stored
 * as a set of name/value pairs in {@link Ext.grid.property.Property Properties}. By default, the editors
 * shown are inferred from the data in the cell. More control over this can be specified by using the 
 * {@link #sourceConfig} option. Example usage:
 *
 *     @example
 *     Ext.create('Ext.grid.property.Grid', {
 *         title: 'Properties Grid',
 *         width: 300,
 *         renderTo: Ext.getBody(),
 *         source: {
 *             &quot;(name)&quot;: &quot;My Object&quot;,
 *             &quot;Created&quot;: Ext.Date.parse('10/15/2006', 'm/d/Y'),
 *             &quot;Available&quot;: false,
 *             &quot;Version&quot;: 0.01,
 *             &quot;Description&quot;: &quot;A test object&quot;
 *         }
 *     });
 */
Ext.define('Ext.grid.property.Grid', {
 
    extend: 'Ext.grid.Panel',
 
    alias: 'widget.propertygrid',
 
    alternateClassName: 'Ext.grid.PropertyGrid',
 
    uses: [
       'Ext.grid.plugin.CellEditing',
       'Ext.grid.property.Store',
       'Ext.grid.property.HeaderContainer',
       'Ext.XTemplate',
       'Ext.grid.CellEditor',
       'Ext.form.field.Date',
       'Ext.form.field.Text',
       'Ext.form.field.Number',
       'Ext.form.field.ComboBox'
    ],
    
<span id='Ext-grid-property-Grid-cfg-sourceConfig'>    /**
</span>     * @cfg {Object} sourceConfig
     * This option allows various configurations to be set for each field in the property grid.
     * None of these configurations are required
     * 
     * ####displayName
     * A custom name to appear as label for this field. If specified, the display name will be shown 
     * in the name column instead of the property name. Example usage:
     * 
     *     new Ext.grid.property.Grid({
     *         source: {
     *             clientIsAvailable: true
     *         },
     *         sourceConfig: {
     *             clientIsAvailable: {
     *                 // Custom name different to the field
     *                 displayName: 'Available'
     *             }
     *         }
     *     });
     * 
     * ####renderer
     * A function used to transform the underlying value before it is displayed in the grid.
     * By default, the grid supports strongly-typed rendering of strings, dates, numbers and booleans using built-in form editors, 
     * but any custom type can be supported and associated with the type of the value. Example usage:
     * 
     *     new Ext.grid.property.Grid({
     *         source: {
     *             clientIsAvailable: true
     *         },
     *         sourceConfig: {
     *             clientIsAvailable: {
     *                 // Custom renderer to change the color based on the value
     *                 renderer: function(v){
     *                     var color = v ? 'green' : 'red';
     *                     return '&lt;span style=&quot;color: ' + color + ';&quot;&gt;' + v + '&lt;/span&gt;';
     *                 }
     *             }
     *         }
     *     });
     * 
     * ####type
     * Used to explicitly specify the editor type for a particular value. By default, the type is 
     * automatically inferred from the value. See {@link #inferTypes}. Accepted values are:
     * 
     * - 'date'
     * - 'boolean'
     * - 'number'
     * - 'string'
     * 
     * For more advanced control an editor configuration can be passed (see the next section).
     * Example usage:
     * 
     *     new Ext.grid.property.Grid({
     *         source: {
     *             attending: null
     *         },
     *         sourceConfig: {
     *             attending: {
     *                 // Force the type to be a numberfield, a null value would otherwise default to a textfield
     *                 type: 'number'
     *             }
     *         }
     *     });
     * 
     * ####editor
     * Allows the grid to support additional types of editable fields.  By default, the grid supports strongly-typed editing
     * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
     * associated with a custom input control by specifying a custom editor. Example usage
     * 
     *     new Ext.grid.property.Grid({
     *         // Data object containing properties to edit
     *         source: {
     *             evtStart: '10:00 AM'
     *         },
     * 
     *         sourceConfig: {
     *             evtStart: {
     *                 editor: Ext.create('Ext.form.field.Time', {selectOnFocus: true}),
     *                 displayName: 'Start Time'
     *             }
     *         }
     *     });
     */
 
<span id='Ext-grid-property-Grid-cfg-propertyNames'>   /**
</span>    * @cfg {Object} propertyNames
    * An object containing custom property name/display name pairs.
    * If specified, the display name will be shown in the name column instead of the property name.
    * @deprecated See {@link #sourceConfig} displayName
    */
 
<span id='Ext-grid-property-Grid-cfg-source'>    /**
</span>     * @cfg {Object} source
     * A data object to use as the data source of the grid (see {@link #setSource} for details).
     */
 
<span id='Ext-grid-property-Grid-cfg-customEditors'>    /**
</span>     * @cfg {Object} customEditors
     * An object containing name/value pairs of custom editor type definitions that allow
     * the grid to support additional types of editable fields.  By default, the grid supports strongly-typed editing
     * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
     * associated with a custom input control by specifying a custom editor.  The name of the editor
     * type should correspond with the name of the property that will use the editor.  Example usage:
     *
     *     var grid = new Ext.grid.property.Grid({
     *
     *         // Custom editors for certain property names
     *         customEditors: {
     *             evtStart: Ext.create('Ext.form.TimeField', {selectOnFocus: true})
     *         },
     *
     *         // Displayed name for property names in the source
     *         propertyNames: {
     *             evtStart: 'Start Time'
     *         },
     *
     *         // Data object containing properties to edit
     *         source: {
     *             evtStart: '10:00 AM'
     *         }
     *     });
     * @deprecated See {@link #sourceConfig} editor
     */
 
<span id='Ext-grid-property-Grid-cfg-customRenderers'>    /**
</span>     * @cfg {Object} customRenderers
     * An object containing name/value pairs of custom renderer type definitions that allow
     * the grid to support custom rendering of fields.  By default, the grid supports strongly-typed rendering
     * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
     * associated with the type of the value.  The name of the renderer type should correspond with the name of the property
     * that it will render.  Example usage:
     *
     *     var grid = Ext.create('Ext.grid.property.Grid', {
     *         customRenderers: {
     *             Available: function(v){
     *                 if (v) {
     *                     return '&lt;span style=&quot;color: green;&quot;&gt;Yes&lt;/span&gt;';
     *                 } else {
     *                     return '&lt;span style=&quot;color: red;&quot;&gt;No&lt;/span&gt;';
     *                 }
     *             }
     *         },
     *         source: {
     *             Available: true
     *         }
     *     });
     * @deprecated See {@link #sourceConfig} renderer
     */
 
<span id='Ext-grid-property-Grid-cfg-valueField'>    /**
</span>     * @cfg {String} valueField
     * The name of the field from the property store to use as the value field name.
     * This may be useful if you do not configure the property Grid from an object, but use your own store configuration.
     */
    valueField: 'value',
 
<span id='Ext-grid-property-Grid-cfg-nameField'>    /**
</span>     * @cfg {String} nameField
     * The name of the field from the property store to use as the property field name.
     * This may be useful if you do not configure the property Grid from an object, but use your own store configuration.
     */
    nameField: 'name',
    
<span id='Ext-grid-property-Grid-cfg-inferTypes'>    /**
</span>     * @cfg {Boolean} inferTypes
     * True to automatically infer the {@link #sourceConfig type} based on the initial value passed
     * for each field. This ensures the editor remains the correct type even if the value is blanked
     * and becomes empty.
     */
    inferTypes: true,
 
<span id='Ext-grid-property-Grid-cfg-nameColumnWidth'>    /**
</span>     * @cfg {Number/String} [nameColumnWidth=115]
     * Specify the width for the name column. The value column will take any remaining space.
     */
 
<span id='Ext-grid-property-Grid-cfg-enableColumnMove'>    // private config overrides
</span>    enableColumnMove: false,
<span id='Ext-grid-property-Grid-cfg-columnLines'>    columnLines: true,
</span><span id='Ext-grid-property-Grid-property-stripeRows'>    stripeRows: false,
</span><span id='Ext-grid-property-Grid-property-trackMouseOver'>    trackMouseOver: false,
</span><span id='Ext-grid-property-Grid-property-clicksToEdit'>    clicksToEdit: 1,
</span><span id='Ext-grid-property-Grid-property-enableHdMenu'>    enableHdMenu: false,
</span>    
<span id='Ext-grid-property-Grid-property-gridCls'>    gridCls: Ext.baseCSSPrefix + 'property-grid',
</span>
<span id='Ext-grid-property-Grid-method-initComponent'>    // private
</span>    initComponent : function(){
        var me = this;
 
        me.source = me.source || {};
        me.addCls(me.gridCls);
        me.plugins = me.plugins || [];
 
        // Enable cell editing. Inject a custom startEdit which always edits column 1 regardless of which column was clicked.
        me.plugins.push(new Ext.grid.plugin.CellEditing({
            clicksToEdit: me.clicksToEdit,
 
            // Inject a startEdit which always edits the value column
            startEdit: function(record, column) {
                // Maintainer: Do not change this 'this' to 'me'! It is the CellEditing object's own scope.
                return this.self.prototype.startEdit.call(this, record, me.headerCt.child('#' + me.valueField));
            }
        }));
 
        me.selModel = {
            selType: 'cellmodel',
            onCellSelect: function(position) {
                if (position.column != 1) {
                    position.column = 1;
                }
                return this.self.prototype.onCellSelect.call(this, position);
            }
        };
        
        me.sourceConfig = Ext.apply({}, me.sourceConfig);  
 
        // Create a property.Store from the source object unless configured with a store
        if (!me.store) {
            me.propStore = me.store = new Ext.grid.property.Store(me, me.source);
        }
        me.configure(me.sourceConfig);
 
        if (me.sortableColumns) {
            me.store.sort('name', 'ASC');
        }
        me.columns = new Ext.grid.property.HeaderContainer(me, me.store);
 
        me.addEvents(
<span id='Ext-grid-property-Grid-event-beforepropertychange'>            /**
</span>             * @event beforepropertychange
             * Fires before a property value changes.  Handlers can return false to cancel the property change
             * (this will internally call {@link Ext.data.Model#reject} on the property's record).
             * @param {Object} source The source data object for the grid (corresponds to the same object passed in
             * as the {@link #source} config property).
             * @param {String} recordId The record's id in the data store
             * @param {Object} value The current edited property value
             * @param {Object} oldValue The original property value prior to editing
             */
            'beforepropertychange',
<span id='Ext-grid-property-Grid-event-propertychange'>            /**
</span>             * @event propertychange
             * Fires after a property value has changed.
             * @param {Object} source The source data object for the grid (corresponds to the same object passed in
             * as the {@link #source} config property).
             * @param {String} recordId The record's id in the data store
             * @param {Object} value The current edited property value
             * @param {Object} oldValue The original property value prior to editing
             */
            'propertychange'
        );
        me.callParent();
 
        // Inject a custom implementation of walkCells which only goes up or down
        me.getView().walkCells = this.walkCells;
 
        // Set up our default editor set for the 4 atomic data types
        me.editors = {
            'date'    : new Ext.grid.CellEditor({ field: new Ext.form.field.Date({selectOnFocus: true})}),
            'string'  : new Ext.grid.CellEditor({ field: new Ext.form.field.Text({selectOnFocus: true})}),
            'number'  : new Ext.grid.CellEditor({ field: new Ext.form.field.Number({selectOnFocus: true})}),
            'boolean' : new Ext.grid.CellEditor({ field: new Ext.form.field.ComboBox({
                editable: false,
                store: [[ true, me.headerCt.trueText ], [false, me.headerCt.falseText ]]
            })})
        };
 
        // Track changes to the data so we can fire our events.
        me.store.on('update', me.onUpdate, me);
    },
    
<span id='Ext-grid-property-Grid-method-configure'>    configure: function(config){
</span>        var me = this,
            store = me.store,
            i = 0,
            len = me.store.getCount(),
            nameField = me.nameField,
            valueField = me.valueField,
            name, value, rec, type;
        
        me.configureLegacy(config);  
        
        if (me.inferTypes) {
            for (; i &lt; len; ++i) {
                rec = store.getAt(i);
                name = rec.get(nameField);
                if (!me.getConfig(name, 'type')) {
                    value = rec.get(valueField);
                    if (Ext.isDate(value)) {
                        type = 'date';
                    } else if (Ext.isNumber(value)) {
                        type = 'number';
                    } else if (Ext.isBoolean(value)) {
                        type = 'boolean';
                    } else {
                        type = 'string';
                    }
                    me.setConfig(name, 'type', type);
                }
            }
        }
    },
    
<span id='Ext-grid-property-Grid-method-getConfig'>    getConfig: function(fieldName, key, defaultValue) {
</span>        var config = this.sourceConfig[fieldName],
            out;
            
        if (config) {
            out = config[key];
        }    
        return out || defaultValue;
    },
    
<span id='Ext-grid-property-Grid-method-setConfig'>    setConfig: function(fieldName, key, value) {
</span>        var sourceCfg = this.sourceConfig,
            o = sourceCfg[fieldName];
            
        if (!o) {
            o = sourceCfg[fieldName] = {
                __copied: true
            };
        } else if (!o.__copied) {
            o = Ext.apply({
                __copied: true
            }, o);
            sourceCfg[fieldName] = o;
        }
        o[key] = value;
        return value;
    },
    
<span id='Ext-grid-property-Grid-method-configureLegacy'>    // to be deprecated in 4.2
</span>    configureLegacy: function(config){
        var me = this,
            o, key, value;
            
        me.copyLegacyObject(config, me.customRenderers, 'renderer');
        me.copyLegacyObject(config, me.customEditors, 'editor');
        me.copyLegacyObject(config, me.propertyNames, 'displayName');
        
        //&lt;debug&gt;
        // exclude types since it's new
        if (me.customRenderers || me.customEditors || me.propertyNames) {
            if (Ext.global.console &amp;&amp; Ext.global.console.warn) {
                Ext.global.console.warn(this.$className, 'customRenderers, customEditors &amp; propertyNames have been consolidated into a new config, see &quot;sourceConfig&quot;. These configurations will be deprecated.');
            }
        }
        //&lt;/debug&gt;
    },
    
<span id='Ext-grid-property-Grid-method-copyLegacyObject'>    copyLegacyObject: function(config, o, keyName){
</span>        var key, value;
        
        for (key in o) {
            if (o.hasOwnProperty(key)) {
                if (!config[key]) {
                    config[key] = {};
                }
                config[key][keyName] = o[key];
            }    
        }
    },
 
<span id='Ext-grid-property-Grid-method-onUpdate'>    // @private
</span>    onUpdate : function(store, record, operation) {
        var me = this,
            v, oldValue;
 
        if (me.rendered &amp;&amp; operation == Ext.data.Model.EDIT) {
            v = record.get(me.valueField);
            oldValue = record.modified.value;
            if (me.fireEvent('beforepropertychange', me.source, record.getId(), v, oldValue) !== false) {
                if (me.source) {
                    me.source[record.getId()] = v;
                }
                record.commit();
                me.fireEvent('propertychange', me.source, record.getId(), v, oldValue);
            } else {
                record.reject();
            }
        }
    },
 
<span id='Ext-grid-property-Grid-method-walkCells'>    // Custom implementation of walkCells which only goes up and down.
</span>    walkCells: function(pos, direction, e, preventWrap, verifierFn, scope) {
        if (direction == 'left') {
            direction = 'up';
        } else if (direction == 'right') {
            direction = 'down';
        }
        pos = Ext.view.Table.prototype.walkCells.call(this, pos, direction, e, preventWrap, verifierFn, scope);
        if (pos &amp;&amp; !pos.column) {
            pos.column = 1;
        }
        return pos;
    },
 
<span id='Ext-grid-property-Grid-method-getCellEditor'>    // @private
</span>    // Returns the correct editor type for the property type, or a custom one keyed by the property name
    getCellEditor : function(record, column) {
        var me = this,
            propName = record.get(me.nameField),
            val = record.get(me.valueField),
            editor = me.getConfig(propName, 'editor'),
            type = me.getConfig(propName, 'type'),
            editors = me.editors;
 
        // A custom editor was found. If not already wrapped with a CellEditor, wrap it, and stash it back
        // If it's not even a Field, just a config object, instantiate it before wrapping it.
        if (editor) {
            if (!(editor instanceof Ext.grid.CellEditor)) {
                if (!(editor instanceof Ext.form.field.Base)) {
                    editor = Ext.ComponentManager.create(editor, 'textfield');
                }
                editor = me.setConfig(propName, 'editor', new Ext.grid.CellEditor({ field: editor }));
            }
        } else if (type) {
            switch (type) {
                case 'date':
                    editor = editors.date;
                    break;
                case 'number':
                    editor = editors.number;
                    break;
                case 'boolean':
                    editor = me.editors['boolean'];
                    break;
                default:
                    editor = editors.string;
            }
        } else if (Ext.isDate(val)) {
            editor = editors.date;
        } else if (Ext.isNumber(val)) {
            editor = editors.number;
        } else if (Ext.isBoolean(val)) {
            editor = editors['boolean'];
        } else {
            editor = editors.string;
        }
 
        // Give the editor a unique ID because the CellEditing plugin caches them
        editor.editorId = propName;
        return editor;
    },
 
<span id='Ext-grid-property-Grid-method-beforeDestroy'>    beforeDestroy: function() {
</span>        var me = this;
        me.callParent();
        me.destroyEditors(me.editors);
        me.destroyEditors(me.customEditors);
        delete me.source;
    },
 
<span id='Ext-grid-property-Grid-method-destroyEditors'>    destroyEditors: function (editors) {
</span>        for (var ed in editors) {
            if (editors.hasOwnProperty(ed)) {
                Ext.destroy(editors[ed]);
            }
        }
    },
 
<span id='Ext-grid-property-Grid-method-setSource'>    /**
</span>     * Sets the source data object containing the property data.  The data object can contain one or more name/value
     * pairs representing all of the properties of an object to display in the grid, and this data will automatically
     * be loaded into the grid's {@link #store}.  The values should be supplied in the proper data type if needed,
     * otherwise string type will be assumed.  If the grid already contains data, this method will replace any
     * existing data.  See also the {@link #source} config value.  Example usage:
     *
     *     grid.setSource({
     *         &quot;(name)&quot;: &quot;My Object&quot;,
     *         &quot;Created&quot;: Ext.Date.parse('10/15/2006', 'm/d/Y'),  // date type
     *         &quot;Available&quot;: false,  // boolean type
     *         &quot;Version&quot;: .01,      // decimal type
     *         &quot;Description&quot;: &quot;A test object&quot;
     *     });
     *
     * @param {Object} source The data object.
     * @param {Object} [sourceConfig] A new {@link #sourceConfig object}. If this argument is not passed
     * the current configuration will be re-used. To reset the config, pass `null` or an empty object literal.
     */
    setSource: function(source, sourceConfig) {
        var me = this;
        
        me.source = source;
        if (sourceConfig !== undefined) {
            me.sourceConfig = Ext.apply({}, sourceConfig);  
            me.configure(me.sourceConfig);
        }
        me.propStore.setSource(source);
    },
 
<span id='Ext-grid-property-Grid-method-getSource'>    /**
</span>     * Gets the source data object containing the property data.  See {@link #setSource} for details regarding the
     * format of the data object.
     * @return {Object} The data object.
     */
    getSource: function() {
        return this.propStore.getSource();
    },
 
<span id='Ext-grid-property-Grid-method-setProperty'>    /**
</span>     * Sets the value of a property.
     * @param {String} prop The name of the property to set.
     * @param {Object} value The value to test.
     * @param {Boolean} [create=false] `true` to create the property if it doesn't already exist.
     */
    setProperty: function(prop, value, create) {
        this.propStore.setValue(prop, value, create);
    },
 
<span id='Ext-grid-property-Grid-method-removeProperty'>    /**
</span>     * Removes a property from the grid.
     * @param {String} prop The name of the property to remove.
     */
    removeProperty: function(prop) {
        this.propStore.remove(prop);
    }
 
<span id='Ext-grid-property-Grid-cfg-store'>    /**
</span>     * @cfg {Object} store
     * @private
     */
<span id='Ext-grid-property-Grid-cfg-columns'>    /**
</span>     * @cfg {Object} columns
     * @private
     */
});
</pre>
</body>
</html>