forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
388 lines (388 loc) · 14.6 KB
/
tabs.js
File metadata and controls
388 lines (388 loc) · 14.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
import { cancelButton } from './widgets';
import { label } from './utils';
import { style } from './style';
import { store } from 'solid-logic-jss';
/**
* @ignore
*/
class ContainerElement extends HTMLElement {
}
export class TabWidgetElement extends HTMLElement {
}
/**
* @ignore
*/
class TabElement extends HTMLElement {
}
/**
* Use this widget to generate tabs from triples set in the global store.
*
* [Here you can see examples of the tabs](https://solidos.github.io/solid-ui/examples/tabs/).
*
* It assumes that items to use for tabs will be in a collection by default,
* e.g.:
*
* ```turtle
* :subject :predicate ( :item1 :item2 ) .
* ```
*
* You can override this by setting `ordered: false`, in which case it expects
* unordered triples:
*
* ```turtle
* :subject :predicate :item1, :item 2 .
* ```
*
* Triples that are not ordered in collection are in principle not sorted,
* which means that tabs could change order every time you render the widget.
* But in this case the widget will try to sort it in order to keep it
* consistent.
*
* In both of these cases you need to define options `subject` and `predicate`
* to tell the widget which triples it should be looking for.
*
* Finally you can set items manually, using the `items` option, e.g.:
*
* ```javascript
* {
* items: [
* namedNode('https://domain.tld/#item1'),
* namedNode('https://domain.tld/#item2')
* ]
* }
* ```
*
* When you set items manually you do not need to set `subject` and
* `predicate`.
*
* In any case you probably want to set the renderMain option to specify
* what should be rendered for the various items, e.g.:
*
* ```javascript
* {
* renderMain: (bodyMain, subject) => {
* bodyMain.innerHTML = renderItem(subject)
* }
* }
* ```
*
* **Note:** `renderItem` is a custom function that you need to define yourself.
*
* The option `renderTabSettings` allows you to render a custom view in the
* body container that is shown when you hold the ALT key and click on a
* tab. It works very much like the `renderMain` option:
*
* ```javascript
* {
* renderTabSettings: (bodyMain, subject) => {
* bodyMain.innerHTML = renderTabSettings(subject)
* }
* }
* ```
*
* **Note:** `renderTabSettings` is a custom function that you need to define
* yourself.
*
* By default the widget will try to guess the label by using the
* [[utils.label]] function. If you want to customize this yourself, you can
* use the `renderTab` option:
*
* ```javascript
* {
* renderTab: (tabDiv, subject) => {
* tabDiv.innerText = renderTabText(subject)
* }
* }
* ```
*
* **Note:** `renderTabText` is a custom function you need to define yourself.
*
* The option renderTab is also important if you want to set which tab should
* be selected once the widget is rendered. By default it will simply select
* the first tab, but you can override by setting `dataset.name` on the tab
* and referring to the same string in `selectedTab`:
*
* ```javascript
* {
* renderTab: (tabDiv, subject) => {
* tabDiv.dataset.name = subject.uri
* },
* selectedTab: item2.uri
* }
* ```
*
* You can apply a color to use for tabs and border of the container by using
* option `background-color`. This is #ddddcc by default.
*
* You can override the document object that the widget uses to generate DOM
* elements by setting the option `dom`. This is encouraged to set if you
* intend your functionality to be used in environments that don't provide
* a global `document` object.
*
* If you want to render a close button next to the tabs you can set option
* `onClose` which takes a callback function that is triggered when the
* button is clicked:
*
* ```javascript
* {
* onClose: (event) => {
* // do something that hides the widget altogether
* }
* }
* ```
*
* The option `orientation` allows you to set which side the tabs should be
* located: `'0'` = Top, `'1'` = Left, `'2'` = Bottom, `'3'` = Right
*
* If you don't want to render anything in the body container by default,
* you can set the option `startEmpty` to `true`.
*
* @param options
*/
const tabsDefaultBackgroundColor = '#ddddcc';
export function tabWidget(options) {
const subject = options.subject;
const dom = options.dom || document;
const orientation = parseInt(options.orientation || '0');
const backgroundColor = options.backgroundColor || tabsDefaultBackgroundColor;
const flipped = orientation & 2;
const vertical = orientation & 1;
const onClose = options.onClose;
const [selectedColor, color] = getColors(backgroundColor);
const bodyMainStyle = `flex: 2; width: auto; height: 100%; border: 0.1em; border-style: solid; border-color: ${selectedColor}; padding: 1em;`;
const rootElement = dom.createElement('div'); // 20200117a
rootElement.setAttribute('style', style.tabsRootElement);
rootElement.style.flexDirection = (vertical ? 'row' : 'column') + (flipped ? '-reverse' : '');
const navElement = rootElement.appendChild(dom.createElement('nav'));
navElement.setAttribute('style', style.tabsNavElement);
const mainElement = rootElement.appendChild(dom.createElement('main'));
mainElement.setAttribute('style', style.tabsMainElement); // override tabbedtab.css
const tabContainer = navElement.appendChild(dom.createElement('ul'));
tabContainer.setAttribute('style', style.tabContainer);
tabContainer.style.flexDirection = `${vertical ? 'column' : 'row'}`;
const tabElement = 'li';
const bodyContainer = mainElement;
rootElement.tabContainer = tabContainer;
rootElement.bodyContainer = bodyContainer;
const corners = ['0.2em', '0.2em', '0', '0']; // top left, TR, BR, BL
const cornersPrepped = corners.concat(corners).slice(orientation, orientation + 4);
const cornersStyle = `border-radius: ${cornersPrepped.join(' ')};`;
const margins = ['0.3em', '0.3em', '0', '0.3em']; // top, right, bottom, left
const marginsPrepped = margins.concat(margins).slice(orientation, orientation + 4);
const marginsStyle = `margin: ${marginsPrepped.join(' ')};`;
const paddingStyle = `padding: ${marginsPrepped.join(' ')};`;
const tabStyle = cornersStyle + `position: relative; padding: 0.7em; max-width: 20em; color: ${color};`;
const unselectedStyle = `${tabStyle + marginsStyle} opacity: 50%; background-color: ${backgroundColor};`;
const selectedStyle = `${tabStyle + marginsStyle} background-color: ${selectedColor};`;
const shownStyle = 'height: 100%; width: 100%;';
const hiddenStyle = shownStyle + 'display: none;';
rootElement.refresh = orderedSync;
orderedSync();
if (!options.startEmpty && tabContainer.children.length && options.selectedTab) {
const selectedTab0 = Array.from(tabContainer.children) // Version left for compatibility with ??
.map((tab) => tab.firstChild)
.find((tab) => tab.dataset.name === options.selectedTab);
const selectedTabURI = options.selectedTab.uri;
const selectedTab1 = Array.from(tabContainer.children)
// @ts-ignore
.find((tab) => tab.subject &&
// @ts-ignore
tab.subject.uri &&
// @ts-ignore
tab.subject.uri === selectedTabURI);
const tab = selectedTab1 || selectedTab0 || tabContainer.children[0];
const clickMe = tab.firstChild;
// @ts-ignore
if (clickMe)
clickMe.click();
}
else if (!options.startEmpty) {
tabContainer.children[0].firstChild.click(); // Open first tab
}
return rootElement;
function addCancelButton(tabContainer) {
if (tabContainer.dataset.onCloseSet) {
// @@ TODO: this is only here to make the browser tests work
// Discussion at https://github.com/solidos/solid-ui/pull/110#issuecomment-527080663
const existingCancelButton = tabContainer.querySelector('.unstyled');
tabContainer.removeChild(existingCancelButton);
}
const extraTab = dom.createElement(tabElement);
extraTab.classList.add('unstyled');
const tabCancelButton = cancelButton(dom, onClose);
tabCancelButton.setAttribute('style', tabCancelButton.getAttribute('style') + paddingStyle);
extraTab.appendChild(tabCancelButton);
tabContainer.appendChild(extraTab);
tabContainer.dataset.onCloseSet = 'true';
}
function getItems() {
if (options.items)
return options.items;
if (options.ordered !== false) {
// options.ordered defaults to true
return store.the(subject, options.predicate).elements;
}
else {
return store.each(subject, options.predicate);
}
}
function makeNewSlot(item) {
const ele = dom.createElement(tabElement);
ele.setAttribute('style', unselectedStyle);
ele.subject = item;
const div = ele.appendChild(dom.createElement('button'));
div.setAttribute('style', style.makeNewSlot);
div.onclick = function () {
resetTabStyle();
resetBodyStyle();
ele.setAttribute('style', selectedStyle);
if (!ele.bodyTR)
return;
ele.bodyTR.setAttribute('style', shownStyle);
const bodyMain = getOrCreateContainerElement(ele);
if (options.renderMain && ele.subject && bodyMain.asSettings !== false) {
bodyMain.innerHTML = 'loading item ...' + item;
options.renderMain(bodyMain, ele.subject);
bodyMain.asSettings = false;
}
};
if (options.renderTabSettings && ele.subject) {
const ellipsis = dom.createElement('button');
ellipsis.textContent = '...';
ellipsis.setAttribute('style', style.ellipsis);
ellipsis.onclick = function () {
resetTabStyle();
resetBodyStyle();
ele.setAttribute('style', selectedStyle);
if (!ele.bodyTR)
return;
ele.bodyTR.setAttribute('style', shownStyle);
const bodyMain = getOrCreateContainerElement(ele);
if (options.renderTabSettings && ele.subject && bodyMain.asSettings !== true) {
bodyMain.innerHTML = 'loading settings ...' + item;
options.renderTabSettings(bodyMain, ele.subject);
bodyMain.asSettings = true;
}
};
ele.appendChild(ellipsis);
}
if (options.renderTab) {
options.renderTab(div, item);
}
else {
div.innerHTML = label(item);
}
return ele;
function getOrCreateContainerElement(ele) {
var _a;
const bodyMain = (_a = ele.bodyTR) === null || _a === void 0 ? void 0 : _a.children[0];
if (bodyMain)
return bodyMain;
const newBodyMain = ele.bodyTR.appendChild(dom.createElement('main'));
newBodyMain.setAttribute('style', bodyMainStyle);
return newBodyMain;
}
}
// @@ Use common one from utils?
function orderedSync() {
const items = getItems();
let slot, i, j, left, right;
let differ = false;
// Find how many match at each end
for (left = 0; left < tabContainer.children.length; left++) {
slot = tabContainer.children[left];
if (left >= items.length || (slot.subject && !slot.subject.sameTerm(items[left]))) {
differ = true;
break;
}
}
if (!differ && items.length === tabContainer.children.length) {
return; // The two just match in order: a case to optimize for
}
for (right = tabContainer.children.length - 1; right >= 0; right--) {
slot = tabContainer.children[right];
j = right - tabContainer.children.length + items.length;
if (slot.subject && !slot.subject.sameTerm(items[j])) {
break;
}
}
// The elements left ... right in tabContainer.children do not match
const insertables = items.slice(left, right - tabContainer.children.length + items.length + 1);
while (right >= left) {
// remove extra
tabContainer.removeChild(tabContainer.children[left]);
bodyContainer.removeChild(bodyContainer.children[left]);
right -= 1;
}
for (i = 0; i < insertables.length; i++) {
const newSlot = makeNewSlot(insertables[i]);
const newBodyDiv = dom.createElement('div');
newSlot.bodyTR = newBodyDiv;
if (left === tabContainer.children.length) {
// None left of original on right
tabContainer.appendChild(newSlot);
bodyContainer.appendChild(newBodyDiv);
}
else {
tabContainer.insertBefore(newSlot, tabContainer.children[left + i]);
bodyContainer.insertBefore(newBodyDiv, bodyContainer.children[left + i]);
}
}
if (onClose) {
addCancelButton(tabContainer);
}
}
function resetTabStyle() {
for (let i = 0; i < tabContainer.children.length; i++) {
const tab = tabContainer.children[i];
if (tab.classList.contains('unstyled')) {
continue;
}
else {
tab.setAttribute('style', unselectedStyle);
}
}
}
function resetBodyStyle() {
for (let i = 0; i < bodyContainer.children.length; i++) {
bodyContainer.children[i].setAttribute('style', hiddenStyle);
}
}
}
/**
* @internal
*/
function getColors(backgroundColor) {
return isLight(backgroundColor)
? [colorBlend(backgroundColor, '#ffffff', 0.3), '#000000']
: [colorBlend(backgroundColor, '#000000', 0.3), '#ffffff'];
}
/**
* @internal
*/
function colorBlend(a, b, mix) {
let ca, cb, res;
let str = '#';
const hex = '0123456789abcdef';
for (let i = 0; i < 3; i++) {
ca = parseInt(a.slice(i * 2 + 1, i * 2 + 3), 16);
cb = parseInt(b.slice(i * 2 + 1, i * 2 + 3), 16);
res = ca * (1.0 - mix) + cb * mix; // @@@ rounding
const res2 = parseInt(('' + res).split('.')[0]); // @@ ugh
const h = parseInt(('' + res2 / 16).split('.')[0]); // @@ ugh
const l = parseInt(('' + (res2 % 16)).split('.')[0]); // @@ ugh
str += hex[h] + hex[l];
}
return str;
}
/**
* @internal
*/
function isLight(x) {
let total = 0;
for (let i = 0; i < 3; i++) {
total += parseInt(x.slice(i * 2 + 1, i * 2 + 3), 16);
}
return total > 128 * 3;
}
//# sourceMappingURL=tabs.js.map