-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
66 lines (62 loc) · 1.8 KB
/
common.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
export function getControls(containerId) {
const container = document.getElementById(containerId);
const inputs = container.querySelectorAll('input, select');
return Array.from(inputs).reduce((acc, input) => {
return Object.defineProperty(acc, input.id, {
get() {
const t = input.type;
switch (t) {
case 'checkbox':
return input.checked;
case 'number':
case 'range':
return Number(input.value);
default:
return input.value;
}
},
});
}, {});
}
export function fileInputSource(inputId, callback) {
document.getElementById(inputId).addEventListener('change', async (e) => {
const files = e.target.files;
if (!files.length) {
return;
}
for (const file of files) {
callback(file);
}
});
}
export function downloadElem() {
const a = document.createElement('a');
a.textContent = 'Download';
a.setAttribute('class', 'disabled');
return a;
}
export function downloadReady(elem, blob, name, newExt, autoDownload) {
elem.href = URL.createObjectURL(blob);
elem.setAttribute('class', '');
elem.download = name.slice(0, name.lastIndexOf('.') + 1) + newExt;
if (autoDownload) {
elem.click();
}
}
export function getFileList(listId) {
const fileListElem = document.getElementById(listId);
return {
add(file) {
const fileElem = document.createElement('li');
const nameElem = fileElem.appendChild(document.createElement('span'));
nameElem.textContent = file.name;
const aElem = fileElem.appendChild(downloadElem());
fileListElem.appendChild(fileElem);
return {
downloadReady: (blob, newExt, autoDownload) => {
downloadReady(aElem, blob, file.name, newExt, autoDownload);
},
};
},
};
}