Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / examples / ux / GroupTabPanel.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 Ext.ns('Ext.ux');
8
9 Ext.ux.GroupTabPanel = Ext.extend(Ext.TabPanel, {
10     tabPosition: 'left',
11     
12     alternateColor: false,
13     
14     alternateCls: 'x-grouptabs-panel-alt',
15     
16     defaultType: 'grouptab',
17     
18     deferredRender: false,
19     
20     activeGroup : null,
21     
22     initComponent: function(){
23         Ext.ux.GroupTabPanel.superclass.initComponent.call(this);
24         
25         this.addEvents(
26             'beforegroupchange',
27             'groupchange'
28         );
29         this.elements = 'body,header';
30         this.stripTarget = 'header';
31         
32         this.tabPosition = this.tabPosition == 'right' ? 'right' : 'left';
33         
34         this.addClass('x-grouptabs-panel');
35         
36         if (this.tabStyle && this.tabStyle != '') {
37             this.addClass('x-grouptabs-panel-' + this.tabStyle);
38         }
39         
40         if (this.alternateColor) {
41             this.addClass(this.alternateCls);
42         }
43         
44         this.on('beforeadd', function(gtp, item, index){
45             this.initGroup(item, index);
46         });                  
47     },
48     
49     initEvents : function() {
50         this.mon(this.strip, 'mousedown', this.onStripMouseDown, this);
51     },
52         
53     onRender: function(ct, position){
54         Ext.TabPanel.superclass.onRender.call(this, ct, position);
55         if(this.plain){
56             var pos = this.tabPosition == 'top' ? 'header' : 'footer';
57             this[pos].addClass('x-tab-panel-'+pos+'-plain');
58         }
59
60         var st = this[this.stripTarget];
61
62         this.stripWrap = st.createChild({cls:'x-tab-strip-wrap ', cn:{
63             tag:'ul', cls:'x-grouptabs-strip x-grouptabs-tab-strip-'+this.tabPosition}});
64
65         var beforeEl = (this.tabPosition=='bottom' ? this.stripWrap : null);
66         this.strip = new Ext.Element(this.stripWrap.dom.firstChild);
67
68                 this.header.addClass('x-grouptabs-panel-header');
69                 this.bwrap.addClass('x-grouptabs-bwrap');
70         this.body.addClass('x-tab-panel-body-'+this.tabPosition + ' x-grouptabs-panel-body');
71
72         if (!this.groupTpl) {
73             var tt = new Ext.Template(
74                 '<li class="{cls}" id="{id}">', 
75                 '<a class="x-grouptabs-expand" onclick="return false;"></a>', 
76                 '<a class="x-grouptabs-text {iconCls}" href="#" onclick="return false;">',
77                 '<span>{text}</span></a>', 
78                 '</li>'
79             );
80             tt.disableFormats = true;
81             tt.compile();
82             Ext.ux.GroupTabPanel.prototype.groupTpl = tt;
83         }
84         this.items.each(this.initGroup, this);
85     },
86     
87     afterRender: function(){
88         Ext.ux.GroupTabPanel.superclass.afterRender.call(this);
89         
90         this.tabJoint = Ext.fly(this.body.dom.parentNode).createChild({
91             cls: 'x-tab-joint'
92         });
93         
94         this.addClass('x-tab-panel-' + this.tabPosition);
95         this.header.setWidth(this.tabWidth);
96         
97         if (this.activeGroup !== undefined) {
98             var group = (typeof this.activeGroup == 'object') ? this.activeGroup : this.items.get(this.activeGroup);
99             delete this.activeGroup;
100             this.setActiveGroup(group);
101             group.setActiveTab(group.getMainItem());
102         }
103     },
104
105     getGroupEl : Ext.TabPanel.prototype.getTabEl,
106         
107     // private
108     findTargets: function(e){
109         var item = null,
110             itemEl = e.getTarget('li', this.strip);
111         if (itemEl) {
112             item = this.findById(itemEl.id.split(this.idDelimiter)[1]);
113             if (item.disabled) {
114                 return {
115                     expand: null,
116                     item: null,
117                     el: null
118                 };
119             }
120         }
121         return {
122             expand: e.getTarget('.x-grouptabs-expand', this.strip),
123             isGroup: !e.getTarget('ul.x-grouptabs-sub', this.strip),
124             item: item,
125             el: itemEl
126         };
127     },
128     
129     // private
130     onStripMouseDown: function(e){
131         if (e.button != 0) {
132             return;
133         }
134         e.preventDefault();
135         var t = this.findTargets(e);
136         if (t.expand) {
137             this.toggleGroup(t.el);
138         }
139         else if (t.item) {
140             if(t.isGroup) {
141                 t.item.setActiveTab(t.item.getMainItem());
142             }
143             else {
144                 t.item.ownerCt.setActiveTab(t.item);
145             }
146         }
147     },
148     
149     expandGroup: function(groupEl){
150         if(groupEl.isXType) {
151             groupEl = this.getGroupEl(groupEl);
152         }
153         Ext.fly(groupEl).addClass('x-grouptabs-expanded');
154                 this.syncTabJoint();
155     },
156     
157     toggleGroup: function(groupEl){
158         if(groupEl.isXType) {
159             groupEl = this.getGroupEl(groupEl);
160         }        
161         Ext.fly(groupEl).toggleClass('x-grouptabs-expanded');
162                 this.syncTabJoint();
163     },    
164
165     collapseGroup: function(groupEl){
166         if(groupEl.isXType) {
167             groupEl = this.getGroupEl(groupEl);
168         }
169         Ext.fly(groupEl).removeClass('x-grouptabs-expanded');
170                 this.syncTabJoint();
171     },
172         
173     syncTabJoint: function(groupEl){
174         if (!this.tabJoint) {
175             return;
176         }
177         
178         groupEl = groupEl || this.getGroupEl(this.activeGroup);
179         if(groupEl) {
180             this.tabJoint.setHeight(Ext.fly(groupEl).getHeight() - 2); 
181                         
182             var y = Ext.isGecko2 ? 0 : 1;
183             if (this.tabPosition == 'left'){
184                 this.tabJoint.alignTo(groupEl, 'tl-tr', [-2,y]);
185             }
186             else {
187                 this.tabJoint.alignTo(groupEl, 'tr-tl', [1,y]);
188             }           
189         }
190         else {
191             this.tabJoint.hide();
192         }
193     },
194     
195     getActiveTab : function() {
196         if(!this.activeGroup) return null;
197         return this.activeGroup.getTabEl(this.activeGroup.activeTab) || null;  
198     },
199     
200     onResize: function(){
201         Ext.ux.GroupTabPanel.superclass.onResize.apply(this, arguments);
202         this.syncTabJoint();
203     },
204     
205     createCorner: function(el, pos){
206         return Ext.fly(el).createChild({
207             cls: 'x-grouptabs-corner x-grouptabs-corner-' + pos
208         });
209     },
210     
211     initGroup: function(group, index){
212         var before = this.strip.dom.childNodes[index],   
213             p = this.getTemplateArgs(group);
214         if (index === 0) {
215             p.cls += ' x-tab-first';
216         }
217         p.cls += ' x-grouptabs-main';
218         p.text = group.getMainItem().title;
219         
220         var el = before ? this.groupTpl.insertBefore(before, p) : this.groupTpl.append(this.strip, p),
221             tl = this.createCorner(el, 'top-' + this.tabPosition),
222             bl = this.createCorner(el, 'bottom-' + this.tabPosition);
223
224         group.tabEl = el;
225         if (group.expanded) {
226             this.expandGroup(el);
227         }
228
229         if (Ext.isIE6 || (Ext.isIE && !Ext.isStrict)){
230             bl.setLeft('-10px');
231             bl.setBottom('-5px');
232             tl.setLeft('-10px');
233             tl.setTop('-5px');
234         }
235
236         this.mon(group, {
237             scope: this,
238             changemainitem: this.onGroupChangeMainItem,
239             beforetabchange: this.onGroupBeforeTabChange
240         });
241     },
242     
243     setActiveGroup : function(group) {
244         group = this.getComponent(group);
245         if(!group){
246             return false;
247         }
248         if(!this.rendered){
249             this.activeGroup = group;
250             return true;
251         }
252         if(this.activeGroup != group && this.fireEvent('beforegroupchange', this, group, this.activeGroup) !== false){
253             if(this.activeGroup){
254                 this.activeGroup.activeTab = null;
255                 var oldEl = this.getGroupEl(this.activeGroup);
256                 if(oldEl){
257                     Ext.fly(oldEl).removeClass('x-grouptabs-strip-active');
258                 }
259             }
260
261             var groupEl = this.getGroupEl(group);
262             Ext.fly(groupEl).addClass('x-grouptabs-strip-active');
263                         
264             this.activeGroup = group;
265             this.stack.add(group);
266
267             this.layout.setActiveItem(group);
268             this.syncTabJoint(groupEl);
269
270             this.fireEvent('groupchange', this, group);
271             return true;
272         }
273         return false; 
274     },
275     
276     onGroupBeforeTabChange: function(group, newTab, oldTab){
277         if(group !== this.activeGroup || newTab !== oldTab) {
278             this.strip.select('.x-grouptabs-sub > li.x-grouptabs-strip-active', true).removeClass('x-grouptabs-strip-active');
279         } 
280         this.expandGroup(this.getGroupEl(group));
281         if(group !== this.activeGroup) {
282             return this.setActiveGroup(group);
283         }        
284     },
285     
286     getFrameHeight: function(){
287         var h = this.el.getFrameWidth('tb');
288         h += (this.tbar ? this.tbar.getHeight() : 0) +
289         (this.bbar ? this.bbar.getHeight() : 0);
290         
291         return h;
292     },
293     
294     adjustBodyWidth: function(w){
295         return w - this.tabWidth;
296     }
297 });
298
299 Ext.reg('grouptabpanel', Ext.ux.GroupTabPanel);