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'>/**
19 </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'}`.
44 Configuration options for the {@link Ext.tab.Tab} that represents the component can be passed in by specifying the tabConfig option:
46 Ext.create('Ext.tab.Panel', {
49 renderTo: document.body,
55 title: 'Custom Title',
56 tooltip: 'A button tooltip'
63 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.
64 If you do not set an {@link #activeTab}, no tabs will be active by default.
65 {@img Ext.tab.Panel/Ext.tab.Panel1.png TabPanel component}
68 Ext.create('Ext.tab.Panel', {
83 renderTo : Ext.getBody()
86 It is easy to control the visibility of items in the tab bar. Specify hidden: true to have the
87 tab button hidden initially. Items can be subsequently hidden and show by accessing the
88 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`.
123 Ext.create('Ext.tab.Panel', {
132 html : 'A simple tab'
139 renderTo : Ext.getBody()
142 Another useful configuration of TabPanel is {@link #tabPosition}. This allows you to change the position where the tabs are displayed. The available
143 options for this are `'top'` (default) and `'bottom'`.
144 {@img Ext.tab.Panel/Ext.tab.Panel2.png TabPanel component}
147 Ext.create('Ext.tab.Panel', {
152 tabPosition: 'bottom',
156 html : 'A simple tab'
163 renderTo : Ext.getBody()
166 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
167 an instance of a tab.
171 var tabs = Ext.create('Ext.tab.Panel', {
176 html : 'A simple tab'
183 renderTo : Ext.getBody()
186 var tab = Ext.getCmp('my-tab');
188 Ext.create('Ext.button.Button', {
189 renderTo: Ext.getBody(),
190 text : 'Select the first tab',
192 handler : function() {
193 tabs.setActiveTab(tab);
197 Ext.create('Ext.button.Button', {
198 text : 'Select the second tab',
200 handler : function() {
201 tabs.setActiveTab(1);
203 renderTo : Ext.getBody()
206 The {@link #getActiveTab} is a another useful method in TabPanel which will return the current active tab.
210 var tabs = Ext.create('Ext.tab.Panel', {
214 html : 'A simple tab'
221 renderTo : Ext.getBody()
224 Ext.create('Ext.button.Button', {
225 text : 'Get active tab',
227 handler : function() {
228 var tab = tabs.getActiveTab();
229 alert('Current tab: ' + tab.title);
231 renderTo : Ext.getBody()
234 Adding a new tab is very simple with a TabPanel. You simple call the {@link #add} method with an config object for a panel.
238 var tabs = Ext.Create('Ext.tab.Panel', {
242 html : 'A simple tab'
249 renderTo : Ext.getBody()
252 Ext.create('Ext.button.Button', {
255 handler : function() {
257 title: 'Tab ' + (tabs.items.length + 1), //we use the tabs.items property to get the length of current items/tabs
261 tabs.setActiveTab(tab);
263 renderTo : Ext.getBody()
266 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.
270 var tabs = Ext.Create('Ext.tab.Panel', {
274 html : 'A simple tab'
277 id : 'remove-this-tab',
282 renderTo : Ext.getBody()
285 Ext.Create('Ext.button.Button', {
288 handler : function() {
289 var tab = Ext.getCmp('remove-this-tab');
292 renderTo : Ext.getBody()
298 Ext.define('Ext.tab.Panel', {
299 extend: 'Ext.panel.Panel',
300 alias: 'widget.tabpanel',
301 alternateClassName: ['Ext.TabPanel'],
303 requires: ['Ext.layout.container.Card', 'Ext.tab.Bar'],
305 <span id='Ext-tab-Panel-cfg-tabPosition'> /**
306 </span> * @cfg {String} tabPosition The position where the tab strip should be rendered (defaults to <code>'top'</code>).
307 * In 4.0, The only other supported value is <code>'bottom'</code>.
311 <span id='Ext-tab-Panel-cfg-tabBar'> /**
312 </span> * @cfg {Object} tabBar Optional configuration object for the internal {@link Ext.tab.Bar}. If present, this is
313 * passed straight through to the TabBar's constructor
316 <span id='Ext-tab-Panel-cfg-layout'> /**
317 </span> * @cfg {Object} layout Optional configuration object for the internal {@link Ext.layout.container.Card card layout}.
318 * If present, this is passed straight through to the layout's constructor
321 <span id='Ext-tab-Panel-cfg-removePanelHeader'> /**
322 </span> * @cfg {Boolean} removePanelHeader True to instruct each Panel added to the TabContainer to not render its header
323 * element. This is to ensure that the title of the panel does not appear twice. Defaults to true.
325 removePanelHeader: true,
327 <span id='Ext-tab-Panel-cfg-Boolean'> /**
328 </span> * @cfg Boolean plain
329 * True to not show the full background on the TabBar
333 <span id='Ext-tab-Panel-cfg-itemCls'> /**
334 </span> * @cfg {String} itemCls The class added to each child item of this TabPanel. Defaults to 'x-tabpanel-child'.
336 itemCls: 'x-tabpanel-child',
338 <span id='Ext-tab-Panel-cfg-minTabWidth'> /**
339 </span> * @cfg {Number} minTabWidth The minimum width for a tab in the {@link #tabBar}. Defaults to <code>30</code>.
342 <span id='Ext-tab-Panel-cfg-deferredRender'> /**
343 </span> * @cfg {Boolean} deferredRender
344 * <p><tt>true</tt> by default to defer the rendering of child <tt>{@link Ext.container.Container#items items}</tt>
345 * to the browsers DOM until a tab is activated. <tt>false</tt> will render all contained
346 * <tt>{@link Ext.container.Container#items items}</tt> as soon as the {@link Ext.layout.container.Card layout}
347 * is rendered. If there is a significant amount of content or a lot of heavy controls being
348 * rendered into panels that are not displayed by default, setting this to <tt>true</tt> might
349 * improve performance.</p>
350 * <br><p>The <tt>deferredRender</tt> property is internally passed to the layout manager for
351 * TabPanels ({@link Ext.layout.container.Card}) as its {@link Ext.layout.container.Card#deferredRender}
352 * configuration value.</p>
353 * <br><p><b>Note</b>: leaving <tt>deferredRender</tt> as <tt>true</tt> means that the content
354 * within an unactivated tab will not be available</p>
356 deferredRender : true,
359 initComponent: function() {
361 dockedItems = me.dockedItems || [],
362 activeTab = me.activeTab || 0;
364 me.layout = Ext.create('Ext.layout.container.Card', Ext.apply({
366 deferredRender: me.deferredRender,
370 <span id='Ext-tab-Panel-property-tabBar'> /**
371 </span> * @property tabBar
373 * Internal reference to the docked TabBar
375 me.tabBar = Ext.create('Ext.tab.Bar', Ext.apply({}, me.tabBar, {
376 dock: me.tabPosition,
379 cardLayout: me.layout,
383 if (dockedItems && !Ext.isArray(dockedItems)) {
384 dockedItems = [dockedItems];
387 dockedItems.push(me.tabBar);
388 me.dockedItems = dockedItems;
391 <span id='Ext-tab-Panel-event-beforetabchange'> /**
392 </span> * @event beforetabchange
393 * Fires before a tab change (activated by {@link #setActiveTab}). Return false in any listener to cancel
395 * @param {Ext.tab.Panel} tabPanel The TabPanel
396 * @param {Ext.Component} newCard The card that is about to be activated
397 * @param {Ext.Component} oldCard The card that is currently active
401 <span id='Ext-tab-Panel-event-tabchange'> /**
402 </span> * @event tabchange
403 * Fires when a new tab has been activated (activated by {@link #setActiveTab}).
404 * @param {Ext.tab.Panel} tabPanel The TabPanel
405 * @param {Ext.Component} newCard The newly activated item
406 * @param {Ext.Component} oldCard The previously active item
410 me.callParent(arguments);
413 me.setActiveTab(activeTab);
414 //set the active tab after initial layout
415 me.on('afterlayout', me.afterInitialLayout, me, {single: true});
418 <span id='Ext-tab-Panel-method-afterInitialLayout'> /**
420 * We have to wait until after the initial layout to visually activate the activeTab (if set).
421 * The active tab has different margins than normal tabs, so if the initial layout happens with
422 * a tab active, its layout will be offset improperly due to the active margin style. Waiting
423 * until after the initial layout avoids this issue.
425 afterInitialLayout: function() {
427 card = me.getComponent(me.activeTab);
430 me.layout.setActiveItem(card);
434 <span id='Ext-tab-Panel-method-setActiveTab'> /**
435 </span> * Makes the given card active (makes it the visible card in the TabPanel's CardLayout and highlights the Tab)
436 * @param {Ext.Component} card The card to make active
438 setActiveTab: function(card) {
442 card = me.getComponent(card);
444 previous = me.getActiveTab();
446 if (previous && previous !== card && me.fireEvent('beforetabchange', me, card, previous) === false) {
450 me.tabBar.setActiveTab(card.tab);
453 me.layout.setActiveItem(card);
456 if (previous && previous !== card) {
457 me.fireEvent('tabchange', me, card, previous);
462 <span id='Ext-tab-Panel-method-getActiveTab'> /**
463 </span> * Returns the item that is currently active inside this TabPanel. Note that before the TabPanel first activates a
464 * child component this will return whatever was configured in the {@link #activeTab} config option
465 * @return {Ext.Component/Integer} The currently active item
467 getActiveTab: function() {
468 return this.activeTab;
471 <span id='Ext-tab-Panel-method-getTabBar'> /**
472 </span> * Returns the {@link Ext.tab.Bar} currently used in this TabPanel
473 * @return {Ext.TabBar} The TabBar
475 getTabBar: function() {
479 <span id='Ext-tab-Panel-method-onAdd'> /**
481 * Makes sure we have a Tab for each item added to the TabPanel
483 onAdd: function(item, index) {
485 cfg = item.tabConfig || {},
489 disabled: item.disabled,
490 closable: item.closable,
495 if (item.closeText) {
496 defaultConfig.closeText = item.closeText;
498 cfg = Ext.applyIf(cfg, defaultConfig);
499 item.tab = me.tabBar.insert(index, cfg);
503 enable: me.onItemEnable,
504 disable: me.onItemDisable,
505 beforeshow: me.onItemBeforeShow,
506 iconchange: me.onItemIconChange,
507 titlechange: me.onItemTitleChange
511 if (me.removePanelHeader) {
512 item.preventHeader = true;
517 if (item.isPanel && me.border) {
518 item.setBorder(false);
522 // ensure that there is at least one active tab
523 if (this.rendered && me.items.getCount() === 1) {
528 <span id='Ext-tab-Panel-method-onItemEnable'> /**
530 * Enable corresponding tab when item is enabled.
532 onItemEnable: function(item){
536 <span id='Ext-tab-Panel-method-onItemDisable'> /**
538 * Disable corresponding tab when item is enabled.
540 onItemDisable: function(item){
544 <span id='Ext-tab-Panel-method-onItemBeforeShow'> /**
546 * Sets activeTab before item is shown.
548 onItemBeforeShow: function(item) {
549 if (item !== this.activeTab) {
550 this.setActiveTab(item);
555 <span id='Ext-tab-Panel-method-onItemIconChange'> /**
557 * Update the tab iconCls when panel iconCls has been set or changed.
559 onItemIconChange: function(item, newIconCls) {
560 item.tab.setIconCls(newIconCls);
561 this.getTabBar().doLayout();
564 <span id='Ext-tab-Panel-method-onItemTitleChange'> /**
566 * Update the tab title when panel title has been set or changed.
568 onItemTitleChange: function(item, newTitle) {
569 item.tab.setText(newTitle);
570 this.getTabBar().doLayout();
574 <span id='Ext-tab-Panel-method-doRemove'> /**
576 * If we're removing the currently active tab, activate the nearest one. The item is removed when we call super,
577 * so we can do preprocessing before then to find the card's index
579 doRemove: function(item, autoDestroy) {
582 <span id='Ext-tab-Panel-property-hasItemsLeft'> /**
583 </span> * At this point the item hasn't been removed from the items collection.
584 * As such, if we want to check if there are no more tabs left, we have to
585 * check for one, as opposed to 0.
587 hasItemsLeft = items.getCount() > 1;
589 if (me.destroying || !hasItemsLeft) {
591 } else if (item === me.activeTab) {
592 me.setActiveTab(item.next() || items.getAt(0));
594 me.callParent(arguments);
596 // Remove the two references
597 delete item.tab.card;
601 <span id='Ext-tab-Panel-method-onRemove'> /**
603 * Makes sure we remove the corresponding Tab when an item is removed
605 onRemove: function(item, autoDestroy) {
610 enable: me.onItemEnable,
611 disable: me.onItemDisable,
612 beforeshow: me.onItemBeforeShow
614 if (!me.destroying && item.tab.ownerCt == me.tabBar) {
615 me.tabBar.remove(item.tab);