-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
238 lines (184 loc) · 6.46 KB
/
app.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// FAQS TOGGLES --> this one toggles only one open at a time
// Ref - http://stackoverflow.com/questions/37745154/only-open-one-accordion-tab-at-one-time
document.addEventListener("DOMContentLoaded", function(event) {
var acc = document.getElementsByClassName("accordion");
var panel = document.getElementsByClassName('panel');
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}
});
// Transitions firing onload fix
document.addEventListener("DOMContentLoaded", function() {
let node = document.querySelector('.preload-transitions');
node.classList.remove('preload-transitions');
});
// Collapsable burger on small devices + opaque background when opened
const burger = document.getElementById('burger');
const ul = document.querySelector('ul');
const links = document.querySelectorAll('ul li a');
const container = document.querySelector('.background-opacity');
burger.addEventListener('click', (e) => {
if(ul.classList != 'show'){
burger.classList.toggle('open');
ul.classList.toggle('show');
container.classList.toggle('burger-opened');
} else {
burger.classList.toggle('open');
ul.classList.toggle('show');
container.classList.toggle('burger-opened');
}
e.preventDefault();
})
burger.addEventListener('touchend', (e) => {
if(ul.classList != 'show'){
burger.classList.toggle('open');
ul.classList.toggle('show');
container.classList.toggle('burger-opened');
} else {
burger.classList.toggle('open');
ul.classList.toggle('show');
container.classList.toggle('burger-opened');
}
e.preventDefault();
})
document.addEventListener('click', (e) => {
if(e.target.parentElement.parentElement != 'ul.navbar.show'){
// console.log(e);
burger.classList.remove('open');
ul.classList.remove('show');
container.classList.remove('burger-opened');
}
})
links.forEach((link) => {
link.addEventListener('click', () => {
ul.classList.remove('show');
container.classList.remove('burger-opened');
})
})
// ScrollTop gentle (fix)
window.onload = function() {
document.body.scrollTop = document.documentElement.scrollTop = 0;
};
// FORM CONTROL - VALIDATION
const submitBtn = document.getElementById('form-submit');
const form = document.getElementById('form');
const fname = document.getElementById('fname');
const surname = document.getElementById('surname');
const street = document.getElementById('street');
const zip = document.getElementById('zip');
const dayPhone = document.getElementById('dayPhone');
const email = document.getElementById('email');
const emailVerify = document.getElementById('email-verify');
const messageTextarea = document.getElementById('message');
submitBtn.addEventListener('click', (e) => {
e.preventDefault();
checkInputs();
});
function checkInputs() {
// get values of inputs
const firstNameValue = fname.value.trim();
const surnameValue = surname.value.trim();
const streetValue = street.value.trim();
const zipValue = zip.value.trim();
const emailValue = email.value.trim();
const emailVerifyValue = emailVerify.value.trim();
const messageValue = messageTextarea.value.trim();
if(firstNameValue === '' || firstNameValue === 'Henrycounk'){
// show error
// add error class
setErrorFor(fname, 'Please provide your first name');
fname.parentElement.scrollIntoView();
return;
} else {
// add success class
setSuccessFor(fname);
}
if(surnameValue === ''){
// show error
// add error class
setErrorFor(surname, 'Please provide your last name');
surname.parentElement.scrollIntoView();
return;
} else {
// add success class
setSuccessFor(surname)
}
if(streetValue === ''){
// show error
// add error class
setErrorFor(street, 'Please provide your street');
street.parentElement.scrollIntoView();
return;
} else {
// add success class
setSuccessFor(street)
}
if(zipValue === ''){
// show error
// add error class
setErrorFor(zip, 'Please provide your zip code');
zip.parentElement.scrollIntoView();
return;
} else {
// add success class
setSuccessFor(zip)
}
if(emailValue === ''){
setErrorFor(email, 'Email cannot be blank');
email.parentElement.scrollIntoView();
return;
} else if(!isEmail(emailValue)){
setErrorFor(email, 'Email is not valid');
email.parentElement.scrollIntoView();
return;
} else {
setSuccessFor(email)
}
if(emailVerifyValue === ''){
setErrorFor(emailVerify, 'Email confirmation cannot be blank');
emailVerify.parentElement.scrollIntoView();
return;
} else if(emailValue !== emailVerifyValue){
setErrorFor(emailVerify, 'The emails provided do not match');
emailVerify.parentElement.scrollIntoView();
return;
} else {
setSuccessFor(emailVerify);
}
let spamWord = 'http';
if(messageValue.indexOf(spamWord) != -1){
setErrorFor(messageTextarea, 'Links are not allowed within comments');
return;
} else {
setSuccessFor(messageTextarea);
}
form.submit();
}
function setErrorFor(input, message){
const formControl = input.parentElement;
const smallFormControl = formControl.querySelector('small');
// add error inside small
smallFormControl.innerText = message;
// add error class
formControl.className = 'group error';
}
function setSuccessFor(input){
const formControl = input.parentElement;
formControl.className = 'group success';
}
function isEmail(email) {
return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}