Roo/bootstrap/TabGroup.js
[roojs1] / Roo / bootstrap / TabGroup.js
1 /*
2  * - LGPL
3  *
4  * column
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.TabGroup
10  * @extends Roo.bootstrap.Column
11  * Bootstrap Column class
12  * @cfg {String} navId the navigation id (for use with navbars) - will be auto generated if it does not exist..
13  * @cfg {Boolean} carousel true to make the group behave like a carousel
14  * @cfg {Number} bullets show the panel pointer.. default 0
15  * @cfg {Boolena} autoslide (true|false) auto slide .. default false
16  * @cfg {Number} timer auto slide timer .. default 0 millisecond
17  * 
18  * @constructor
19  * Create a new TabGroup
20  * @param {Object} config The config object
21  */
22
23 Roo.bootstrap.TabGroup = function(config){
24     Roo.bootstrap.TabGroup.superclass.constructor.call(this, config);
25     if (!this.navId) {
26         this.navId = Roo.id();
27     }
28     this.tabs = [];
29     Roo.bootstrap.TabGroup.register(this);
30     
31 };
32
33 Roo.extend(Roo.bootstrap.TabGroup, Roo.bootstrap.Column,  {
34     
35     carousel : false,
36     transition : false,
37     bullets : 0,
38     timer : 0,
39     autoslide : false,
40     slideFn : false,
41     
42     getAutoCreate : function()
43     {
44         var cfg = Roo.apply({}, Roo.bootstrap.TabGroup.superclass.getAutoCreate.call(this));
45         
46         cfg.cls += ' tab-content';
47         
48         Roo.log('get auto create...............');
49         
50         if (this.carousel && !Roo.isTouch) {
51             cfg.cls += ' carousel slide';
52             
53             cfg.cn = [{
54                cls : 'carousel-inner'
55             }];
56         
57             if(this.bullets > 0){
58                 
59                 var bullets = {
60                     cls : 'carousel-bullets',
61                     cn : []
62                 };
63                 
64                 if(this.bullets_cls){
65                     bullets.cls = bullets.cls + ' ' + this.bullets_cls;
66                 }
67                 
68                 for (var i = 0; i < this.bullets; i++){
69                     bullets.cn.push({
70                         cls : 'bullet bullet-' + i
71                     });
72                 }
73                 
74                 bullets.cn.push({
75                     cls : 'clear'
76                 });
77                 
78                 cfg.cn[0].cn = bullets;
79             }
80         }
81         
82         return cfg;
83     },
84     
85     initEvents:  function()
86     {
87         Roo.log('-------- init events on tab group ---------');
88         
89         var _this = this;
90         
91         if(this.bullets > 0 && !Roo.isTouch){
92             this.initBullet();
93         }
94         
95         if(this.autoslide){
96             this.slideFn = window.setInterval(function() {
97                 _this.showPanelNext();
98             }, this.timer);
99         }
100         
101     },
102     
103     getChildContainer : function()
104     {
105         return this.carousel ? this.el.select('.carousel-inner', true).first() : this.el;
106     },
107     
108     /**
109     * register a Navigation item
110     * @param {Roo.bootstrap.NavItem} the navitem to add
111     */
112     register : function(item)
113     {
114         this.tabs.push( item);
115         item.navId = this.navId; // not really needed..
116     
117     },
118     
119     getActivePanel : function()
120     {
121         var r = false;
122         Roo.each(this.tabs, function(t) {
123             if (t.active) {
124                 r = t;
125                 return false;
126             }
127             return null;
128         });
129         return r;
130         
131     },
132     getPanelByName : function(n)
133     {
134         var r = false;
135         Roo.each(this.tabs, function(t) {
136             if (t.tabId == n) {
137                 r = t;
138                 return false;
139             }
140             return null;
141         });
142         return r;
143     },
144     indexOfPanel : function(p)
145     {
146         var r = false;
147         Roo.each(this.tabs, function(t,i) {
148             if (t.tabId == p.tabId) {
149                 r = i;
150                 return false;
151             }
152             return null;
153         });
154         return r;
155     },
156     /**
157      * show a specific panel
158      * @param {Roo.bootstrap.TabPanel|number|string} panel to change to (use the tabId to specify a specific one)
159      * @return {boolean} false if panel was not shown (invalid entry or beforedeactivate fails.)
160      */
161     showPanel : function (pan)
162     {
163         if(this.transition){
164             Roo.log("waiting for the transitionend");
165             return;
166         }
167         
168         if (typeof(pan) == 'number') {
169             pan = this.tabs[pan];
170         }
171         if (typeof(pan) == 'string') {
172             pan = this.getPanelByName(pan);
173         }
174         if (pan.tabId == this.getActivePanel().tabId) {
175             return true;
176         }
177         var cur = this.getActivePanel();
178         
179         if (false === cur.fireEvent('beforedeactivate')) {
180             return false;
181         }
182         
183         if(this.bullets > 0 && !Roo.isTouch){
184             this.setActiveBullet(this.indexOfPanel(pan));
185         }
186         
187         if (this.carousel && typeof(Roo.get(document.body).dom.style.transition) != 'undefined') {
188             
189             this.transition = true;
190             var dir = this.indexOfPanel(pan) > this.indexOfPanel(cur)  ? 'next' : 'prev';
191             var lr = dir == 'next' ? 'left' : 'right';
192             pan.el.addClass(dir); // or prev
193             pan.el.dom.offsetWidth; // find the offset with - causing a reflow?
194             cur.el.addClass(lr); // or right
195             pan.el.addClass(lr);
196             
197             var _this = this;
198             cur.el.on('transitionend', function() {
199                 Roo.log("trans end?");
200                 
201                 pan.el.removeClass([lr,dir]);
202                 pan.setActive(true);
203                 
204                 cur.el.removeClass([lr]);
205                 cur.setActive(false);
206                 
207                 _this.transition = false;
208                 
209             }, this, { single:  true } );
210             
211             return true;
212         }
213         
214         cur.setActive(false);
215         pan.setActive(true);
216         
217         return true;
218         
219     },
220     showPanelNext : function()
221     {
222         var i = this.indexOfPanel(this.getActivePanel());
223         
224         if (i >= this.tabs.length - 1 && !this.autoslide) {
225             return;
226         }
227         
228         if (i >= this.tabs.length - 1 && this.autoslide) {
229             i = -1;
230         }
231         
232         this.showPanel(this.tabs[i+1]);
233     },
234     
235     showPanelPrev : function()
236     {
237         var i = this.indexOfPanel(this.getActivePanel());
238         
239         if (i  < 1 && !this.autoslide) {
240             return;
241         }
242         
243         if (i < 1 && this.autoslide) {
244             i = this.tabs.length;
245         }
246         
247         this.showPanel(this.tabs[i-1]);
248     },
249     
250     initBullet : function()
251     {
252         for (var i = 0; i < this.bullets; i++){
253             var bullet = this.el.select('.bullet-' + i, true).first();
254
255             if(!bullet){
256                 continue;
257             }
258
259             bullet.on('click', (function(e, el, o, ii, t){
260
261                 e.preventDefault();
262
263                 _this.showPanel(ii);
264
265                 if(_this.autoslide && _this.slideFn){
266                     clearInterval(_this.slideFn);
267                     _this.slideFn = window.setInterval(function() {
268                         _this.showPanelNext();
269                     }, _this.timer);
270                 }
271
272             }).createDelegate(this, [i, bullet], true));
273         }
274     },
275     
276     setActiveBullet : function(i)
277     {
278         if(Roo.isTouch){
279             return;
280         }
281         
282         Roo.each(this.el.select('.bullet', true).elements, function(el){
283             el.removeClass('selected');
284         });
285
286         var bullet = this.el.select('.bullet-' + i, true).first();
287         
288         if(!bullet){
289             return;
290         }
291         
292         bullet.addClass('selected');
293     }
294     
295     
296   
297 });
298
299  
300
301  
302  
303 Roo.apply(Roo.bootstrap.TabGroup, {
304     
305     groups: {},
306      /**
307     * register a Navigation Group
308     * @param {Roo.bootstrap.NavGroup} the navgroup to add
309     */
310     register : function(navgrp)
311     {
312         this.groups[navgrp.navId] = navgrp;
313         
314     },
315     /**
316     * fetch a Navigation Group based on the navigation ID
317     * if one does not exist , it will get created.
318     * @param {string} the navgroup to add
319     * @returns {Roo.bootstrap.NavGroup} the navgroup 
320     */
321     get: function(navId) {
322         if (typeof(this.groups[navId]) == 'undefined') {
323             this.register(new Roo.bootstrap.TabGroup({ navId : navId }));
324         }
325         return this.groups[navId] ;
326     }
327     
328     
329     
330 });
331
332