-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
154 lines (131 loc) · 4.54 KB
/
scripts.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
"use strict";
function asyncRequest(xhr, body) {
return new Promise(function(resolve, reject) {
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function() {
reject({
status: this.status,
statusText: xhr.statusText
});
};
if (body)
xhr.send(body);
else
xhr.send();
});
}
const photos = {
innsbruck: {
title: "Innsbruck",
text: "Naše první společná dovolená.",
},
oscadnica: {
title: "Oščadnica",
text: "Zimní nádhera na snowboardech.",
},
rysy: {
title: "Rysy",
text: "Cesta, která nás v mnohém otestovala.",
},
corgitrek: {
title: "Corgi Trek",
text: "Závod s naším krátkonohým parťákem.",
},
};
function onDocumentLoad(event) {
const mediaQuery = window.matchMedia('screen');
if (!mediaQuery.matches)
return;
const body = document.body;
const lightbox = document.getElementById('lightbox');
function onGalleryPhotoClick(event) {
event.stopPropagation();
event.preventDefault();
const photoId = event.target.attributes['data-photo'].value;
const photo = photos[photoId];
body.classList.add("modal-background");
lightbox.classList.add("visible");
lightbox.innerHTML = `
<picture>
<img src="media/${photoId}-full.jpg">
</picture>
<h3>${photo.title}</h3>
<p>${photo.text}</p>
`;
}
function onLightboxClick(event) {
event.stopPropagation();
event.preventDefault();
lightbox.classList.remove("visible");
body.classList.remove("modal-background");
}
lightbox.addEventListener('click', onLightboxClick);
const galleries = document.getElementsByClassName('gallery');
for (const gallery of galleries) {
for (const photo of gallery.children) {
photo.addEventListener('click', onGalleryPhotoClick);
}
}
const formSection = document.getElementById('form');
const formStatus = document.getElementById('form-status');
const form = formSection.children[1];
function onFormSubmit(event) {
event.preventDefault();
event.submitter.disabled = true;
formStatus.classList.remove("hidden", "bi-circle");
formStatus.classList.remove("error", "bi-exclamation-circle-fill");
formStatus.classList.remove("success", "bi-check-circle-fill");
formStatus.classList.add("spin-before", "bi-arrow-repeat");
formStatus.innerHTML = "Odesílám";
const form = event.currentTarget;
const formData = new FormData(form);
const xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
function asyncSybmit_fulfilled() {
event.submitter.disabled = false;
formStatus.classList.remove("hidden", "bi-circle");
formStatus.classList.remove("error", "bi-exclamation-circle-fill");
formStatus.classList.add("success", "bi-check-circle-fill");
formStatus.classList.remove("spin-before", "bi-arrow-repeat");
formStatus.innerHTML = "Odesláno";
}
function asyncSybmit_rejected() {
event.submitter.disabled = false;
formStatus.classList.remove("hidden", "bi-circle");
formStatus.classList.add("error", "bi-exclamation-circle-fill");
formStatus.classList.remove("success", "bi-check-circle-fill");
formStatus.classList.remove("spin-before", "bi-arrow-repeat");
formStatus.innerHTML = "Nezdařilo se";
}
asyncRequest(xhr, formData).then(asyncSybmit_fulfilled, asyncSybmit_rejected);
}
function refreshFormFields() {
const willAttend = form.elements.willAttend.value === 'true';
const wantAccomodation = form.elements.wantAccomodation.value === 'true';
form.elements.contactEmail.disabled = !willAttend;
form.elements.contactPhone.disabled = !willAttend;
form.elements.numAdults.disabled = !willAttend;
form.elements.numChildren.disabled = !willAttend;
form.elements.extra.disabled = !willAttend;
for (const radioButton of form.elements.wantAccomodation)
radioButton.disabled = !willAttend;
form.elements.accomodationFrom.disabled = !willAttend || !wantAccomodation;
form.elements.numNights.disabled = !willAttend || !wantAccomodation;
}
for (const radioButton of form.elements.willAttend)
radioButton.addEventListener('change', refreshFormFields);
for (const radioButton of form.elements.wantAccomodation)
radioButton.addEventListener('change', refreshFormFields);
refreshFormFields();
form.addEventListener('submit', onFormSubmit);
}
window.addEventListener('load', onDocumentLoad);