-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.mjs
74 lines (62 loc) · 2.12 KB
/
ui.mjs
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
const options = [
{
label: 'sprite scale',
name: 'scale',
options: {
'1': '1',
'2': '2',
'3': '3'
}
},
{
label: 'step mode',
name: 'stepMode',
options: {
'500 ms between steps': '500msPerStep',
'render at 10 fps': '10fps',
'render at end only': 'onlyEnd',
}
},
];
export function ui() {
const u = new URL(location.href);
const searchParams = u.searchParams;
const values = {};
const uiEl = document.createElement('div');
uiEl.className = 'ui';
const repoDivEl = document.createElement('div');
const aEl = document.createElement('a');
aEl.appendChild(document.createTextNode('github repo'));
aEl.href = 'https://github.com/josepedrodias/wave-function-collapse';
aEl.target = '_blank';
repoDivEl.appendChild(aEl);
uiEl.appendChild(repoDivEl);
options.forEach((opt) => {
const divEl = document.createElement('div');
const labelEl = document.createElement('label');
labelEl.setAttribute('for', opt.name);
labelEl.appendChild( document.createTextNode(opt.label) );
divEl.appendChild(labelEl);
const selectEl = document.createElement('select');
selectEl.id = opt.name;
let currentValue = searchParams.get(opt.name);
for (const [k, v] of Object.entries(opt.options)) {
const optionEl = document.createElement('option');
optionEl.appendChild( document.createTextNode(k) );
optionEl.value = v;
if (currentValue === null) currentValue = v;
if (v === currentValue) optionEl.selected = true;
selectEl.appendChild(optionEl);
}
values[opt.name] = currentValue;
selectEl.addEventListener('change', () => {
const newValue = selectEl.value;
searchParams.set(opt.name, newValue);
location.search = searchParams.toString();
});
divEl.appendChild(selectEl);
uiEl.appendChild(divEl);
});
document.body.appendChild(uiEl);
return values;
}