-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabContainer.js
178 lines (154 loc) · 6.35 KB
/
tabContainer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Atto TabContainer : convert a raw list of DOM nodes into a simple, self-contained tab widget
//
// author: Ryan Corradini
// date: 3 July 2012
// license: MIT
//
define(
["atto/core","atto/lmnt","require"],
function(atto, lmnt) {
// make sure the appopriate CSS has been loaded for this widget
var forWidget = "atto-tabs";
if (!document.querySelector("style[data-for-widget='"+forWidget+"']")) {
require(["text!atto/tabContainer.css"], function(rawCss) { atto.addWidgetCss(rawCss, forWidget); });
}
function constructor(rootNode, optionArray) {
var _root = rootNode || document.createElement('div'),
_tabs = [],
_curr = 0,
i = 0,
nd = null,
nCount = _root.childElementCount || lmnt.childElementCount(_root),
lastId = 0,
_registry = {},
opts = optionArray || {},
currTitle = null,
currPanel = null;
// DOM container hooks
var __frag = document.createDocumentFragment(),
_tabRow = document.createElement('ul'),
_tabBody = document.createElement('div'),
tabCount = 0;
// initial setup
_tabRow.className = 'aw-tab-row';
_tabBody.className = 'aw-tab-body';
__frag.appendChild(_tabRow);
__frag.appendChild(_tabBody);
function _getUniqueId() {
var newVal = 'tab-' + lastId++;
_registry[newVal] = newVal;
return newVal;
}
function _delTab(id) {
var lbl, pnl, newLbl, newPnl;
if (id in _registry) {
lbl = atto.byId(id+'-label');
if (lbl.classList.contains('selected')) {
lbl.classList.remove('selected');
if (lbl.nextSibling) {
newLbl = lbl.nextSibling;
} else if (lbl.previousSibling) {
newLbl = lbl.previousSibling;
}
}
_tabRow.removeChild(lbl);
pnl = atto.byId(id+'-panel');
if (pnl.classList.contains('selected')) {
pnl.classList.remove('selected');
if (pnl.nextSibling) {
newPnl = pnl.nextSibling;
} else if (pnl.previousSibling) {
newPnl = pnl.previousSibling;
}
}
_tabBody.removeChild(pnl);
if (newLbl && newPnl) { _selectTab(newLbl, newPnl); }
}
}
function _selectTab(label, panel) {
if (currPanel && currPanel != panel) {
currTitle.classList.remove('selected');
currPanel.classList.remove('selected');
}
currTitle = label;
currPanel = panel;
label.classList.add('selected');
panel.classList.add('selected');
}
function _addTab(label, content, args) {
var sID = '',
ndTab = null,
ndTabChild = null,
ndPane = null;
sID = _getUniqueId();
tabCount += 1;
// create tab header
ndTab = document.createElement('li');
ndTab.className = 'aw-tab';
ndTab.id = sID+'-label';
if (opts.useHashLinks) {
ndTabChild = document.createElement('a');
ndTabChild.href = '#'+sID+'-panel';
} else {
ndTabChild = document.createElement('span');
}
ndTabChild.className = 'aw-tab-label';
ndTabChild.innerHTML = label;
ndTab.appendChild(ndTabChild);
// tab-close link
if (args && args.home) {
ndTab.classList.add('home');
} else {
ndTabChild = document.createElement('a');
ndTabChild.className = 'aw-tab-close-button';
ndTabChild.innerHTML = '×';
ndTabChild.onclick = function(ndTarget) {
return function(e) {
atto.stopEventCascade(e);
// strip off the '-label' portion of the id
if (ndTarget.id) { _delTab(ndTarget.id.substr(0, ndTarget.id.length-6)); }
return false;
}
}(ndTab);
ndTab.appendChild(ndTabChild);
}
_tabRow.appendChild(ndTab);
// create tab pane
ndPane = document.createElement('div');
ndPane.className = 'aw-tab-panel';
ndPane.id = sID+'-panel';
ndPane.innerHTML = content;
ndTab.onclick = function(ndTarget) {
return function() {
_selectTab(this, ndTarget);
}
}(ndPane);
_tabBody.appendChild(ndPane);
if (tabCount==1) {
_selectTab(ndTab, ndPane);
}
}
// grab any child elements (not raw text nodes) to form my initial tabs
var elems = lmnt.children(_root);
for (i=0; i<nCount; i++) {
nd = elems[i];
var args = {
'home': (i==0 && opts.persistent_home)
}
_addTab(nd.title ? nd.title : "Tab " + (tabCount+1), nd.innerHTML, args);
}
_root.innerHTML = '';
_root.classList.add('aw-tabcontainer');
_root.appendChild(__frag);
return {
"root" : _root,
"tabs" : _tabs,
"currentTab": _curr,
"addTab" : _addTab,
"deleteTab" : _delTab
} // end of public interface
} // end of constructor
return constructor;
} // end function
);