allow string based values for comboboxarray
[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 false;
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             //class="carousel-item carousel-item-next carousel-item-left"
239             
240             this.transition = true;
241             var dir = this.indexOfPanel(pan) > this.indexOfPanel(cur)  ? 'next' : 'prev';
242             var lr = dir == 'next' ? 'left' : 'right';
243             pan.el.addClass(dir); // or prev
244             pan.el.addClass('carousel-item-' + dir); // or prev
245             pan.el.dom.offsetWidth; // find the offset with - causing a reflow?
246             cur.el.addClass(lr); // or right
247             pan.el.addClass(lr);
248             cur.el.addClass('carousel-item-' +lr); // or right
249             pan.el.addClass('carousel-item-' +lr);
250             
251             
252             var _this = this;
253             cur.el.on('transitionend', function() {
254                 Roo.log("trans end?");
255                 
256                 pan.el.removeClass([lr,dir, 'carousel-item-' + lr, 'carousel-item-' + dir]);
257                 pan.setActive(true);
258                 
259                 cur.el.removeClass([lr, 'carousel-item-' + lr]);
260                 cur.setActive(false);
261                 
262                 _this.transition = false;
263                 
264             }, this, { single:  true } );
265             
266             return true;
267         }
268         
269         cur.setActive(false);
270         pan.setActive(true);
271         
272         return true;
273         
274     },
275     showPanelNext : function()
276     {
277         var i = this.indexOfPanel(this.getActivePanel());
278         
279         if (i >= this.tabs.length - 1 && !this.autoslide) {
280             return;
281         }
282         
283         if (i >= this.tabs.length - 1 && this.autoslide) {
284             i = -1;
285         }
286         
287         this.showPanel(this.tabs[i+1]);
288     },
289     
290     showPanelPrev : function()
291     {
292         var i = this.indexOfPanel(this.getActivePanel());
293         
294         if (i  < 1 && !this.autoslide) {
295             return;
296         }
297         
298         if (i < 1 && this.autoslide) {
299             i = this.tabs.length;
300         }
301         
302         this.showPanel(this.tabs[i-1]);
303     },
304     
305     
306     addBullet: function()
307     {
308         if(!this.bullets || Roo.isTouch){
309             return;
310         }
311         var ctr = this.el.select('.carousel-bullets',true).first();
312         var i = this.el.select('.carousel-bullets .bullet',true).getCount() ;
313         var bullet = ctr.createChild({
314             cls : 'bullet bullet-' + i
315         },ctr.dom.lastChild);
316         
317         
318         var _this = this;
319         
320         bullet.on('click', (function(e, el, o, ii, t){
321
322             e.preventDefault();
323
324             this.showPanel(ii);
325
326             if(this.autoslide && this.slideFn){
327                 clearInterval(this.slideFn);
328                 this.slideFn = window.setInterval(function() {
329                     _this.showPanelNext();
330                 }, this.timer);
331             }
332
333         }).createDelegate(this, [i, bullet], true));
334                 
335         
336     },
337      
338     setActiveBullet : function(i)
339     {
340         if(Roo.isTouch){
341             return;
342         }
343         
344         Roo.each(this.el.select('.bullet', true).elements, function(el){
345             el.removeClass('selected');
346         });
347
348         var bullet = this.el.select('.bullet-' + i, true).first();
349         
350         if(!bullet){
351             return;
352         }
353         
354         bullet.addClass('selected');
355     }
356     
357     
358   
359 });
360
361  
362
363  
364  
365 Roo.apply(Roo.bootstrap.TabGroup, {
366     
367     groups: {},
368      /**
369     * register a Navigation Group
370     * @param {Roo.bootstrap.NavGroup} the navgroup to add
371     */
372     register : function(navgrp)
373     {
374         this.groups[navgrp.navId] = navgrp;
375         
376     },
377     /**
378     * fetch a Navigation Group based on the navigation ID
379     * if one does not exist , it will get created.
380     * @param {string} the navgroup to add
381     * @returns {Roo.bootstrap.NavGroup} the navgroup 
382     */
383     get: function(navId) {
384         if (typeof(this.groups[navId]) == 'undefined') {
385             this.register(new Roo.bootstrap.TabGroup({ navId : navId }));
386         }
387         return this.groups[navId] ;
388     }
389     
390     
391     
392 });
393
394