-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
78 lines (66 loc) · 1.82 KB
/
script.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
67
68
69
70
71
72
73
74
75
76
77
78
const form = document.getElementById('generate-form');
const qr = document.getElementById('qrcode');
// Button submit
const onGenerateSubmit = (e) => {
e.preventDefault();
clearUI();
const url = document.getElementById('url').value;
const size = document.getElementById('size').value;
// Validate url
if (url === '') {
alert('Please enter a URL');
} else {
showSpinner();
// Show spinner for 1 sec
setTimeout(() => {
hideSpinner();
generateQRCode(url, size);
// Generate the save button after the qr code image src is ready
setTimeout(() => {
// Get save url
const saveUrl = qr.querySelector('img').src;
// Create save button
createSaveBtn(saveUrl);
}, 50);
}, 1000);
}
};
// Generate QR code
const generateQRCode = (url, size) => {
const qrcode = new QRCode('qrcode', {
text: url,
width: size,
height: size,
});
};
// Clear QR code and save button
const clearUI = () => {
qr.innerHTML = '';
const saveBtn = document.getElementById('save-link');
if (saveBtn) {
saveBtn.remove();
}
};
// Show spinner
const showSpinner = () => {
const spinner = document.getElementById('spinner');
spinner.style.display = 'block';
};
// Hide spinner
const hideSpinner = () => {
const spinner = document.getElementById('spinner');
spinner.style.display = 'none';
};
// Create save button to download QR code as image
const createSaveBtn = (saveUrl) => {
const link = document.createElement('a');
link.id = 'save-link';
link.classList =
'bg-red-500 hover:bg-red-700 text-white font-bold py-2 rounded w-1/3 m-auto my-5';
link.href = saveUrl;
link.download = 'qrcode';
link.innerHTML = 'Save Image';
document.getElementById('generated').appendChild(link);
};
hideSpinner();
form.addEventListener('submit', onGenerateSubmit);