~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/extjs/source/widgets/Container.js

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

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