forked from gongfudev/Exquis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs.js
74 lines (61 loc) · 2.86 KB
/
tabs.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
define([], function(){
// config
// {
// tabsRoot: domId
// tabs: [ { name, initHandler, clickHandler } ]
// }
var create = function(config){
var tabsRoot = document.getElementById(config.tabsRoot),
tabsHeaderRoot = document.createElement("div"),
tabsContentRoot = document.createElement("div"),
activeContentSelector = '#'+tabsRoot.id+'>div.tabs__content>div:not(.invisible)',
activeTabConfig,
tabNameToParentDivMap = {};
tabsRoot.appendChild(tabsHeaderRoot);
tabsRoot.classList.add("tabs");
tabsContentRoot.classList.add("tabs__content");
tabsRoot.appendChild(tabsContentRoot);
tabsHeaderRoot.classList.add("tabs__header");
config.tabs.forEach(function(tabConfig, index){
var tabHeader = document.createElement("div");
tabHeader.classList.add("tabs__title");
tabHeader.innerHTML = tabConfig.name;
if(index === 0){
tabHeader.classList.add("tabs__title--active");
activeTabConfig = tabConfig;
}
tabsHeaderRoot.appendChild(tabHeader);
var tabContentDiv = document.createElement("div");
if(index > 0){
tabContentDiv.classList.add("invisible");
}
tabContentDiv.id = tabsRoot.id + "_" + tabConfig.name;
tabsContentRoot.appendChild(tabContentDiv);
tabNameToParentDivMap[tabConfig.name] = tabContentDiv;
tabHeader.addEventListener("click", async function(event){
var activeHeader = tabsHeaderRoot.querySelector('.tabs__title--active'),
activeContent = document.querySelector(activeContentSelector);
if (event.target === activeHeader) { return; }
await tabConfig.clickHandler(tabContentDiv);
tabHeader.classList.add('tabs__title--active');
activeHeader.classList.remove('tabs__title--active');
tabContentDiv.classList.remove('invisible');
activeContent.classList.add('invisible');
activeTabConfig = tabConfig;
});
tabConfig.initHandler && tabConfig.initHandler(tabContentDiv);
});
// add dummy tab to display a bottom border
var bottomBorderDiv = document.createElement('div');
bottomBorderDiv.classList.add('tabs__title--empty', 'tabs__title');
tabsHeaderRoot.appendChild(bottomBorderDiv);
var refreshActiveTab = function(){
activeTabConfig.clickHandler(document.querySelector(activeContentSelector));
};
var getParentDiv = function(tabName){
return tabNameToParentDivMap[tabName];
};
return {refreshActiveTab, getParentDiv};
};
return create;
});