Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / docs / source / Container.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.2
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.Container"></div>/**
16  * @class Ext.Container
17  * @extends Ext.BoxComponent
18  * <p>Base class for any {@link Ext.BoxComponent} that may contain other Components. Containers handle the
19  * basic behavior of containing items, namely adding, inserting and removing items.</p>
20  *
21  * <p>The most commonly used Container classes are {@link Ext.Panel}, {@link Ext.Window} and {@link Ext.TabPanel}.
22  * If you do not need the capabilities offered by the aforementioned classes you can create a lightweight
23  * Container to be encapsulated by an HTML element to your specifications by using the
24  * <code><b>{@link Ext.Component#autoEl autoEl}</b></code> config option. This is a useful technique when creating
25  * embedded {@link Ext.layout.ColumnLayout column} layouts inside {@link Ext.form.FormPanel FormPanels}
26  * for example.</p>
27  *
28  * <p>The code below illustrates both how to explicitly create a Container, and how to implicitly
29  * create one using the <b><code>'container'</code></b> xtype:<pre><code>
30 // explicitly create a Container
31 var embeddedColumns = new Ext.Container({
32     autoEl: 'div',  // This is the default
33     layout: 'column',
34     defaults: {
35         // implicitly create Container by specifying xtype
36         xtype: 'container',
37         autoEl: 'div', // This is the default.
38         layout: 'form',
39         columnWidth: 0.5,
40         style: {
41             padding: '10px'
42         }
43     },
44 //  The two items below will be Ext.Containers, each encapsulated by a &lt;DIV> element.
45     items: [{
46         items: {
47             xtype: 'datefield',
48             name: 'startDate',
49             fieldLabel: 'Start date'
50         }
51     }, {
52         items: {
53             xtype: 'datefield',
54             name: 'endDate',
55             fieldLabel: 'End date'
56         }
57     }]
58 });</code></pre></p>
59  *
60  * <p><u><b>Layout</b></u></p>
61  * <p>Container classes delegate the rendering of child Components to a layout
62  * manager class which must be configured into the Container using the
63  * <code><b>{@link #layout}</b></code> configuration property.</p>
64  * <p>When either specifying child <code>{@link #items}</code> of a Container,
65  * or dynamically {@link #add adding} Components to a Container, remember to
66  * consider how you wish the Container to arrange those child elements, and
67  * whether those child elements need to be sized using one of Ext's built-in
68  * <b><code>{@link #layout}</code></b> schemes. By default, Containers use the
69  * {@link Ext.layout.ContainerLayout ContainerLayout} scheme which only
70  * renders child components, appending them one after the other inside the
71  * Container, and <b>does not apply any sizing</b> at all.</p>
72  * <p>A common mistake is when a developer neglects to specify a
73  * <b><code>{@link #layout}</code></b> (e.g. widgets like GridPanels or
74  * TreePanels are added to Containers for which no <code><b>{@link #layout}</b></code>
75  * has been specified). If a Container is left to use the default
76  * {@link Ext.layout.ContainerLayout ContainerLayout} scheme, none of its
77  * child components will be resized, or changed in any way when the Container
78  * is resized.</p>
79  * <p>Certain layout managers allow dynamic addition of child components.
80  * Those that do include {@link Ext.layout.CardLayout},
81  * {@link Ext.layout.AnchorLayout}, {@link Ext.layout.FormLayout}, and
82  * {@link Ext.layout.TableLayout}. For example:<pre><code>
83 //  Create the GridPanel.
84 var myNewGrid = new Ext.grid.GridPanel({
85     store: myStore,
86     columns: myColumnModel,
87     title: 'Results', // the title becomes the title of the tab
88 });
89
90 myTabPanel.add(myNewGrid); // {@link Ext.TabPanel} implicitly uses {@link Ext.layout.CardLayout CardLayout}
91 myTabPanel.{@link Ext.TabPanel#setActiveTab setActiveTab}(myNewGrid);
92  * </code></pre></p>
93  * <p>The example above adds a newly created GridPanel to a TabPanel. Note that
94  * a TabPanel uses {@link Ext.layout.CardLayout} as its layout manager which
95  * means all its child items are sized to {@link Ext.layout.FitLayout fit}
96  * exactly into its client area.
97  * <p><b><u>Overnesting is a common problem</u></b>.
98  * An example of overnesting occurs when a GridPanel is added to a TabPanel
99  * by wrapping the GridPanel <i>inside</i> a wrapping Panel (that has no
100  * <code><b>{@link #layout}</b></code> specified) and then add that wrapping Panel
101  * to the TabPanel. The point to realize is that a GridPanel <b>is</b> a
102  * Component which can be added directly to a Container. If the wrapping Panel
103  * has no <code><b>{@link #layout}</b></code> configuration, then the overnested
104  * GridPanel will not be sized as expected.<p>
105  *
106  * <p><u><b>Adding via remote configuration</b></u></p>
107  *
108  * <p>A server side script can be used to add Components which are generated dynamically on the server.
109  * An example of adding a GridPanel to a TabPanel where the GridPanel is generated by the server
110  * based on certain parameters:
111  * </p><pre><code>
112 // execute an Ajax request to invoke server side script:
113 Ext.Ajax.request({
114     url: 'gen-invoice-grid.php',
115     // send additional parameters to instruct server script
116     params: {
117         startDate: Ext.getCmp('start-date').getValue(),
118         endDate: Ext.getCmp('end-date').getValue()
119     },
120     // process the response object to add it to the TabPanel:
121     success: function(xhr) {
122         var newComponent = eval(xhr.responseText); // see discussion below
123         myTabPanel.add(newComponent); // add the component to the TabPanel
124         myTabPanel.setActiveTab(newComponent);
125     },
126     failure: function() {
127         Ext.Msg.alert("Grid create failed", "Server communication failure");
128     }
129 });
130 </code></pre>
131  * <p>The server script needs to return an executable Javascript statement which, when processed
132  * using <code>eval()</code>, will return either a config object with an {@link Ext.Component#xtype xtype},
133  * or an instantiated Component. The server might return this for example:</p><pre><code>
134 (function() {
135     function formatDate(value){
136         return value ? value.dateFormat('M d, Y') : '';
137     };
138
139     var store = new Ext.data.Store({
140         url: 'get-invoice-data.php',
141         baseParams: {
142             startDate: '01/01/2008',
143             endDate: '01/31/2008'
144         },
145         reader: new Ext.data.JsonReader({
146             record: 'transaction',
147             idProperty: 'id',
148             totalRecords: 'total'
149         }, [
150            'customer',
151            'invNo',
152            {name: 'date', type: 'date', dateFormat: 'm/d/Y'},
153            {name: 'value', type: 'float'}
154         ])
155     });
156
157     var grid = new Ext.grid.GridPanel({
158         title: 'Invoice Report',
159         bbar: new Ext.PagingToolbar(store),
160         store: store,
161         columns: [
162             {header: "Customer", width: 250, dataIndex: 'customer', sortable: true},
163             {header: "Invoice Number", width: 120, dataIndex: 'invNo', sortable: true},
164             {header: "Invoice Date", width: 100, dataIndex: 'date', renderer: formatDate, sortable: true},
165             {header: "Value", width: 120, dataIndex: 'value', renderer: 'usMoney', sortable: true}
166         ],
167     });
168     store.load();
169     return grid;  // return instantiated component
170 })();
171 </code></pre>
172  * <p>When the above code fragment is passed through the <code>eval</code> function in the success handler
173  * of the Ajax request, the code is executed by the Javascript processor, and the anonymous function
174  * runs, and returns the instantiated grid component.</p>
175  * <p>Note: since the code above is <i>generated</i> by a server script, the <code>baseParams</code> for
176  * the Store, the metadata to allow generation of the Record layout, and the ColumnModel
177  * can all be generated into the code since these are all known on the server.</p>
178  *
179  * @xtype container
180  */
181 Ext.Container = Ext.extend(Ext.BoxComponent, {
182     <div id="cfg-Ext.Container-monitorResize"></div>/**
183      * @cfg {Boolean} monitorResize
184      * True to automatically monitor window resize events to handle anything that is sensitive to the current size
185      * of the viewport.  This value is typically managed by the chosen <code>{@link #layout}</code> and should not need
186      * to be set manually.
187      */
188     <div id="cfg-Ext.Container-layout"></div>/**
189      * @cfg {String/Object} layout
190      * <p><b>*Important</b>: In order for child items to be correctly sized and
191      * positioned, typically a layout manager <b>must</b> be specified through
192      * the <code>layout</code> configuration option.</p>
193      * <br><p>The sizing and positioning of child {@link items} is the responsibility of
194      * the Container's layout manager which creates and manages the type of layout
195      * you have in mind.  For example:</p><pre><code>
196 new Ext.Window({
197     width:300, height: 300,
198     layout: 'fit', // explicitly set layout manager: override the default (layout:'auto')
199     items: [{
200         title: 'Panel inside a Window'
201     }]
202 }).show();
203      * </code></pre>
204      * <p>If the {@link #layout} configuration is not explicitly specified for
205      * a general purpose container (e.g. Container or Panel) the
206      * {@link Ext.layout.ContainerLayout default layout manager} will be used
207      * which does nothing but render child components sequentially into the
208      * Container (no sizing or positioning will be performed in this situation).
209      * Some container classes implicitly specify a default layout
210      * (e.g. FormPanel specifies <code>layout:'form'</code>). Other specific
211      * purpose classes internally specify/manage their internal layout (e.g.
212      * GridPanel, TabPanel, TreePanel, Toolbar, Menu, etc.).</p>
213      * <br><p><b><code>layout</code></b> may be specified as either as an Object or
214      * as a String:</p><div><ul class="mdetail-params">
215      *
216      * <li><u>Specify as an Object</u></li>
217      * <div><ul class="mdetail-params">
218      * <li>Example usage:</li>
219 <pre><code>
220 layout: {
221     type: 'vbox',
222     padding: '5',
223     align: 'left'
224 }
225 </code></pre>
226      *
227      * <li><code><b>type</b></code></li>
228      * <br/><p>The layout type to be used for this container.  If not specified,
229      * a default {@link Ext.layout.ContainerLayout} will be created and used.</p>
230      * <br/><p>Valid layout <code>type</code> values are:</p>
231      * <div class="sub-desc"><ul class="mdetail-params">
232      * <li><code><b>{@link Ext.layout.AbsoluteLayout absolute}</b></code></li>
233      * <li><code><b>{@link Ext.layout.AccordionLayout accordion}</b></code></li>
234      * <li><code><b>{@link Ext.layout.AnchorLayout anchor}</b></code></li>
235      * <li><code><b>{@link Ext.layout.ContainerLayout auto}</b></code> &nbsp;&nbsp;&nbsp; <b>Default</b></li>
236      * <li><code><b>{@link Ext.layout.BorderLayout border}</b></code></li>
237      * <li><code><b>{@link Ext.layout.CardLayout card}</b></code></li>
238      * <li><code><b>{@link Ext.layout.ColumnLayout column}</b></code></li>
239      * <li><code><b>{@link Ext.layout.FitLayout fit}</b></code></li>
240      * <li><code><b>{@link Ext.layout.FormLayout form}</b></code></li>
241      * <li><code><b>{@link Ext.layout.HBoxLayout hbox}</b></code></li>
242      * <li><code><b>{@link Ext.layout.MenuLayout menu}</b></code></li>
243      * <li><code><b>{@link Ext.layout.TableLayout table}</b></code></li>
244      * <li><code><b>{@link Ext.layout.ToolbarLayout toolbar}</b></code></li>
245      * <li><code><b>{@link Ext.layout.VBoxLayout vbox}</b></code></li>
246      * </ul></div>
247      *
248      * <li>Layout specific configuration properties</li>
249      * <br/><p>Additional layout specific configuration properties may also be
250      * specified. For complete details regarding the valid config options for
251      * each layout type, see the layout class corresponding to the <code>type</code>
252      * specified.</p>
253      *
254      * </ul></div>
255      *
256      * <li><u>Specify as a String</u></li>
257      * <div><ul class="mdetail-params">
258      * <li>Example usage:</li>
259 <pre><code>
260 layout: 'vbox',
261 layoutConfig: {
262     padding: '5',
263     align: 'left'
264 }
265 </code></pre>
266      * <li><code><b>layout</b></code></li>
267      * <br/><p>The layout <code>type</code> to be used for this container (see list
268      * of valid layout type values above).</p><br/>
269      * <li><code><b>{@link #layoutConfig}</b></code></li>
270      * <br/><p>Additional layout specific configuration properties. For complete
271      * details regarding the valid config options for each layout type, see the
272      * layout class corresponding to the <code>layout</code> specified.</p>
273      * </ul></div></ul></div>
274      */
275     <div id="cfg-Ext.Container-layoutConfig"></div>/**
276      * @cfg {Object} layoutConfig
277      * This is a config object containing properties specific to the chosen
278      * <b><code>{@link #layout}</code></b> if <b><code>{@link #layout}</code></b>
279      * has been specified as a <i>string</i>.</p>
280      */
281     <div id="cfg-Ext.Container-bufferResize"></div>/**
282      * @cfg {Boolean/Number} bufferResize
283      * When set to true (50 milliseconds) or a number of milliseconds, the layout assigned for this container will buffer
284      * the frequency it calculates and does a re-layout of components. This is useful for heavy containers or containers
285      * with a large quantity of sub-components for which frequent layout calls would be expensive. Defaults to <code>50</code>.
286      */
287     bufferResize: 50,
288
289     <div id="cfg-Ext.Container-activeItem"></div>/**
290      * @cfg {String/Number} activeItem
291      * A string component id or the numeric index of the component that should be initially activated within the
292      * container's layout on render.  For example, activeItem: 'item-1' or activeItem: 0 (index 0 = the first
293      * item in the container's collection).  activeItem only applies to layout styles that can display
294      * items one at a time (like {@link Ext.layout.AccordionLayout}, {@link Ext.layout.CardLayout} and
295      * {@link Ext.layout.FitLayout}).  Related to {@link Ext.layout.ContainerLayout#activeItem}.
296      */
297     <div id="cfg-Ext.Container-items"></div>/**
298      * @cfg {Object/Array} items
299      * <pre><b>** IMPORTANT</b>: be sure to <b>{@link #layout specify a <code>layout</code>} if needed ! **</b></pre>
300      * <p>A single item, or an array of child Components to be added to this container,
301      * for example:</p>
302      * <pre><code>
303 // specifying a single item
304 items: {...},
305 layout: 'fit',    // specify a layout!
306
307 // specifying multiple items
308 items: [{...}, {...}],
309 layout: 'anchor', // specify a layout!
310      * </code></pre>
311      * <p>Each item may be:</p>
312      * <div><ul class="mdetail-params">
313      * <li>any type of object based on {@link Ext.Component}</li>
314      * <li>a fully instanciated object or</li>
315      * <li>an object literal that:</li>
316      * <div><ul class="mdetail-params">
317      * <li>has a specified <code>{@link Ext.Component#xtype xtype}</code></li>
318      * <li>the {@link Ext.Component#xtype} specified is associated with the Component
319      * desired and should be chosen from one of the available xtypes as listed
320      * in {@link Ext.Component}.</li>
321      * <li>If an <code>{@link Ext.Component#xtype xtype}</code> is not explicitly
322      * specified, the {@link #defaultType} for that Container is used.</li>
323      * <li>will be "lazily instanciated", avoiding the overhead of constructing a fully
324      * instanciated Component object</li>
325      * </ul></div></ul></div>
326      * <p><b>Notes</b>:</p>
327      * <div><ul class="mdetail-params">
328      * <li>Ext uses lazy rendering. Child Components will only be rendered
329      * should it become necessary. Items are automatically laid out when they are first
330      * shown (no sizing is done while hidden), or in response to a {@link #doLayout} call.</li>
331      * <li>Do not specify <code>{@link Ext.Panel#contentEl contentEl}</code>/
332      * <code>{@link Ext.Panel#html html}</code> with <code>items</code>.</li>
333      * </ul></div>
334      */
335     <div id="cfg-Ext.Container-defaults"></div>/**
336      * @cfg {Object|Function} defaults
337      * <p>This option is a means of applying default settings to all added items whether added through the {@link #items}
338      * config or via the {@link #add} or {@link #insert} methods.</p>
339      * <p>If an added item is a config object, and <b>not</b> an instantiated Component, then the default properties are
340      * unconditionally applied. If the added item <b>is</b> an instantiated Component, then the default properties are
341      * applied conditionally so as not to override existing properties in the item.</p>
342      * <p>If the defaults option is specified as a function, then the function will be called using this Container as the
343      * scope (<code>this</code> reference) and passing the added item as the first parameter. Any resulting object
344      * from that call is then applied to the item as default properties.</p>
345      * <p>For example, to automatically apply padding to the body of each of a set of
346      * contained {@link Ext.Panel} items, you could pass: <code>defaults: {bodyStyle:'padding:15px'}</code>.</p>
347      * <p>Usage:</p><pre><code>
348 defaults: {               // defaults are applied to items, not the container
349     autoScroll:true
350 },
351 items: [
352     {
353         xtype: 'panel',   // defaults <b>do not</b> have precedence over
354         id: 'panel1',     // options in config objects, so the defaults
355         autoScroll: false // will not be applied here, panel1 will be autoScroll:false
356     },
357     new Ext.Panel({       // defaults <b>do</b> have precedence over options
358         id: 'panel2',     // options in components, so the defaults
359         autoScroll: false // will be applied here, panel2 will be autoScroll:true.
360     })
361 ]
362      * </code></pre>
363      */
364
365
366     <div id="cfg-Ext.Container-autoDestroy"></div>/** @cfg {Boolean} autoDestroy
367      * If true the container will automatically destroy any contained component that is removed from it, else
368      * destruction must be handled manually (defaults to true).
369      */
370     autoDestroy : true,
371
372     <div id="cfg-Ext.Container-forceLayout"></div>/** @cfg {Boolean} forceLayout
373      * If true the container will force a layout initially even if hidden or collapsed. This option
374      * is useful for forcing forms to render in collapsed or hidden containers. (defaults to false).
375      */
376     forceLayout: false,
377
378     <div id="cfg-Ext.Container-hideBorders"></div>/** @cfg {Boolean} hideBorders
379      * True to hide the borders of each contained component, false to defer to the component's existing
380      * border settings (defaults to false).
381      */
382     <div id="cfg-Ext.Container-defaultType"></div>/** @cfg {String} defaultType
383      * <p>The default {@link Ext.Component xtype} of child Components to create in this Container when
384      * a child item is specified as a raw configuration object, rather than as an instantiated Component.</p>
385      * <p>Defaults to <code>'panel'</code>, except {@link Ext.menu.Menu} which defaults to <code>'menuitem'</code>,
386      * and {@link Ext.Toolbar} and {@link Ext.ButtonGroup} which default to <code>'button'</code>.</p>
387      */
388     defaultType : 'panel',
389
390     <div id="cfg-Ext.Container-resizeEvent"></div>/** @cfg {String} resizeEvent
391      * The event to listen to for resizing in layouts. Defaults to <code>'resize'</code>.
392      */
393     resizeEvent: 'resize',
394
395     <div id="cfg-Ext.Container-bubbleEvents"></div>/**
396      * @cfg {Array} bubbleEvents
397      * <p>An array of events that, when fired, should be bubbled to any parent container.
398      * See {@link Ext.util.Observable#enableBubble}.
399      * Defaults to <code>['add', 'remove']</code>.
400      */
401     bubbleEvents: ['add', 'remove'],
402
403     // private
404     initComponent : function(){
405         Ext.Container.superclass.initComponent.call(this);
406
407         this.addEvents(
408             <div id="event-Ext.Container-afterlayout"></div>/**
409              * @event afterlayout
410              * Fires when the components in this container are arranged by the associated layout manager.
411              * @param {Ext.Container} this
412              * @param {ContainerLayout} layout The ContainerLayout implementation for this container
413              */
414             'afterlayout',
415             <div id="event-Ext.Container-beforeadd"></div>/**
416              * @event beforeadd
417              * Fires before any {@link Ext.Component} is added or inserted into the container.
418              * A handler can return false to cancel the add.
419              * @param {Ext.Container} this
420              * @param {Ext.Component} component The component being added
421              * @param {Number} index The index at which the component will be added to the container's items collection
422              */
423             'beforeadd',
424             <div id="event-Ext.Container-beforeremove"></div>/**
425              * @event beforeremove
426              * Fires before any {@link Ext.Component} is removed from the container.  A handler can return
427              * false to cancel the remove.
428              * @param {Ext.Container} this
429              * @param {Ext.Component} component The component being removed
430              */
431             'beforeremove',
432             <div id="event-Ext.Container-add"></div>/**
433              * @event add
434              * @bubbles
435              * Fires after any {@link Ext.Component} is added or inserted into the container.
436              * @param {Ext.Container} this
437              * @param {Ext.Component} component The component that was added
438              * @param {Number} index The index at which the component was added to the container's items collection
439              */
440             'add',
441             <div id="event-Ext.Container-remove"></div>/**
442              * @event remove
443              * @bubbles
444              * Fires after any {@link Ext.Component} is removed from the container.
445              * @param {Ext.Container} this
446              * @param {Ext.Component} component The component that was removed
447              */
448             'remove'
449         );
450
451         <div id="prop-Ext.Container-items"></div>/**
452          * The collection of components in this container as a {@link Ext.util.MixedCollection}
453          * @type MixedCollection
454          * @property items
455          */
456         var items = this.items;
457         if(items){
458             delete this.items;
459             this.add(items);
460         }
461     },
462
463     // private
464     initItems : function(){
465         if(!this.items){
466             this.items = new Ext.util.MixedCollection(false, this.getComponentId);
467             this.getLayout(); // initialize the layout
468         }
469     },
470
471     // private
472     setLayout : function(layout){
473         if(this.layout && this.layout != layout){
474             this.layout.setContainer(null);
475         }
476         this.layout = layout;
477         this.initItems();
478         layout.setContainer(this);
479     },
480
481     afterRender: function(){
482         // Render this Container, this should be done before setLayout is called which
483         // will hook onResize
484         Ext.Container.superclass.afterRender.call(this);
485         if(!this.layout){
486             this.layout = 'auto';
487         }
488         if(Ext.isObject(this.layout) && !this.layout.layout){
489             this.layoutConfig = this.layout;
490             this.layout = this.layoutConfig.type;
491         }
492         if(Ext.isString(this.layout)){
493             this.layout = new Ext.Container.LAYOUTS[this.layout.toLowerCase()](this.layoutConfig);
494         }
495         this.setLayout(this.layout);
496
497         // If a CardLayout, the active item set
498         if(this.activeItem !== undefined){
499             var item = this.activeItem;
500             delete this.activeItem;
501             this.layout.setActiveItem(item);
502         }
503
504         // If we have no ownerCt, render and size all children
505         if(!this.ownerCt){
506             this.doLayout(false, true);
507         }
508
509         // This is a manually configured flag set by users in conjunction with renderTo.
510         // Not to be confused with the flag by the same name used in Layouts.
511         if(this.monitorResize === true){
512             Ext.EventManager.onWindowResize(this.doLayout, this, [false]);
513         }
514     },
515
516     <div id="method-Ext.Container-getLayoutTarget"></div>/**
517      * <p>Returns the Element to be used to contain the child Components of this Container.</p>
518      * <p>An implementation is provided which returns the Container's {@link #getEl Element}, but
519      * if there is a more complex structure to a Container, this may be overridden to return
520      * the element into which the {@link #layout layout} renders child Components.</p>
521      * @return {Ext.Element} The Element to render child Components into.
522      */
523     getLayoutTarget : function(){
524         return this.el;
525     },
526
527     // private - used as the key lookup function for the items collection
528     getComponentId : function(comp){
529         return comp.getItemId();
530     },
531
532     <div id="method-Ext.Container-add"></div>/**
533      * <p>Adds {@link Ext.Component Component}(s) to this Container.</p>
534      * <br><p><b>Description</b></u> :
535      * <div><ul class="mdetail-params">
536      * <li>Fires the {@link #beforeadd} event before adding</li>
537      * <li>The Container's {@link #defaults default config values} will be applied
538      * accordingly (see <code>{@link #defaults}</code> for details).</li>
539      * <li>Fires the {@link #add} event after the component has been added.</li>
540      * </ul></div>
541      * <br><p><b>Notes</b></u> :
542      * <div><ul class="mdetail-params">
543      * <li>If the Container is <i>already rendered</i> when <code>add</code>
544      * is called, you may need to call {@link #doLayout} to refresh the view which causes
545      * any unrendered child Components to be rendered. This is required so that you can
546      * <code>add</code> multiple child components if needed while only refreshing the layout
547      * once. For example:<pre><code>
548 var tb = new {@link Ext.Toolbar}();
549 tb.render(document.body);  // toolbar is rendered
550 tb.add({text:'Button 1'}); // add multiple items ({@link #defaultType} for {@link Ext.Toolbar Toolbar} is 'button')
551 tb.add({text:'Button 2'});
552 tb.{@link #doLayout}();             // refresh the layout
553      * </code></pre></li>
554      * <li><i>Warning:</i> Containers directly managed by the BorderLayout layout manager
555      * may not be removed or added.  See the Notes for {@link Ext.layout.BorderLayout BorderLayout}
556      * for more details.</li>
557      * </ul></div>
558      * @param {...Object/Array} component
559      * <p>Either one or more Components to add or an Array of Components to add.  See
560      * <code>{@link #items}</code> for additional information.</p>
561      * @return {Ext.Component/Array} The Components that were added.
562      */
563     add : function(comp){
564         this.initItems();
565         var args = arguments.length > 1;
566         if(args || Ext.isArray(comp)){
567             var result = [];
568             Ext.each(args ? arguments : comp, function(c){
569                 result.push(this.add(c));
570             }, this);
571             return result;
572         }
573         var c = this.lookupComponent(this.applyDefaults(comp));
574         var index = this.items.length;
575         if(this.fireEvent('beforeadd', this, c, index) !== false && this.onBeforeAdd(c) !== false){
576             this.items.add(c);
577             // *onAdded
578             c.onAdded(this, index);
579             this.onAdd(c);
580             this.fireEvent('add', this, c, index);
581         }
582         return c;
583     },
584
585     onAdd : function(c){
586         // Empty template method
587     },
588
589     // private
590     onAdded : function(container, pos) {
591         //overridden here so we can cascade down, not worth creating a template method.
592         this.ownerCt = container;
593         this.initRef();
594         //initialize references for child items
595         this.cascade(function(c){
596             c.initRef();
597         });
598         this.fireEvent('added', this, container, pos);
599     },
600
601     <div id="method-Ext.Container-insert"></div>/**
602      * Inserts a Component into this Container at a specified index. Fires the
603      * {@link #beforeadd} event before inserting, then fires the {@link #add} event after the
604      * Component has been inserted.
605      * @param {Number} index The index at which the Component will be inserted
606      * into the Container's items collection
607      * @param {Ext.Component} component The child Component to insert.<br><br>
608      * Ext uses lazy rendering, and will only render the inserted Component should
609      * it become necessary.<br><br>
610      * A Component config object may be passed in order to avoid the overhead of
611      * constructing a real Component object if lazy rendering might mean that the
612      * inserted Component will not be rendered immediately. To take advantage of
613      * this 'lazy instantiation', set the {@link Ext.Component#xtype} config
614      * property to the registered type of the Component wanted.<br><br>
615      * For a list of all available xtypes, see {@link Ext.Component}.
616      * @return {Ext.Component} component The Component (or config object) that was
617      * inserted with the Container's default config values applied.
618      */
619     insert : function(index, comp){
620         this.initItems();
621         var a = arguments, len = a.length;
622         if(len > 2){
623             var result = [];
624             for(var i = len-1; i >= 1; --i) {
625                 result.push(this.insert(index, a[i]));
626             }
627             return result;
628         }
629         var c = this.lookupComponent(this.applyDefaults(comp));
630         index = Math.min(index, this.items.length);
631         if(this.fireEvent('beforeadd', this, c, index) !== false && this.onBeforeAdd(c) !== false){
632             if(c.ownerCt == this){
633                 this.items.remove(c);
634             }
635             this.items.insert(index, c);
636             c.onAdded(this, index);
637             this.onAdd(c);
638             this.fireEvent('add', this, c, index);
639         }
640         return c;
641     },
642
643     // private
644     applyDefaults : function(c){
645         var d = this.defaults;
646         if(d){
647             if(Ext.isFunction(d)){
648                 d = d.call(this, c);
649             }
650             if(Ext.isString(c)){
651                 c = Ext.ComponentMgr.get(c);
652                 Ext.apply(c, d);
653             }else if(!c.events){
654                 Ext.applyIf(c, d);
655             }else{
656                 Ext.apply(c, d);
657             }
658         }
659         return c;
660     },
661
662     // private
663     onBeforeAdd : function(item){
664         if(item.ownerCt){
665             item.ownerCt.remove(item, false);
666         }
667         if(this.hideBorders === true){
668             item.border = (item.border === true);
669         }
670     },
671
672     <div id="method-Ext.Container-remove"></div>/**
673      * Removes a component from this container.  Fires the {@link #beforeremove} event before removing, then fires
674      * the {@link #remove} event after the component has been removed.
675      * @param {Component/String} component The component reference or id to remove.
676      * @param {Boolean} autoDestroy (optional) True to automatically invoke the removed Component's {@link Ext.Component#destroy} function.
677      * Defaults to the value of this Container's {@link #autoDestroy} config.
678      * @return {Ext.Component} component The Component that was removed.
679      */
680     remove : function(comp, autoDestroy){
681         this.initItems();
682         var c = this.getComponent(comp);
683         if(c && this.fireEvent('beforeremove', this, c) !== false){
684             this.doRemove(c, autoDestroy);
685             this.fireEvent('remove', this, c);
686         }
687         return c;
688     },
689
690     onRemove: function(c){
691         // Empty template method
692     },
693
694     // private
695     doRemove: function(c, autoDestroy){
696         var l = this.layout,
697             hasLayout = l && this.rendered;
698
699         if(hasLayout){
700             l.onRemove(c);
701         }
702         this.items.remove(c);
703         c.onRemoved();
704         this.onRemove(c);
705         if(autoDestroy === true || (autoDestroy !== false && this.autoDestroy)){
706             c.destroy();
707         }
708         if(hasLayout){
709             l.afterRemove(c);
710         }
711     },
712
713     <div id="method-Ext.Container-removeAll"></div>/**
714      * Removes all components from this container.
715      * @param {Boolean} autoDestroy (optional) True to automatically invoke the removed Component's {@link Ext.Component#destroy} function.
716      * Defaults to the value of this Container's {@link #autoDestroy} config.
717      * @return {Array} Array of the destroyed components
718      */
719     removeAll: function(autoDestroy){
720         this.initItems();
721         var item, rem = [], items = [];
722         this.items.each(function(i){
723             rem.push(i);
724         });
725         for (var i = 0, len = rem.length; i < len; ++i){
726             item = rem[i];
727             this.remove(item, autoDestroy);
728             if(item.ownerCt !== this){
729                 items.push(item);
730             }
731         }
732         return items;
733     },
734
735     <div id="method-Ext.Container-getComponent"></div>/**
736      * Examines this container's <code>{@link #items}</code> <b>property</b>
737      * and gets a direct child component of this container.
738      * @param {String/Number} comp This parameter may be any of the following:
739      * <div><ul class="mdetail-params">
740      * <li>a <b><code>String</code></b> : representing the <code>{@link Ext.Component#itemId itemId}</code>
741      * or <code>{@link Ext.Component#id id}</code> of the child component </li>
742      * <li>a <b><code>Number</code></b> : representing the position of the child component
743      * within the <code>{@link #items}</code> <b>property</b></li>
744      * </ul></div>
745      * <p>For additional information see {@link Ext.util.MixedCollection#get}.
746      * @return Ext.Component The component (if found).
747      */
748     getComponent : function(comp){
749         if(Ext.isObject(comp)){
750             comp = comp.getItemId();
751         }
752         return this.items.get(comp);
753     },
754
755     // private
756     lookupComponent : function(comp){
757         if(Ext.isString(comp)){
758             return Ext.ComponentMgr.get(comp);
759         }else if(!comp.events){
760             return this.createComponent(comp);
761         }
762         return comp;
763     },
764
765     // private
766     createComponent : function(config, defaultType){
767         if (config.render) {
768             return config;
769         }
770         // add in ownerCt at creation time but then immediately
771         // remove so that onBeforeAdd can handle it
772         var c = Ext.create(Ext.apply({
773             ownerCt: this
774         }, config), defaultType || this.defaultType);
775         delete c.initialConfig.ownerCt;
776         delete c.ownerCt;
777         return c;
778     },
779
780     /**
781      * @private
782      * We can only lay out if there is a view area in which to layout.
783      * display:none on the layout target, *or any of its parent elements* will mean it has no view area.
784      */
785     canLayout : function() {
786         var el = this.getVisibilityEl();
787         return el && el.dom && !el.isStyle("display", "none");
788     },
789
790     <div id="method-Ext.Container-doLayout"></div>/**
791      * Force this container's layout to be recalculated. A call to this function is required after adding a new component
792      * to an already rendered container, or possibly after changing sizing/position properties of child components.
793      * @param {Boolean} shallow (optional) True to only calc the layout of this component, and let child components auto
794      * calc layouts as required (defaults to false, which calls doLayout recursively for each subcontainer)
795      * @param {Boolean} force (optional) True to force a layout to occur, even if the item is hidden.
796      * @return {Ext.Container} this
797      */
798
799     doLayout : function(shallow, force){
800         var rendered = this.rendered,
801             forceLayout = force || this.forceLayout;
802
803         if(this.collapsed || !this.canLayout()){
804             this.deferLayout = this.deferLayout || !shallow;
805             if(!forceLayout){
806                 return;
807             }
808             shallow = shallow && !this.deferLayout;
809         } else {
810             delete this.deferLayout;
811         }
812         if(rendered && this.layout){
813             this.layout.layout();
814         }
815         if(shallow !== true && this.items){
816             var cs = this.items.items;
817             for(var i = 0, len = cs.length; i < len; i++){
818                 var c = cs[i];
819                 if(c.doLayout){
820                     c.doLayout(false, forceLayout);
821                 }
822             }
823         }
824         if(rendered){
825             this.onLayout(shallow, forceLayout);
826         }
827         // Initial layout completed
828         this.hasLayout = true;
829         delete this.forceLayout;
830     },
831
832     onLayout : Ext.emptyFn,
833
834     // private
835     shouldBufferLayout: function(){
836         /*
837          * Returns true if the container should buffer a layout.
838          * This is true only if the container has previously been laid out
839          * and has a parent container that is pending a layout.
840          */
841         var hl = this.hasLayout;
842         if(this.ownerCt){
843             // Only ever buffer if we've laid out the first time and we have one pending.
844             return hl ? !this.hasLayoutPending() : false;
845         }
846         // Never buffer initial layout
847         return hl;
848     },
849
850     // private
851     hasLayoutPending: function(){
852         // Traverse hierarchy to see if any parent container has a pending layout.
853         var pending = false;
854         this.ownerCt.bubble(function(c){
855             if(c.layoutPending){
856                 pending = true;
857                 return false;
858             }
859         });
860         return pending;
861     },
862
863     onShow : function(){
864         // removes css classes that were added to hide
865         Ext.Container.superclass.onShow.call(this);
866         // If we were sized during the time we were hidden, layout.
867         if(Ext.isDefined(this.deferLayout)){
868             delete this.deferLayout;
869             this.doLayout(true);
870         }
871     },
872
873     <div id="method-Ext.Container-getLayout"></div>/**
874      * Returns the layout currently in use by the container.  If the container does not currently have a layout
875      * set, a default {@link Ext.layout.ContainerLayout} will be created and set as the container's layout.
876      * @return {ContainerLayout} layout The container's layout
877      */
878     getLayout : function(){
879         if(!this.layout){
880             var layout = new Ext.layout.AutoLayout(this.layoutConfig);
881             this.setLayout(layout);
882         }
883         return this.layout;
884     },
885
886     // private
887     beforeDestroy : function(){
888         var c;
889         if(this.items){
890             while(c = this.items.first()){
891                 this.doRemove(c, true);
892             }
893         }
894         if(this.monitorResize){
895             Ext.EventManager.removeResizeListener(this.doLayout, this);
896         }
897         Ext.destroy(this.layout);
898         Ext.Container.superclass.beforeDestroy.call(this);
899     },
900
901     <div id="method-Ext.Container-bubble"></div>/**
902      * Bubbles up the component/container heirarchy, calling the specified function with each component. The scope (<i>this</i>) of
903      * function call will be the scope provided or the current component. The arguments to the function
904      * will be the args provided or the current component. If the function returns false at any point,
905      * the bubble is stopped.
906      * @param {Function} fn The function to call
907      * @param {Object} scope (optional) The scope of the function (defaults to current node)
908      * @param {Array} args (optional) The args to call the function with (default to passing the current component)
909      * @return {Ext.Container} this
910      */
911     bubble : function(fn, scope, args){
912         var p = this;
913         while(p){
914             if(fn.apply(scope || p, args || [p]) === false){
915                 break;
916             }
917             p = p.ownerCt;
918         }
919         return this;
920     },
921
922     <div id="method-Ext.Container-cascade"></div>/**
923      * Cascades down the component/container heirarchy from this component (called first), calling the specified function with
924      * each component. The scope (<i>this</i>) of
925      * function call will be the scope provided or the current component. The arguments to the function
926      * will be the args provided or the current component. If the function returns false at any point,
927      * the cascade is stopped on that branch.
928      * @param {Function} fn The function to call
929      * @param {Object} scope (optional) The scope of the function (defaults to current component)
930      * @param {Array} args (optional) The args to call the function with (defaults to passing the current component)
931      * @return {Ext.Container} this
932      */
933     cascade : function(fn, scope, args){
934         if(fn.apply(scope || this, args || [this]) !== false){
935             if(this.items){
936                 var cs = this.items.items;
937                 for(var i = 0, len = cs.length; i < len; i++){
938                     if(cs[i].cascade){
939                         cs[i].cascade(fn, scope, args);
940                     }else{
941                         fn.apply(scope || cs[i], args || [cs[i]]);
942                     }
943                 }
944             }
945         }
946         return this;
947     },
948
949     <div id="method-Ext.Container-findById"></div>/**
950      * Find a component under this container at any level by id
951      * @param {String} id
952      * @return Ext.Component
953      */
954     findById : function(id){
955         var m, ct = this;
956         this.cascade(function(c){
957             if(ct != c && c.id === id){
958                 m = c;
959                 return false;
960             }
961         });
962         return m || null;
963     },
964
965     <div id="method-Ext.Container-findByType"></div>/**
966      * Find a component under this container at any level by xtype or class
967      * @param {String/Class} xtype The xtype string for a component, or the class of the component directly
968      * @param {Boolean} shallow (optional) False to check whether this Component is descended from the xtype (this is
969      * the default), or true to check whether this Component is directly of the specified xtype.
970      * @return {Array} Array of Ext.Components
971      */
972     findByType : function(xtype, shallow){
973         return this.findBy(function(c){
974             return c.isXType(xtype, shallow);
975         });
976     },
977
978     <div id="method-Ext.Container-find"></div>/**
979      * Find a component under this container at any level by property
980      * @param {String} prop
981      * @param {String} value
982      * @return {Array} Array of Ext.Components
983      */
984     find : function(prop, value){
985         return this.findBy(function(c){
986             return c[prop] === value;
987         });
988     },
989
990     <div id="method-Ext.Container-findBy"></div>/**
991      * Find a component under this container at any level by a custom function. If the passed function returns
992      * true, the component will be included in the results. The passed function is called with the arguments (component, this container).
993      * @param {Function} fn The function to call
994      * @param {Object} scope (optional)
995      * @return {Array} Array of Ext.Components
996      */
997     findBy : function(fn, scope){
998         var m = [], ct = this;
999         this.cascade(function(c){
1000             if(ct != c && fn.call(scope || c, c, ct) === true){
1001                 m.push(c);
1002             }
1003         });
1004         return m;
1005     },
1006
1007     <div id="method-Ext.Container-get"></div>/**
1008      * Get a component contained by this container (alias for items.get(key))
1009      * @param {String/Number} key The index or id of the component
1010      * @return {Ext.Component} Ext.Component
1011      */
1012     get : function(key){
1013         return this.items.get(key);
1014     }
1015 });
1016
1017 Ext.Container.LAYOUTS = {};
1018 Ext.reg('container', Ext.Container);
1019 </pre>    
1020 </body>
1021 </html>