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         if(this.bullets > 0 && !Roo.isTouch){
90             this.initBullet();
91         }
92         
93         if(this.autoslide){
94             this.slideFn = window.setInterval(function() {
95                 _this.showPanelNext();
96             }, this.timer);
97         }
98         
99     },
100     
101     getChildContainer : function()
102     {
103         return this.carousel ? this.el.select('.carousel-inner', true).first() : this.el;
104     },
105     
106     /**
107     * register a Navigation item
108     * @param {Roo.bootstrap.NavItem} the navitem to add
109     */
110     register : function(item)
111     {
112         this.tabs.push( item);
113         item.navId = this.navId; // not really needed..
114     
115     },
116     
117     getActivePanel : function()
118     {
119         var r = false;
120         Roo.each(this.tabs, function(t) {
121             if (t.active) {
122                 r = t;
123                 return false;
124             }
125             return null;
126         });
127         return r;
128         
129     },
130     getPanelByName : function(n)
131     {
132         var r = false;
133         Roo.each(this.tabs, function(t) {
134             if (t.tabId == n) {
135                 r = t;
136                 return false;
137             }
138             return null;
139         });
140         return r;
141     },
142     indexOfPanel : function(p)
143     {
144         var r = false;
145         Roo.each(this.tabs, function(t,i) {
146             if (t.tabId == p.tabId) {
147                 r = i;
148                 return false;
149             }
150             return null;
151         });
152         return r;
153     },
154     /**
155      * show a specific panel
156      * @param {Roo.bootstrap.TabPanel|number|string} panel to change to (use the tabId to specify a specific one)
157      * @return {boolean} false if panel was not shown (invalid entry or beforedeactivate fails.)
158      */
159     showPanel : function (pan)
160     {
161         if(this.transition){
162             Roo.log("waiting for the transitionend");
163             return;
164         }
165         
166         if (typeof(pan) == 'number') {
167             pan = this.tabs[pan];
168         }
169         if (typeof(pan) == 'string') {
170             pan = this.getPanelByName(pan);
171         }
172         if (pan.tabId == this.getActivePanel().tabId) {
173             return true;
174         }
175         var cur = this.getActivePanel();
176         
177         if (false === cur.fireEvent('beforedeactivate')) {
178             return false;
179         }
180         
181         if(this.bullets > 0 && !Roo.isTouch){
182             this.setActiveBullet(this.indexOfPanel(pan));
183         }
184         
185         if (this.carousel && typeof(Roo.get(document.body).dom.style.transition) != 'undefined') {
186             
187             this.transition = true;
188             var dir = this.indexOfPanel(pan) > this.indexOfPanel(cur)  ? 'next' : 'prev';
189             var lr = dir == 'next' ? 'left' : 'right';
190             pan.el.addClass(dir); // or prev
191             pan.el.dom.offsetWidth; // find the offset with - causing a reflow?
192             cur.el.addClass(lr); // or right
193             pan.el.addClass(lr);
194             
195             var _this = this;
196             cur.el.on('transitionend', function() {
197                 Roo.log("trans end?");
198                 
199                 pan.el.removeClass([lr,dir]);
200                 pan.setActive(true);
201                 
202                 cur.el.removeClass([lr]);
203                 cur.setActive(false);
204                 
205                 _this.transition = false;
206                 
207             }, this, { single:  true } );
208             
209             return true;
210         }
211         
212         cur.setActive(false);
213         pan.setActive(true);
214         
215         return true;
216         
217     },
218     showPanelNext : function()
219     {
220         var i = this.indexOfPanel(this.getActivePanel());
221         
222         if (i >= this.tabs.length - 1 && !this.autoslide) {
223             return;
224         }
225         
226         if (i >= this.tabs.length - 1 && this.autoslide) {
227             i = -1;
228         }
229         
230         this.showPanel(this.tabs[i+1]);
231     },
232     
233     showPanelPrev : function()
234     {
235         var i = this.indexOfPanel(this.getActivePanel());
236         
237         if (i  < 1 && !this.autoslide) {
238             return;
239         }
240         
241         if (i < 1 && this.autoslide) {
242             i = this.tabs.length;
243         }
244         
245         this.showPanel(this.tabs[i-1]);
246     },
247     
248     initBullet : function()
249     {
250         if(Roo.isTouch){
251             return;
252         }
253         
254         var _this = this;
255         
256         for (var i = 0; i < this.bullets; i++){
257             var bullet = this.el.select('.bullet-' + i, true).first();
258
259             if(!bullet){
260                 continue;
261             }
262
263             bullet.on('click', (function(e, el, o, ii, t){
264
265                 e.preventDefault();
266
267                 _this.showPanel(ii);
268
269                 if(_this.autoslide && _this.slideFn){
270                     clearInterval(_this.slideFn);
271                     _this.slideFn = window.setInterval(function() {
272                         _this.showPanelNext();
273                     }, _this.timer);
274                 }
275
276             }).createDelegate(this, [i, bullet], true));
277         }
278     },
279     
280     setActiveBullet : function(i)
281     {
282         if(Roo.isTouch){
283             return;
284         }
285         
286         Roo.each(this.el.select('.bullet', true).elements, function(el){
287             el.removeClass('selected');
288         });
289
290         var bullet = this.el.select('.bullet-' + i, true).first();
291         
292         if(!bullet){
293             return;
294         }
295         
296         bullet.addClass('selected');
297     }
298     
299     
300   
301 });
302
303  
304
305  
306  
307 Roo.apply(Roo.bootstrap.TabGroup, {
308     
309     groups: {},
310      /**
311     * register a Navigation Group
312     * @param {Roo.bootstrap.NavGroup} the navgroup to add
313     */
314     register : function(navgrp)
315     {
316         this.groups[navgrp.navId] = navgrp;
317         
318     },
319     /**
320     * fetch a Navigation Group based on the navigation ID
321     * if one does not exist , it will get created.
322     * @param {string} the navgroup to add
323     * @returns {Roo.bootstrap.NavGroup} the navgroup 
324     */
325     get: function(navId) {
326         if (typeof(this.groups[navId]) == 'undefined') {
327             this.register(new Roo.bootstrap.TabGroup({ navId : navId }));
328         }
329         return this.groups[navId] ;
330     }
331     
332     
333     
334 });
335
336