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