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