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