Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (83 loc) · 3.26 KB
/
Copy pathscript.js
File metadata and controls
114 lines (83 loc) · 3.26 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
/*
Pseudocode for Pulling Form Data to Generate QR Code
When the form is submitted
stop the page from refreshing
read the input value from form
create a new, fresh QR code
add the input value as QR data -- done
make the QR code -- done
render the QR as SVG -- done
place the SVG inside the preview div -- done
*/
let typeNumber = 4;
let errorCorrectionLevel = 'L';
let hrefUrl = '';
const EXAMPLE = 'example.com';
const form = document.querySelector('form');
const downloadLink = document.querySelector('#downloadLink');
const urlInput = document.querySelector('#url');
const urlPreview = document.getElementById('urlPreview');
const logoInput = document.querySelector('#logo');
const logoPreview = document.querySelector('#logoPreview');
const placeHolder = document.getElementById('placeHolder');
form.addEventListener('submit', function(event){ // adds event listener to form which triggers the below function
event.preventDefault(); // prevents page reload
let url = urlInput.value.trim(); // sets url variable as the url input from form
/*
Pseudocode for Logo Upload:
store user logo as variable
render user logo on page
*/
// const logo = logoInput.files[0];
// if (logo !== null) {
// const reader = new FileReader(logo);
// reader.addEventListener('load', function(){
// console.log(reader.result);
// });
// reader.readAsText(logo);
// logoPreview.innerHTML = reader.readAsText(logo).createSvgTag();
// }
if (url === '') {
renderQrCode(EXAMPLE);
downloadLink.href = '';
downloadLink.download = 'qr-code.svg';
downloadLink.innerHTML = '';
if (hrefUrl !== ''){
URL.revokeObjectURL(hrefUrl);
hrefUrl = '';
};
alert('No URL Was Submitted: Please enter the URL you want a QR code created for.');
}
else {
const svgString = renderQrCode(url); // stores the returned value of renderQrCode as svgString variable to be passed into update function
updateDownloadLink(svgString);
};
}
);
//function to render QR code and URL
function renderQrCode(input) { // input is used as the arbitrary parameter name for passing argument
const qr = qrcode(typeNumber, errorCorrectionLevel); // by declaring variable in the function, it is refreshed each time the function runs
qr.addData(input); // add the url input as QR data
qr.make(); // make the QR code
const svgString = qr.createSvgTag();
placeHolder.innerHTML = svgString; // render the QR as SVG and place in Div
urlPreview.textContent = input; // displays url used for qr code
return svgString;
};
function updateDownloadLink(input) { // no need to use i argument since it's not used anywhere in function
if (hrefUrl !== ''){
URL.revokeObjectURL(hrefUrl);
};
const blob = new Blob([input], {type: 'image/svg+xml'});
hrefUrl = URL.createObjectURL(blob);
downloadLink.href = hrefUrl;
downloadLink.download = 'qr-code.svg';
downloadLink.innerHTML = 'Download File';
}
renderQrCode(EXAMPLE); // starting example qr code
/*
Pseudocode for creating the download feature:
Get the generated SVG string
Turn it into a downloadable file
Attach that file to a download button
*/