/hippo/src/main/webapp/ext/src/widgets/Container.js

http://hdbc.googlecode.com/ · JavaScript · 894 lines · 290 code · 40 blank · 564 comment · 83 complexity · 0a7898c1e5a0c16bf39ca87cd189a63e MD5 · raw file

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