-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (257 loc) · 9.82 KB
/
Copy pathscript.js
File metadata and controls
299 lines (257 loc) · 9.82 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
document.addEventListener('DOMContentLoaded', function () {
// Elements
const carousel = document.getElementById('carousel-section');
const slidesContainer = document.querySelector('.slides-container');
const slides = Array.from(document.querySelectorAll('.slide'));
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const pausePlayBtn = document.getElementById('pause-play-btn');
const pauseIcon = document.getElementById('pause-icon');
const playIcon = document.getElementById('play-icon');
const dots = Array.from(document.querySelectorAll('input[name="carousel-dots"]'));
const dotsFieldset = document.querySelector('.carousel-dots');
const slideChangeRegion = document.getElementById('slide-change-region');
// Preferences
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
// State
const totalSlides = slides.length;
let currentIndex = 0;
let autoplayInterval = null;
let isManuallyPaused = false; // becomes true after any user interaction (or reduced motion)
let isTransitioning = false;
let transitionQueue = [];
// Utility: inert support + focus management
const supportsInert = ('inert' in document.createElement('div'));
function forEachTabbable(root, cb) {
root.querySelectorAll('a,button,input,select,textarea,[tabindex]')
.forEach(cb);
}
function setSlideInactive(el) {
el.setAttribute('aria-hidden', 'true');
el.classList.remove('active');
if (supportsInert) {
el.setAttribute('inert', '');
} else {
forEachTabbable(el, n => n.setAttribute('tabindex', '-1'));
}
}
function setSlideActive(el) {
el.setAttribute('aria-hidden', 'false');
el.classList.add('active');
if (supportsInert) {
el.removeAttribute('inert');
} else {
// restore formerly disabled tabbables
forEachTabbable(el, n => {
if (n.getAttribute('tabindex') === '-1') n.removeAttribute('tabindex');
});
}
}
// Button label updates with destination slide names
function updateNavButtonLabels() {
const prevIndex = (currentIndex - 1 + totalSlides) % totalSlides;
const nextIndex = (currentIndex + 1) % totalSlides;
const prevName = slides[prevIndex].querySelector('h3')?.textContent?.trim() || `Slide ${prevIndex + 1}`;
const nextName = slides[nextIndex].querySelector('h3')?.textContent?.trim() || `Slide ${nextIndex + 1}`;
prevBtn.setAttribute('aria-label', `Previous slide: ${prevName}`);
nextBtn.setAttribute('aria-label', `Next slide: ${nextName}`);
}
// Make dots self-describing using the h3 text
function updateDotAriaLabels() {
dots.forEach((dot, i) => {
const name = slides[i].querySelector('h3')?.textContent?.trim();
if (name) dot.setAttribute('aria-label', `Slide ${i + 1}: ${name}`);
});
}
function getTargetIndex() {
return transitionQueue.length > 0 ? transitionQueue[transitionQueue.length - 1].index : currentIndex;
}
function processQueue() {
if (isTransitioning || transitionQueue.length === 0) return;
const nextItem = transitionQueue.shift();
showSlide(nextItem.index, nextItem.moveFocus, nextItem.announce);
}
function showSlide(index, moveFocus = false, announce = false) {
const newIndex = (index + totalSlides) % totalSlides;
if (newIndex === currentIndex && transitionQueue.length === 0) return;
isTransitioning = true;
const oldIndex = currentIndex;
currentIndex = newIndex;
slidesContainer.style.transform = `translateX(-${currentIndex * 100}%)`;
setSlideInactive(slides[oldIndex]);
setSlideActive(slides[currentIndex]);
slides[currentIndex].setAttribute('aria-posinset', String(currentIndex + 1));
slides[currentIndex].setAttribute('aria-setsize', String(totalSlides));
dots[currentIndex].checked = true;
const transitionDuration = mediaQuery.matches ? 0 : 750;
setTimeout(() => {
isTransitioning = false;
// Always move focus for user-initiated nav when requested
if (moveFocus) {
const h = slides[currentIndex].querySelector('h3');
if (h) {
h.focus();
} else {
slides[currentIndex].focus();
}
}
if (announce) {
slideChangeRegion.textContent = `Slide ${currentIndex + 1} of ${totalSlides}`;
}
updateNavButtonLabels();
processQueue();
if (!isManuallyPaused) {
startAutoplay();
}
}, transitionDuration);
}
function startAutoplay() {
clearInterval(autoplayInterval);
if (!isManuallyPaused) {
autoplayInterval = setInterval(() => {
const targetIndex = getTargetIndex();
// Autoplay should NOT move focus or announce
queueNavigation(targetIndex + 1, false, false, false);
}, 7000);
}
}
function stopAutoplay() {
clearInterval(autoplayInterval);
}
function pauseCarousel() {
// No need to do anything if already paused
if (isManuallyPaused) return;
isManuallyPaused = true;
stopAutoplay();
updatePausePlayButtonUI();
}
function resumeCarousel() {
// No need to do anything if already playing
if (!isManuallyPaused) return;
isManuallyPaused = false;
updatePausePlayButtonUI();
startAutoplay();
}
function updatePausePlayButtonUI() {
if (isManuallyPaused) {
pausePlayBtn.setAttribute('aria-label', 'Start automatic rotation');
pausePlayBtn.setAttribute('title', 'Start automatic rotation');
pausePlayBtn.setAttribute('aria-pressed', 'false');
pauseIcon.style.display = 'none';
playIcon.style.display = 'block';
} else {
pausePlayBtn.setAttribute('aria-label', 'Stop automatic rotation');
pausePlayBtn.setAttribute('title', 'Stop automatic rotation');
pausePlayBtn.setAttribute('aria-pressed', 'true');
pauseIcon.style.display = 'block';
playIcon.style.display = 'none';
}
}
// Unified navigation
// - userInitiated true: lock paused, allow focus movement
// - userInitiated false (autoplay): don't lock paused, don't move focus
function queueNavigation(index, moveFocus, announce, userInitiated = true) {
stopAutoplay();
if (userInitiated) {
isManuallyPaused = true; // lock paused after any user interaction
updatePausePlayButtonUI();
}
transitionQueue.push({
index,
moveFocus, // respect caller: user actions -> true, autoplay -> false
announce
});
processQueue();
}
// Event listeners (user initiated => moveFocus = true)
nextBtn.addEventListener('click', () => {
const targetIndex = getTargetIndex();
queueNavigation(targetIndex + 1, true, true, true);
});
prevBtn.addEventListener('click', () => {
const targetIndex = getTargetIndex();
queueNavigation(targetIndex - 1, true, true, true);
});
dots.forEach(dot => {
dot.addEventListener('change', () => {
const dotIndex = parseInt(dot.value, 10);
queueNavigation(dotIndex, true, false, true);
});
});
// Rich keyboarding on the dots group (also user initiated)
dotsFieldset.addEventListener('keydown', (e) => {
const current = dots.findIndex(d => d.checked);
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
e.preventDefault();
const i = (current + 1) % totalSlides;
dots[i].checked = true;
queueNavigation(i, true, true, true);
dots[i].focus();
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
e.preventDefault();
const i = (current - 1 + totalSlides) % totalSlides;
dots[i].checked = true;
queueNavigation(i, true, true, true);
dots[i].focus();
} else if (e.key === 'Home') {
e.preventDefault();
dots[0].checked = true;
queueNavigation(0, true, true, true);
dots[0].focus();
} else if (e.key === 'End') {
e.preventDefault();
dots[totalSlides - 1].checked = true;
queueNavigation(totalSlides - 1, true, true, true);
dots[totalSlides - 1].focus();
}
});
// Pause/resume button
pausePlayBtn.addEventListener('click', () => {
if (isManuallyPaused) {
resumeCarousel();
} else {
pauseCarousel();
transitionQueue = [];
}
});
// Pause on focus/hover (conservative), do not auto-resume on leave
slidesContainer.addEventListener('mouseenter', () => pauseCarousel());
slidesContainer.addEventListener('focusin', () => pauseCarousel());
const interactiveControls = [prevBtn, nextBtn, dotsFieldset];
interactiveControls.forEach(control => {
// Pause when the mouse enters a control
control.addEventListener('mouseenter', () => {
pauseCarousel();
});
// Pause when a control (or its children) receives focus
control.addEventListener('focusin', () => {
pauseCarousel();
});
});
// Escape key quickly pauses if running
carousel.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
// Announce the pause to screen readers on Esc press
pauseCarousel(true);
}
});
function init() {
// Respect reduced motion (start paused)
if (mediaQuery.matches) {
isManuallyPaused = true;
}
updateDotAriaLabels();
updatePausePlayButtonUI();
updateNavButtonLabels();
// initial active/inactive setup safety
slides.forEach((s, i) => i === 0 ? setSlideActive(s) : setSlideInactive(s));
if (!isManuallyPaused) startAutoplay();
// If the browser restores focus (back navigation), pause
if (document.activeElement && carousel.contains(document.activeElement)) {
isManuallyPaused = true;
stopAutoplay();
updatePausePlayButtonUI();
}
}
init();
});