3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @author Ed Spencer, Tommy Maintz, Brian Moeskau
17 * @class Ext.tab.Panel
18 * @extends Ext.panel.Panel
20 A basic tab container. TabPanels can be used exactly like a standard {@link Ext.panel.Panel} for layout purposes, but also
21 have special support for containing child Components (`{@link Ext.container.Container#items items}`) that are managed
22 using a {@link Ext.layout.container.Card CardLayout layout manager}, and displayed as separate tabs.
26 By default, a tab's close tool _destroys_ the child tab Component and all its descendants. This makes the child tab
27 Component, and all its descendants __unusable__. To enable re-use of a tab, configure the TabPanel with `{@link #autoDestroy autoDestroy: false}`.
29 __TabPanel's layout:__
31 TabPanels use a Dock layout to position the {@link Ext.tab.Bar TabBar} at the top of the widget. Panels added to the TabPanel will have their
32 header hidden by default because the Tab will automatically take the Panel's configured title and icon.
34 TabPanels use their {@link Ext.panel.Panel#header header} or {@link Ext.panel.Panel#footer footer} element (depending on the {@link #tabPosition}
35 configuration) to accommodate the tab selector buttons. This means that a TabPanel will not display any configured title, and will not display any
36 configured header {@link Ext.panel.Panel#tools tools}.
38 To display a header, embed the TabPanel in a {@link Ext.panel.Panel Panel} which uses `{@link Ext.container.Container#layout layout:'fit'}`.
41 Configuration options for the {@link Ext.tab.Tab} that represents the component can be passed in by specifying the tabConfig option:
43 Ext.create('Ext.tab.Panel', {
46 renderTo: document.body,
52 title: 'Custom Title',
53 tooltip: 'A button tooltip'
60 Here is a basic TabPanel rendered to the body. This also shows the useful configuration {@link #activeTab}, which allows you to set the active tab on render.
61 If you do not set an {@link #activeTab}, no tabs will be active by default.
62 {@img Ext.tab.Panel/Ext.tab.Panel1.png TabPanel component}
65 Ext.create('Ext.tab.Panel', {
80 renderTo : Ext.getBody()
83 It is easy to control the visibility of items in the tab bar. Specify hidden: true to have the
84 tab button hidden initially. Items can be subsequently hidden and show by accessing the
85 tab property on the child item.
89 var tabs = Ext.create('Ext.tab.Panel', {
92 renderTo: document.body,
109 setTimeout(function(){
110 tabs.child('#home').tab.hide();
111 var users = tabs.child('#users');
113 tabs.setActiveTab(users);
116 You can remove the background of the TabBar by setting the {@link #plain} property to `true`.
120 Ext.create('Ext.tab.Panel', {
129 html : 'A simple tab'
136 renderTo : Ext.getBody()
139 Another useful configuration of TabPanel is {@link #tabPosition}. This allows you to change the position where the tabs are displayed. The available
140 options for this are `'top'` (default) and `'bottom'`.
141 {@img Ext.tab.Panel/Ext.tab.Panel2.png TabPanel component}
144 Ext.create('Ext.tab.Panel', {
149 tabPosition: 'bottom',
153 html : 'A simple tab'
160 renderTo : Ext.getBody()
163 The {@link #setActiveTab} is a very useful method in TabPanel which will allow you to change the current active tab. You can either give it an index or
164 an instance of a tab.
168 var tabs = Ext.create('Ext.tab.Panel', {
173 html : 'A simple tab'
180 renderTo : Ext.getBody()
183 var tab = Ext.getCmp('my-tab');
185 Ext.create('Ext.button.Button', {
186 renderTo: Ext.getBody(),
187 text : 'Select the first tab',
189 handler : function() {
190 tabs.setActiveTab(tab);
194 Ext.create('Ext.button.Button', {
195 text : 'Select the second tab',
197 handler : function() {
198 tabs.setActiveTab(1);
200 renderTo : Ext.getBody()
203 The {@link #getActiveTab} is a another useful method in TabPanel which will return the current active tab.
207 var tabs = Ext.create('Ext.tab.Panel', {
211 html : 'A simple tab'
218 renderTo : Ext.getBody()
221 Ext.create('Ext.button.Button', {
222 text : 'Get active tab',
224 handler : function() {
225 var tab = tabs.getActiveTab();
226 alert('Current tab: ' + tab.title);
228 renderTo : Ext.getBody()
231 Adding a new tab is very simple with a TabPanel. You simple call the {@link #add} method with an config object for a panel.
235 var tabs = Ext.Create('Ext.tab.Panel', {
239 html : 'A simple tab'
246 renderTo : Ext.getBody()
249 Ext.create('Ext.button.Button', {
252 handler : function() {
254 title: 'Tab ' + (tabs.items.length + 1), //we use the tabs.items property to get the length of current items/tabs
258 tabs.setActiveTab(tab);
260 renderTo : Ext.getBody()
263 Additionally, removing a tab is very also simple with a TabPanel. You simple call the {@link #remove} method with an config object for a panel.
267 var tabs = Ext.Create('Ext.tab.Panel', {
271 html : 'A simple tab'
274 id : 'remove-this-tab',
279 renderTo : Ext.getBody()
282 Ext.Create('Ext.button.Button', {
285 handler : function() {
286 var tab = Ext.getCmp('remove-this-tab');
289 renderTo : Ext.getBody()
295 Ext.define('Ext.tab.Panel', {
296 extend: 'Ext.panel.Panel',
297 alias: 'widget.tabpanel',
298 alternateClassName: ['Ext.TabPanel'],
300 requires: ['Ext.layout.container.Card', 'Ext.tab.Bar'],
303 * @cfg {String} tabPosition The position where the tab strip should be rendered (defaults to <code>'top'</code>).
304 * In 4.0, The only other supported value is <code>'bottom'</code>.
309 * @cfg {Object} tabBar Optional configuration object for the internal {@link Ext.tab.Bar}. If present, this is
310 * passed straight through to the TabBar's constructor
314 * @cfg {Object} layout Optional configuration object for the internal {@link Ext.layout.container.Card card layout}.
315 * If present, this is passed straight through to the layout's constructor
319 * @cfg {Boolean} removePanelHeader True to instruct each Panel added to the TabContainer to not render its header
320 * element. This is to ensure that the title of the panel does not appear twice. Defaults to true.
322 removePanelHeader: true,
326 * True to not show the full background on the TabBar
331 * @cfg {String} itemCls The class added to each child item of this TabPanel. Defaults to 'x-tabpanel-child'.
333 itemCls: 'x-tabpanel-child',
336 * @cfg {Number} minTabWidth The minimum width for a tab in the {@link #tabBar}. Defaults to <code>30</code>.
340 * @cfg {Boolean} deferredRender
341 * <p><tt>true</tt> by default to defer the rendering of child <tt>{@link Ext.container.Container#items items}</tt>
342 * to the browsers DOM until a tab is activated. <tt>false</tt> will render all contained
343 * <tt>{@link Ext.container.Container#items items}</tt> as soon as the {@link Ext.layout.container.Card layout}
344 * is rendered. If there is a significant amount of content or a lot of heavy controls being
345 * rendered into panels that are not displayed by default, setting this to <tt>true</tt> might
346 * improve performance.</p>
347 * <br><p>The <tt>deferredRender</tt> property is internally passed to the layout manager for
348 * TabPanels ({@link Ext.layout.container.Card}) as its {@link Ext.layout.container.Card#deferredRender}
349 * configuration value.</p>
350 * <br><p><b>Note</b>: leaving <tt>deferredRender</tt> as <tt>true</tt> means that the content
351 * within an unactivated tab will not be available</p>
353 deferredRender : true,
356 initComponent: function() {
358 dockedItems = me.dockedItems || [],
359 activeTab = me.activeTab || 0;
361 me.layout = Ext.create('Ext.layout.container.Card', Ext.apply({
363 deferredRender: me.deferredRender,
370 * Internal reference to the docked TabBar
372 me.tabBar = Ext.create('Ext.tab.Bar', Ext.apply({}, me.tabBar, {
373 dock: me.tabPosition,
376 cardLayout: me.layout,
380 if (dockedItems && !Ext.isArray(dockedItems)) {
381 dockedItems = [dockedItems];
384 dockedItems.push(me.tabBar);
385 me.dockedItems = dockedItems;
389 * @event beforetabchange
390 * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
392 * @param {Ext.tab.Panel} tabPanel The TabPanel
393 * @param {Ext.Component} newCard The card that is about to be activated
394 * @param {Ext.Component} oldCard The card that is currently active
400 * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
401 * @param {Ext.tab.Panel} tabPanel The TabPanel
402 * @param {Ext.Component} newCard The newly activated item
403 * @param {Ext.Component} oldCard The previously active item
407 me.callParent(arguments);
410 me.setActiveTab(activeTab);
411 //set the active tab after initial layout
412 me.on('afterlayout', me.afterInitialLayout, me, {single: true});
417 * We have to wait until after the initial layout to visually activate the activeTab (if set).
418 * The active tab has different margins than normal tabs, so if the initial layout happens with
419 * a tab active, its layout will be offset improperly due to the active margin style. Waiting
420 * until after the initial layout avoids this issue.
422 afterInitialLayout: function() {
424 card = me.getComponent(me.activeTab);
427 me.layout.setActiveItem(card);
432 * Makes the given card active (makes it the visible card in the TabPanel's CardLayout and highlights the Tab)
433 * @param {Ext.Component} card The card to make active
435 setActiveTab: function(card) {
439 card = me.getComponent(card);
441 previous = me.getActiveTab();
443 if (previous && previous !== card && me.fireEvent('beforetabchange', me, card, previous) === false) {
447 me.tabBar.setActiveTab(card.tab);
450 me.layout.setActiveItem(card);
453 if (previous && previous !== card) {
454 me.fireEvent('tabchange', me, card, previous);
460 * Returns the item that is currently active inside this TabPanel. Note that before the TabPanel first activates a
461 * child component this will return whatever was configured in the {@link #activeTab} config option
462 * @return {Ext.Component/Integer} The currently active item
464 getActiveTab: function() {
465 return this.activeTab;
469 * Returns the {@link Ext.tab.Bar} currently used in this TabPanel
470 * @return {Ext.TabBar} The TabBar
472 getTabBar: function() {
478 * Makes sure we have a Tab for each item added to the TabPanel
480 onAdd: function(item, index) {
482 cfg = item.tabConfig || {},
486 disabled: item.disabled,
487 closable: item.closable,
492 if (item.closeText) {
493 defaultConfig.closeText = item.closeText;
495 cfg = Ext.applyIf(cfg, defaultConfig);
496 item.tab = me.tabBar.insert(index, cfg);
500 enable: me.onItemEnable,
501 disable: me.onItemDisable,
502 beforeshow: me.onItemBeforeShow,
503 iconchange: me.onItemIconChange,
504 titlechange: me.onItemTitleChange
508 if (me.removePanelHeader) {
509 item.preventHeader = true;
514 if (item.isPanel && me.border) {
515 item.setBorder(false);
519 // ensure that there is at least one active tab
520 if (this.rendered && me.items.getCount() === 1) {
527 * Enable corresponding tab when item is enabled.
529 onItemEnable: function(item){
535 * Disable corresponding tab when item is enabled.
537 onItemDisable: function(item){
543 * Sets activeTab before item is shown.
545 onItemBeforeShow: function(item) {
546 if (item !== this.activeTab) {
547 this.setActiveTab(item);
554 * Update the tab iconCls when panel iconCls has been set or changed.
556 onItemIconChange: function(item, newIconCls) {
557 item.tab.setIconCls(newIconCls);
558 this.getTabBar().doLayout();
563 * Update the tab title when panel title has been set or changed.
565 onItemTitleChange: function(item, newTitle) {
566 item.tab.setText(newTitle);
567 this.getTabBar().doLayout();
573 * If we're removing the currently active tab, activate the nearest one. The item is removed when we call super,
574 * so we can do preprocessing before then to find the card's index
576 doRemove: function(item, autoDestroy) {
580 * At this point the item hasn't been removed from the items collection.
581 * As such, if we want to check if there are no more tabs left, we have to
582 * check for one, as opposed to 0.
584 hasItemsLeft = items.getCount() > 1;
586 if (me.destroying || !hasItemsLeft) {
588 } else if (item === me.activeTab) {
589 me.setActiveTab(item.next() || items.getAt(0));
591 me.callParent(arguments);
593 // Remove the two references
594 delete item.tab.card;
600 * Makes sure we remove the corresponding Tab when an item is removed
602 onRemove: function(item, autoDestroy) {
607 enable: me.onItemEnable,
608 disable: me.onItemDisable,
609 beforeshow: me.onItemBeforeShow
611 if (!me.destroying && item.tab.ownerCt == me.tabBar) {
612 me.tabBar.remove(item.tab);