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