-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTabbedPanel.js
59 lines (46 loc) · 1.5 KB
/
TabbedPanel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* TabbedPanel.js
*
*
*/
function TabbedPanel(elm) {
var self = this;
this.DOM = {
container: elm,
tabbar: elm.querySelector('ul'),
tabbarButtons: Array.prototype.slice.call(elm.querySelectorAll('ul > li')),
tabs: elm.querySelector('div.tabs'),
tabList: Array.prototype.slice.call(elm.querySelectorAll('div.tabs > div'))
}
this.index = 0;
this.length = this.DOM.tabList.length;
this.DOM.tabbar.onclick = function(e){
var target = e.target;
var index;
if(self.DOM.tabbar.contains(target)){
do{
index = self.DOM.tabbarButtons.indexOf(target);
if(index>-1) break;
}
while(target=target.parentElement);
if(!target.classList.contains('disabled'))
if(self.index!=index)
self.setIndex(index);
}
}
//Show the first tab as default
self.setIndex(0);
}
TabbedPanel.prototype.setIndex = function(index){
var self = this;
if(index>-1 && index<self.length){
//hide current tab
self.DOM.tabbarButtons[self.index].classList.remove('active');
self.DOM.tabList[self.index].classList.remove('active');
//show tab
self.DOM.tabbarButtons[index].classList.add('active');
self.DOM.tabList[index].classList.add('active');
//update index
self.index = index;
}
}