-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
80 lines (65 loc) · 2.62 KB
/
main.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
75
76
77
78
79
80
import Settings from "./settings.mjs";
import Log from "./log.mjs";
import Barcode from "./barcode/barcode.mjs";
const demo_itf_text = "056055"
const problematic_themes = ["dark"];
const elements = {
"container": document.querySelector('#container'),
"input": document.querySelector('#data-input'),
"barcode_container": document.querySelector('#barcode-container'),
"barcode": document.querySelector('#barcode'),
"settings": Object.assign({}, ...Array.from(document.querySelectorAll('#settings-container > *[id]'), (x, i) => ({[x.id]: x}))),
"log_container": document.querySelector('#log-container'),
"log": document.querySelector('#log'),
"problematic_theme_warning": document.querySelector('#problematic-theme.warning')
};
window.elements = elements;
elements.container.reset();
window.settings = Settings;
Log.attachToContainer(elements.log);
function generateBarcode(input_string="") {
Log.clear();
if(input_string.length === 0) {
Log.appendWarning("Creating demo barcode as input is empty");
elements.barcode.innerText = Barcode.itf(demo_itf_text);
} else {
try {
elements.barcode.innerText = Barcode[window.settings.symbology](input_string);
} catch(e) {
elements.barcode.innerText = "Error :[";
Log.appendError(e);
}
}
elements.log_container.scrollTop = elements.log_container.scrollHeight;
}
function setBarcodeClassName(settings) {
elements.barcode.className = `font-size-${settings.font_size} font-${settings.font} theme-${settings.theme}`;
}
elements.input.oninput = (event) => generateBarcode(event.target.value);
elements.barcode_container.onclick = (event) => {
const range = document.createRange();
range.selectNode(event.target);
window.getSelection().empty();
window.getSelection().addRange(range);
const selection_text = window.getSelection()?.toString().trim();
if(selection_text) navigator.clipboard.writeText(selection_text);
};
elements.settings.symbology.onchange = (event) => window.setTimeout(() => generateBarcode(elements.input.value), 1);
elements.settings.theme.onchange = (event) => {
if(problematic_themes.includes(event.target.value)) {
elements.problematic_theme_warning.classList.add("visible");
} else {
elements.problematic_theme_warning.classList.remove("visible");
}
}
for(const key of Object.keys(window.settings)) {
if(elements.settings[key] === undefined) continue;
elements.settings[key].addEventListener("change", (event) => {
window.settings[key] = event.target.value;
window.settings.save();
setBarcodeClassName(window.settings);
});
elements.settings[key].value = window.settings[key];
elements.settings[key].dispatchEvent(new Event("change"));
}
generateBarcode();