f00627dc98b6073b7e222822fcdbdb32a53f92f4
[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 {Number} timer auto slide timer .. default 0 millisecond
17  * @cfg {Boolean} showarrow (true|false) show arrow default true
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     showarrow : true,
44     
45     getAutoCreate : function()
46     {
47         var cfg = Roo.apply({}, Roo.bootstrap.TabGroup.superclass.getAutoCreate.call(this));
48         
49         cfg.cls += ' tab-content';
50         
51         if (this.carousel) {
52             cfg.cls += ' carousel slide';
53             
54             cfg.cn = [{
55                cls : 'carousel-inner',
56                cn : []
57             }];
58         
59             if(this.bullets  && !Roo.isTouch){
60                 
61                 var bullets = {
62                     cls : 'carousel-bullets',
63                     cn : []
64                 };
65                
66                 if(this.bullets_cls){
67                     bullets.cls = bullets.cls + ' ' + this.bullets_cls;
68                 }
69                 
70                 bullets.cn.push({
71                     cls : 'clear'
72                 });
73                 
74                 cfg.cn[0].cn.push(bullets);
75             }
76             
77             if(this.showarrow){
78                 cfg.cn[0].cn.push({
79                     tag : 'div',
80                     class : 'carousel-arrow',
81                     cn : [
82                         {
83                             tag : 'div',
84                             class : 'carousel-prev',
85                             cn : [
86                                 {
87                                     tag : 'i',
88                                     class : 'fa fa-chevron-left'
89                                 }
90                             ]
91                         },
92                         {
93                             tag : 'div',
94                             class : 'carousel-next',
95                             cn : [
96                                 {
97                                     tag : 'i',
98                                     class : 'fa fa-chevron-right'
99                                 }
100                             ]
101                         }
102                     ]
103                 });
104             }
105             
106         }
107         
108         return cfg;
109     },
110     
111     initEvents:  function()
112     {
113 //        if(Roo.isTouch && this.slideOnTouch && !this.showarrow){
114 //            this.el.on("touchstart", this.onTouchStart, this);
115 //        }
116         
117         if(this.autoslide){
118             var _this = this;
119             
120             this.slideFn = window.setInterval(function() {
121                 _this.showPanelNext();
122             }, this.timer);
123         }
124         
125         if(this.showarrow){
126             this.el.select('.carousel-prev', true).first().on('click', this.showPanelPrev, this);
127             this.el.select('.carousel-next', true).first().on('click', this.showPanelNext, this);
128         }
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     
143     getChildContainer : function()
144     {
145         return this.carousel ? this.el.select('.carousel-inner', true).first() : this.el;
146     },
147     
148     /**
149     * register a Navigation item
150     * @param {Roo.bootstrap.NavItem} the navitem to add
151     */
152     register : function(item)
153     {
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         this.groups[navgrp.navId] = navgrp;
369         
370     },
371     /**
372     * fetch a Navigation Group based on the navigation ID
373     * if one does not exist , it will get created.
374     * @param {string} the navgroup to add
375     * @returns {Roo.bootstrap.NavGroup} the navgroup 
376     */
377     get: function(navId) {
378         if (typeof(this.groups[navId]) == 'undefined') {
379             this.register(new Roo.bootstrap.TabGroup({ navId : navId }));
380         }
381         return this.groups[navId] ;
382     }
383     
384     
385     
386 });
387
388