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