X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/c930e9176a5a85509c5b0230e2bff5c22a591432..0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6:/src/widgets/grid/ColumnModel.js diff --git a/src/widgets/grid/ColumnModel.js b/src/widgets/grid/ColumnModel.js index 09126668..ca45507c 100644 --- a/src/widgets/grid/ColumnModel.js +++ b/src/widgets/grid/ColumnModel.js @@ -1,8 +1,8 @@ /*! - * Ext JS Library 3.0.0 - * Copyright(c) 2006-2009 Ext JS, LLC - * licensing@extjs.com - * http://www.extjs.com/license + * Ext JS Library 3.3.1 + * Copyright(c) 2006-2010 Sencha Inc. + * licensing@sencha.com + * http://www.sencha.com/license */ /** * @class Ext.grid.ColumnModel @@ -35,7 +35,7 @@ * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial * order of the column display. A Column's display may be initially hidden using the * {@link Ext.grid.Column#hidden hidden} config property (and then shown using the column - * header menu). Field's that are not included in the ColumnModel will not be displayable at all.

+ * header menu). Fields that are not included in the ColumnModel will not be displayable at all.

*

How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the * {@link Ext.data.Store Store} the column draws its data from is configured through the * {@link Ext.grid.Column#dataIndex dataIndex}. If the @@ -88,99 +88,109 @@ * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above). */ -Ext.grid.ColumnModel = function(config){ - /** - * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration - * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may - * be specified. - * @property config - * @type Array - */ - if(config.columns){ - Ext.apply(this, config); - this.setConfig(config.columns, true); - }else{ - this.setConfig(config, true); - } - this.addEvents( - /** - * @event widthchange - * Fires when the width of a column is programmaticially changed using - * {@link #setColumnWidth}. - * Note internal resizing suppresses the event from firing. See also - * {@link Ext.grid.GridPanel}.{@link #columnresize}. - * @param {ColumnModel} this - * @param {Number} columnIndex The column index - * @param {Number} newWidth The new width - */ - "widthchange", - /** - * @event headerchange - * Fires when the text of a header changes. - * @param {ColumnModel} this - * @param {Number} columnIndex The column index - * @param {String} newText The new header text - */ - "headerchange", - /** - * @event hiddenchange - * Fires when a column is hidden or "unhidden". - * @param {ColumnModel} this - * @param {Number} columnIndex The column index - * @param {Boolean} hidden true if hidden, false otherwise - */ - "hiddenchange", - /** - * @event columnmoved - * Fires when a column is moved. - * @param {ColumnModel} this - * @param {Number} oldIndex - * @param {Number} newIndex - */ - "columnmoved", - /** - * @event configchange - * Fires when the configuration is changed - * @param {ColumnModel} this - */ - "configchange" - ); - Ext.grid.ColumnModel.superclass.constructor.call(this); -}; -Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { +Ext.grid.ColumnModel = Ext.extend(Ext.util.Observable, { /** * @cfg {Number} defaultWidth (optional) The width of columns which have no {@link #width} * specified (defaults to 100). This property shall preferably be configured through the * {@link #defaults} config property. */ defaultWidth: 100, + /** * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no * sortable specified (defaults to false). This property shall preferably be configured * through the {@link #defaults} config property. */ defaultSortable: false, + /** * @cfg {Array} columns An Array of object literals. The config options defined by * {@link Ext.grid.Column} are the options which may appear in the object literal for each * individual column definition. */ + /** * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column} * configuration options to all {@link #columns}. Configuration options specified with * individual {@link Ext.grid.Column column} configs will supersede these {@link #defaults}. */ + constructor : function(config) { + /** + * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration + * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may + * be specified. + * @property config + * @type Array + */ + if (config.columns) { + Ext.apply(this, config); + this.setConfig(config.columns, true); + } else { + this.setConfig(config, true); + } + + this.addEvents( + /** + * @event widthchange + * Fires when the width of a column is programmaticially changed using + * {@link #setColumnWidth}. + * Note internal resizing suppresses the event from firing. See also + * {@link Ext.grid.GridPanel}.{@link #columnresize}. + * @param {ColumnModel} this + * @param {Number} columnIndex The column index + * @param {Number} newWidth The new width + */ + "widthchange", + + /** + * @event headerchange + * Fires when the text of a header changes. + * @param {ColumnModel} this + * @param {Number} columnIndex The column index + * @param {String} newText The new header text + */ + "headerchange", + + /** + * @event hiddenchange + * Fires when a column is hidden or "unhidden". + * @param {ColumnModel} this + * @param {Number} columnIndex The column index + * @param {Boolean} hidden true if hidden, false otherwise + */ + "hiddenchange", + + /** + * @event columnmoved + * Fires when a column is moved. + * @param {ColumnModel} this + * @param {Number} oldIndex + * @param {Number} newIndex + */ + "columnmoved", + + /** + * @event configchange + * Fires when the configuration is changed + * @param {ColumnModel} this + */ + "configchange" + ); + + Ext.grid.ColumnModel.superclass.constructor.call(this); + }, + /** * Returns the id of the column at the specified index. * @param {Number} index The column index * @return {String} the id */ - getColumnId : function(index){ + getColumnId : function(index) { return this.config[index].id; }, - getColumnAt : function(index){ + getColumnAt : function(index) { return this.config[index]; }, @@ -194,14 +204,18 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { * @param {Boolean} initial Specify true to bypass cleanup which deletes the totalWidth * and destroys existing editors. */ - setConfig : function(config, initial){ + setConfig : function(config, initial) { var i, c, len; - if(!initial){ // cleanup + + if (!initial) { // cleanup delete this.totalWidth; - for(i = 0, len = this.config.length; i < len; i++){ + + for (i = 0, len = this.config.length; i < len; i++) { c = this.config[i]; - if(c.editor){ - c.editor.destroy(); + + if (c.setEditor) { + //check here, in case we have a special column like a CheckboxSelectionModel + c.setEditor(null); } } } @@ -214,17 +228,25 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { this.config = config; this.lookup = {}; - // if no id, create one - for(i = 0, len = config.length; i < len; i++){ + + for (i = 0, len = config.length; i < len; i++) { c = Ext.applyIf(config[i], this.defaults); - if(!c.isColumn){ - var cls = Ext.grid.Column.types[c.xtype || 'gridcolumn']; - c = new cls(c); + + // if no id, create one using column's ordinal position + if (Ext.isEmpty(c.id)) { + c.id = i; + } + + if (!c.isColumn) { + var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn']; + c = new Cls(c); config[i] = c; } + this.lookup[c.id] = c; } - if(!initial){ + + if (!initial) { this.fireEvent('configchange', this); } }, @@ -234,7 +256,7 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { * @param {String} id The column id * @return {Object} the column */ - getColumnById : function(id){ + getColumnById : function(id) { return this.lookup[id]; }, @@ -243,9 +265,9 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { * @param {String} id The column id * @return {Number} the index, or -1 if not found */ - getIndexById : function(id){ - for(var i = 0, len = this.config.length; i < len; i++){ - if(this.config[i].id == id){ + getIndexById : function(id) { + for (var i = 0, len = this.config.length; i < len; i++) { + if (this.config[i].id == id) { return i; } } @@ -257,10 +279,12 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { * @param {Number} oldIndex The index of the column to move. * @param {Number} newIndex The position at which to reinsert the coolumn. */ - moveColumn : function(oldIndex, newIndex){ - var c = this.config[oldIndex]; - this.config.splice(oldIndex, 1); - this.config.splice(newIndex, 0, c); + moveColumn : function(oldIndex, newIndex) { + var config = this.config, + c = config[oldIndex]; + + config.splice(oldIndex, 1); + config.splice(newIndex, 0, c); this.dataMap = null; this.fireEvent("columnmoved", this, oldIndex, newIndex); }, @@ -270,17 +294,22 @@ Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, { * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns. * @return {Number} */ - getColumnCount : function(visibleOnly){ - if(visibleOnly === true){ - var c = 0; - for(var i = 0, len = this.config.length; i < len; i++){ - if(!this.isHidden(i)){ + getColumnCount : function(visibleOnly) { + var length = this.config.length, + c = 0, + i; + + if (visibleOnly === true) { + for (i = 0; i < length; i++) { + if (!this.isHidden(i)) { c++; } } + return c; } - return this.config.length; + + return length; }, /** @@ -292,19 +321,27 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ return c.hidden; }); - * @param {Function} fn - * @param {Object} scope (optional) + * @param {Function} fn A function which, when passed a {@link Ext.grid.Column Column} object, must + * return true if the column is to be included in the returned Array. + * @param {Object} scope (optional) The scope (this reference) in which the function + * is executed. Defaults to this ColumnModel. * @return {Array} result */ - getColumnsBy : function(fn, scope){ - var r = []; - for(var i = 0, len = this.config.length; i < len; i++){ - var c = this.config[i]; - if(fn.call(scope||this, c, i) === true){ - r[r.length] = c; + getColumnsBy : function(fn, scope) { + var config = this.config, + length = config.length, + result = [], + i, c; + + for (i = 0; i < length; i++){ + c = config[i]; + + if (fn.call(scope || this, c, i) === true) { + result[result.length] = c; } } - return r; + + return result; }, /** @@ -312,8 +349,8 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @return {Boolean} */ - isSortable : function(col){ - return this.config[col].sortable; + isSortable : function(col) { + return !!this.config[col].sortable; }, /** @@ -321,7 +358,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @return {Boolean} */ - isMenuDisabled : function(col){ + isMenuDisabled : function(col) { return !!this.config[col].menuDisabled; }, @@ -330,11 +367,12 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index. * @return {Function} The function used to render the cell. See {@link #setRenderer}. */ - getRenderer : function(col){ - if(!this.config[col].renderer){ - return Ext.grid.ColumnModel.defaultRenderer; - } - return this.config[col].renderer; + getRenderer : function(col) { + return this.config[col].renderer || Ext.grid.ColumnModel.defaultRenderer; + }, + + getRendererScope : function(col) { + return this.config[col].scope; }, /** @@ -354,7 +392,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ *

  • colIndex : Number

    Column index

  • *
  • store : Ext.data.Store

    The {@link Ext.data.Store} object from which the Record was extracted.

  • */ - setRenderer : function(col, fn){ + setRenderer : function(col, fn) { this.config[col].renderer = fn; }, @@ -363,8 +401,12 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @return {Number} */ - getColumnWidth : function(col){ - return this.config[col].width; + getColumnWidth : function(col) { + var width = this.config[col].width; + if(typeof width != 'number'){ + width = this.defaultWidth; + } + return width; }, /** @@ -374,10 +416,11 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Boolean} suppressEvent True to suppress firing the {@link #widthchange} * event. Defaults to false. */ - setColumnWidth : function(col, width, suppressEvent){ + setColumnWidth : function(col, width, suppressEvent) { this.config[col].width = width; this.totalWidth = null; - if(!suppressEvent){ + + if (!suppressEvent) { this.fireEvent("widthchange", this, col, width); } }, @@ -387,11 +430,11 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Boolean} includeHidden True to include hidden column widths * @return {Number} */ - getTotalWidth : function(includeHidden){ - if(!this.totalWidth){ + getTotalWidth : function(includeHidden) { + if (!this.totalWidth) { this.totalWidth = 0; - for(var i = 0, len = this.config.length; i < len; i++){ - if(includeHidden || !this.isHidden(i)){ + for (var i = 0, len = this.config.length; i < len; i++) { + if (includeHidden || !this.isHidden(i)) { this.totalWidth += this.getColumnWidth(i); } } @@ -404,7 +447,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @return {String} */ - getColumnHeader : function(col){ + getColumnHeader : function(col) { return this.config[col].header; }, @@ -413,7 +456,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @param {String} header The new header */ - setColumnHeader : function(col, header){ + setColumnHeader : function(col, header) { this.config[col].header = header; this.fireEvent("headerchange", this, col, header); }, @@ -423,7 +466,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @return {String} */ - getColumnTooltip : function(col){ + getColumnTooltip : function(col) { return this.config[col].tooltip; }, /** @@ -431,7 +474,7 @@ var columns = grid.getColumnModel().getColumnsBy(function(c){ * @param {Number} col The column index * @param {String} tooltip The new tooltip */ - setColumnTooltip : function(col, tooltip){ + setColumnTooltip : function(col, tooltip) { this.config[col].tooltip = tooltip; }, @@ -444,7 +487,7 @@ var fieldName = grid.getColumnModel().getDataIndex(columnIndex); * @param {Number} col The column index * @return {String} The column's dataIndex */ - getDataIndex : function(col){ + getDataIndex : function(col) { return this.config[col].dataIndex; }, @@ -453,7 +496,7 @@ var fieldName = grid.getColumnModel().getDataIndex(columnIndex); * @param {Number} col The column index * @param {String} dataIndex The new dataIndex */ - setDataIndex : function(col, dataIndex){ + setDataIndex : function(col, dataIndex) { this.config[col].dataIndex = dataIndex; }, @@ -462,7 +505,7 @@ var fieldName = grid.getColumnModel().getDataIndex(columnIndex); * @param {String} col The dataIndex to find * @return {Number} The column index, or -1 if no match was found */ - findColumnIndex : function(dataIndex){ + findColumnIndex : function(dataIndex) { var c = this.config; for(var i = 0, len = c.length; i < len; i++){ if(c[i].dataIndex == dataIndex){ @@ -496,8 +539,12 @@ var grid = new Ext.grid.GridPanel({ * @param {Number} rowIndex The row index * @return {Boolean} */ - isCellEditable : function(colIndex, rowIndex){ - return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false; + isCellEditable : function(colIndex, rowIndex) { + var c = this.config[colIndex], + ed = c.editable; + + //force boolean + return !!(ed || (!Ext.isDefined(ed) && c.editor)); }, /** @@ -507,7 +554,7 @@ var grid = new Ext.grid.GridPanel({ * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap * the {@link Ext.form.Field Field} used to edit the cell. */ - getCellEditor : function(colIndex, rowIndex){ + getCellEditor : function(colIndex, rowIndex) { return this.config[colIndex].getCellEditor(rowIndex); }, @@ -516,35 +563,38 @@ var grid = new Ext.grid.GridPanel({ * @param {Number} col The column index * @param {Boolean} editable True if the column is editable */ - setEditable : function(col, editable){ + setEditable : function(col, editable) { this.config[col].editable = editable; }, - /** - * Returns true if the column is hidden. + * Returns true if the column is {@link Ext.grid.Column#hidden hidden}, + * false otherwise. * @param {Number} colIndex The column index * @return {Boolean} */ - isHidden : function(colIndex){ - return this.config[colIndex].hidden; + isHidden : function(colIndex) { + return !!this.config[colIndex].hidden; // ensure returns boolean }, - /** - * Returns true if the column width cannot be changed + * Returns true if the column is {@link Ext.grid.Column#fixed fixed}, + * false otherwise. + * @param {Number} colIndex The column index + * @return {Boolean} */ - isFixed : function(colIndex){ - return this.config[colIndex].fixed; + isFixed : function(colIndex) { + return !!this.config[colIndex].fixed; }, /** * Returns true if the column can be resized * @return {Boolean} */ - isResizable : function(colIndex){ + isResizable : function(colIndex) { return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true; }, + /** * Sets if a column is hidden.
    
    @@ -553,7 +603,7 @@ myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first colu
          * @param {Number} colIndex The column index
          * @param {Boolean} hidden True if the column is hidden
          */
    -    setHidden : function(colIndex, hidden){
    +    setHidden : function(colIndex, hidden) {
             var c = this.config[colIndex];
             if(c.hidden !== hidden){
                 c.hidden = hidden;
    @@ -567,25 +617,38 @@ myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first colu
          * @param {Number} col The column index
          * @param {Object} editor The editor object
          */
    -    setEditor : function(col, editor){
    -        Ext.destroy(this.config[col].editor);
    -        this.config[col].editor = editor;
    +    setEditor : function(col, editor) {
    +        this.config[col].setEditor(editor);
         },
     
         /**
    -     * Destroys this column model by purging any event listeners, and removing any editors.
    +     * Destroys this column model by purging any event listeners. Destroys and dereferences all Columns.
          */
    -    destroy : function(){
    -        for(var i = 0, c = this.config, len = c.length; i < len; i++){
    -            Ext.destroy(c[i].editor);
    +    destroy : function() {
    +        var length = this.config.length,
    +            i = 0;
    +
    +        for (; i < length; i++){
    +            this.config[i].destroy(); // Column's destroy encapsulates all cleanup.
             }
    +        delete this.config;
    +        delete this.lookup;
             this.purgeListeners();
    +    },
    +
    +    /**
    +     * @private
    +     * Setup any saved state for the column, ensures that defaults are applied.
    +     */
    +    setState : function(col, state) {
    +        state = Ext.applyIf(state, this.defaults);
    +        Ext.apply(this.config[col], state);
         }
     });
     
     // private
    -Ext.grid.ColumnModel.defaultRenderer = function(value){
    -    if(typeof value == "string" && value.length < 1){
    +Ext.grid.ColumnModel.defaultRenderer = function(value) {
    +    if (typeof value == "string" && value.length < 1) {
             return " ";
         }
         return value;