Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 jpm 1
/*
2
 * Ext JS Library 2.0.2
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
4
 * licensing@extjs.com
5
 *
6
 * http://extjs.com/license
7
 */
8
 
9
/**
10
 * @class Ext.grid.ColumnModel
11
 * @extends Ext.util.Observable
12
 * This is the default implementation of a ColumnModel used by the Grid. This class is initialized
13
 * with an Array of column config objects.
14
 * <br><br>
15
 * An individual column's config object defines the header string, the {@link Ext.data.Record}
16
 * field the column draws its data from, an otional rendering function to provide customized
17
 * data formatting, and the ability to apply a CSS class to all cells in a column through its
18
 * {@link #id} config option.<br>
19
 * <br>Usage:<br>
20
 <pre><code>
21
 var colModel = new Ext.grid.ColumnModel([
22
	{header: "Ticker", width: 60, sortable: true},
23
	{header: "Company Name", width: 150, sortable: true},
24
	{header: "Market Cap.", width: 100, sortable: true},
25
	{header: "$ Sales", width: 100, sortable: true, renderer: money},
26
	{header: "Employees", width: 100, sortable: true, resizable: false}
27
 ]);
28
 </code></pre>
29
 * <p>
30
 * The config options listed for this class are options which may appear in each
31
 * individual column definition.
32
 * @constructor
33
 * @param {Object} config An Array of column config objects. See this class's
34
 * config objects for details.
35
*/
36
Ext.grid.ColumnModel = function(config){
37
	/**
38
     * The width of columns which have no width specified (defaults to 100)
39
     * @type Number
40
     */
41
    this.defaultWidth = 100;
42
 
43
    /**
44
     * Default sortable of columns which have no sortable specified (defaults to false)
45
     * @type Boolean
46
     */
47
    this.defaultSortable = false;
48
 
49
    /**
50
     * The config passed into the constructor
51
     * @property {Array} config
52
     */
53
    if(config.columns){
54
        Ext.apply(this, config);
55
        this.setConfig(config.columns, true);
56
    }else{
57
        this.setConfig(config, true);
58
    }
59
    this.addEvents(
60
        /**
61
	     * @event widthchange
62
	     * Fires when the width of a column changes.
63
	     * @param {ColumnModel} this
64
	     * @param {Number} columnIndex The column index
65
	     * @param {Number} newWidth The new width
66
	     */
67
	    "widthchange",
68
        /**
69
	     * @event headerchange
70
	     * Fires when the text of a header changes.
71
	     * @param {ColumnModel} this
72
	     * @param {Number} columnIndex The column index
73
	     * @param {String} newText The new header text
74
	     */
75
	    "headerchange",
76
        /**
77
	     * @event hiddenchange
78
	     * Fires when a column is hidden or "unhidden".
79
	     * @param {ColumnModel} this
80
	     * @param {Number} columnIndex The column index
81
	     * @param {Boolean} hidden true if hidden, false otherwise
82
	     */
83
	    "hiddenchange",
84
	    /**
85
         * @event columnmoved
86
         * Fires when a column is moved.
87
         * @param {ColumnModel} this
88
         * @param {Number} oldIndex
89
         * @param {Number} newIndex
90
         */
91
        "columnmoved",
92
        // deprecated - to be removed
93
        "columnlockchange",
94
        /**
95
         * @event configchanged
96
         * Fires when the configuration is changed
97
         * @param {ColumnModel} this
98
         */
99
        "configchange"
100
    );
101
    Ext.grid.ColumnModel.superclass.constructor.call(this);
102
};
103
Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, {
104
    /**
105
     * @cfg {String} id (optional) Defaults to the column's initial ordinal position.
106
     * A name which identifies this column. The id is used to create a CSS class name which
107
     * is applied to all table cells (including headers) in that column. The class name
108
     * takes the form of <pre>x-grid3-td-<b>id</b></pre>
109
     * <br><br>
110
     * Header cells will also recieve this class name, but will also have the class <pr>x-grid3-hd</pre>,
111
     * so to target header cells, use CSS selectors such as:<pre>.x-grid3-hd.x-grid3-td-<b>id</b></pre>
112
     * The {@link Ext.grid.Grid#autoExpandColumn} grid config option references the column
113
     * via this identifier.
114
     */
115
    /**
116
     * @cfg {String} header The header text to display in the Grid view.
117
     */
118
    /**
119
     * @cfg {String} dataIndex (optional) The name of the field in the grid's {@link Ext.data.Store}'s
120
     * {@link Ext.data.Record} definition from which to draw the column's value. If not
121
     * specified, the column's index is used as an index into the Record's data Array.
122
     */
123
    /**
124
     * @cfg {Number} width (optional) The initial width in pixels of the column. Using this
125
     * instead of {@link Ext.grid.Grid#autoSizeColumns} is more efficient.
126
     */
127
    /**
128
     * @cfg {Boolean} sortable (optional) True if sorting is to be allowed on this column.
129
     * Defaults to the value of the {@link #defaultSortable} property.
130
     * Whether local/remote sorting is used is specified in {@link Ext.data.Store#remoteSort}.
131
     */
132
    /**
133
     * @cfg {Boolean} fixed (optional) True if the column width cannot be changed.  Defaults to false.
134
     */
135
    /**
136
     * @cfg {Boolean} resizable (optional) False to disable column resizing. Defaults to true.
137
     */
138
    /**
139
     * @cfg {Boolean} menuDisabled (optional) True to disable the column menu. Defaults to false.
140
     */
141
    /**
142
     * @cfg {Boolean} hidden (optional) True to hide the column. Defaults to false.
143
     */
144
    /**
145
     * @cfg {String} tooltip (optional) A text string to use as the column header's tooltip.  If Quicktips are enabled, this
146
     * value will be used as the text of the quick tip, otherwise it will be set as the header's HTML title attribute.
147
     * Defaults to ''.
148
     */
149
    /**
150
     * @cfg {Function} renderer (optional) A function used to generate HTML markup for a cell
151
     * given the cell's data value. See {@link #setRenderer}. If not specified, the
152
     * default renderer uses the raw data value.
153
     */
154
    /**
155
     * @cfg {String} align (optional) Set the CSS text-align property of the column.  Defaults to undefined.
156
     */
157
    /**
158
     * @cfg {String} css (optional) Set custom CSS for all table cells in the column (excluding headers).  Defaults to undefined.
159
     */
160
    /**
161
     * @cfg {Boolean} hideable (optional) Specify as <tt>false</tt> to prevent the user from hiding this column.  Defaults to true.
162
     */
163
    /**
164
     * @cfg {Ext.form.Field} editor (optional) The {@link Ext.form.Field} to use when editing values in this column if
165
     * editing is supported by the grid.
166
     */
167
 
168
    /**
169
     * Returns the id of the column at the specified index.
170
     * @param {Number} index The column index
171
     * @return {String} the id
172
     */
173
    getColumnId : function(index){
174
        return this.config[index].id;
175
    },
176
 
177
    /**
178
     * Reconfigures this column model
179
     * @param {Array} config Array of Column configs
180
     */
181
    setConfig : function(config, initial){
182
        if(!initial){ // cleanup
183
            delete this.totalWidth;
184
            for(var i = 0, len = this.config.length; i < len; i++){
185
                var c = this.config[i];
186
                if(c.editor){
187
                    c.editor.destroy();
188
                }
189
            }
190
        }
191
        this.config = config;
192
        this.lookup = {};
193
        // if no id, create one
194
        for(var i = 0, len = config.length; i < len; i++){
195
            var c = config[i];
196
            if(typeof c.renderer == "string"){
197
                c.renderer = Ext.util.Format[c.renderer];
198
            }
199
            if(typeof c.id == "undefined"){
200
                c.id = i;
201
            }
202
            if(c.editor && c.editor.isFormField){
203
                c.editor = new Ext.grid.GridEditor(c.editor);
204
            }
205
            this.lookup[c.id] = c;
206
        }
207
        if(!initial){
208
            this.fireEvent('configchange', this);
209
        }
210
    },
211
 
212
    /**
213
     * Returns the column for a specified id.
214
     * @param {String} id The column id
215
     * @return {Object} the column
216
     */
217
    getColumnById : function(id){
218
        return this.lookup[id];
219
    },
220
 
221
    /**
222
     * Returns the index for a specified column id.
223
     * @param {String} id The column id
224
     * @return {Number} the index, or -1 if not found
225
     */
226
    getIndexById : function(id){
227
        for(var i = 0, len = this.config.length; i < len; i++){
228
            if(this.config[i].id == id){
229
                return i;
230
            }
231
        }
232
        return -1;
233
    },
234
 
235
    // private
236
    moveColumn : function(oldIndex, newIndex){
237
        var c = this.config[oldIndex];
238
        this.config.splice(oldIndex, 1);
239
        this.config.splice(newIndex, 0, c);
240
        this.dataMap = null;
241
        this.fireEvent("columnmoved", this, oldIndex, newIndex);
242
    },
243
 
244
    // deprecated - to be removed
245
    isLocked : function(colIndex){
246
        return this.config[colIndex].locked === true;
247
    },
248
 
249
    // deprecated - to be removed
250
    setLocked : function(colIndex, value, suppressEvent){
251
        if(this.isLocked(colIndex) == value){
252
            return;
253
        }
254
        this.config[colIndex].locked = value;
255
        if(!suppressEvent){
256
            this.fireEvent("columnlockchange", this, colIndex, value);
257
        }
258
    },
259
 
260
    // deprecated - to be removed
261
    getTotalLockedWidth : function(){
262
        var totalWidth = 0;
263
        for(var i = 0; i < this.config.length; i++){
264
            if(this.isLocked(i) && !this.isHidden(i)){
265
                this.totalWidth += this.getColumnWidth(i);
266
            }
267
        }
268
        return totalWidth;
269
    },
270
 
271
    // deprecated - to be removed
272
    getLockedCount : function(){
273
        for(var i = 0, len = this.config.length; i < len; i++){
274
            if(!this.isLocked(i)){
275
                return i;
276
            }
277
        }
278
    },
279
 
280
    /**
281
     * Returns the number of columns.
282
     * @return {Number}
283
     */
284
    getColumnCount : function(visibleOnly){
285
        if(visibleOnly === true){
286
            var c = 0;
287
            for(var i = 0, len = this.config.length; i < len; i++){
288
                if(!this.isHidden(i)){
289
                    c++;
290
                }
291
            }
292
            return c;
293
        }
294
        return this.config.length;
295
    },
296
 
297
    /**
298
     * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
299
     * @param {Function} fn
300
     * @param {Object} scope (optional)
301
     * @return {Array} result
302
     */
303
    getColumnsBy : function(fn, scope){
304
        var r = [];
305
        for(var i = 0, len = this.config.length; i < len; i++){
306
            var c = this.config[i];
307
            if(fn.call(scope||this, c, i) === true){
308
                r[r.length] = c;
309
            }
310
        }
311
        return r;
312
    },
313
 
314
    /**
315
     * Returns true if the specified column is sortable.
316
     * @param {Number} col The column index
317
     * @return {Boolean}
318
     */
319
    isSortable : function(col){
320
        if(typeof this.config[col].sortable == "undefined"){
321
            return this.defaultSortable;
322
        }
323
        return this.config[col].sortable;
324
    },
325
 
326
    /**
327
     * Returns true if the specified column menu is disabled.
328
     * @param {Number} col The column index
329
     * @return {Boolean}
330
     */
331
    isMenuDisabled : function(col){
332
        return !!this.config[col].menuDisabled;
333
    },
334
 
335
    /**
336
     * Returns the rendering (formatting) function defined for the column.
337
     * @param {Number} col The column index.
338
     * @return {Function} The function used to render the cell. See {@link #setRenderer}.
339
     */
340
    getRenderer : function(col){
341
        if(!this.config[col].renderer){
342
            return Ext.grid.ColumnModel.defaultRenderer;
343
        }
344
        return this.config[col].renderer;
345
    },
346
 
347
    /**
348
     * Sets the rendering (formatting) function for a column.  See {@link Ext.util.Format} for some
349
     * default formatting functions.
350
     * @param {Number} col The column index
351
     * @param {Function} fn The function to use to process the cell's raw data
352
     * to return HTML markup for the grid view. The render function is called with
353
     * the following parameters:<ul>
354
     * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
355
     * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
356
     * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
357
     * <li><b>attr</b> : String<p class="sub-desc">An HTML attribute definition string to apply to the data container element <i>within</i> the table cell
358
     * (e.g. 'style="color:red;"').</p></li></ul></p></li>
359
     * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
360
     * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
361
     * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
362
     * <li><b>store</b> : Ext.data.Store<p class="sub-desc">The {@link Ext.data.Store} object from which the Record was extracted.</p></li></ul>
363
     */
364
    setRenderer : function(col, fn){
365
        this.config[col].renderer = fn;
366
    },
367
 
368
    /**
369
     * Returns the width for the specified column.
370
     * @param {Number} col The column index
371
     * @return {Number}
372
     */
373
    getColumnWidth : function(col){
374
        return this.config[col].width || this.defaultWidth;
375
    },
376
 
377
    /**
378
     * Sets the width for a column.
379
     * @param {Number} col The column index
380
     * @param {Number} width The new width
381
     */
382
    setColumnWidth : function(col, width, suppressEvent){
383
        this.config[col].width = width;
384
        this.totalWidth = null;
385
        if(!suppressEvent){
386
             this.fireEvent("widthchange", this, col, width);
387
        }
388
    },
389
 
390
    /**
391
     * Returns the total width of all columns.
392
     * @param {Boolean} includeHidden True to include hidden column widths
393
     * @return {Number}
394
     */
395
    getTotalWidth : function(includeHidden){
396
        if(!this.totalWidth){
397
            this.totalWidth = 0;
398
            for(var i = 0, len = this.config.length; i < len; i++){
399
                if(includeHidden || !this.isHidden(i)){
400
                    this.totalWidth += this.getColumnWidth(i);
401
                }
402
            }
403
        }
404
        return this.totalWidth;
405
    },
406
 
407
    /**
408
     * Returns the header for the specified column.
409
     * @param {Number} col The column index
410
     * @return {String}
411
     */
412
    getColumnHeader : function(col){
413
        return this.config[col].header;
414
    },
415
 
416
    /**
417
     * Sets the header for a column.
418
     * @param {Number} col The column index
419
     * @param {String} header The new header
420
     */
421
    setColumnHeader : function(col, header){
422
        this.config[col].header = header;
423
        this.fireEvent("headerchange", this, col, header);
424
    },
425
 
426
    /**
427
     * Returns the tooltip for the specified column.
428
     * @param {Number} col The column index
429
     * @return {String}
430
     */
431
    getColumnTooltip : function(col){
432
            return this.config[col].tooltip;
433
    },
434
    /**
435
     * Sets the tooltip for a column.
436
     * @param {Number} col The column index
437
     * @param {String} tooltip The new tooltip
438
     */
439
    setColumnTooltip : function(col, tooltip){
440
            this.config[col].tooltip = tooltip;
441
    },
442
 
443
    /**
444
     * Returns the dataIndex for the specified column.
445
     * @param {Number} col The column index
446
     * @return {Number}
447
     */
448
    getDataIndex : function(col){
449
        return this.config[col].dataIndex;
450
    },
451
 
452
    /**
453
     * Sets the dataIndex for a column.
454
     * @param {Number} col The column index
455
     * @param {Number} dataIndex The new dataIndex
456
     */
457
    setDataIndex : function(col, dataIndex){
458
        this.config[col].dataIndex = dataIndex;
459
    },
460
 
461
    findColumnIndex : function(dataIndex){
462
        var c = this.config;
463
        for(var i = 0, len = c.length; i < len; i++){
464
            if(c[i].dataIndex == dataIndex){
465
                return i;
466
            }
467
        }
468
        return -1;
469
    },
470
 
471
    /**
472
     * Returns true if the cell is editable.
473
     * @param {Number} colIndex The column index
474
     * @param {Number} rowIndex The row index
475
     * @return {Boolean}
476
     */
477
    isCellEditable : function(colIndex, rowIndex){
478
        return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
479
    },
480
 
481
    /**
482
     * Returns the editor defined for the cell/column.
483
     * @param {Number} colIndex The column index
484
     * @param {Number} rowIndex The row index
485
     * @return {Object}
486
     */
487
    getCellEditor : function(colIndex, rowIndex){
488
        return this.config[colIndex].editor;
489
    },
490
 
491
    /**
492
     * Sets if a column is editable.
493
     * @param {Number} col The column index
494
     * @param {Boolean} editable True if the column is editable
495
     */
496
    setEditable : function(col, editable){
497
        this.config[col].editable = editable;
498
    },
499
 
500
 
501
    /**
502
     * Returns true if the column is hidden.
503
     * @param {Number} colIndex The column index
504
     * @return {Boolean}
505
     */
506
    isHidden : function(colIndex){
507
        return this.config[colIndex].hidden;
508
    },
509
 
510
 
511
    /**
512
     * Returns true if the column width cannot be changed
513
     */
514
    isFixed : function(colIndex){
515
        return this.config[colIndex].fixed;
516
    },
517
 
518
    /**
519
     * Returns true if the column can be resized
520
     * @return {Boolean}
521
     */
522
    isResizable : function(colIndex){
523
        return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
524
    },
525
    /**
526
     * Sets if a column is hidden.
527
     * @param {Number} colIndex The column index
528
     * @param {Boolean} hidden True if the column is hidden
529
     */
530
    setHidden : function(colIndex, hidden){
531
        var c = this.config[colIndex];
532
        if(c.hidden !== hidden){
533
            c.hidden = hidden;
534
            this.totalWidth = null;
535
            this.fireEvent("hiddenchange", this, colIndex, hidden);
536
        }
537
    },
538
 
539
    /**
540
     * Sets the editor for a column.
541
     * @param {Number} col The column index
542
     * @param {Object} editor The editor object
543
     */
544
    setEditor : function(col, editor){
545
        this.config[col].editor = editor;
546
    }
547
});
548
 
549
// private
550
Ext.grid.ColumnModel.defaultRenderer = function(value){
551
	if(typeof value == "string" && value.length < 1){
552
	    return "&#160;";
553
	}
554
	return value;
555
};
556
 
557
// Alias for backwards compatibility
558
Ext.grid.DefaultColumnModel = Ext.grid.ColumnModel;