-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (112 loc) · 3.88 KB
/
script.js
File metadata and controls
145 lines (112 loc) · 3.88 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// COLORS
const mainColorPicker = document.querySelector('#color');
const mainColorValue = document.querySelector('#color-value');
const backgroundColorPicker = document.querySelector('#bg-color');
const backgroundColorValue = document.querySelector('#bg-color-value');
const updateColor = e => {
const value = e.target.value;
mainColorValue.innerText = value;
};
const updateBackgroundColor = e => {
const value = e.target.value;
backgroundColorValue.innerText = value;
};
const addColorPickerEventListeners = () => {
mainColorPicker.addEventListener('change', updateColor);
backgroundColorPicker.addEventListener('change', updateBackgroundColor);
};
addColorPickerEventListeners();
// SLIDERS
const sizeSlider = document.querySelector('#size');
const sizeValue = document.querySelector('#size-value');
const marginSlider = document.querySelector('#margin');
const marginValue = document.querySelector('#margin-value');
const updateSize = e => {
const value = e.target.value;
sizeValue.innerText = `${value} x ${value}`;
};
const updateMargin = e => {
const value = e.target.value;
marginValue.innerText = `${value} px`;
};
const addSliderEventListeners = () => {
sizeSlider.addEventListener('change', updateSize);
marginSlider.addEventListener('change', updateMargin);
};
addSliderEventListeners();
// URL / TEXT / DATA
const dataInput = document.querySelector('#data');
// FORMAT
const imageFormat = document.querySelector('input[name="format"]:checked');
// BUTTON
const submitButton = document.querySelector('#cta');
const prepareParameters = params => {
const prepared = {
data: params.data,
size: `${params.size}x${params.size}`,
color: params.color.replace('#', ''),
bgcolor: params.bgColor.replace('#', ''),
qzone: params.qZone,
format: params.format,
};
return prepared;
};
const settingsContainer = document.querySelector('#qr-code-settings');
const resultsContainer = document.querySelector('#qr-code-result');
const qrCodeImage = document.querySelector('#qr-code-image');
const displayQrCode = imgUrl => {
settingsContainer.classList.add('flipped');
resultsContainer.classList.add('flipped');
qrCodeImage.setAttribute('src', imgUrl);
};
const getQrCode = parameters => {
const baseUrl = 'https://api.qrserver.com/v1/create-qr-code/';
const urlParams = new URLSearchParams(parameters).toString();
const fullUrl = `${baseUrl}?${urlParams}`;
fetch(fullUrl).then(response => {
if (response.status === 200) {
displayQrCode(fullUrl);
}
});
};
const showInputError = () => {
dataInput.classList.add('error');
};
const dataInputEventListener = () => {
dataInput.addEventListener('change', e => {
if (e.target.value !== '') {
dataInput.classList.remove('error');
submitButton.removeAttribute('disabled');
} else {
dataInput.classList.add('error');
submitButton.setAttribute('disabled', true);
}
});
};
dataInputEventListener();
const onSubmit = () => {
const data = dataInput.value;
if (!data.length) {
return showInputError();
}
const color = mainColorPicker.value;
const bgColor = backgroundColorPicker.value;
const size = sizeSlider.value;
const qZone = marginSlider.value;
const format = imageFormat.value;
const parameters = prepareParameters({ data, color, bgColor, size, qZone, format });
getQrCode(parameters);
};
const addSubmitEventListener = () => {
submitButton.addEventListener('click', onSubmit);
};
addSubmitEventListener();
const editButton = document.querySelector('#edit');
const onEdit = () => {
settingsContainer.classList.remove('flipped');
resultsContainer.classList.remove('flipped');
};
const addEditEventListener = () => {
editButton.addEventListener('click', onEdit);
};
addEditEventListener();