-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (75 loc) · 2.89 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict"
const marginSLider = document.querySelector("#slider-marg");
const sizeSlider = document.querySelector("#slider-size");
const qrColor = document.querySelector("#qr-color");
const backgroundColor = document.querySelector("#bg-color");
const dataInput = document.querySelector("#detail");
const imageFormat = document.querySelector('input[name="img-format"]:checked');
const submitButton = document.querySelector("#finalSubmit");
const infoContainer = document.querySelector("#qr-code-info");
const resContainer = document.querySelector("#qr-code-result");
const qrCodeImg = document.querySelector("#qr-code-image");
const goBack = document.querySelector("#edit");
marginSLider.addEventListener("change",function(){
const margVal = marginSLider.value;
document.querySelector("#marg-val").innerHTML = margVal + "px";
});
sizeSlider.addEventListener("change",function(){
const sizeVal = sizeSlider.value;
document.querySelector("#size-val").innerHTML = sizeVal + " x " + sizeVal;
});
qrColor.addEventListener("change",function(){
const qrColorVal = qrColor.value;
document.querySelector("#qr-color-val").innerHTML = qrColorVal;
});
backgroundColor.addEventListener("change",function(){
const bgColorVal = backgroundColor.value;
document.querySelector("#bg-color-val").innerHTML = bgColorVal;
});
dataInput.addEventListener("input", function() {
submitButton.disabled = dataInput.value.trim() === "";
});
const prepareParameters = (params) =>{
return{
data: params.data,
size: params.size,
color: params.color.replace('#',''),
bgcolor: params.bgColor.replace('#',''),
margin: params.margin,
format: params.format
}
}
const dsiplayQrCode = (imgURL) =>{
infoContainer.classList.add("flipped");
resContainer.classList.add("flipped");
qrCodeImg.setAttribute('src', imgURL);
}
const getQrCode = (parameters) =>{
const urlParams = new URLSearchParams(prepareParameters(parameters)).toString();
const url = "https://api.qrserver.com/v1/create-qr-code/";
fetch(`${url}?${urlParams}`).then(response=>{
if(response.status===200){
dsiplayQrCode(`${url}?${urlParams}`);
}
});
}
submitButton.addEventListener("click",function(e){
e.preventDefault();
if(qrColor.value===backgroundColor.value){
alert("The colour of the QR code and background cant be the same");
return;
}
console.log("clicked");
const data = dataInput.value;
const size = sizeSlider.value;
const color = qrColor.value;
const bgColor = backgroundColor.value;
const margin = marginSLider.value;
const format = imageFormat.id;
const parameters = ({data, size, color, bgColor, margin, format});
getQrCode(parameters);
});
goBack.addEventListener("click",function(){
infoContainer.classList.remove("flipped");
resContainer.classList.remove("flipped");
});