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     });
35 };
36
37 Roo.extend(Roo.bootstrap.dash.TabBox, Roo.bootstrap.Component,  {
38
39     title : '',
40     icon : false,
41     showtabs : true,
42     
43     getChildContainer : function()
44     {
45         return this.el.select('.tab-content', true).first();
46     },
47     
48     getAutoCreate : function(){
49         
50         var header = {
51             tag: 'li',
52             cls: 'pull-left header',
53             html: this.title,
54             cn : []
55         };
56         
57         if(this.icon){
58             header.cn.push({
59                 tag: 'i',
60                 cls: 'fa ' + this.icon
61             });
62         }
63         
64         
65         var cfg = {
66             tag: 'div',
67             cls: 'nav-tabs-custom',
68             cn: [
69                 {
70                     tag: 'ul',
71                     cls: 'nav nav-tabs pull-right',
72                     cn: [
73                         header
74                     ]
75                 },
76                 {
77                     tag: 'div',
78                     cls: 'tab-content no-padding',
79                     cn: []
80                 }
81             ]
82         }
83
84         return  cfg;
85     },
86     initEvents : function()
87     {
88         //Roo.log('add add pane handler');
89         this.on('addpane', this.onAddPane, this);
90     },
91      /**
92      * Updates the box title
93      * @param {String} html to set the title to.
94      */
95     setTitle : function(value)
96     {
97         this.el.select('.nav-tabs .header', true).first().dom.innerHTML = value;
98     },
99     onAddPane : function(pane)
100     {
101         //Roo.log('addpane');
102         //Roo.log(pane);
103         // tabs are rendere left to right..
104         if(!this.showtabs){
105             return;
106         }
107         
108         var ctr = this.el.select('.nav-tabs', true).first();
109          
110          
111         var existing = ctr.select('.nav-tab',true);
112         var qty = existing.getCount();;
113         
114         
115         var tab = ctr.createChild({
116             tag : 'li',
117             cls : 'nav-tab' + (qty ? '' : ' active'),
118             cn : [
119                 {
120                     tag : 'a',
121                     href:'#',
122                     html : pane.title
123                 }
124             ]
125         }, qty ? existing.first().dom : ctr.select('.header', true).first().dom );
126         pane.tab = tab;
127         
128         tab.on('click', this.onTabClick.createDelegate(this, [pane], true));
129         if (!qty) {
130             pane.el.addClass('active');
131         }
132         
133                 
134     },
135     onTabClick : function(ev,un,ob,pane)
136     {
137         //Roo.log('tab - prev default');
138         ev.preventDefault();
139         
140         
141         this.el.select('.nav-tabs li.nav-tab', true).removeClass('active');
142         pane.tab.addClass('active');
143         //Roo.log(pane.title);
144         this.getChildContainer().select('.tab-pane',true).removeClass('active');
145         // technically we should have a deactivate event.. but maybe add later.
146         // and it should not de-activate the selected tab...
147         
148         pane.el.addClass('active');
149         pane.fireEvent('activate');
150         
151         
152     }
153     
154     
155 });
156
157