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} headerScrollable (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     
53     getChildContainer : function()
54     {
55         return this.el.select('.tab-content', true).first();
56     },
57     
58     getAutoCreate : function(){
59         
60         var header = {
61             tag: 'li',
62             cls: 'pull-left header',
63             html: this.title,
64             cn : []
65         };
66         
67         if(this.icon){
68             header.cn.push({
69                 tag: 'i',
70                 cls: 'fa ' + this.icon
71             });
72         }
73         
74         
75         var cfg = {
76             tag: 'div',
77             cls: 'nav-tabs-custom',
78             cn: [
79                 {
80                     tag: 'ul',
81                     cls: 'nav nav-tabs pull-right',
82                     cn: [
83                         header
84                     ]
85                 },
86                 {
87                     tag: 'div',
88                     cls: 'tab-content no-padding',
89                     cn: []
90                 }
91             ]
92         }
93
94         return  cfg;
95     },
96     initEvents : function()
97     {
98         //Roo.log('add add pane handler');
99         this.on('addpane', this.onAddPane, this);
100     },
101      /**
102      * Updates the box title
103      * @param {String} html to set the title to.
104      */
105     setTitle : function(value)
106     {
107         this.el.select('.nav-tabs .header', true).first().dom.innerHTML = value;
108     },
109     onAddPane : function(pane)
110     {
111         this.panes.push(pane);
112         //Roo.log('addpane');
113         //Roo.log(pane);
114         // tabs are rendere left to right..
115         if(!this.showtabs){
116             return;
117         }
118         
119         var ctr = this.el.select('.nav-tabs', true).first();
120          
121          
122         var existing = ctr.select('.nav-tab',true);
123         var qty = existing.getCount();;
124         
125         
126         var tab = ctr.createChild({
127             tag : 'li',
128             cls : 'nav-tab' + (qty ? '' : ' active'),
129             cn : [
130                 {
131                     tag : 'a',
132                     href:'#',
133                     html : pane.title
134                 }
135             ]
136         }, qty ? existing.first().dom : ctr.select('.header', true).first().dom );
137         pane.tab = tab;
138         
139         tab.on('click', this.onTabClick.createDelegate(this, [pane], true));
140         if (!qty) {
141             pane.el.addClass('active');
142         }
143         
144                 
145     },
146     onTabClick : function(ev,un,ob,pane)
147     {
148         //Roo.log('tab - prev default');
149         ev.preventDefault();
150         
151         
152         this.el.select('.nav-tabs li.nav-tab', true).removeClass('active');
153         pane.tab.addClass('active');
154         //Roo.log(pane.title);
155         this.getChildContainer().select('.tab-pane',true).removeClass('active');
156         // technically we should have a deactivate event.. but maybe add later.
157         // and it should not de-activate the selected tab...
158         this.fireEvent('activatepane', pane);
159         pane.el.addClass('active');
160         pane.fireEvent('activate');
161         
162         
163     },
164     
165     getActivePane : function()
166     {
167         var r = false;
168         Roo.each(this.panes, function(p) {
169             if(p.el.hasClass('active')){
170                 r = p;
171                 return false;
172             }
173             
174             return;
175         });
176         
177         return r;
178     }
179     
180     
181 });
182
183