-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
264 lines (227 loc) · 8.08 KB
/
Copy pathscript.js
File metadata and controls
264 lines (227 loc) · 8.08 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* =========================================================
Visit Croatia — interactive components
1. Mobile navigation toggle
2. Carousel (autoplay + pause on hover, arrows, dots)
3. Accordion (single-open)
4. Contact form (client-side validation)
========================================================= */
document.addEventListener('DOMContentLoaded', () => {
initMobileNav();
initCarousel();
initAccordion();
initForm();
initExperienceCards();
});
/* ---------------------------------------------------------
Experience cards -> pre-fill the plan form's interests
--------------------------------------------------------- */
function initExperienceCards() {
const cards = document.querySelectorAll('.card[data-interest]');
const interests = document.getElementById('interests');
if (!cards.length || !interests) return;
cards.forEach((card) => {
card.addEventListener('click', () => {
// The href="#plan" already scrolls to the form; just fill the field
interests.value = card.dataset.interest;
});
});
}
/* ---------------------------------------------------------
1. Mobile navigation
--------------------------------------------------------- */
function initMobileNav() {
const toggle = document.getElementById('navToggle');
const nav = document.getElementById('nav');
if (!toggle || !nav) return;
const setOpen = (open) => {
nav.classList.toggle('open', open);
toggle.setAttribute('aria-expanded', String(open));
};
toggle.addEventListener('click', () => {
setOpen(nav.classList.contains('open') === false);
});
// Close the menu after tapping a link
nav.querySelectorAll('a').forEach((link) => {
link.addEventListener('click', () => setOpen(false));
});
// Close on Escape
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') setOpen(false);
});
}
/* ---------------------------------------------------------
2. Carousel
--------------------------------------------------------- */
function initCarousel() {
const root = document.querySelector('[data-carousel]');
if (!root) return;
const track = root.querySelector('[data-carousel-track]');
const slides = Array.from(track.children);
const prevBtn = root.querySelector('[data-carousel-prev]');
const nextBtn = root.querySelector('[data-carousel-next]');
const dotsWrap = root.querySelector('[data-carousel-dots]');
const AUTOPLAY_MS = 5000;
let index = 0;
let timer = null;
let isHovered = false;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Build one dot per slide
const dots = slides.map((_, i) => {
const dot = document.createElement('button');
dot.type = 'button';
dot.setAttribute('role', 'tab');
dot.setAttribute('aria-label', `Go to slide ${i + 1}`);
dot.addEventListener('click', () => {
goTo(i);
restart();
});
dotsWrap.appendChild(dot);
return dot;
});
function goTo(i) {
index = (i + slides.length) % slides.length;
track.style.transform = `translateX(-${index * 100}%)`;
dots.forEach((dot, d) =>
dot.setAttribute('aria-selected', String(d === index))
);
}
const next = () => goTo(index + 1);
const prev = () => goTo(index - 1);
function start() {
stop();
// Don't autoplay for reduced-motion users, or while the pointer is hovering
if (reduceMotion || isHovered) return;
timer = setInterval(next, AUTOPLAY_MS);
}
function stop() {
if (timer) clearInterval(timer);
timer = null;
}
function restart() {
stop();
start();
}
nextBtn.addEventListener('click', () => { next(); restart(); });
prevBtn.addEventListener('click', () => { prev(); restart(); });
// Pause autoplay while the pointer is over the carousel
root.addEventListener('mouseenter', () => { isHovered = true; stop(); });
root.addEventListener('mouseleave', () => { isHovered = false; start(); });
// Keyboard support when the carousel has focus
root.setAttribute('tabindex', '0');
root.addEventListener('keydown', (e) => {
if (e.key === 'ArrowRight') { next(); restart(); }
if (e.key === 'ArrowLeft') { prev(); restart(); }
});
// Pause when the tab is hidden
document.addEventListener('visibilitychange', () => {
document.hidden ? stop() : start();
});
goTo(0);
start();
}
/* ---------------------------------------------------------
3. Accordion (single-open)
--------------------------------------------------------- */
function initAccordion() {
const accordion = document.querySelector('[data-accordion]');
if (!accordion) return;
const triggers = Array.from(accordion.querySelectorAll('.accordion-trigger'));
const close = (trigger) => {
const panel = trigger.nextElementSibling;
trigger.setAttribute('aria-expanded', 'false');
panel.style.height = '0px';
};
const open = (trigger) => {
const panel = trigger.nextElementSibling;
trigger.setAttribute('aria-expanded', 'true');
panel.style.height = panel.scrollHeight + 'px';
};
triggers.forEach((trigger) => {
trigger.addEventListener('click', () => {
const isOpen = trigger.getAttribute('aria-expanded') === 'true';
// Single-open: close everything first
triggers.forEach(close);
if (!isOpen) open(trigger);
});
});
// Keep an open panel sized correctly on resize
window.addEventListener('resize', () => {
triggers.forEach((trigger) => {
if (trigger.getAttribute('aria-expanded') === 'true') {
const panel = trigger.nextElementSibling;
panel.style.height = panel.scrollHeight + 'px';
}
});
});
}
/* ---------------------------------------------------------
4. Form validation
--------------------------------------------------------- */
function initForm() {
const form = document.querySelector('[data-form]');
if (!form) return;
const success = form.querySelector('[data-form-success]');
const emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const setError = (field, message) => {
const wrapper = field.closest('.field');
const errorEl = form.querySelector(`[data-error-for="${field.name}"]`);
if (wrapper) wrapper.classList.toggle('invalid', Boolean(message));
field.setAttribute('aria-invalid', message ? 'true' : 'false');
if (errorEl) errorEl.textContent = message || '';
};
const validateField = (field) => {
const value = field.value.trim();
if (field.type === 'checkbox') {
const msg = field.checked ? '' : 'Please check this box to continue.';
setError(field, msg);
return !msg;
}
if (field.required && !value) {
setError(field, 'This field is required.');
return false;
}
if (field.type === 'email' && !emailRe.test(value)) {
setError(field, 'Please enter a valid email address.');
return false;
}
if (field.type === 'number' && value) {
const n = Number(value);
const min = Number(field.min), max = Number(field.max);
if (n < min || n > max) {
setError(field, `Please enter a number between ${min} and ${max}.`);
return false;
}
}
setError(field, '');
return true;
};
const fields = Array.from(form.querySelectorAll('input, select, textarea'))
.filter((el) => el.required || el.name === 'interests');
// Live-clear errors as the user fixes them
fields.forEach((field) => {
const evt = field.type === 'checkbox' || field.tagName === 'SELECT' ? 'change' : 'input';
field.addEventListener(evt, () => {
if (field.closest('.field')?.classList.contains('invalid') ||
field.name === 'consent') {
validateField(field);
}
});
});
form.addEventListener('submit', (e) => {
e.preventDefault();
success.hidden = true;
let firstInvalid = null;
fields.forEach((field) => {
const ok = validateField(field);
if (!ok && !firstInvalid) firstInvalid = field;
});
if (firstInvalid) {
firstInvalid.focus();
return;
}
// No backend — simulate a successful submission
form.reset();
success.hidden = false;
success.scrollIntoView({ behavior: 'smooth', block: 'center' });
});
}