source: trunk/web/addons/job_monarch/lib/extjs/source/widgets/grid/ColumnModel.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

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