-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtabbedView.js
More file actions
158 lines (131 loc) · 3.59 KB
/
tabbedView.js
File metadata and controls
158 lines (131 loc) · 3.59 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
import style from './style.js';
style.add(
// Wrapper
'.tabbedView { display: flex; flex-flow: row wrap; align-content: flex-start; }',
'.tabbedView.left, .tabbedView.right { flex-flow: column wrap; }',
'.tabbedView.right > .tabLabel { order: 3; }',
// Hide things that shouldn't be shown
'.tabButton, .tabContent { display: none; }',
'.tabContent { order: 2; flex: 1 100%; }',
// Show current tab
'.tabButton:checked + .tabLabel + .tabContent { display: flex; }',
// Make the labels look nice
'.tabLabel { display: flex; padding: 2px 5px; border: 1px solid white; margin-right: 5px; }',
'.tabLabel.end { order: 1; }',
'.tabButton:checked + .tabLabel { background-color: rgb(68, 100, 189); }',
'.tabbedView.left > .tabLabel {}',
'.tabbedView.right > .tabLabel {}',
);
let groupID = 0;
export default function TabManager() {
const group = groupID;
groupID += 1;
const tabs = [];
const tabSettings = {
left: false,
};
const view = document.createElement('div');
view.classList.add('tabbedView');
function addTab(name = '', content = '') {
const id = tabs.length ? tabs[tabs.length - 1].id + 1 : 0;
const elements = newTab(`${group}-${id}`, group);
const tab = {
id,
elements,
content,
// Set first tab as active by default
active: tabs.length === 0,
};
tabs.push(tab);
function setName(value = name) {
elements[1].textContent = value;
}
function setContent(value = content) {
tab.content = value;
if (typeof value === 'string') {
elements[2].innerHTML = value;
}
}
function setEnd(value = false) {
elements[1].classList.toggle('end', value === true);
}
function setActive() {
if (tab.active) return;
tabs.forEach((t) => t.active = false);
tab.active = true;
}
// Initialize
setName();
setContent();
setEnd();
const wrapper = {
id,
setName,
setContent,
setEnd,
setActive,
};
Object.defineProperty(wrapper, 'active', {
get() {
return tab.active;
},
enumerable: true,
});
return wrapper;
}
function render(raw = false) {
view.classList.toggle('left', tabSettings.left);
// Update content of tabs
tabs.forEach(({
elements: [button, label, content],
content: contents,
active = false,
}) => {
if (active) {
button.checked = true;
}
let value = contents;
if (typeof value === 'function') {
value = value();
} else if (typeof value.render === 'function') {
value = value.render(true);
}
if (typeof value === 'string') {
content.innerHTML = value;
} else if (value instanceof HTMLElement) {
content.innerHTML = '';
content.appendChild(value);
} else return;
// Add tab to view
view.appendChild(button);
view.appendChild(label);
view.appendChild(content);
});
if (raw) return view;
return view.outerHTML;
}
function settings({
left = false,
}) {
tabSettings.left = left;
}
return {
addTab,
render,
settings,
};
}
function newTab(id = 0, group = 0) {
const name = `tab${id}`;
const button = document.createElement('input');
button.id = name;
button.type = 'radio';
button.name = `view${group}`;
button.classList.add('tabButton');
const label = document.createElement('label');
label.classList.add('tabLabel');
label.htmlFor = name;
const content = document.createElement('div');
content.classList.add('tabContent');
return [button, label, content];
}