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