4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-tab-Panel-method-constructor'><span id='Ext-tab-Panel'>/**
19 </span></span> * @author Ed Spencer, Tommy Maintz, Brian Moeskau
20 * @class Ext.tab.Panel
21 * @extends Ext.panel.Panel
23 A basic tab container. TabPanels can be used exactly like a standard {@link Ext.panel.Panel} for layout purposes, but also
24 have special support for containing child Components (`{@link Ext.container.Container#items items}`) that are managed
25 using a {@link Ext.layout.container.Card CardLayout layout manager}, and displayed as separate tabs.
29 By default, a tab's close tool _destroys_ the child tab Component and all its descendants. This makes the child tab
30 Component, and all its descendants __unusable__. To enable re-use of a tab, configure the TabPanel with `{@link #autoDestroy autoDestroy: false}`.
32 __TabPanel's layout:__
34 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
35 header hidden by default because the Tab will automatically take the Panel's configured title and icon.
37 TabPanels use their {@link Ext.panel.Panel#header header} or {@link Ext.panel.Panel#footer footer} element (depending on the {@link #tabPosition}
38 configuration) to accommodate the tab selector buttons. This means that a TabPanel will not display any configured title, and will not display any
39 configured header {@link Ext.panel.Panel#tools tools}.
41 To display a header, embed the TabPanel in a {@link Ext.panel.Panel Panel} which uses `{@link Ext.container.Container#layout layout:'fit'}`.
45 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.
46 If you do not set an {@link #activeTab}, no tabs will be active by default.
47 {@img Ext.tab.Panel/Ext.tab.Panel1.png TabPanel component}
50 Ext.create('Ext.tab.Panel', {
65 renderTo : Ext.getBody()
68 It is easy to control the visibility of items in the tab bar. Specify hidden: true to have the
69 tab button hidden initially. Items can be subsequently hidden and show by accessing the
70 tab property on the child item.
74 var tabs = Ext.create('Ext.tab.Panel', {
77 renderTo: document.body,
94 setTimeout(function(){
95 tabs.child('#home').tab.hide();
96 var users = tabs.child('#users');
98 tabs.setActiveTab(users);
101 You can remove the background of the TabBar by setting the {@link #plain} property to `false`.
105 Ext.create('Ext.tab.Panel', {
114 html : 'A simple tab'
121 renderTo : Ext.getBody()
124 Another useful configuration of TabPanel is {@link #tabPosition}. This allows you to change the position where the tabs are displayed. The available
125 options for this are `'top'` (default) and `'bottom'`.
126 {@img Ext.tab.Panel/Ext.tab.Panel2.png TabPanel component}
129 Ext.create('Ext.tab.Panel', {
134 tabPosition: 'bottom',
138 html : 'A simple tab'
145 renderTo : Ext.getBody()
148 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
149 an instance of a tab.
153 var tabs = Ext.create('Ext.tab.Panel', {
158 html : 'A simple tab'
165 renderTo : Ext.getBody()
168 var tab = Ext.getCmp('my-tab');
170 Ext.create('Ext.button.Button', {
171 renderTo: Ext.getBody(),
172 text : 'Select the first tab',
174 handler : function() {
175 tabs.setActiveTab(tab);
179 Ext.create('Ext.button.Button', {
180 text : 'Select the second tab',
182 handler : function() {
183 tabs.setActiveTab(1);
185 renderTo : Ext.getBody()
188 The {@link #getActiveTab} is a another useful method in TabPanel which will return the current active tab.
192 var tabs = Ext.create('Ext.tab.Panel', {
196 html : 'A simple tab'
203 renderTo : Ext.getBody()
206 Ext.create('Ext.button.Button', {
207 text : 'Get active tab',
209 handler : function() {
210 var tab = tabs.getActiveTab();
211 alert('Current tab: ' + tab.title);
213 renderTo : Ext.getBody()
216 Adding a new tab is very simple with a TabPanel. You simple call the {@link #add} method with an config object for a panel.
220 var tabs = Ext.Create('Ext.tab.Panel', {
224 html : 'A simple tab'
231 renderTo : Ext.getBody()
234 Ext.create('Ext.button.Button', {
237 handler : function() {
239 title: 'Tab ' + (tabs.items.length + 1), //we use the tabs.items property to get the length of current items/tabs
243 tabs.setActiveTab(tab);
245 renderTo : Ext.getBody()
248 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.
252 var tabs = Ext.Create('Ext.tab.Panel', {
256 html : 'A simple tab'
259 id : 'remove-this-tab',
264 renderTo : Ext.getBody()
267 Ext.Create('Ext.button.Button', {
270 handler : function() {
271 var tab = Ext.getCmp('remove-this-tab');
274 renderTo : Ext.getBody()
279 * @param {Object} config The configuration options
283 Ext.define('Ext.tab.Panel', {
284 extend: 'Ext.panel.Panel',
285 alias: 'widget.tabpanel',
286 alternateClassName: ['Ext.TabPanel'],
288 requires: ['Ext.layout.container.Card', 'Ext.tab.Bar'],
290 <span id='Ext-tab-Panel-cfg-tabPosition'> /**
291 </span> * @cfg {String} tabPosition The position where the tab strip should be rendered (defaults to <code>'top'</code>).
292 * In 4.0, The only other supported value is <code>'bottom'</code>.
296 <span id='Ext-tab-Panel-cfg-tabBar'> /**
297 </span> * @cfg {Object} tabBar Optional configuration object for the internal {@link Ext.tab.Bar}. If present, this is
298 * passed straight through to the TabBar's constructor
301 <span id='Ext-tab-Panel-cfg-layout'> /**
302 </span> * @cfg {Object} layout Optional configuration object for the internal {@link Ext.layout.container.Card card layout}.
303 * If present, this is passed straight through to the layout's constructor
306 <span id='Ext-tab-Panel-cfg-removePanelHeader'> /**
307 </span> * @cfg {Boolean} removePanelHeader True to instruct each Panel added to the TabContainer to not render its header
308 * element. This is to ensure that the title of the panel does not appear twice. Defaults to true.
310 removePanelHeader: true,
312 <span id='Ext-tab-Panel-cfg-Boolean'> /**
313 </span> * @cfg Boolean plain
314 * True to not show the full background on the TabBar
318 <span id='Ext-tab-Panel-cfg-itemCls'> /**
319 </span> * @cfg {String} itemCls The class added to each child item of this TabPanel. Defaults to 'x-tabpanel-child'.
321 itemCls: 'x-tabpanel-child',
323 <span id='Ext-tab-Panel-cfg-minTabWidth'> /**
324 </span> * @cfg {Number} minTabWidth The minimum width for a tab in the {@link #tabBar}. Defaults to <code>30</code>.
327 <span id='Ext-tab-Panel-cfg-deferredRender'> /**
328 </span> * @cfg {Boolean} deferredRender
329 * <p><tt>true</tt> by default to defer the rendering of child <tt>{@link Ext.container.Container#items items}</tt>
330 * to the browsers DOM until a tab is activated. <tt>false</tt> will render all contained
331 * <tt>{@link Ext.container.Container#items items}</tt> as soon as the {@link Ext.layout.container.Card layout}
332 * is rendered. If there is a significant amount of content or a lot of heavy controls being
333 * rendered into panels that are not displayed by default, setting this to <tt>true</tt> might
334 * improve performance.</p>
335 * <br><p>The <tt>deferredRender</tt> property is internally passed to the layout manager for
336 * TabPanels ({@link Ext.layout.container.Card}) as its {@link Ext.layout.container.Card#deferredRender}
337 * configuration value.</p>
338 * <br><p><b>Note</b>: leaving <tt>deferredRender</tt> as <tt>true</tt> means that the content
339 * within an unactivated tab will not be available</p>
341 deferredRender : true,
344 initComponent: function() {
346 dockedItems = me.dockedItems || [],
347 activeTab = me.activeTab || 0;
349 me.layout = Ext.create('Ext.layout.container.Card', Ext.apply({
351 deferredRender: me.deferredRender,
355 <span id='Ext-tab-Panel-property-tabBar'> /**
356 </span> * @property tabBar
358 * Internal reference to the docked TabBar
360 me.tabBar = Ext.create('Ext.tab.Bar', Ext.apply({}, me.tabBar, {
361 dock: me.tabPosition,
364 cardLayout: me.layout,
368 if (dockedItems && !Ext.isArray(dockedItems)) {
369 dockedItems = [dockedItems];
372 dockedItems.push(me.tabBar);
373 me.dockedItems = dockedItems;
376 <span id='Ext-tab-Panel-event-beforetabchange'> /**
377 </span> * @event beforetabchange
378 * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
380 * @param {Ext.tab.Panel} tabPanel The TabPanel
381 * @param {Ext.Component} newCard The card that is about to be activated
382 * @param {Ext.Component} oldCard The card that is currently active
386 <span id='Ext-tab-Panel-event-tabchange'> /**
387 </span> * @event tabchange
388 * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
389 * @param {Ext.tab.Panel} tabPanel The TabPanel
390 * @param {Ext.Component} newCard The newly activated item
391 * @param {Ext.Component} oldCard The previously active item
395 me.callParent(arguments);
398 me.setActiveTab(activeTab);
399 //set the active tab after initial layout
400 me.on('afterlayout', me.afterInitialLayout, me, {single: true});
403 <span id='Ext-tab-Panel-method-afterInitialLayout'> /**
405 * We have to wait until after the initial layout to visually activate the activeTab (if set).
406 * The active tab has different margins than normal tabs, so if the initial layout happens with
407 * a tab active, its layout will be offset improperly due to the active margin style. Waiting
408 * until after the initial layout avoids this issue.
410 afterInitialLayout: function() {
412 card = me.getComponent(me.activeTab);
415 me.layout.setActiveItem(card);
419 <span id='Ext-tab-Panel-method-setActiveTab'> /**
420 </span> * Makes the given card active (makes it the visible card in the TabPanel's CardLayout and highlights the Tab)
421 * @param {Ext.Component} card The card to make active
423 setActiveTab: function(card) {
427 card = me.getComponent(card);
429 previous = me.getActiveTab();
431 if (previous && previous !== card && me.fireEvent('beforetabchange', me, card, previous) === false) {
435 me.tabBar.setActiveTab(card.tab);
438 me.layout.setActiveItem(card);
441 if (previous && previous !== card) {
442 me.fireEvent('tabchange', me, card, previous);
447 <span id='Ext-tab-Panel-method-getActiveTab'> /**
448 </span> * Returns the item that is currently active inside this TabPanel. Note that before the TabPanel first activates a
449 * child component this will return whatever was configured in the {@link #activeTab} config option
450 * @return {Ext.Component/Integer} The currently active item
452 getActiveTab: function() {
453 return this.activeTab;
456 <span id='Ext-tab-Panel-method-getTabBar'> /**
457 </span> * Returns the {@link Ext.tab.Bar} currently used in this TabPanel
458 * @return {Ext.TabBar} The TabBar
460 getTabBar: function() {
464 <span id='Ext-tab-Panel-method-onAdd'> /**
466 * Makes sure we have a Tab for each item added to the TabPanel
468 onAdd: function(item, index) {
471 item.tab = me.tabBar.insert(index, {
474 disabled: item.disabled,
475 closable: item.closable,
482 enable: me.onItemEnable,
483 disable: me.onItemDisable,
484 beforeshow: me.onItemBeforeShow,
485 iconchange: me.onItemIconChange,
486 titlechange: me.onItemTitleChange
490 if (me.removePanelHeader) {
491 item.preventHeader = true;
496 if (item.isPanel && me.border) {
497 item.setBorder(false);
501 // ensure that there is at least one active tab
502 if (this.rendered && me.items.getCount() === 1) {
507 <span id='Ext-tab-Panel-method-onItemEnable'> /**
509 * Enable corresponding tab when item is enabled.
511 onItemEnable: function(item){
515 <span id='Ext-tab-Panel-method-onItemDisable'> /**
517 * Disable corresponding tab when item is enabled.
519 onItemDisable: function(item){
523 <span id='Ext-tab-Panel-method-onItemBeforeShow'> /**
525 * Sets activeTab before item is shown.
527 onItemBeforeShow: function(item) {
528 if (item !== this.activeTab) {
529 this.setActiveTab(item);
534 <span id='Ext-tab-Panel-method-onItemIconChange'> /**
536 * Update the tab iconCls when panel iconCls has been set or changed.
538 onItemIconChange: function(item, newIconCls) {
539 item.tab.setIconCls(newIconCls);
540 this.getTabBar().doLayout();
543 <span id='Ext-tab-Panel-method-onItemTitleChange'> /**
545 * Update the tab title when panel title has been set or changed.
547 onItemTitleChange: function(item, newTitle) {
548 item.tab.setText(newTitle);
549 this.getTabBar().doLayout();
553 <span id='Ext-tab-Panel-method-doRemove'> /**
555 * If we're removing the currently active tab, activate the nearest one. The item is removed when we call super,
556 * so we can do preprocessing before then to find the card's index
558 doRemove: function(item, autoDestroy) {
561 <span id='Ext-tab-Panel-property-hasItemsLeft'> /**
562 </span> * At this point the item hasn't been removed from the items collection.
563 * As such, if we want to check if there are no more tabs left, we have to
564 * check for one, as opposed to 0.
566 hasItemsLeft = items.getCount() > 1;
568 if (me.destroying || !hasItemsLeft) {
570 } else if (item === me.activeTab) {
571 me.setActiveTab(item.next() || items.getAt(0));
573 me.callParent(arguments);
575 // Remove the two references
576 delete item.tab.card;
580 <span id='Ext-tab-Panel-method-onRemove'> /**
582 * Makes sure we remove the corresponding Tab when an item is removed
584 onRemove: function(item, autoDestroy) {
589 enable: me.onItemEnable,
590 disable: me.onItemDisable,
591 beforeshow: me.onItemBeforeShow
593 if (!me.destroying && item.tab.ownerCt == me.tabBar) {
594 me.tabBar.remove(item.tab);