/** * @class Ext.AbstractContainer * @extends Ext.Component *

Base class for any {@link Ext.BoxComponent} that may contain other Components. Containers handle the * basic behavior of containing items, namely adding, inserting and removing items.

* *

The most commonly used Container classes are {@link Ext.Panel}, {@link Ext.Window} and {@link Ext.TabPanel}. * If you do not need the capabilities offered by the aforementioned classes you can create a lightweight * Container to be encapsulated by an HTML element to your specifications by using the * {@link Ext.Component#autoEl autoEl} config option. This is a useful technique when creating * embedded {@link Ext.layout.ColumnLayout column} layouts inside {@link Ext.form.FormPanel FormPanels} * for example.

* *

The code below illustrates both how to explicitly create a Container, and how to implicitly * create one using the 'container' xtype:


// explicitly create a Container
var embeddedColumns = new Ext.Container({
    autoEl: 'div',  // This is the default
    layout: 'column',
    defaults: {
        // implicitly create Container by specifying xtype
        xtype: 'container',
        autoEl: 'div', // This is the default.
        layout: 'form',
        columnWidth: 0.5,
        style: {
            padding: '10px'
        }
    },
//  The two items below will be Ext.Containers, each encapsulated by a <DIV> element.
    items: [{
        items: {
            xtype: 'datefield',
            name: 'startDate',
            fieldLabel: 'Start date'
        }
    }, {
        items: {
            xtype: 'datefield',
            name: 'endDate',
            fieldLabel: 'End date'
        }
    }]
});

* *

Layout

*

Container classes delegate the rendering of child Components to a layout * manager class which must be configured into the Container using the * {@link #layout} configuration property.

*

When either specifying child {@link #items} of a Container, * or dynamically {@link #add adding} Components to a Container, remember to * consider how you wish the Container to arrange those child elements, and * whether those child elements need to be sized using one of Ext's built-in * {@link #layout} schemes. By default, Containers use the * {@link Ext.layout.ContainerLayout ContainerLayout} scheme which only * renders child components, appending them one after the other inside the * Container, and does not apply any sizing at all.

*

A common mistake is when a developer neglects to specify a * {@link #layout} (e.g. widgets like GridPanels or * TreePanels are added to Containers for which no {@link #layout} * has been specified). If a Container is left to use the default * {@link Ext.layout.ContainerLayout ContainerLayout} scheme, none of its * child components will be resized, or changed in any way when the Container * is resized.

*

Certain layout managers allow dynamic addition of child components. * Those that do include {@link Ext.layout.CardLayout}, * {@link Ext.layout.AnchorLayout}, {@link Ext.layout.FormLayout}, and * {@link Ext.layout.TableLayout}. For example:


//  Create the GridPanel.
var myNewGrid = new Ext.grid.GridPanel({
    store: myStore,
    columns: myColumnModel,
    title: 'Results', // the title becomes the title of the tab
});

myTabPanel.add(myNewGrid); // {@link Ext.TabPanel} implicitly uses {@link Ext.layout.CardLayout CardLayout}
myTabPanel.{@link Ext.TabPanel#setActiveTab setActiveTab}(myNewGrid);
 * 

*

The example above adds a newly created GridPanel to a TabPanel. Note that * a TabPanel uses {@link Ext.layout.CardLayout} as its layout manager which * means all its child items are sized to {@link Ext.layout.FitLayout fit} * exactly into its client area. *

Overnesting is a common problem. * An example of overnesting occurs when a GridPanel is added to a TabPanel * by wrapping the GridPanel inside a wrapping Panel (that has no * {@link #layout} specified) and then add that wrapping Panel * to the TabPanel. The point to realize is that a GridPanel is a * Component which can be added directly to a Container. If the wrapping Panel * has no {@link #layout} configuration, then the overnested * GridPanel will not be sized as expected.

* *

Adding via remote configuration

* *

A server side script can be used to add Components which are generated dynamically on the server. * An example of adding a GridPanel to a TabPanel where the GridPanel is generated by the server * based on certain parameters: *


// execute an Ajax request to invoke server side script:
Ext.Ajax.request({
    url: 'gen-invoice-grid.php',
    // send additional parameters to instruct server script
    params: {
        startDate: Ext.getCmp('start-date').getValue(),
        endDate: Ext.getCmp('end-date').getValue()
    },
    // process the response object to add it to the TabPanel:
    success: function(xhr) {
        var newComponent = eval(xhr.responseText); // see discussion below
        myTabPanel.add(newComponent); // add the component to the TabPanel
        myTabPanel.setActiveTab(newComponent);
    },
    failure: function() {
        Ext.Msg.alert("Grid create failed", "Server communication failure");
    }
});
*

The server script needs to return an executable Javascript statement which, when processed * using eval(), will return either a config object with an {@link Ext.Component#xtype xtype}, * or an instantiated Component. The server might return this for example:


(function() {
    function formatDate(value){
        return value ? value.dateFormat('M d, Y') : '';
    };

    var store = new Ext.data.Store({
        url: 'get-invoice-data.php',
        baseParams: {
            startDate: '01/01/2008',
            endDate: '01/31/2008'
        },
        reader: new Ext.data.JsonReader({
            record: 'transaction',
            idProperty: 'id',
            totalRecords: 'total'
        }, [
           'customer',
           'invNo',
           {name: 'date', type: 'date', dateFormat: 'm/d/Y'},
           {name: 'value', type: 'float'}
        ])
    });

    var grid = new Ext.grid.GridPanel({
        title: 'Invoice Report',
        bbar: new Ext.PagingToolbar(store),
        store: store,
        columns: [
            {header: "Customer", width: 250, dataIndex: 'customer', sortable: true},
            {header: "Invoice Number", width: 120, dataIndex: 'invNo', sortable: true},
            {header: "Invoice Date", width: 100, dataIndex: 'date', renderer: formatDate, sortable: true},
            {header: "Value", width: 120, dataIndex: 'value', renderer: 'usMoney', sortable: true}
        ],
    });
    store.load();
    return grid;  // return instantiated component
})();
*

When the above code fragment is passed through the eval function in the success handler * of the Ajax request, the code is executed by the Javascript processor, and the anonymous function * runs, and returns the instantiated grid component.

*

Note: since the code above is generated by a server script, the baseParams for * the Store, the metadata to allow generation of the Record layout, and the ColumnModel * can all be generated into the code since these are all known on the server.

* * @xtype container */ Ext.define('Ext.AbstractContainer', { /* Begin Definitions */ extend: 'Ext.Component', requires: [ 'Ext.util.MixedCollection', 'Ext.layout.Manager', 'Ext.layout.container.Auto', 'Ext.ComponentMgr', 'Ext.ComponentQuery', 'Ext.ZIndexManager' ], /* End Definitions */
/** * @cfg {String/Object} layout *

*Important: In order for child items to be correctly sized and * positioned, typically a layout manager must be specified through * the layout configuration option.

*

The sizing and positioning of child {@link items} is the responsibility of * the Container's layout manager which creates and manages the type of layout * you have in mind. For example:

*

If the {@link #layout} configuration is not explicitly specified for * a general purpose container (e.g. Container or Panel) the * {@link Ext.layout.container.Auto default layout manager} will be used * which does nothing but render child components sequentially into the * Container (no sizing or positioning will be performed in this situation).

*

layout may be specified as either as an Object or * as a String:

*/
/** * @cfg {String/Number} activeItem * A string component id or the numeric index of the component that should be initially activated within the * container's layout on render. For example, activeItem: 'item-1' or activeItem: 0 (index 0 = the first * item in the container's collection). activeItem only applies to layout styles that can display * items one at a time (like {@link Ext.layout.CardLayout} and * {@link Ext.layout.FitLayout}). Related to {@link Ext.layout.ContainerLayout#activeItem}. */
/** * @cfg {Object/Array} items *
** IMPORTANT: be sure to {@link #layout specify a layout} if needed ! **
*

A single item, or an array of child Components to be added to this container, * for example:

*

// specifying a single item
items: {...},
layout: 'fit',    // specify a layout!

// specifying multiple items
items: [{...}, {...}],
layout: 'hbox', // specify a layout!
       
*

Each item may be:

*
*

Notes:

*
*/
/** * @cfg {Object|Function} defaults *

This option is a means of applying default settings to all added items whether added through the {@link #items} * config or via the {@link #add} or {@link #insert} methods.

*

If an added item is a config object, and not an instantiated Component, then the default properties are * unconditionally applied. If the added item is an instantiated Component, then the default properties are * applied conditionally so as not to override existing properties in the item.

*

If the defaults option is specified as a function, then the function will be called using this Container as the * scope (this reference) and passing the added item as the first parameter. Any resulting object * from that call is then applied to the item as default properties.

*

For example, to automatically apply padding to the body of each of a set of * contained {@link Ext.Panel} items, you could pass: defaults: {bodyStyle:'padding:15px'}.

*

Usage:


defaults: {               // defaults are applied to items, not the container
    autoScroll:true
},
items: [
    {
        xtype: 'panel',   // defaults do not have precedence over
        id: 'panel1',     // options in config objects, so the defaults
        autoScroll: false // will not be applied here, panel1 will be autoScroll:false
    },
    new Ext.Panel({       // defaults do have precedence over options
        id: 'panel2',     // options in components, so the defaults
        autoScroll: false // will be applied here, panel2 will be autoScroll:true.
    })
]
*/
/** @cfg {Boolean} suspendLayout * If true, suspend calls to doLayout. Usefule when batching multiple adds to a container and not passing them * as multiple arguments or an array. */ suspendLayout : false,
/** @cfg {Boolean} autoDestroy * If true the container will automatically destroy any contained component that is removed from it, else * destruction must be handled manually (defaults to true). * Defaults to false. */ autoDestroy : true,
/** @cfg {String} defaultType *

The default {@link Ext.Component xtype} of child Components to create in this Container when * a child item is specified as a raw configuration object, rather than as an instantiated Component.

*

Defaults to 'panel'.

*/ defaultType: 'panel', isContainer : true, baseCls: Ext.baseCSSPrefix + 'container',
/** * @cfg {Array} bubbleEvents *

An array of events that, when fired, should be bubbled to any parent container. * See {@link Ext.util.Observable#enableBubble}. * Defaults to ['add', 'remove']. */ bubbleEvents: ['add', 'remove'], // @private initComponent : function(){ var me = this; me.addEvents(

/** * @event afterlayout * Fires when the components in this container are arranged by the associated layout manager. * @param {Ext.container.Container} this * @param {ContainerLayout} layout The ContainerLayout implementation for this container */ 'afterlayout',
/** * @event beforeadd * Fires before any {@link Ext.Component} is added or inserted into the container. * A handler can return false to cancel the add. * @param {Ext.container.Container} this * @param {Ext.Component} component The component being added * @param {Number} index The index at which the component will be added to the container's items collection */ 'beforeadd',
/** * @event beforeremove * Fires before any {@link Ext.Component} is removed from the container. A handler can return * false to cancel the remove. * @param {Ext.container.Container} this * @param {Ext.Component} component The component being removed */ 'beforeremove',
/** * @event add * @bubbles * Fires after any {@link Ext.Component} is added or inserted into the container. * @param {Ext.container.Container} this * @param {Ext.Component} component The component that was added * @param {Number} index The index at which the component was added to the container's items collection */ 'add',
/** * @event remove * @bubbles * Fires after any {@link Ext.Component} is removed from the container. * @param {Ext.container.Container} this * @param {Ext.Component} component The component that was removed */ 'remove',
/** * @event beforecardswitch * Fires before this container switches the active card. This event * is only available if this container uses a CardLayout. Note that * TabPanel and Carousel both get a CardLayout by default, so both * will have this event. * A handler can return false to cancel the card switch. * @param {Ext.container.Container} this * @param {Ext.Component} newCard The card that will be switched to * @param {Ext.Component} oldCard The card that will be switched from * @param {Number} index The index of the card that will be switched to * @param {Boolean} animated True if this cardswitch will be animated */ 'beforecardswitch',
/** * @event cardswitch * Fires after this container switches the active card. If the card * is switched using an animation, this event will fire after the * animation has finished. This event is only available if this container * uses a CardLayout. Note that TabPanel and Carousel both get a CardLayout * by default, so both will have this event. * @param {Ext.container.Container} this * @param {Ext.Component} newCard The card that has been switched to * @param {Ext.Component} oldCard The card that has been switched from * @param {Number} index The index of the card that has been switched to * @param {Boolean} animated True if this cardswitch was animated */ 'cardswitch' ); // layoutOnShow stack me.layoutOnShow = new Ext.util.MixedCollection(); Ext.AbstractContainer.superclass.initComponent.call(me); me.initItems(); }, // @private initItems : function() { var me = this, items = this.items;
/** * The MixedCollection containing all the child items of this container. * @property items * @type Ext.util.MixedCollection */ me.items = Ext.create('Ext.util.MixedCollection', false, me.getComponentId); if (items) { if (!Ext.isArray(items)) { items = [items]; } me.add(items); } }, // @private afterRender : function() { this.getLayout(); Ext.AbstractContainer.superclass.afterRender.apply(this, arguments); }, // @private setLayout : function(layout) { var currentLayout = this.layout; if (currentLayout && currentLayout.isLayout && currentLayout != layout) { currentLayout.setOwner(null); } this.layout = layout; layout.setOwner(this); },
/** * Returns the {@link Ext.layout.ContainerLayout layout} instance currently associated with this Container. * If a layout has not been instantiated yet, that is done first * @return {Ext.layout.ContainerLayout} The layout */ getLayout : function() { var me = this; if (!me.layout || !me.layout.isLayout) { me.setLayout(Ext.layout.Manager.create(me.layout, 'autocontainer')); } return me.layout; },
/** * Manually force this container's layout to be recalculated. The framwork uses this internally to refresh layouts * form most cases. * @return {Ext.container.Container} this */ doLayout : function() { var me = this, layout = me.getLayout(); if (me.rendered && layout && !me.suspendLayout) { layout.layout(); } return me; }, // @private afterLayout : function(layout) { this.fireEvent('afterlayout', this, layout); }, // @private prepareItems : function(items, applyDefaults) { if (!Ext.isArray(items)) { items = [items]; } // Make sure defaults are applied and item is initialized var i = 0, len = items.length, item; for (; i < len; i++) { item = items[i]; if (applyDefaults) { item = this.applyDefaults(item); } items[i] = this.lookupComponent(item); } return items; }, // @private applyDefaults : function(config) { var defaults = this.defaults; if (defaults) { if (Ext.isFunction(defaults)) { defaults = defaults.call(this, config); } if (Ext.isString(config)) { config = Ext.ComponentMgr.get(config); Ext.applyIf(config, defaults); } else if (!config.isComponent) { Ext.applyIf(config, defaults); } else { Ext.applyIf(config, defaults); } } return config; }, // @private lookupComponent : function(comp) { if (Ext.isString(comp)) { return Ext.ComponentMgr.get(comp); } else { return this.createComponent(comp); } return comp; }, // @private createComponent : function(config, defaultType) { // // add in ownerCt at creation time but then immediately // // remove so that onBeforeAdd can handle it // var component = Ext.create(Ext.apply({ownerCt: this}, config), defaultType || this.defaultType); // // delete component.initialConfig.ownerCt; // delete component.ownerCt; return Ext.ComponentMgr.create(config, defaultType || this.defaultType); }, // @private - used as the key lookup function for the items collection getComponentId : function(comp) { return comp.getItemId(); },
/** *

Adds {@link Ext.Component Component}(s) to this Container.

*

Description : *

*

Notes : *

* @param {...Object/Array} component *

Either one or more Components to add or an Array of Components to add. See * {@link #items} for additional information.

* @return {Ext.Component/Array} The Components that were added. */ add : function() { var me = this, args = Array.prototype.slice.call(arguments), hasMultipleArgs, items, results = [], i, ln, item, index = -1, cmp; if (typeof args[0] == 'number') { index = args.shift(); } hasMultipleArgs = args.length > 1; if (hasMultipleArgs || Ext.isArray(args[0])) { items = hasMultipleArgs ? args : args[0]; // Suspend Layouts while we add multiple items to the container me.suspendLayout = true; for (i = 0, ln = items.length; i < ln; i++) { item = items[i]; if (!item) { throw "Trying to add a null item as a child of Container with itemId/id: " + me.getItemId(); } if (index != -1) { item = me.add(index + i, item); } else { item = me.add(item); } results.push(item); } // Resume Layouts now that all items have been added and do a single layout for all the items just added me.suspendLayout = false; me.doLayout(); return results; } cmp = me.prepareItems(args[0], true)[0]; // Floating Components are not added into the items collection // But they do get an upward ownerCt link so that they can traverse // up to their z-index parent. if (cmp.floating) { cmp.onAdded(me, index); } else { index = (index !== -1) ? index : me.items.length; if (me.fireEvent('beforeadd', me, cmp, index) !== false && me.onBeforeAdd(cmp) !== false) { me.items.insert(index, cmp); cmp.onAdded(me, index); me.onAdd(cmp, index); me.fireEvent('add', me, cmp, index); } me.doLayout(); } return cmp; }, /** * @private *

Called by Component#doAutoRender

*

Register a Container configured floating: true with this Container's {@link Ext.ZIndexManager ZIndexManager}.

*

Components added in ths way will not participate in the layout, but will be rendered * upon first show in the way that {@link Ext.window.Window Window}s are.

*

*/ registerFloatingItem: function(cmp) { var me = this; if (!me.floatingItems) { me.floatingItems = new Ext.ZIndexManager(me); } me.floatingItems.register(cmp); }, onAdd : Ext.emptyFn, onRemove : Ext.emptyFn,
/** * Inserts a Component into this Container at a specified index. Fires the * {@link #beforeadd} event before inserting, then fires the {@link #add} event after the * Component has been inserted. * @param {Number} index The index at which the Component will be inserted * into the Container's items collection * @param {Ext.Component} component The child Component to insert.

* Ext uses lazy rendering, and will only render the inserted Component should * it become necessary.

* A Component config object may be passed in order to avoid the overhead of * constructing a real Component object if lazy rendering might mean that the * inserted Component will not be rendered immediately. To take advantage of * this 'lazy instantiation', set the {@link Ext.Component#xtype} config * property to the registered type of the Component wanted.

* For a list of all available xtypes, see {@link Ext.Component}. * @return {Ext.Component} component The Component (or config object) that was * inserted with the Container's default config values applied. */ insert : function(index, comp) { return this.add(index, comp); },
/** * Moves a Component within the Container * @param {Number} fromIdx The index the Component you wish to move is currently at. * @param {Number} toIdx The new index for the Component. * @return {Ext.Component} component The Component (or config object) that was moved. */ move : function(fromIdx, toIdx) { var items = this.items, item; item = items.removeAt(fromIdx); if (item === false) { return false; } items.insert(toIdx, item); this.doLayout(); return item; }, // @private onBeforeAdd : function(item) { if (item.ownerCt) { item.ownerCt.remove(item, false); } if (this.hideBorders === true){ item.border = (item.border === true); } },
/** * Removes a component from this container. Fires the {@link #beforeremove} event before removing, then fires * the {@link #remove} event after the component has been removed. * @param {Component/String} component The component reference or id to remove. * @param {Boolean} autoDestroy (optional) True to automatically invoke the removed Component's {@link Ext.Component#destroy} function. * Defaults to the value of this Container's {@link #autoDestroy} config. * @return {Ext.Component} component The Component that was removed. */ remove : function(comp, autoDestroy) { var me = this, c = me.getComponent(comp); // if (!c) { console.warn("Attempted to remove a component that does not exist. Ext.container.Container: remove takes an argument of the component to remove. cmp.remove() is incorrect usage."); } // if (c && me.fireEvent('beforeremove', me, c) !== false) { me.doRemove(c, autoDestroy); me.fireEvent('remove', me, c); } return c; }, // @private doRemove : function(component, autoDestroy) { var me = this, layout = me.layout, hasLayout = layout && me.rendered; me.items.remove(component); component.onRemoved(); if (hasLayout) { layout.onRemove(component); } me.onRemove(component, autoDestroy); if (autoDestroy === true || (autoDestroy !== false && me.autoDestroy)) { component.destroy(); } if (hasLayout && !autoDestroy) { layout.afterRemove(component); } if (!me.destroying) { me.doLayout(); } },
/** * Removes all components from this container. * @param {Boolean} autoDestroy (optional) True to automatically invoke the removed Component's {@link Ext.Component#destroy} function. * Defaults to the value of this Container's {@link #autoDestroy} config. * @return {Array} Array of the destroyed components */ removeAll : function(autoDestroy) { var me = this, removeItems = me.items.items.slice(), items = [], i = 0, len = removeItems.length, item; // Suspend Layouts while we remove multiple items from the container me.suspendLayout = true; for (; i < len; i++) { item = removeItems[i]; me.remove(item, autoDestroy); if (item.ownerCt !== me) { items.push(item); } } // Resume Layouts now that all items have been removed and do a single layout me.suspendLayout = false; me.doLayout(); return items; }, // Used by ComponentQuery to retrieve all of the items // which can potentially be considered a child of this Container. // This should be overriden by components which have child items // that are not contained in items. For example dockedItems, menu, etc getRefItems : function(deep) { var items = this.items.items.slice(), len = items.length, i = 0, item; // Include floating items in the list. // These will only be present after they are rendered. if (this.floatingItems) { items = items.concat(this.floatingItems.accessList); len = items.length; } if (deep) { for (; i < len; i++) { item = items[i]; if (item.getRefItems) { items = items.concat(item.getRefItems(true)); } } } return items; },
/** * Examines this container's {@link #items} property * and gets a direct child component of this container. * @param {String/Number} comp This parameter may be any of the following: *
*

For additional information see {@link Ext.util.MixedCollection#get}. * @return Ext.Component The component (if found). */ getComponent : function(comp) { if (Ext.isObject(comp)) { comp = comp.getItemId(); } return this.items.get(comp); },

/** * Retrieves all descendant components which match the passed selector. * Executes an Ext.ComponentQuery.query using this container as its root. * @param {String} selector Selector complying to an Ext.ComponentQuery selector * @return {Array} Ext.Component's which matched the selector */ query : function(selector) { return Ext.ComponentQuery.query(selector, this); },
/** * Retrieves the first direct child of this container which matches the passed selector. * The passed in selector must comply with an Ext.ComponentQuery selector. * @param {String} selector An Ext.ComponentQuery selector * @return Ext.Component */ child : function(selector) { return this.query('> ' + selector)[0] || null; },
/** * Retrieves the first descendant of this container which matches the passed selector. * The passed in selector must comply with an Ext.ComponentQuery selector. * @param {String} selector An Ext.ComponentQuery selector * @return Ext.Component */ down : function(selector) { return this.query(selector)[0] || null; }, // inherit docs show : function() { Ext.AbstractContainer.superclass.show.apply(this, arguments); this.performDeferredLayouts(); }, // Lay out any descendant containers who queued a layout operation during the time this was hidden // This is also called by Panel after it expands because descendants of a collapsed Panel allso queue any layout ops. performDeferredLayouts: function() { var layoutCollection = this.layoutOnShow, ln = layoutCollection.getCount(), i = 0, needsLayout, item; for (; i < ln; i++) { item = layoutCollection.get(i); needsLayout = item.needsLayout; if (Ext.isObject(needsLayout)) { item.doComponentLayout(needsLayout.width, needsLayout.height, needsLayout.isSetSize, needsLayout.ownerCt); } } layoutCollection.clear(); }, // @private beforeDestroy : function() { var me = this, items = me.items, c; if (items) { while ((c = items.first())) { me.doRemove(c, true); } } Ext.destroy( me.layout, me.floatingItems ); Ext.AbstractContainer.superclass.beforeDestroy.call(me); } });