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