sync
[roojs1] / Roo / bootstrap / TabPanel.js
1 /*
2  * - LGPL
3  *
4  * TabPanel
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.TabPanel
10  * @extends Roo.bootstrap.Component
11  * Bootstrap TabPanel class
12  * @cfg {Boolean} active panel active
13  * @cfg {String} html panel content
14  * @cfg {String} tabId  unique tab ID (will be autogenerated if not set. - used to match TabItem to Panel)
15  * @cfg {String} navId The Roo.bootstrap.NavGroup which triggers show hide ()
16  * @cfg {String} href click to link..
17  * @cfg {Boolean} touchSlide if swiping slides tab to next panel (default off)
18  * 
19  * 
20  * @constructor
21  * Create a new TabPanel
22  * @param {Object} config The config object
23  */
24
25 Roo.bootstrap.TabPanel = function(config){
26     Roo.bootstrap.TabPanel.superclass.constructor.call(this, config);
27     this.addEvents({
28         /**
29              * @event changed
30              * Fires when the active status changes
31              * @param {Roo.bootstrap.TabPanel} this
32              * @param {Boolean} state the new state
33             
34          */
35         'changed': true,
36         /**
37              * @event beforedeactivate
38              * Fires before a tab is de-activated - can be used to do validation on a form.
39              * @param {Roo.bootstrap.TabPanel} this
40              * @return {Boolean} false if there is an error
41             
42          */
43         'beforedeactivate': true
44      });
45     
46     this.tabId = this.tabId || Roo.id();
47   
48 };
49
50 Roo.extend(Roo.bootstrap.TabPanel, Roo.bootstrap.Component,  {
51     
52     active: false,
53     html: false,
54     tabId: false,
55     navId : false,
56     href : '',
57     touchSlide : false,
58     getAutoCreate : function(){
59         
60         
61         var cfg = {
62             tag: 'div',
63             // item is needed for carousel - not sure if it has any effect otherwise
64             cls: 'carousel-item tab-pane item' + ((this.href.length) ? ' clickable ' : ''),
65             html: this.html || ''
66         };
67         
68         if(this.active){
69             cfg.cls += ' active';
70         }
71         
72         if(this.tabId){
73             cfg.tabId = this.tabId;
74         }
75         
76         
77         
78         return cfg;
79     },
80     
81     initEvents:  function()
82     {
83         var p = this.parent();
84         
85         this.navId = this.navId || p.navId;
86         
87         if (typeof(this.navId) != 'undefined') {
88             // not really needed.. but just in case.. parent should be a NavGroup.
89             var tg = Roo.bootstrap.TabGroup.get(this.navId);
90             
91             tg.register(this);
92             
93             var i = tg.tabs.length - 1;
94             
95             if(this.active && tg.bullets > 0 && i < tg.bullets){
96                 tg.setActiveBullet(i);
97             }
98         }
99         
100         this.el.on('click', this.onClick, this);
101         
102         if(Roo.isTouch && this.touchSlide){
103             this.el.on("touchstart", this.onTouchStart, this);
104             this.el.on("touchmove", this.onTouchMove, this);
105             this.el.on("touchend", this.onTouchEnd, this);
106         }
107         
108     },
109     
110     onRender : function(ct, position)
111     {
112         Roo.bootstrap.TabPanel.superclass.onRender.call(this, ct, position);
113     },
114     
115     setActive : function(state)
116     {
117         Roo.log("panel - set active " + this.tabId + "=" + state);
118         
119         this.active = state;
120         if (!state) {
121             this.el.removeClass('active');
122             
123         } else  if (!this.el.hasClass('active')) {
124             this.el.addClass('active');
125         }
126         
127         this.fireEvent('changed', this, state);
128     },
129     
130     onClick : function(e)
131     {
132         e.preventDefault();
133         
134         if(!this.href.length){
135             return;
136         }
137         
138         window.location.href = this.href;
139     },
140     
141     startX : 0,
142     startY : 0,
143     endX : 0,
144     endY : 0,
145     swiping : false,
146     
147     onTouchStart : function(e)
148     {
149         this.swiping = false;
150         
151         this.startX = e.browserEvent.touches[0].clientX;
152         this.startY = e.browserEvent.touches[0].clientY;
153     },
154     
155     onTouchMove : function(e)
156     {
157         this.swiping = true;
158         
159         this.endX = e.browserEvent.touches[0].clientX;
160         this.endY = e.browserEvent.touches[0].clientY;
161     },
162     
163     onTouchEnd : function(e)
164     {
165         if(!this.swiping){
166             this.onClick(e);
167             return;
168         }
169         
170         var tabGroup = this.parent();
171         
172         if(this.endX > this.startX){ // swiping right
173             tabGroup.showPanelPrev();
174             return;
175         }
176         
177         if(this.startX > this.endX){ // swiping left
178             tabGroup.showPanelNext();
179             return;
180         }
181     }
182     
183     
184 });
185  
186
187  
188
189