Roo/bootstrap/dash/TabBox.js
[roojs1] / Roo / bootstrap / dash / TabBox.js
1 /*
2  * - LGPL
3  *
4  * TabBox
5  * 
6  */
7 Roo.bootstrap.dash = Roo.bootstrap.dash || {};
8
9 /**
10  * @class Roo.bootstrap.dash.TabBox
11  * @extends Roo.bootstrap.Component
12  * Bootstrap TabBox class
13  * @cfg {String} title Title of the TabBox
14  * @cfg {String} icon Icon of the TabBox
15  * @cfg {Boolean} showtabs (true|false) show the tabs default true
16  * 
17  * @constructor
18  * Create a new TabBox
19  * @param {Object} config The config object
20  */
21
22
23 Roo.bootstrap.dash.TabBox = function(config){
24     Roo.bootstrap.dash.TabBox.superclass.constructor.call(this, config);
25     this.addEvents({
26         // raw events
27         /**
28          * @event addpane
29          * When a pane is added
30          * @param {Roo.bootstrap.dash.TabPane} pane
31          */
32         "addpane" : true,
33         /**
34          * @event activatepane
35          * When a pane is activated
36          * @param {Roo.bootstrap.dash.TabPane} pane
37          */
38         "activatepane" : true
39         
40          
41     });
42     
43     this.panes = [];
44 };
45
46 Roo.extend(Roo.bootstrap.dash.TabBox, Roo.bootstrap.Component,  {
47
48     title : '',
49     icon : false,
50     showtabs : true,
51     
52     getChildContainer : function()
53     {
54         return this.el.select('.tab-content', true).first();
55     },
56     
57     getAutoCreate : function(){
58         
59         var header = {
60             tag: 'li',
61             cls: 'pull-left header',
62             html: this.title,
63             cn : []
64         };
65         
66         if(this.icon){
67             header.cn.push({
68                 tag: 'i',
69                 cls: 'fa ' + this.icon
70             });
71         }
72         
73         
74         var cfg = {
75             tag: 'div',
76             cls: 'nav-tabs-custom',
77             cn: [
78                 {
79                     tag: 'ul',
80                     cls: 'nav nav-tabs pull-right',
81                     cn: [
82                         header
83                     ]
84                 },
85                 {
86                     tag: 'div',
87                     cls: 'tab-content no-padding',
88                     cn: []
89                 }
90             ]
91         }
92
93         return  cfg;
94     },
95     initEvents : function()
96     {
97         //Roo.log('add add pane handler');
98         this.on('addpane', this.onAddPane, this);
99     },
100      /**
101      * Updates the box title
102      * @param {String} html to set the title to.
103      */
104     setTitle : function(value)
105     {
106         this.el.select('.nav-tabs .header', true).first().dom.innerHTML = value;
107     },
108     onAddPane : function(pane)
109     {
110         this.panes.push(pane);
111         //Roo.log('addpane');
112         //Roo.log(pane);
113         // tabs are rendere left to right..
114         if(!this.showtabs){
115             return;
116         }
117         
118         var ctr = this.el.select('.nav-tabs', true).first();
119          
120          
121         var existing = ctr.select('.nav-tab',true);
122         var qty = existing.getCount();;
123         
124         
125         var tab = ctr.createChild({
126             tag : 'li',
127             cls : 'nav-tab' + (qty ? '' : ' active'),
128             cn : [
129                 {
130                     tag : 'a',
131                     href:'#',
132                     html : pane.title
133                 }
134             ]
135         }, qty ? existing.first().dom : ctr.select('.header', true).first().dom );
136         pane.tab = tab;
137         
138         tab.on('click', this.onTabClick.createDelegate(this, [pane], true));
139         if (!qty) {
140             pane.el.addClass('active');
141         }
142         
143                 
144     },
145     onTabClick : function(ev,un,ob,pane)
146     {
147         //Roo.log('tab - prev default');
148         ev.preventDefault();
149         
150         
151         this.el.select('.nav-tabs li.nav-tab', true).removeClass('active');
152         pane.tab.addClass('active');
153         //Roo.log(pane.title);
154         this.getChildContainer().select('.tab-pane',true).removeClass('active');
155         // technically we should have a deactivate event.. but maybe add later.
156         // and it should not de-activate the selected tab...
157         this.fireEvent('activatepane', pane);
158         pane.el.addClass('active');
159         pane.fireEvent('activate');
160         
161         
162     },
163     
164     getActivePane : function()
165     {
166         var r = false;
167         Roo.each(this.panes, function(p) {
168             if(p.el.hasClass('active')){
169                 r = p;
170                 return false;
171             }
172             
173             return;
174         });
175         
176         return r;
177     }
178     
179     
180 });
181
182