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
18 * A basic tab container. TabPanels can be used exactly like a standard {@link Ext.panel.Panel} for
19 * layout purposes, but also have special support for containing child Components
20 * (`{@link Ext.container.Container#items items}`) that are managed using a
21 * {@link Ext.layout.container.Card CardLayout layout manager}, and displayed as separate tabs.
23 * **Note:** By default, a tab's close tool _destroys_ the child tab Component and all its descendants.
24 * This makes the child tab Component, and all its descendants **unusable**. To enable re-use of a tab,
25 * configure the TabPanel with `{@link #autoDestroy autoDestroy: false}`.
27 * ## TabPanel's layout
29 * TabPanels use a Dock layout to position the {@link Ext.tab.Bar TabBar} at the top of the widget.
30 * Panels added to the TabPanel will have their header hidden by default because the Tab will
31 * automatically take the Panel's configured title and icon.
33 * TabPanels use their {@link Ext.panel.Header header} or {@link Ext.panel.Panel#fbar footer}
34 * element (depending on the {@link #tabPosition} configuration) to accommodate the tab selector buttons.
35 * This means that a TabPanel will not display any configured title, and will not display any configured
36 * header {@link Ext.panel.Panel#tools tools}.
38 * To display a header, embed the TabPanel in a {@link Ext.panel.Panel Panel} which uses
39 * `{@link Ext.container.Container#layout layout: 'fit'}`.
43 * Configuration options for the {@link Ext.tab.Tab} that represents the component can be passed in
44 * by specifying the tabConfig option:
47 * Ext.create('Ext.tab.Panel', {
50 * renderTo: document.body,
56 * title: 'Custom Title',
57 * tooltip: 'A button tooltip'
64 * Here is a basic TabPanel rendered to the body. This also shows the useful configuration {@link #activeTab},
65 * which allows you to set the active tab on render. If you do not set an {@link #activeTab}, no tabs will be
69 * Ext.create('Ext.tab.Panel', {
77 * html : 'A simple tab'
81 * html : 'Another one'
84 * renderTo : Ext.getBody()
87 * It is easy to control the visibility of items in the tab bar. Specify hidden: true to have the
88 * tab button hidden initially. Items can be subsequently hidden and show by accessing the
89 * tab property on the child item.
92 * var tabs = Ext.create('Ext.tab.Panel', {
95 * renderTo: document.body,
112 * setTimeout(function(){
113 * tabs.child('#home').tab.hide();
114 * var users = tabs.child('#users');
116 * tabs.setActiveTab(users);
119 * You can remove the background of the TabBar by setting the {@link #plain} property to `true`.
122 * Ext.create('Ext.tab.Panel', {
131 * html : 'A simple tab'
135 * html : 'Another one'
138 * renderTo : Ext.getBody()
141 * Another useful configuration of TabPanel is {@link #tabPosition}. This allows you to change the
142 * position where the tabs are displayed. The available options for this are `'top'` (default) and
146 * Ext.create('Ext.tab.Panel', {
151 * tabPosition: 'bottom',
155 * html : 'A simple tab'
159 * html : 'Another one'
162 * renderTo : Ext.getBody()
165 * The {@link #setActiveTab} is a very useful method in TabPanel which will allow you to change the
166 * current active tab. You can either give it an index or an instance of a tab. For example:
169 * var tabs = Ext.create('Ext.tab.Panel', {
174 * html : 'A simple tab'
178 * html : 'Another one'
181 * renderTo : Ext.getBody()
184 * var tab = Ext.getCmp('my-tab');
186 * Ext.create('Ext.button.Button', {
187 * renderTo: Ext.getBody(),
188 * text : 'Select the first tab',
190 * handler : function() {
191 * tabs.setActiveTab(tab);
195 * Ext.create('Ext.button.Button', {
196 * text : 'Select the second tab',
198 * handler : function() {
199 * tabs.setActiveTab(1);
201 * renderTo : Ext.getBody()
204 * 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'
215 * html : 'Another one'
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
232 * object for a panel.
235 * var tabs = Ext.create('Ext.tab.Panel', {
239 * html : 'A simple tab'
243 * html : 'Another one'
246 * renderTo : Ext.getBody()
249 * Ext.create('Ext.button.Button', {
252 * handler : function() {
253 * var tab = tabs.add({
254 * // we use the tabs.items property to get the length of current items/tabs
255 * title: 'Tab ' + (tabs.items.length + 1),
256 * html : 'Another one'
259 * tabs.setActiveTab(tab);
261 * renderTo : Ext.getBody()
264 * Additionally, removing a tab is very also simple with a TabPanel. You simple call the {@link #remove} method
265 * with an config object for a panel.
268 * var tabs = Ext.create('Ext.tab.Panel', {
272 * html : 'A simple tab'
275 * id : 'remove-this-tab',
277 * html : 'Another one'
280 * renderTo : Ext.getBody()
283 * Ext.create('Ext.button.Button', {
284 * text : 'Remove tab',
286 * handler : function() {
287 * var tab = Ext.getCmp('remove-this-tab');
290 * renderTo : Ext.getBody()
293 Ext.define('Ext.tab.Panel', {
294 extend: 'Ext.panel.Panel',
295 alias: 'widget.tabpanel',
296 alternateClassName: ['Ext.TabPanel'],
298 requires: ['Ext.layout.container.Card', 'Ext.tab.Bar'],
301 * @cfg {String} tabPosition
302 * The position where the tab strip should be rendered. Can be `top` or `bottom`.
307 * @cfg {String/Number} activeItem
308 * Doesn't apply for {@link Ext.tab.Panel TabPanel}, use {@link #activeTab} instead.
312 * @cfg {String/Number/Ext.Component} activeTab
313 * The tab to activate initially. Either an ID, index or the tab component itself.
317 * @cfg {Object} tabBar
318 * Optional configuration object for the internal {@link Ext.tab.Bar}.
319 * If present, this is passed straight through to the TabBar's constructor
323 * @cfg {Object} layout
324 * Optional configuration object for the internal {@link Ext.layout.container.Card card layout}.
325 * If present, this is passed straight through to the layout's constructor
329 * @cfg {Boolean} removePanelHeader
330 * True to instruct each Panel added to the TabContainer to not render its header element.
331 * This is to ensure that the title of the panel does not appear twice.
333 removePanelHeader: true,
336 * @cfg {Boolean} plain
337 * True to not show the full background on the TabBar.
342 * @cfg {String} itemCls
343 * The class added to each child item of this TabPanel.
345 itemCls: 'x-tabpanel-child',
348 * @cfg {Number} minTabWidth
349 * The minimum width for a tab in the {@link #tabBar}.
351 minTabWidth: undefined,
354 * @cfg {Number} maxTabWidth The maximum width for each tab.
356 maxTabWidth: undefined,
359 * @cfg {Boolean} deferredRender
361 * True by default to defer the rendering of child {@link Ext.container.Container#items items} to the browsers DOM
362 * until a tab is activated. False will render all contained {@link Ext.container.Container#items items} as soon as
363 * the {@link Ext.layout.container.Card layout} is rendered. If there is a significant amount of content or a lot of
364 * heavy controls being rendered into panels that are not displayed by default, setting this to true might improve
367 * The deferredRender property is internally passed to the layout manager for TabPanels ({@link
368 * Ext.layout.container.Card}) as its {@link Ext.layout.container.Card#deferredRender} configuration value.
370 * **Note**: leaving deferredRender as true means that the content within an unactivated tab will not be available
372 deferredRender : true,
375 initComponent: function() {
377 dockedItems = me.dockedItems || [],
378 activeTab = me.activeTab || 0;
380 me.layout = Ext.create('Ext.layout.container.Card', Ext.apply({
382 deferredRender: me.deferredRender,
387 * @property {Ext.tab.Bar} tabBar Internal reference to the docked TabBar
389 me.tabBar = Ext.create('Ext.tab.Bar', Ext.apply({}, me.tabBar, {
390 dock: me.tabPosition,
393 cardLayout: me.layout,
397 if (dockedItems && !Ext.isArray(dockedItems)) {
398 dockedItems = [dockedItems];
401 dockedItems.push(me.tabBar);
402 me.dockedItems = dockedItems;
407 * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
409 * @param {Ext.tab.Panel} tabPanel The TabPanel
410 * @param {Ext.Component} newCard The card that is about to be activated
411 * @param {Ext.Component} oldCard The card that is currently active
417 * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
418 * @param {Ext.tab.Panel} tabPanel The TabPanel
419 * @param {Ext.Component} newCard The newly activated item
420 * @param {Ext.Component} oldCard The previously active item
424 me.callParent(arguments);
427 me.setActiveTab(activeTab);
428 //set the active tab after initial layout
429 me.on('afterlayout', me.afterInitialLayout, me, {single: true});
434 * We have to wait until after the initial layout to visually activate the activeTab (if set).
435 * The active tab has different margins than normal tabs, so if the initial layout happens with
436 * a tab active, its layout will be offset improperly due to the active margin style. Waiting
437 * until after the initial layout avoids this issue.
439 afterInitialLayout: function() {
441 card = me.getComponent(me.activeTab);
444 me.layout.setActiveItem(card);
449 * Makes the given card active. Makes it the visible card in the TabPanel's CardLayout and highlights the Tab.
450 * @param {String/Number/Ext.Component} card The card to make active. Either an ID, index or the component itself.
452 setActiveTab: function(card) {
456 card = me.getComponent(card);
458 previous = me.getActiveTab();
460 if (previous && previous !== card && me.fireEvent('beforetabchange', me, card, previous) === false) {
464 me.tabBar.setActiveTab(card.tab);
467 me.layout.setActiveItem(card);
470 if (previous && previous !== card) {
471 me.fireEvent('tabchange', me, card, previous);
477 * Returns the item that is currently active inside this TabPanel. Note that before the TabPanel first activates a
478 * child component this will return whatever was configured in the {@link #activeTab} config option
479 * @return {String/Number/Ext.Component} The currently active item
481 getActiveTab: function() {
482 return this.activeTab;
486 * Returns the {@link Ext.tab.Bar} currently used in this TabPanel
487 * @return {Ext.tab.Bar} The TabBar
489 getTabBar: function() {
495 * Makes sure we have a Tab for each item added to the TabPanel
497 onAdd: function(item, index) {
499 cfg = item.tabConfig || {},
503 disabled: item.disabled,
504 closable: item.closable,
509 if (item.closeText) {
510 defaultConfig.closeText = item.closeText;
512 cfg = Ext.applyIf(cfg, defaultConfig);
513 item.tab = me.tabBar.insert(index, cfg);
517 enable: me.onItemEnable,
518 disable: me.onItemDisable,
519 beforeshow: me.onItemBeforeShow,
520 iconchange: me.onItemIconChange,
521 titlechange: me.onItemTitleChange
525 if (me.removePanelHeader) {
526 item.preventHeader = true;
531 if (item.isPanel && me.border) {
532 item.setBorder(false);
536 // ensure that there is at least one active tab
537 if (this.rendered && me.items.getCount() === 1) {
544 * Enable corresponding tab when item is enabled.
546 onItemEnable: function(item){
552 * Disable corresponding tab when item is enabled.
554 onItemDisable: function(item){
560 * Sets activeTab before item is shown.
562 onItemBeforeShow: function(item) {
563 if (item !== this.activeTab) {
564 this.setActiveTab(item);
571 * Update the tab iconCls when panel iconCls has been set or changed.
573 onItemIconChange: function(item, newIconCls) {
574 item.tab.setIconCls(newIconCls);
575 this.getTabBar().doLayout();
580 * Update the tab title when panel title has been set or changed.
582 onItemTitleChange: function(item, newTitle) {
583 item.tab.setText(newTitle);
584 this.getTabBar().doLayout();
590 * If we're removing the currently active tab, activate the nearest one. The item is removed when we call super,
591 * so we can do preprocessing before then to find the card's index
593 doRemove: function(item, autoDestroy) {
596 // At this point the item hasn't been removed from the items collection.
597 // As such, if we want to check if there are no more tabs left, we have to
598 // check for one, as opposed to 0.
599 hasItemsLeft = items.getCount() > 1;
601 if (me.destroying || !hasItemsLeft) {
603 } else if (item === me.activeTab) {
604 me.setActiveTab(item.next() || items.getAt(0));
606 me.callParent(arguments);
608 // Remove the two references
609 delete item.tab.card;
615 * Makes sure we remove the corresponding Tab when an item is removed
617 onRemove: function(item, autoDestroy) {
622 enable: me.onItemEnable,
623 disable: me.onItemDisable,
624 beforeshow: me.onItemBeforeShow
626 if (!me.destroying && item.tab.ownerCt == me.tabBar) {
627 me.tabBar.remove(item.tab);