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