-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabPanel.js
More file actions
414 lines (336 loc) · 12.6 KB
/
TabPanel.js
File metadata and controls
414 lines (336 loc) · 12.6 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** TabPanel
//******************************************************************************
/**
* Standard tab control used to show/hide individual panels.
*
******************************************************************************/
javaxt.dhtml.TabPanel = function(parent, config) {
this.className = "javaxt.dhtml.TabPanel";
var me = this;
var tabList;
var tabContent;
var defaultConfig = {
/** If true, will insert a "close" icon into the tab that will allow
* users to close/remove the tab from the tab panel.
*/
closable: false,
/** Style for individual elements within the component. Note that you can
* provide CSS class names instead of individual style definitions.
*/
style : {
tabBar: {
border: "1px solid #ccc",
backgroundColor: "#eaeaea",
height: "30px",
borderBottom: "0px"
},
activeTab: {
lineHeight: "30px",
padding: "0 7px",
backgroundColor: "#fafafa",
cursor: "default",
borderRight: "1px solid #ccc",
borderBottom: "1px solid #fafafa"
},
inactiveTab: {
lineHeight: "30px",
padding: "0 7px",
cursor: "pointer",
borderRight: "1px solid #ccc",
borderBottom: "0px"
},
tabBody: {
border: "1px solid #ccc",
verticalAlign: "top"
},
closeIcon: {
}
}
};
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
//Create main table
var table = createTable(parent);
table.setAttribute("desc", me.className);
//Row 1
var td = table.addRow().addColumn();
setStyle(td, "tabBar");
td.style.width = "100%";
tabList = createElement("ul", td, {
listStyle: "none outside none",
height: "100%",
padding: 0,
margin: 0
});
//Row 2
var td = table.addRow().addColumn();
setStyle(td, "tabBody");
td.style.width = "100%";
td.style.height = "100%";
tabContent = createElement("div", td, {
width: "100%",
height: "100%",
position: "relative"
});
me.el = table;
};
//**************************************************************************
//** addTab
//**************************************************************************
/** Used to add a new tab to the panel.
* @param label Tab title.
* @param el Tab contents. Rendered when the tab is active. Accepts strings,
* DOM elements, and nulls
*/
this.addTab = function(label, el){
var div = createElement("div", tabContent, {
width: "100%",
height: "100%",
position: "absolute"
});
if (el==null) div.innerHTML = "";
else{
if (isElement(el)){
var p = el.parentNode;
if (p) p.removeChild(el);
div.appendChild(el);
}
else{
if (typeof el === "string"){
div.innerHTML = el;
}
}
}
var tab = createElement("li", tabList);
setStyle(tab, "inactiveTab");
tab.style.position = "relative";
tab.style.float = "left";
tab.style.height = "100%";
tab.innerHTML = label;
tab.el = div;
tab.onclick = function(){
raiseTab(this);
};
tab.onselectstart = function () {return false;};
tab.onmousedown = function () {return false;};
for (var i=0; i<tabList.childNodes.length; i++){
var t = tabList.childNodes[i];
if (t!==tab){
setStyle(t, "inactiveTab");
t.style.position = "relative";
t.style.float = "left";
t.style.height = "100%";
hideTabContent(t);
}
}
div.style.display='none'; //<-- style used to test whether the tab is visible (see raiseTab)
raiseTab(tab);
};
//**************************************************************************
//** getTabs
//**************************************************************************
/** Returns an of tabs in the tab panel. Each entry in the array will
* include the following:
* <ul>
* <li>name: Name of the tab and tab label</li>
* <li>header: DOM element for the tab header</li>
* <li>body: DOM element for the tab content</li>
* <li>hidden: Boolean</li>
* <li>active: Boolean</li>
* </ul>
*/
this.getTabs = function(){
var tabs = [];
for (var i=0; i<tabList.childNodes.length; i++){
var tab = tabList.childNodes[i];
tabs.push(getTabInfo(tab));
}
return tabs;
};
//**************************************************************************
//** raiseTab
//**************************************************************************
/** Used to raise a tab
* @param id Accepts a tab index (stating at 0) or a tab name
*/
this.raiseTab = function(id){
var tab = findTab(id);
if (tab) raiseTab(tab);
};
var raiseTab = function(tab){
if (tab.style.display === 'none') return; //tab is hidden
if (tab.el.style.display === 'none'){
//Find current tab
var currTab = null;
for (var i=0; i<tabList.childNodes.length; i++){
var t = tabList.childNodes[i];
if (t.el.style.display === 'block'){
currTab = t;
break;
}
}
//Make tab active
setStyle(tab, "activeTab");
tab.style.position = "relative";
tab.style.float = "left";
tab.style.height = "100%";
tab.el.style.display = '';
//Make other tabs inactive
for (var i=0; i<tabList.childNodes.length; i++){
var t = tabList.childNodes[i];
if (t!==tab){
if (t.style.display !== 'none'){
setInactive(t);
}
}
}
//Display tab content
tab.el.style.display = 'block';
tab.el.style.zIndex = 1; //this was added so groupboxes in forms would render correctly
//Call onTabChange
me.onTabChange(getTabInfo(tab), getTabInfo(currTab));
}
};
//**************************************************************************
//** getTabInfo
//**************************************************************************
var getTabInfo = function(tab){
if (tab==null) return null;
var hidden = (tab.style.display === 'none');
var active = (tab.el.style.display !== 'none');
return {
name: tab.innerText,
el: tab.el, //should be renamed to body or content
header: tab,
body: tab.el,
hidden: hidden,
active: active
};
};
//**************************************************************************
//** setActiveTab
//**************************************************************************
/** Same as raiseTab()
* @param id Accepts a tab index (stating at 0) or a tab name
*/
this.setActiveTab = this.raiseTab;
//**************************************************************************
//** onTabChange
//**************************************************************************
/** Called whenever a tab is raised.
*/
this.onTabChange = function(currTab, prevTab){};
//**************************************************************************
//** removeTab
//**************************************************************************
/** Used to remove a tab from the tab panel.
* @param id Accepts a tab index (stating at 0) or a tab name
*/
this.removeTab = function(id){
var tab = findTab(id);
if (tab){
var nextTab = tab.nextSibling;
if (!nextTab) nextTab = tab.previousSibling;
tabContent.removeChild(tab.el);
tabList.removeChild(tab);
if (nextTab) raiseTab(nextTab);
}
};
//**************************************************************************
//** hideTab
//**************************************************************************
/** Used to hide a tab in the tab panel. Unlike the removeTab() method, the
* tab will remain in the tab panel, but in a hidden state.
* @param id Accepts a tab index (stating at 0) or a tab name
*/
this.hideTab = function(id){
var tab = findTab(id);
if (tab){
if (tab.style.display === 'none') return;
var nextTab = tab.nextSibling;
if (!nextTab) nextTab = tab.previousSibling;
setInactive(tab);
tab.style.display = 'none';
hideTabContent(tab);
if (nextTab) raiseTab(nextTab);
}
};
//**************************************************************************
//** showTab
//**************************************************************************
/** Used to make a hidden tab visible. See hideTab()
* @param id Accepts a tab index (stating at 0) or a tab name
*/
this.showTab = function(id){
var tab = findTab(id);
if (tab) tab.style.display = '';
};
//**************************************************************************
//** findTab
//**************************************************************************
var findTab = function(id){
if (isNaN(id)){
if (typeof id === "string"){
for (var i=0; i<tabList.childNodes.length; i++){
var t = tabList.childNodes[i];
if (t.innerHTML === id ){
return t;
}
}
}
}
else{
if (id<tabList.childNodes.length){
return tabList.childNodes[id];
}
}
return null;
};
//**************************************************************************
//** setInactive
//**************************************************************************
/** Updated the style of a given tab and hides its contents.
*/
var setInactive = function(tab){
setStyle(tab, "inactiveTab");
tab.style.position = "relative";
tab.style.float = "left";
tab.style.height = "100%";
hideTabContent(tab);
};
//**************************************************************************
//** hideTabContent
//**************************************************************************
/** Used to hide the element/panel associated with a tab
*/
var hideTabContent = function(tab){
tab.el.style.display = 'none';
tab.el.style.zIndex = '';
};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
var createTable = javaxt.dhtml.utils.createTable;
var createElement = javaxt.dhtml.utils.createElement;
var isElement = javaxt.dhtml.utils.isElement;
var setStyle = function(el, style){
javaxt.dhtml.utils.setStyle(el, config.style[style]);
};
init();
};