Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / RowExpander.js
index 7d22ae3..8ffd904 100644 (file)
-/*!
- * Ext JS Library 3.1.0
- * Copyright(c) 2006-2009 Ext JS, LLC
- * licensing@extjs.com
- * http://www.extjs.com/license
+/*
+
+This file is part of Ext JS 4
+
+Copyright (c) 2011 Sencha Inc
+
+Contact:  http://www.sencha.com/contact
+
+GNU General Public License Usage
+This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+
+If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
+
+*/
+// feature idea to enable Ajax loading and then the content
+// cache would actually make sense. Should we dictate that they use
+// data or support raw html as well?
+
+/**
+ * @class Ext.ux.RowExpander
+ * @extends Ext.AbstractPlugin
+ * Plugin (ptype = 'rowexpander') that adds the ability to have a Column in a grid which enables
+ * a second row body which expands/contracts.  The expand/contract behavior is configurable to react
+ * on clicking of the column, double click of the row, and/or hitting enter while a row is selected.
+ *
+ * @ptype rowexpander
  */
-Ext.ns('Ext.ux.grid');\r
-\r
-/**\r
- * @class Ext.ux.grid.RowExpander\r
- * @extends Ext.util.Observable\r
- * Plugin (ptype = 'rowexpander') that adds the ability to have a Column in a grid which enables\r
- * a second row body which expands/contracts.  The expand/contract behavior is configurable to react\r
- * on clicking of the column, double click of the row, and/or hitting enter while a row is selected.\r
- *\r
- * @ptype rowexpander\r
- */\r
-Ext.ux.grid.RowExpander = Ext.extend(Ext.util.Observable, {\r
-    /**\r
-     * @cfg {Boolean} expandOnEnter\r
-     * <tt>true</tt> to toggle selected row(s) between expanded/collapsed when the enter\r
-     * key is pressed (defaults to <tt>true</tt>).\r
-     */\r
-    expandOnEnter : true,\r
-    /**\r
-     * @cfg {Boolean} expandOnDblClick\r
-     * <tt>true</tt> to toggle a row between expanded/collapsed when double clicked\r
-     * (defaults to <tt>true</tt>).\r
-     */\r
-    expandOnDblClick : true,\r
-\r
-    header : '',\r
-    width : 20,\r
-    sortable : false,\r
-    fixed : true,\r
-    menuDisabled : true,\r
-    dataIndex : '',\r
-    id : 'expander',\r
-    lazyRender : true,\r
-    enableCaching : true,\r
-\r
-    constructor: function(config){\r
-        Ext.apply(this, config);\r
-\r
-        this.addEvents({\r
-            /**\r
-             * @event beforeexpand\r
-             * Fires before the row expands. Have the listener return false to prevent the row from expanding.\r
-             * @param {Object} this RowExpander object.\r
-             * @param {Object} Ext.data.Record Record for the selected row.\r
-             * @param {Object} body body element for the secondary row.\r
-             * @param {Number} rowIndex The current row index.\r
-             */\r
-            beforeexpand: true,\r
-            /**\r
-             * @event expand\r
-             * Fires after the row expands.\r
-             * @param {Object} this RowExpander object.\r
-             * @param {Object} Ext.data.Record Record for the selected row.\r
-             * @param {Object} body body element for the secondary row.\r
-             * @param {Number} rowIndex The current row index.\r
-             */\r
-            expand: true,\r
-            /**\r
-             * @event beforecollapse\r
-             * Fires before the row collapses. Have the listener return false to prevent the row from collapsing.\r
-             * @param {Object} this RowExpander object.\r
-             * @param {Object} Ext.data.Record Record for the selected row.\r
-             * @param {Object} body body element for the secondary row.\r
-             * @param {Number} rowIndex The current row index.\r
-             */\r
-            beforecollapse: true,\r
-            /**\r
-             * @event collapse\r
-             * Fires after the row collapses.\r
-             * @param {Object} this RowExpander object.\r
-             * @param {Object} Ext.data.Record Record for the selected row.\r
-             * @param {Object} body body element for the secondary row.\r
-             * @param {Number} rowIndex The current row index.\r
-             */\r
-            collapse: true\r
-        });\r
-\r
-        Ext.ux.grid.RowExpander.superclass.constructor.call(this);\r
-\r
-        if(this.tpl){\r
-            if(typeof this.tpl == 'string'){\r
-                this.tpl = new Ext.Template(this.tpl);\r
-            }\r
-            this.tpl.compile();\r
-        }\r
-\r
-        this.state = {};\r
-        this.bodyContent = {};\r
-    },\r
-\r
-    getRowClass : function(record, rowIndex, p, ds){\r
-        p.cols = p.cols-1;\r
-        var content = this.bodyContent[record.id];\r
-        if(!content && !this.lazyRender){\r
-            content = this.getBodyContent(record, rowIndex);\r
-        }\r
-        if(content){\r
-            p.body = content;\r
-        }\r
-        return this.state[record.id] ? 'x-grid3-row-expanded' : 'x-grid3-row-collapsed';\r
-    },\r
-\r
-    init : function(grid){\r
-        this.grid = grid;\r
-\r
-        var view = grid.getView();\r
-        view.getRowClass = this.getRowClass.createDelegate(this);\r
-\r
-        view.enableRowBody = true;\r
-\r
-\r
-        grid.on('render', this.onRender, this);\r
-        grid.on('destroy', this.onDestroy, this);\r
-    },\r
-\r
-    // @private\r
-    onRender: function() {\r
-        var grid = this.grid;\r
-        var mainBody = grid.getView().mainBody;\r
-        mainBody.on('mousedown', this.onMouseDown, this, {delegate: '.x-grid3-row-expander'});\r
-        if (this.expandOnEnter) {\r
-            this.keyNav = new Ext.KeyNav(this.grid.getGridEl(), {\r
-                'enter' : this.onEnter,\r
-                scope: this\r
-            });\r
-        }\r
-        if (this.expandOnDblClick) {\r
-            grid.on('rowdblclick', this.onRowDblClick, this);\r
-        }\r
-    },\r
-    \r
-    // @private    \r
-    onDestroy: function() {\r
-        if(this.keyNav){\r
-            this.keyNav.disable();\r
-            delete this.keyNav;\r
-        }\r
-        /*\r
-         * A majority of the time, the plugin will be destroyed along with the grid,\r
-         * which means the mainBody won't be available. On the off chance that the plugin\r
-         * isn't destroyed with the grid, take care of removing the listener.\r
-         */\r
-        var mainBody = this.grid.getView().mainBody;\r
-        if(mainBody){\r
-            mainBody.un('mousedown', this.onMouseDown, this);\r
-        }\r
-    },\r
-    // @private\r
-    onRowDblClick: function(grid, rowIdx, e) {\r
-        this.toggleRow(rowIdx);\r
-    },\r
-\r
-    onEnter: function(e) {\r
-        var g = this.grid;\r
-        var sm = g.getSelectionModel();\r
-        var sels = sm.getSelections();\r
-        for (var i = 0, len = sels.length; i < len; i++) {\r
-            var rowIdx = g.getStore().indexOf(sels[i]);\r
-            this.toggleRow(rowIdx);\r
-        }\r
-    },\r
-\r
-    getBodyContent : function(record, index){\r
-        if(!this.enableCaching){\r
-            return this.tpl.apply(record.data);\r
-        }\r
-        var content = this.bodyContent[record.id];\r
-        if(!content){\r
-            content = this.tpl.apply(record.data);\r
-            this.bodyContent[record.id] = content;\r
-        }\r
-        return content;\r
-    },\r
-\r
-    onMouseDown : function(e, t){\r
-        e.stopEvent();\r
-        var row = e.getTarget('.x-grid3-row');\r
-        this.toggleRow(row);\r
-    },\r
-\r
-    renderer : function(v, p, record){\r
-        p.cellAttr = 'rowspan="2"';\r
-        return '<div class="x-grid3-row-expander">&#160;</div>';\r
-    },\r
-\r
-    beforeExpand : function(record, body, rowIndex){\r
-        if(this.fireEvent('beforeexpand', this, record, body, rowIndex) !== false){\r
-            if(this.tpl && this.lazyRender){\r
-                body.innerHTML = this.getBodyContent(record, rowIndex);\r
-            }\r
-            return true;\r
-        }else{\r
-            return false;\r
-        }\r
-    },\r
-\r
-    toggleRow : function(row){\r
-        if(typeof row == 'number'){\r
-            row = this.grid.view.getRow(row);\r
-        }\r
-        this[Ext.fly(row).hasClass('x-grid3-row-collapsed') ? 'expandRow' : 'collapseRow'](row);\r
-    },\r
-\r
-    expandRow : function(row){\r
-        if(typeof row == 'number'){\r
-            row = this.grid.view.getRow(row);\r
-        }\r
-        var record = this.grid.store.getAt(row.rowIndex);\r
-        var body = Ext.DomQuery.selectNode('tr:nth(2) div.x-grid3-row-body', row);\r
-        if(this.beforeExpand(record, body, row.rowIndex)){\r
-            this.state[record.id] = true;\r
-            Ext.fly(row).replaceClass('x-grid3-row-collapsed', 'x-grid3-row-expanded');\r
-            this.fireEvent('expand', this, record, body, row.rowIndex);\r
-        }\r
-    },\r
-\r
-    collapseRow : function(row){\r
-        if(typeof row == 'number'){\r
-            row = this.grid.view.getRow(row);\r
-        }\r
-        var record = this.grid.store.getAt(row.rowIndex);\r
-        var body = Ext.fly(row).child('tr:nth(1) div.x-grid3-row-body', true);\r
-        if(this.fireEvent('beforecollapse', this, record, body, row.rowIndex) !== false){\r
-            this.state[record.id] = false;\r
-            Ext.fly(row).replaceClass('x-grid3-row-expanded', 'x-grid3-row-collapsed');\r
-            this.fireEvent('collapse', this, record, body, row.rowIndex);\r
-        }\r
-    }\r
-});\r
-\r
-Ext.preg('rowexpander', Ext.ux.grid.RowExpander);\r
-\r
-//backwards compat\r
-Ext.grid.RowExpander = Ext.ux.grid.RowExpander;
\ No newline at end of file
+Ext.define('Ext.ux.RowExpander', {
+    extend: 'Ext.AbstractPlugin',
+
+    requires: [
+        'Ext.grid.feature.RowBody',
+        'Ext.grid.feature.RowWrap'
+    ],
+
+    alias: 'plugin.rowexpander',
+
+    rowBodyTpl: null,
+
+    /**
+     * @cfg {Boolean} expandOnEnter
+     * <tt>true</tt> to toggle selected row(s) between expanded/collapsed when the enter
+     * key is pressed (defaults to <tt>true</tt>).
+     */
+    expandOnEnter: true,
+
+    /**
+     * @cfg {Boolean} expandOnDblClick
+     * <tt>true</tt> to toggle a row between expanded/collapsed when double clicked
+     * (defaults to <tt>true</tt>).
+     */
+    expandOnDblClick: true,
+
+    /**
+     * @cfg {Boolean} selectRowOnExpand
+     * <tt>true</tt> to select a row when clicking on the expander icon
+     * (defaults to <tt>false</tt>).
+     */
+    selectRowOnExpand: false,
+
+    rowBodyTrSelector: '.x-grid-rowbody-tr',
+    rowBodyHiddenCls: 'x-grid-row-body-hidden',
+    rowCollapsedCls: 'x-grid-row-collapsed',
+
+
+
+    renderer: function(value, metadata, record, rowIdx, colIdx) {
+        if (colIdx === 0) {
+            metadata.tdCls = 'x-grid-td-expander';
+        }
+        return '<div class="x-grid-row-expander">&#160;</div>';
+    },
+
+    /**
+     * @event expandbody
+     * <b<Fired through the grid's View</b>
+     * @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
+     * @param {Ext.data.Model} record The record providing the data.
+     * @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
+     */
+    /**
+     * @event collapsebody
+     * <b<Fired through the grid's View.</b>
+     * @param {HtmlElement} rowNode The &lt;tr> element which owns the expanded row.
+     * @param {Ext.data.Model} record The record providing the data.
+     * @param {HtmlElement} expandRow The &lt;tr> element containing the expanded data.
+     */
+
+    constructor: function() {
+        this.callParent(arguments);
+        var grid = this.getCmp();
+        this.recordsExpanded = {};
+        // <debug>
+        if (!this.rowBodyTpl) {
+            Ext.Error.raise("The 'rowBodyTpl' config is required and is not defined.");
+        }
+        // </debug>
+        // TODO: if XTemplate/Template receives a template as an arg, should
+        // just return it back!
+        var rowBodyTpl = Ext.create('Ext.XTemplate', this.rowBodyTpl),
+            features = [{
+                ftype: 'rowbody',
+                columnId: this.getHeaderId(),
+                recordsExpanded: this.recordsExpanded,
+                rowBodyHiddenCls: this.rowBodyHiddenCls,
+                rowCollapsedCls: this.rowCollapsedCls,
+                getAdditionalData: this.getRowBodyFeatureData,
+                getRowBodyContents: function(data) {
+                    return rowBodyTpl.applyTemplate(data);
+                }
+            },{
+                ftype: 'rowwrap'
+            }];
+
+        if (grid.features) {
+            grid.features = features.concat(grid.features);
+        } else {
+            grid.features = features;
+        }
+
+        // NOTE: features have to be added before init (before Table.initComponent)
+    },
+
+    init: function(grid) {
+        this.callParent(arguments);
+
+        // Columns have to be added in init (after columns has been used to create the
+        // headerCt). Otherwise, shared column configs get corrupted, e.g., if put in the
+        // prototype.
+        grid.headerCt.insert(0, this.getHeaderConfig());
+        grid.on('render', this.bindView, this, {single: true});
+    },
+
+    getHeaderId: function() {
+        if (!this.headerId) {
+            this.headerId = Ext.id();
+        }
+        return this.headerId;
+    },
+
+    getRowBodyFeatureData: function(data, idx, record, orig) {
+        var o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments),
+            id = this.columnId;
+        o.rowBodyColspan = o.rowBodyColspan - 1;
+        o.rowBody = this.getRowBodyContents(data);
+        o.rowCls = this.recordsExpanded[record.internalId] ? '' : this.rowCollapsedCls;
+        o.rowBodyCls = this.recordsExpanded[record.internalId] ? '' : this.rowBodyHiddenCls;
+        o[id + '-tdAttr'] = ' valign="top" rowspan="2" ';
+        if (orig[id+'-tdAttr']) {
+            o[id+'-tdAttr'] += orig[id+'-tdAttr'];
+        }
+        return o;
+    },
+
+    bindView: function() {
+        var view = this.getCmp().getView(),
+            viewEl;
+
+        if (!view.rendered) {
+            view.on('render', this.bindView, this, {single: true});
+        } else {
+            viewEl = view.getEl();
+            if (this.expandOnEnter) {
+                this.keyNav = Ext.create('Ext.KeyNav', viewEl, {
+                    'enter' : this.onEnter,
+                    scope: this
+                });
+            }
+            if (this.expandOnDblClick) {
+                view.on('itemdblclick', this.onDblClick, this);
+            }
+            this.view = view;
+        }
+    },
+
+    onEnter: function(e) {
+        var view = this.view,
+            ds   = view.store,
+            sm   = view.getSelectionModel(),
+            sels = sm.getSelection(),
+            ln   = sels.length,
+            i = 0,
+            rowIdx;
+
+        for (; i < ln; i++) {
+            rowIdx = ds.indexOf(sels[i]);
+            this.toggleRow(rowIdx);
+        }
+    },
+
+    toggleRow: function(rowIdx) {
+        var rowNode = this.view.getNode(rowIdx),
+            row = Ext.get(rowNode),
+            nextBd = Ext.get(row).down(this.rowBodyTrSelector),
+            record = this.view.getRecord(rowNode),
+            grid = this.getCmp();
+
+        if (row.hasCls(this.rowCollapsedCls)) {
+            row.removeCls(this.rowCollapsedCls);
+            nextBd.removeCls(this.rowBodyHiddenCls);
+            this.recordsExpanded[record.internalId] = true;
+            this.view.fireEvent('expandbody', rowNode, record, nextBd.dom);
+        } else {
+            row.addCls(this.rowCollapsedCls);
+            nextBd.addCls(this.rowBodyHiddenCls);
+            this.recordsExpanded[record.internalId] = false;
+            this.view.fireEvent('collapsebody', rowNode, record, nextBd.dom);
+        }
+
+
+        // If Grid is auto-heighting itself, then perform a component layhout to accommodate the new height
+        if (!grid.isFixedHeight()) {
+            grid.doComponentLayout();
+        }
+        this.view.up('gridpanel').invalidateScroller();
+    },
+
+    onDblClick: function(view, cell, rowIdx, cellIndex, e) {
+
+        this.toggleRow(rowIdx);
+    },
+
+    getHeaderConfig: function() {
+        var me                = this,
+            toggleRow         = Ext.Function.bind(me.toggleRow, me),
+            selectRowOnExpand = me.selectRowOnExpand;
+
+        return {
+            id: this.getHeaderId(),
+            width: 24,
+            sortable: false,
+            resizable: false,
+            draggable: false,
+            hideable: false,
+            menuDisabled: true,
+            cls: Ext.baseCSSPrefix + 'grid-header-special',
+            renderer: function(value, metadata) {
+                metadata.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
+
+                return '<div class="' + Ext.baseCSSPrefix + 'grid-row-expander">&#160;</div>';
+            },
+            processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
+                if (type == "mousedown" && e.getTarget('.x-grid-row-expander')) {
+                    var row = e.getTarget('.x-grid-row');
+                    toggleRow(row);
+                    return selectRowOnExpand;
+                }
+            }
+        };
+    }
+});
+