-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
507 lines (462 loc) · 13 KB
/
slider.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
class SliderType
{
static Gallery = new SliderType('Gallery');
static Nav = new SliderType('Nav');
static Link = new SliderType('Link');
constructor(name)
{
this.name = name;
}
toString()
{
return `Direction.${this.name}`;
}
}
class Slider
{
constructor(name, type, f)
{
this.sliderContainer = document.querySelector(name);
this.innerSlider = this.sliderContainer.querySelector('.inner-slider');
this.slides = this.innerSlider.querySelectorAll('.slide');
this.pressed = false;
this.dragged = false;
this.clickedSlide = 0;
this.innerStartX;
this.isClicking = false;
this.clickFunction = f;
this.sliderType = type;
}
}
class Point
{
constructor(x,y)
{
this.x = x;
this.y = y;
this.distanceTo = (point) =>
{
const distance = Math.sqrt((Math.pow(point.x-this.x,2))+(Math.pow(point.y-this.y,2)));
return distance;
}
}
}
let currentBullet = 0;
let distanceThreshold = 20; //if user has dragged this much, don't click
let focusedSlide = 0;
let backedOut = false;
let isZoomed = false;
let startPos = new Point(0,0); //touch start. multi-touch on dual carousel currently not supported
let isScrollingSet = false;
let isScrolling = false;
const narrowThreshold = 550; //px
let touchDuration = 0;
const durationThreshold = 30; //centiseconds?
let touchTimer;
const snapThreshold = 0.4;
let isPointerActive = false;
let allowClick = true;
window.addEventListener('keydown', e =>
{
if(e.key === 'Escape' && isZoomed)
{
zoomOut();
return;
}
});
function setUpSliderListeners(s)
{
s.sliderContainer.addEventListener('wheel', (e) =>
{
if(Math.abs(e.deltaY) > Math.abs(e.deltaX)) //scroll vertically as usual
{
return;
}
s.dragged = true;
s.innerSlider.classList.toggle('dragging', true);
s.sliderContainer.classList.toggle('dragging', true);
if(s.innerSlider.style.left == "")
{
s.innerSlider.style.left = "0px";
}
const posStr = s.innerSlider.style.left;
const oldPos = Number(posStr.substring(0, posStr.length - 2)); //- "px"
s.innerSlider.style.left = `${e.pageX - s.innerStartX}px`;
const newPos = `${oldPos - e.deltaX}px`;
s.innerSlider.style.left = newPos;
checkBoundary(s);
e.stopPropagation();
e.preventDefault();
if(s.sliderType != SliderType.Nav)
{
const centeredSlide = getCenteredSlide(s, e);
setActiveBullet(s, centeredSlide);
focusedSlide = centeredSlide;
}
if(s.sliderType == SliderType.Link)
{
window.clearInterval(auto);
}
});
s.sliderContainer.addEventListener('pointerdown', (e) =>
{
isPointerActive = true;
if(s.sliderType == SliderType.Link)
{
window.clearInterval(auto);
}
touchStart(s, e);
});
s.sliderContainer.addEventListener('pointermove', e => dragMove(s, e));
s.sliderContainer.addEventListener('click', (e) =>
{
if(!allowClick)
{
return;
}
const bnRect = document.querySelector('.bullet-nav').getBoundingClientRect();
if(contains(e.x,e.y,bnRect))
{
return;
}
if(s.sliderType == SliderType.Link)
{
return;
}
if(isZoomed)
{
zoomOut();
}
else
{
galZoom(focusedSlide);
}
});
}
function setUpNavBulletListeners(s, f)
{
const navLinks = s.sliderContainer.querySelectorAll('.nav-link');
navLinks.forEach((bullet,i) =>
{
bullet.addEventListener('pointerdown', (e) =>
{
s.dragged = false;
s.innerSlider.classList.toggle('dragging', false);
s.sliderContainer.classList.toggle('dragging', false);
f(i);
e.stopPropagation();
});
bullet.addEventListener('pointerup', (e) =>
{
e.stopPropagation();
});
});
}
function setUpSliderFocus(container, s)
{
document.addEventListener('focusin', () =>
{
if(backedOut)
{
backedOut = false;
getPreviousFocus(container).focus();
return;
}
if(document.activeElement === container)
{
focusedSlide = Math.min(Math.max(focusedSlide,0),s.slides.length - 1);
const sl = s.sliderType == SliderType.Link ?
s.slides[focusedSlide].querySelector('a') : s.slides[focusedSlide];
sl.focus({preventScroll: true});
}
});
}
function getPreviousFocus(container)
{
const focusable = document.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex=\"-1\"])');
const index = Array.prototype.indexOf.call(focusable, container) - 1;
const f = focusable[Math.max(index, 0)];
return f;
}
function keyboardNavigation(e, s)
{
if(s.sliderType === SliderType.Gallery && e.key === 'Enter')
{
allowClick = false;
setTimeout(() => { allowClick = true}, 100); //prevents simultaneous key & click event
if(isZoomed)
{
zoomOut();
}
else
{
galZoom(focusedSlide);
}
return;
}
if(e.key !== 'Tab' && e.key !== 'ArrowRight' && e.key !== 'ArrowLeft')
{
return;
}
if(s.sliderType == SliderType.Link)
{
window.clearInterval(auto);
}
let dir = ' ';
if(e.key === 'Tab' && e.shiftKey || e.key === 'ArrowLeft')
{
dir = 'left';
}
else if(e.key === 'Tab' || e.key === 'ArrowRight')
{
dir = 'right';
}
else
{
return;
}
dir === 'left' ? focusedSlide-- : focusedSlide++;
const inRange = focusedSlide >= 0 && focusedSlide < s.slides.length;
if(!inRange)
{
focusedSlide = Math.min(Math.max(focusedSlide,0),s.slides.length - 1);
if(e.key === 'Tab' && e.shiftKey)
{
backedOut = true;
//default behavior not prevented, so the doubleCarousel parent
//is focused on. This reactivates :focused-visible.
//Then, the document's focusin listener code on l. 56 activates.
}
return;
}
if(s.sliderType === SliderType.Gallery && isZoomed)
{
galZoom(focusedSlide);
}
e.preventDefault();
s.innerSlider.classList.toggle('dragging', true);
s.sliderContainer.classList.toggle('dragging', true);
const sl = s.sliderType == SliderType.Link ?
s.slides[focusedSlide].querySelector('a') : s.slides[focusedSlide];
sl.focus({preventScroll: true});
moveInnerSlider(s, focusedSlide);
setActiveBullet(s, focusedSlide);
return;
}
function setActiveBullet(s, i)
{
const bullets = s.sliderContainer.querySelectorAll('.nav-link');
bullets[currentBullet].classList.remove('active');
bullets[i].classList.add('active');
currentBullet = i;
}
function moveInnerSlider(s,i)
{
const conL = s.sliderContainer.offsetLeft;
const conW = s.sliderContainer.offsetWidth;
const img = s.slides[i].querySelector('img');
const imgL = s.slides[i].offsetLeft;
const imgW = img.offsetWidth;
const offset = (conW - imgW) * 0.5 + conL;
let newPosX = (conL - imgL) + offset;
newPosX = Math.min(newPosX, 0);
newPosX = Math.max(newPosX, -1 * (s.innerSlider.offsetWidth - conW));
s.innerSlider.style.left = newPosX + 'px';
}
function contains(x,y,r)
{
if(x >= r.left &&
x <= r.right &&
y >= r.top &&
y <= r.bottom)
{
return true;
}
return false;
}
function touchStart(s,e)
{
s.dragged = false;
if(isZoomed)
{
return;
}
activeSlider = s.sliderType == SliderType.Nav ? 1 : 0;
const x = e.x;
const y = e.y;
const bnRect = document.querySelector('.bullet-nav').getBoundingClientRect();
if(contains(x,y,bnRect))
{
return;
}
touchTimer = setInterval(() =>
{
touchDuration += 1;
}, 10);
s.slides.forEach((slide, i) =>
{
const rect = slide.getBoundingClientRect();
if(contains(x,y,rect))
{
s.clickedSlide = i;
}
});
s.pressed = true;
s.innerStartX = e.x - s.innerSlider.offsetLeft; //offset is 0?
startPos = new Point(e.pageX, e.pageY);
isScrollingSet = false;
isScrolling = false;
e.stopPropagation();
};
const dragMove = (s, e) =>
{
if(!s.pressed)
{
return;
}
if(e.pointerType === "touch" && !isScrollingSet)
{
const xDiff = Math.abs(startPos.x - e.pageX);
const yDiff = Math.abs(startPos.y - e.pageY);
isScrollingSet = true;
isScrolling = yDiff > xDiff;
}
if(isScrolling)
{
return;
}
if(isZoomed || !s.pressed)
{
return;
}
s.dragged = true;
s.innerSlider.classList.toggle('dragging', true);
s.sliderContainer.classList.toggle('dragging', true);
s.innerSlider.style.left = `${e.pageX - s.innerStartX}px`;
checkBoundary(s);
e.stopPropagation();
e.preventDefault();
};
function clickEnd(s, e)
{
if(isZoomed || !s.pressed)
{
return;
}
const newPos = new Point(e.pageX, e.pageY);
const diff = startPos.distanceTo(newPos);
if(s.sliderType == SliderType.Link)
{
s.innerSlider.classList.toggle('dragging',false);
s.sliderContainer.classList.toggle('dragging',false);
}
else
{
sliders.forEach((sl)=>
{
sl.innerSlider.classList.toggle('dragging',false);
sl.sliderContainer.classList.toggle('dragging',false);
});
}
if(diff < distanceThreshold /*&& touchDuration < durationThreshold*/)
{
if(s.sliderType != SliderType.Link)
{
s.clickFunction(s.clickedSlide);
}
}
else
{
if(s.sliderType != SliderType.Nav)
{
const centeredSlide = getCenteredSlide(s, e);
setActiveBullet(s, centeredSlide);
focusedSlide = centeredSlide;
const isNarrow = window.innerWidth <= narrowThreshold
&& window.innerWidth < window.innerHeight;
if(s.sliderType == SliderType.Link || isNarrow)
{
moveInnerSlider(s,centeredSlide);
}
}
}
s.dragged = false;
s.pressed = false;
if(s.sliderType != SliderType.Link)
{
e.stopPropagation();
}
touchDuration = 0.0;
clearInterval(touchTimer);
}
function getCenteredSlide(s, e)
{
const conRect = s.sliderContainer.getBoundingClientRect();
const offset = e.pageX > startPos.x ? snapThreshold : 1.0 - snapThreshold;
const center = conRect.width * offset + conRect.left;
for(let i = 0; i < s.slides.length; ++i)
{
const rect = s.slides[i].getBoundingClientRect();
if(center > rect.left && center < rect.right)
{
return i;
}
}
if(center < rect.left)
{
return 0;
}
if(center > rect.right)
{
return s.slides.length - 1;
}
return 0;
}
function checkBoundary (s)
{
const outer = s.sliderContainer.getBoundingClientRect();
const inner = s.innerSlider.getBoundingClientRect();
if (inner.left > outer.left)
{
s.innerSlider.style.left = "0px";
}
if (inner.right < outer.right)
{
s.innerSlider.style.left = `${outer.width - inner.width}px`;
}
}
function galZoom(i)
{
focusedSlide = i;
const img = new Image();
//zoom in on lores img first
img.src = loResImgDir + String(i).padStart(2, '0') + '.jpg';
zoomImage.setAttribute('src', img.src);
zoomContainerContainer.classList.add('visible');
isZoomed = true;
zoomIndex = i;
//sliders[0].slides[i].setAttribute('aria-expanded', true);
sliders[0].slides[i].setAttribute('data-btn-expanded', true);
sliders[0].slides[i].setAttribute('aria-labelledby', 'zoom-out-text');
document.querySelector('body').classList.add('no-scroll');
//switch to hires img as soon as loaded
img.src = hiResImgDir + String(i).padStart(2, '0') + '.jpg';
img.onload = () =>
{
if(i == focusedSlide) //accounts for slides changing while image is loading
{
zoomImage.setAttribute('src', img.src);
}
};
}
function zoomOut()
{
zoomContainerContainer.classList.remove('visible');
//sliders[0].slides[focusedSlide].setAttribute('aria-expanded', false);
sliders[0].slides[focusedSlide].setAttribute('data-btn-expanded', false);
sliders[0].slides[focusedSlide].setAttribute('aria-labelledby', 'label-' + focusedSlide + ' alt-' + focusedSlide);
sliders[0].slides[focusedSlide].focus({preventScroll:true});
const b = document.querySelector('body');
b.classList.remove('no-scroll');
isZoomed = false;
};