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