Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / tab / Bar.js
1 /**
2  * @author Ed Spencer
3  * @class Ext.tab.Bar
4  * @extends Ext.panel.Header
5  * <p>TabBar is used internally by a {@link Ext.tab.Panel TabPanel} and wouldn't usually need to be created manually.</p>
6  *
7  * @xtype tabbar
8  */
9 Ext.define('Ext.tab.Bar', {
10     extend: 'Ext.panel.Header',
11     alias: 'widget.tabbar',
12     baseCls: Ext.baseCSSPrefix + 'tab-bar',
13
14     requires: [
15         'Ext.tab.Tab',
16         'Ext.FocusManager'
17     ],
18
19     // @private
20     defaultType: 'tab',
21
22     /**
23      * @cfg Boolean plain
24      * True to not show the full background on the tabbar
25      */
26     plain: false,
27
28     // @private
29     renderTpl: [
30         '<div class="{baseCls}-body<tpl if="ui"> {baseCls}-body-{ui}<tpl for="uiCls"> {parent.baseCls}-body-{parent.ui}-{.}</tpl></tpl>"<tpl if="bodyStyle"> style="{bodyStyle}"</tpl>></div>',
31         '<div class="{baseCls}-strip<tpl if="ui"> {baseCls}-strip-{ui}<tpl for="uiCls"> {parent.baseCls}-strip-{parent.ui}-{.}</tpl></tpl>"></div>'
32     ],
33
34     /**
35      * @cfg {Number} minTabWidth The minimum width for each tab. Defaults to <tt>30</tt>.
36      */
37     minTabWidth: 30,
38
39     /**
40      * @cfg {Number} maxTabWidth The maximum width for each tab. Defaults to <tt>undefined</tt>.
41      */
42     maxTabWidth: undefined,
43
44     // @private
45     initComponent: function() {
46         var me = this,
47             keys;
48
49         if (me.plain) {
50             me.setUI(me.ui + '-plain');
51         }
52         
53         me.addClsWithUI(me.dock);
54
55         me.addEvents(
56             /**
57              * @event change
58              * Fired when the currently-active tab has changed
59              * @param {Ext.tab.Bar} tabBar The TabBar
60              * @param {Ext.Tab} tab The new Tab
61              * @param {Ext.Component} card The card that was just shown in the TabPanel
62              */
63             'change'
64         );
65
66         Ext.applyIf(me.renderSelectors, {
67             body : '.' + me.baseCls + '-body',
68             strip: '.' + me.baseCls + '-strip'
69         });
70         me.callParent(arguments);
71
72         // TabBar must override the Header's align setting.
73         me.layout.align = (me.orientation == 'vertical') ? 'left' : 'top';
74         me.layout.overflowHandler = Ext.create('Ext.layout.container.boxOverflow.Scroller', me.layout);
75         me.items.removeAt(me.items.getCount() - 1);
76         me.items.removeAt(me.items.getCount() - 1);
77         
78         // Subscribe to Ext.FocusManager for key navigation
79         keys = me.orientation == 'vertical' ? ['up', 'down'] : ['left', 'right'];
80         Ext.FocusManager.subscribe(me, {
81             keys: keys
82         });
83     },
84
85     // @private
86     onAdd: function(tab) {
87         var me = this,
88             tabPanel = me.tabPanel,
89             hasOwner = !!tabPanel;
90
91         me.callParent(arguments);
92         tab.position = me.dock;
93         if (hasOwner) {
94             tab.minWidth = tabPanel.minTabWidth;
95         }
96         else {
97             tab.minWidth = me.minTabWidth + (tab.iconCls ? 25 : 0);
98         }
99         tab.maxWidth = me.maxTabWidth || (hasOwner ? tabPanel.maxTabWidth : undefined);
100     },
101
102     // @private
103     afterRender: function() {
104         var me = this;
105
106         me.mon(me.el, {
107             scope: me,
108             click: me.onClick,
109             delegate: '.' + Ext.baseCSSPrefix + 'tab'
110         });
111         me.callParent(arguments);
112         
113     },
114
115     afterComponentLayout : function() {
116         var me = this;
117         
118         me.callParent(arguments);
119         me.strip.setWidth(me.el.getWidth());
120     },
121
122     // @private
123     onClick: function(e, target) {
124         // The target might not be a valid tab el.
125         var tab = Ext.getCmp(target.id),
126             tabPanel = this.tabPanel,
127             allowActive = true;
128
129         target = e.getTarget();
130
131         if (tab && tab.isDisabled && !tab.isDisabled()) {
132             if (tab.closable && target === tab.closeEl.dom) {
133                 tab.onCloseClick();
134             } else {
135                 if (tabPanel) {
136                     // TabPanel will card setActiveTab of the TabBar
137                     tabPanel.setActiveTab(tab.card);
138                 } else {
139                     this.setActiveTab(tab);
140                 }
141                 tab.focus();
142             }
143         }
144     },
145
146     /**
147      * @private
148      * Closes the given tab by removing it from the TabBar and removing the corresponding card from the TabPanel
149      * @param {Ext.Tab} tab The tab to close
150      */
151     closeTab: function(tab) {
152         var me = this,
153             card = tab.card,
154             tabPanel = me.tabPanel,
155             nextTab;
156             
157         if (card && card.fireEvent('beforeclose', card) === false) {
158             return false;
159         }
160
161         if (tab.active && me.items.getCount() > 1) {
162             nextTab = tab.next('tab') || me.items.items[0];
163             me.setActiveTab(nextTab);
164             if (tabPanel) {
165                 tabPanel.setActiveTab(nextTab.card);
166             }
167         }
168         /*
169          * force the close event to fire. By the time this function returns,
170          * the tab is already destroyed and all listeners have been purged
171          * so the tab can't fire itself.
172          */
173         tab.fireClose();
174         me.remove(tab);
175
176         if (tabPanel && card) {
177             card.fireEvent('close', card);
178             tabPanel.remove(card);
179         }
180         
181         if (nextTab) {
182             nextTab.focus();
183         }
184     },
185
186     /**
187      * @private
188      * Marks the given tab as active
189      * @param {Ext.Tab} tab The tab to mark active
190      */
191     setActiveTab: function(tab) {
192         if (tab.disabled) {
193             return;
194         }
195         var me = this;
196         if (me.activeTab) {
197             me.activeTab.deactivate();
198         }
199         tab.activate();
200         
201         if (me.rendered) {
202             me.layout.layout();
203             tab.el.scrollIntoView(me.layout.getRenderTarget());
204         }
205         me.activeTab = tab;
206         me.fireEvent('change', me, tab, tab.card);
207     }
208 });