@twistedandy/better-carousel 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -12
- package/dist/better-carousel.css +1 -1
- package/dist/better-carousel.js +375 -336
- package/dist/better-carousel.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +1 -8
- package/src/scripts/autoplay.js +22 -24
- package/src/scripts/carousel.js +109 -49
- package/src/scripts/geometry.js +45 -55
- package/src/scripts/input.js +47 -59
- package/src/scripts/layout.js +82 -86
- package/src/scripts/markup.js +50 -19
- package/src/scripts/motion.js +3 -2
- package/src/scripts/navigation.js +24 -42
- package/src/styles/better-carousel.scss +5 -0
- package/src/types/better-carousel.d.ts +4 -4
package/src/scripts/carousel.js
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* It returns nothing if its option is off, or an object with remove(). remove() removes all that the feature added.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { alignOffset, clamp, compute, inView, nearestSnap, physicalSnap, progress, snapIndex, snapOfSlide, snapPosition, targetLocation } from './geometry.js';
|
|
9
|
-
import { observeSize, readLayout, readRects,
|
|
8
|
+
import { abs, alignOffset, clamp, compute, floor, inView, nearestSnap, physicalSnap, progress, round, snapIndex, snapOfSlide, snapPosition, targetLocation } from './geometry.js';
|
|
9
|
+
import { observeSize, readLayout, readRects, waitNear, windowSize } from './layout.js';
|
|
10
10
|
import { normalize, pathFits, placement } from './loop.js';
|
|
11
11
|
import { addClass, build, cleanStyle, copyContent, copySlides, labelSlides, readSlides, restore, setClass, setStyle } from './markup.js';
|
|
12
12
|
import { animator, EASE, snapDuration } from './motion.js';
|
|
@@ -15,6 +15,9 @@ import { breakpoints, classNames, merge, resolve } from './options.js';
|
|
|
15
15
|
// The instance of each root, viewport and container
|
|
16
16
|
const registry = new WeakMap();
|
|
17
17
|
|
|
18
|
+
// true if the browser supports the pan-up and pan-down values of touch-action
|
|
19
|
+
let directional;
|
|
20
|
+
|
|
18
21
|
/**
|
|
19
22
|
* Get the carousel of a root, a viewport or a container.
|
|
20
23
|
*
|
|
@@ -25,7 +28,7 @@ export const find = (element) => registry.get(element);
|
|
|
25
28
|
|
|
26
29
|
/**
|
|
27
30
|
* Make an event emitter. A handler gets (api, name), as in Embla.
|
|
28
|
-
* The handlers of the features run before the handlers of the page, and clear() removes them.
|
|
31
|
+
* The handlers of the features run before the handlers of the page, and clear() removes them. listen() takes event names with spaces between them.
|
|
29
32
|
*
|
|
30
33
|
* @param {object} api
|
|
31
34
|
* @returns {{on: Function, off: Function, listen: Function, clear: Function, emit: Function, has: Function}}
|
|
@@ -44,7 +47,7 @@ function emitter(api) {
|
|
|
44
47
|
user[name]?.delete(handler);
|
|
45
48
|
return api;
|
|
46
49
|
},
|
|
47
|
-
listen: (
|
|
50
|
+
listen: (names, handler) => names.split(' ').forEach((name) => add(own, name, handler)),
|
|
48
51
|
clear: () => Object.values(own).forEach((set) => set.clear()),
|
|
49
52
|
emit(name) {
|
|
50
53
|
own[name]?.forEach((handler) => handler(api, name));
|
|
@@ -113,16 +116,15 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
113
116
|
|
|
114
117
|
const api = {
|
|
115
118
|
goToSnap: (index, instant) => go(physical(index), instant),
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
goToPrev: (instant) => geo ? api.canGoToPrev() && step(selected, -1, instant) : go(selected - 1, instant),
|
|
119
|
+
goToNext: (instant) => advance(1, instant),
|
|
120
|
+
goToPrev: (instant) => advance(-1, instant),
|
|
119
121
|
goToSlide: (index, instant) => go(physical(api.snapOfSlide(index)), instant, index),
|
|
120
|
-
canGoToNext: () =>
|
|
121
|
-
canGoToPrev: () =>
|
|
122
|
+
canGoToNext: () => can(1),
|
|
123
|
+
canGoToPrev: () => can(-1),
|
|
122
124
|
selectedSnap: () => logical(selected),
|
|
123
125
|
previousSnap: () => logical(previous),
|
|
124
126
|
selectedSlide,
|
|
125
|
-
snapOfSlide: (index) => geo ? logical(snapOfSlide(geo
|
|
127
|
+
snapOfSlide: (index) => geo ? logical(snapOfSlide(geo, index)) : index,
|
|
126
128
|
snapProgress: () => geo ? geo.logical.map((index) => progress(geo.snaps[index].location, geo)) : [],
|
|
127
129
|
progress: () => geo ? progress(current(), geo) : 0,
|
|
128
130
|
slideNodes: () => slides.slice(),
|
|
@@ -146,17 +148,19 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
146
148
|
each = { node: null, index: 0, offset: 0, distance: 0, size: 0, visible: 0 },
|
|
147
149
|
/**
|
|
148
150
|
* What the features share: the API, the events, and the state and the actions of the carousel.
|
|
149
|
-
* setup() adds root, viewport, container, cls and options. reinit() adds playing:
|
|
151
|
+
* setup() adds root, viewport, container, cls and options. reinit() adds playing: an object where the autoplay and the auto scroll
|
|
152
|
+
* keep their play state for the next setup after a breakpoint change.
|
|
150
153
|
*/
|
|
151
154
|
context = {
|
|
152
155
|
api,
|
|
153
156
|
emit: events.emit,
|
|
154
|
-
// Listen to
|
|
157
|
+
// Listen to events, such as 'init reinit', until the teardown
|
|
155
158
|
on: events.listen,
|
|
159
|
+
// Get the carousel of an element, for the sync option
|
|
160
|
+
find,
|
|
156
161
|
|
|
157
162
|
// The state
|
|
158
163
|
geo: () => geo,
|
|
159
|
-
nodes: () => nodes,
|
|
160
164
|
selected: () => selected,
|
|
161
165
|
dragging: () => dragging,
|
|
162
166
|
// The location at rest or at the end of the motion. During a free motion, the location now.
|
|
@@ -193,8 +197,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
193
197
|
api.on = events.on;
|
|
194
198
|
api.off = events.off;
|
|
195
199
|
|
|
196
|
-
// The options of the first setup. The lazy option:
|
|
197
|
-
// a start after the first paint adds the arrows and the dots in view, and this moves the page (CLS).
|
|
200
|
+
// The options of the first setup. The lazy option: the carousel waits, and waitNear() starts it before the first paint if it is near the screen.
|
|
198
201
|
let resolved = resolve(element, defaults, userOptions),
|
|
199
202
|
waiting = resolved.lazy ? waitNear(element, resolved.lazy, reinit) : null;
|
|
200
203
|
|
|
@@ -241,6 +244,8 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
241
244
|
}
|
|
242
245
|
|
|
243
246
|
viewport.addEventListener('scroll', onScroll);
|
|
247
|
+
viewport.addEventListener('click', onClick);
|
|
248
|
+
viewport.addEventListener('input', onInput);
|
|
244
249
|
if (options.loop && options.clones === 'fill') {
|
|
245
250
|
addEventListener('resize', onWindow);
|
|
246
251
|
}
|
|
@@ -277,21 +282,23 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
277
282
|
dragging = free = false;
|
|
278
283
|
setClones(0);
|
|
279
284
|
viewport.removeEventListener('scroll', onScroll);
|
|
285
|
+
viewport.removeEventListener('click', onClick);
|
|
286
|
+
viewport.removeEventListener('input', onInput);
|
|
280
287
|
removeEventListener('resize', onWindow);
|
|
281
288
|
|
|
282
289
|
for (const node of nodes) {
|
|
283
290
|
setStyle(node, 'translate', '');
|
|
284
|
-
node
|
|
291
|
+
setStyle(node, '--better-offset', '');
|
|
285
292
|
setClass(node, cls.selected, false);
|
|
286
293
|
setClass(node, cls.inView, false);
|
|
287
294
|
cleanStyle(node);
|
|
288
295
|
}
|
|
289
296
|
|
|
290
297
|
setStyle(container, 'translate', '');
|
|
291
|
-
setStyle(container, '
|
|
292
|
-
setStyle(viewport, '
|
|
298
|
+
setStyle(container, 'will-change', '');
|
|
299
|
+
setStyle(viewport, 'touch-action', '');
|
|
293
300
|
setStyle(viewport, 'height', '');
|
|
294
|
-
root
|
|
301
|
+
setStyle(root, '--better-progress', '');
|
|
295
302
|
|
|
296
303
|
for (const name of ['horizontal', 'vertical', 'fit', 'dragging']) {
|
|
297
304
|
setClass(root, cls[name], false);
|
|
@@ -325,7 +332,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
325
332
|
}
|
|
326
333
|
const keep = geo ? selectedSlide() : requested;
|
|
327
334
|
// After a breakpoint change, the new autoplay and auto scroll keep the play state. New options start them again.
|
|
328
|
-
context.playing = newOptions ? null : {
|
|
335
|
+
context.playing = newOptions ? null : {};
|
|
329
336
|
teardown(true);
|
|
330
337
|
setup();
|
|
331
338
|
requested = keep;
|
|
@@ -356,20 +363,27 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
356
363
|
}
|
|
357
364
|
|
|
358
365
|
/**
|
|
359
|
-
* Copy a changed slide into its clones
|
|
366
|
+
* Copy a changed slide into its clones, for example after the page changed a text or an image source. The carousel writes the classes
|
|
367
|
+
* of the slides, so a change of a slide's own attributes does not count. A style change does not count: a "progress" or an "offset"
|
|
368
|
+
* callback can write the styles of the elements in the slides on each frame.
|
|
360
369
|
*
|
|
361
|
-
* @param {MutationRecord[]} records
|
|
370
|
+
* @param {MutationRecord[]} [records] With no records, copy all slides.
|
|
362
371
|
*/
|
|
363
372
|
function onCopy(records) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
373
|
+
slides.forEach((node, index) => {
|
|
374
|
+
if (!records || records.some(({ type, target }) => node.contains(target) && (type !== 'attributes' || target !== node))) {
|
|
375
|
+
for (let item = index; item < clones.length; item += slides.length) {
|
|
376
|
+
copyContent(clones[item], node);
|
|
377
|
+
}
|
|
369
378
|
}
|
|
370
379
|
});
|
|
371
380
|
}
|
|
372
381
|
|
|
382
|
+
// A form field changes its value or its check with no mutation. A checked radio also changes the radios of its group in other slides, so copy all slides.
|
|
383
|
+
function onInput() {
|
|
384
|
+
onCopy();
|
|
385
|
+
}
|
|
386
|
+
|
|
373
387
|
/**
|
|
374
388
|
* Watch the size of the viewport, the container and the slides.
|
|
375
389
|
*
|
|
@@ -451,18 +465,17 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
451
465
|
function measure() {
|
|
452
466
|
const sign = geo?.sign || 1,
|
|
453
467
|
at = motion.current(),
|
|
454
|
-
layout = readLayout(container, options)
|
|
455
|
-
// The translate values that show now: the written values, or the values of a motion
|
|
456
|
-
rects = readRects(viewport, nodes, layout, at === null ? written : { axis: written.axis, container: sign * at, slides: motion.shifts(sign) || written.slides });
|
|
468
|
+
layout = readLayout(container, options);
|
|
457
469
|
|
|
458
470
|
// With clones: 'fill', the loop fills the window
|
|
459
471
|
lastWindow = options.loop && options.clones === 'fill' ? windowSize(layout.axis) : 0;
|
|
460
472
|
|
|
461
|
-
|
|
462
|
-
|
|
473
|
+
// The translate values that show now: the written values, or the values of a motion
|
|
474
|
+
const rects = readRects(viewport, nodes, layout, at === null ? written : { axis: written.axis, container: sign * at, slides: motion.shifts(sign) || written.slides }, lastWindow),
|
|
475
|
+
values = [layout.axis, layout.rtl, typeof layout.align === 'string' && layout.align, rects.view, rects.gap, rects.area.start, rects.area.size, ...rects.slides.flatMap((slide) => [slide.start, slide.end, slide.cross])];
|
|
463
476
|
|
|
464
477
|
// A change of less than 0.5 px is not a change. During a motion, the rects have small errors, because the browser and bezier() round the easing differently.
|
|
465
|
-
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] ||
|
|
478
|
+
if (ready && values.length === signature.length && values.every((value, index) => value === signature[index] || abs(value - signature[index]) < 0.5)) {
|
|
466
479
|
return false;
|
|
467
480
|
}
|
|
468
481
|
|
|
@@ -470,7 +483,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
470
483
|
|
|
471
484
|
// After a change of the slides, onMutation() gives the slide to keep
|
|
472
485
|
const keep = ready && !changed ? selectedSlide() : requested,
|
|
473
|
-
next = compute(layout, rects,
|
|
486
|
+
next = compute(layout, rects, slides.length, options);
|
|
474
487
|
|
|
475
488
|
if (!next.snaps) {
|
|
476
489
|
pending = next.copies;
|
|
@@ -483,8 +496,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
483
496
|
|
|
484
497
|
// Before the first measure pass, selected is the logical snap of the startSnap option, of goToSnap() or of goToNext().
|
|
485
498
|
// In loop mode, a step before the first snap goes to the last snap.
|
|
486
|
-
|
|
487
|
-
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(geo.period ? (selected % count + count) % count : Math.max(selected, 0)));
|
|
499
|
+
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(snapIndex(selected, geo.logical.length, geo.period)));
|
|
488
500
|
previous = Math.min(previous, total - 1);
|
|
489
501
|
requested = keep;
|
|
490
502
|
if (!dragging) {
|
|
@@ -559,8 +571,13 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
559
571
|
setClass(root, cls.horizontal, x);
|
|
560
572
|
setClass(root, cls.vertical, !x);
|
|
561
573
|
setClass(root, cls.fit, geo.fit);
|
|
562
|
-
|
|
563
|
-
|
|
574
|
+
// A touch can scroll the page across the axis. At the first or the last snap of a vertical carousel, it can also scroll the page up
|
|
575
|
+
// or down. Safari has no pan-up and pan-down, so there the carousel keeps the touch.
|
|
576
|
+
const first = !can(-1),
|
|
577
|
+
last = !can(1),
|
|
578
|
+
edge = !x && (first || last) && (directional ??= CSS.supports('touch-action', 'pan-up')) ? (first && last ? 'pan-y ' : first ? 'pan-up ' : 'pan-down ') : '';
|
|
579
|
+
setStyle(viewport, 'touch-action', geo.fit ? '' : x ? 'pan-y pinch-zoom' : 'pan-x ' + edge + 'pinch-zoom');
|
|
580
|
+
setStyle(container, 'will-change', geo.fit ? '' : 'translate');
|
|
564
581
|
|
|
565
582
|
if (!keep) {
|
|
566
583
|
write(geo.fit ? 0 : location);
|
|
@@ -568,11 +585,11 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
568
585
|
|
|
569
586
|
// Auto height: the height of the tallest slide in view at the target location
|
|
570
587
|
if (options.autoHeight) {
|
|
571
|
-
setStyle(viewport, 'height', x ? Math.max(...inView(geo
|
|
588
|
+
setStyle(viewport, 'height', x ? Math.max(...inView(geo, location, 0).map((index) => geo.slides[index].cross)) + 'px' : '');
|
|
572
589
|
}
|
|
573
590
|
|
|
574
591
|
const chosen = selectedSlide(),
|
|
575
|
-
list = [...new Set(inView(geo
|
|
592
|
+
list = [...new Set(inView(geo, location, options.inViewThreshold).map(original))].sort((a, b) => a - b);
|
|
576
593
|
|
|
577
594
|
nodes.forEach((node, index) => setClass(node, cls.selected, original(index) === chosen));
|
|
578
595
|
|
|
@@ -591,7 +608,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
591
608
|
*/
|
|
592
609
|
function screen(value) {
|
|
593
610
|
const scale = devicePixelRatio || 1;
|
|
594
|
-
return
|
|
611
|
+
return round(value * scale) / scale * geo.sign;
|
|
595
612
|
}
|
|
596
613
|
|
|
597
614
|
/**
|
|
@@ -618,9 +635,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
618
635
|
written.container = screen(value);
|
|
619
636
|
setStyle(container, 'translate', translate(written.container));
|
|
620
637
|
place(value, value);
|
|
621
|
-
|
|
622
|
-
track(value, written.slides);
|
|
623
|
-
}
|
|
638
|
+
track(value, written.slides);
|
|
624
639
|
}
|
|
625
640
|
|
|
626
641
|
/**
|
|
@@ -635,7 +650,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
635
650
|
root.style.setProperty('--better-progress', progress(at, geo));
|
|
636
651
|
} else if (options.progress) {
|
|
637
652
|
const value = snapPosition(geo.snaps, at, geo.period),
|
|
638
|
-
snap =
|
|
653
|
+
snap = floor(value);
|
|
639
654
|
whole.progress = progress(at, geo);
|
|
640
655
|
whole.snap = logical(snap) + value - snap;
|
|
641
656
|
options.progress(whole, api);
|
|
@@ -688,6 +703,30 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
688
703
|
return translate(screen(value)) || '0px';
|
|
689
704
|
}
|
|
690
705
|
|
|
706
|
+
/**
|
|
707
|
+
* Find if the carousel can go one snap in a direction.
|
|
708
|
+
*
|
|
709
|
+
* @param {number} direction 1 or -1.
|
|
710
|
+
* @returns {boolean}
|
|
711
|
+
*/
|
|
712
|
+
function can(direction) {
|
|
713
|
+
return !!geo && !geo.fit && (!!geo.period || (direction > 0 ? selected < geo.snaps.length - 1 : selected > 0));
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/**
|
|
717
|
+
* Go one snap in a direction, if the carousel can. Before the first measure pass, keep the step in the selected snap.
|
|
718
|
+
*
|
|
719
|
+
* @param {number} direction 1 or -1.
|
|
720
|
+
* @param {boolean} [instant]
|
|
721
|
+
*/
|
|
722
|
+
function advance(direction, instant) {
|
|
723
|
+
if (!geo) {
|
|
724
|
+
go(selected + direction, instant);
|
|
725
|
+
} else if (can(direction)) {
|
|
726
|
+
step(selected, direction, instant);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
691
730
|
/**
|
|
692
731
|
* Go to the snap after a snap in a direction. In loop mode, the first snap follows the last snap.
|
|
693
732
|
*
|
|
@@ -719,7 +758,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
719
758
|
}
|
|
720
759
|
|
|
721
760
|
const target = targetLocation(geo.snaps[index].location, current(), geo.period, direction),
|
|
722
|
-
span =
|
|
761
|
+
span = abs(index - selected),
|
|
723
762
|
count = geo.period ? Math.min(span, geo.snaps.length - span) : span;
|
|
724
763
|
|
|
725
764
|
move(index, target, instant ? 0 : snapDuration(options.duration, count), EASE);
|
|
@@ -736,7 +775,7 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
736
775
|
function move(index, target, duration, curve) {
|
|
737
776
|
const from = halt(),
|
|
738
777
|
picked = select(index),
|
|
739
|
-
jump = !duration || geo.fit ||
|
|
778
|
+
jump = !duration || geo.fit || abs(target - from) < 0.5;
|
|
740
779
|
|
|
741
780
|
location = target;
|
|
742
781
|
|
|
@@ -776,10 +815,10 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
776
815
|
// Start in the first period, so that the location does not grow after each pause
|
|
777
816
|
location = normalize(from, geo.period);
|
|
778
817
|
motion.loop(location, direction, geo.period / speed, geo, nodes);
|
|
779
|
-
} else if (
|
|
818
|
+
} else if (abs(to - from) > 1) {
|
|
780
819
|
free = true;
|
|
781
820
|
location = to;
|
|
782
|
-
motion.run(from, to,
|
|
821
|
+
motion.run(from, to, abs(to - from) / speed, [0, 0, 1, 1], events.has('scroll'));
|
|
783
822
|
}
|
|
784
823
|
}
|
|
785
824
|
|
|
@@ -867,4 +906,25 @@ export default function carousel(element, userOptions, defaults, features) {
|
|
|
867
906
|
function onScroll() {
|
|
868
907
|
viewport.scrollLeft = viewport.scrollTop = 0;
|
|
869
908
|
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Pass a click on a clone to the same element in its slide. A clone is inert, so the click goes to the viewport or the container.
|
|
912
|
+
* A click after a drag is cancelled, and it does not pass.
|
|
913
|
+
*
|
|
914
|
+
* @param {MouseEvent} event
|
|
915
|
+
*/
|
|
916
|
+
function onClick(event) {
|
|
917
|
+
if (event.defaultPrevented || !clones.length || (event.target !== viewport && event.target !== container)) {
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// Find the element under the pointer in the clones, and the element at the same place in the slide
|
|
922
|
+
clones.forEach((node) => node.inert = false);
|
|
923
|
+
const node = document.elementFromPoint(event.clientX, event.clientY),
|
|
924
|
+
index = clones.findIndex((clone) => clone.contains(node)),
|
|
925
|
+
all = (item) => item ? [item, ...item.querySelectorAll('*')] : [];
|
|
926
|
+
clones.forEach((item) => item.inert = true);
|
|
927
|
+
|
|
928
|
+
all(slides[index % slides.length])[all(clones[index]).indexOf(node)]?.dispatchEvent(new MouseEvent('click', event));
|
|
929
|
+
}
|
|
870
930
|
}
|
package/src/scripts/geometry.js
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
import { cloneCount } from './loop.js';
|
|
10
10
|
|
|
11
|
+
// Short names of the Math functions, which the minified file can make shorter
|
|
12
|
+
export const { abs, floor, round } = Math;
|
|
13
|
+
|
|
11
14
|
/**
|
|
12
15
|
* Keep a value between two limits.
|
|
13
16
|
*
|
|
@@ -126,6 +129,29 @@ export function groups(list, view, count) {
|
|
|
126
129
|
return result;
|
|
127
130
|
}
|
|
128
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Find the item with the lowest score.
|
|
134
|
+
*
|
|
135
|
+
* @param {Array} list
|
|
136
|
+
* @param {(item: *, index: number) => number} score Infinity excludes an item.
|
|
137
|
+
* @param {number} [margin] A score must be lower than the best score by more than this value. Then the lower index wins a tie.
|
|
138
|
+
* @returns {number} The index, or -1 if all items are excluded.
|
|
139
|
+
*/
|
|
140
|
+
function lowest(list, score, margin = 0) {
|
|
141
|
+
let best = -1,
|
|
142
|
+
gap = Infinity;
|
|
143
|
+
|
|
144
|
+
list.forEach((item, index) => {
|
|
145
|
+
const value = score(item, index);
|
|
146
|
+
if (value < gap - margin) {
|
|
147
|
+
gap = value;
|
|
148
|
+
best = index;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
return best;
|
|
153
|
+
}
|
|
154
|
+
|
|
129
155
|
/**
|
|
130
156
|
* Get the distance between two locations. In loop mode, use the shortest distance modulo the period.
|
|
131
157
|
*
|
|
@@ -136,7 +162,7 @@ export function groups(list, view, count) {
|
|
|
136
162
|
*/
|
|
137
163
|
export function distance(a, b, period) {
|
|
138
164
|
const value = a - b;
|
|
139
|
-
return period ? value -
|
|
165
|
+
return period ? value - round(value / period) * period : value;
|
|
140
166
|
}
|
|
141
167
|
|
|
142
168
|
/**
|
|
@@ -170,8 +196,8 @@ export function snaps({ slides, view, align, slidesPerSnap, containScroll, perio
|
|
|
170
196
|
}
|
|
171
197
|
|
|
172
198
|
if (period || containScroll !== 'keepSnaps') {
|
|
173
|
-
list = list.filter((location, index) => !index ||
|
|
174
|
-
if (period && list.length > 1 &&
|
|
199
|
+
list = list.filter((location, index) => !index || abs(distance(location, list[index - 1], period)) > 1);
|
|
200
|
+
if (period && list.length > 1 && abs(distance(list[0], list[list.length - 1], period)) <= 1) {
|
|
175
201
|
list.pop();
|
|
176
202
|
}
|
|
177
203
|
}
|
|
@@ -217,32 +243,17 @@ export function snaps({ slides, view, align, slidesPerSnap, containScroll, perio
|
|
|
217
243
|
* @returns {number} The slide index, or -1 if the range is empty.
|
|
218
244
|
*/
|
|
219
245
|
export function activeSlide(slides, location, view, align, period, from = 0, to = slides.length) {
|
|
220
|
-
|
|
221
|
-
gap = Infinity;
|
|
222
|
-
|
|
223
|
-
slides.forEach((slide, index) => {
|
|
224
|
-
const value = Math.abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period));
|
|
225
|
-
if (index >= from && index < to && !slide.hidden && value < gap - 0.5) {
|
|
226
|
-
gap = value;
|
|
227
|
-
best = index;
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
return best;
|
|
246
|
+
return lowest(slides, (slide, index) => index >= from && index < to && !slide.hidden ? abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period)) : Infinity, 0.5);
|
|
232
247
|
}
|
|
233
248
|
|
|
234
249
|
/**
|
|
235
250
|
* Find the snap of a slide: the snap whose active slide it is, or the snap nearest to the raw location of the slide.
|
|
236
251
|
*
|
|
237
|
-
* @param {Array<{location: number, slide: number}
|
|
238
|
-
* @param {Array<{start: number, end: number}>} slides
|
|
252
|
+
* @param {{snaps: Array<{location: number, slide: number}>, slides: Array<{start: number, end: number}>, view: number, align: string|Function, period?: number}} geo
|
|
239
253
|
* @param {number} index The slide index.
|
|
240
|
-
* @param {number} view
|
|
241
|
-
* @param {string|Function} align
|
|
242
|
-
* @param {number} [period]
|
|
243
254
|
* @returns {number} The snap index.
|
|
244
255
|
*/
|
|
245
|
-
export function snapOfSlide(list, slides,
|
|
256
|
+
export function snapOfSlide({ snaps: list, slides, view, align, period }, index) {
|
|
246
257
|
const found = list.findIndex((snap) => snap.slide === index),
|
|
247
258
|
slide = slides[index];
|
|
248
259
|
|
|
@@ -262,18 +273,7 @@ export function snapOfSlide(list, slides, index, view, align, period) {
|
|
|
262
273
|
* @returns {number} The snap index.
|
|
263
274
|
*/
|
|
264
275
|
export function nearestSnap(list, location, period) {
|
|
265
|
-
|
|
266
|
-
gap = Infinity;
|
|
267
|
-
|
|
268
|
-
list.forEach((snap, index) => {
|
|
269
|
-
const value = Math.abs(distance(snap.location, location, period));
|
|
270
|
-
if (value < gap) {
|
|
271
|
-
gap = value;
|
|
272
|
-
best = index;
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
return best;
|
|
276
|
+
return Math.max(0, lowest(list, (snap) => abs(distance(snap.location, location, period))));
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
/**
|
|
@@ -295,7 +295,7 @@ export function targetLocation(target, location, period, direction = 0) {
|
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
if (direction < 0) {
|
|
298
|
-
return target + (
|
|
298
|
+
return target + (floor((location + 0.5 - target) / period) + 1) * period;
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
return location + distance(target, location, period);
|
|
@@ -311,7 +311,7 @@ export function targetLocation(target, location, period, direction = 0) {
|
|
|
311
311
|
export function progress(location, { min, max, period }) {
|
|
312
312
|
if (period) {
|
|
313
313
|
const value = (max - location) / period;
|
|
314
|
-
return value -
|
|
314
|
+
return value - floor(value);
|
|
315
315
|
}
|
|
316
316
|
return max > min ? clamp((max - location) / (max - min), 0, 1) : 0;
|
|
317
317
|
}
|
|
@@ -319,14 +319,12 @@ export function progress(location, { min, max, period }) {
|
|
|
319
319
|
/**
|
|
320
320
|
* Find the slides that show at a location.
|
|
321
321
|
*
|
|
322
|
-
* @param {Array<{start: number, end: number}
|
|
322
|
+
* @param {{slides: Array<{start: number, end: number}>, view: number, period?: number}} geo
|
|
323
323
|
* @param {number} location
|
|
324
|
-
* @param {number} view
|
|
325
324
|
* @param {number} threshold The part of a slide that must show, from 0 to 1. With 0, any part is enough.
|
|
326
|
-
* @param {number} [period]
|
|
327
325
|
* @returns {number[]} The slide indexes.
|
|
328
326
|
*/
|
|
329
|
-
export function inView(slides,
|
|
327
|
+
export function inView({ slides, view, period }, location, threshold) {
|
|
330
328
|
const result = [];
|
|
331
329
|
|
|
332
330
|
slides.forEach((slide, index) => {
|
|
@@ -373,26 +371,27 @@ export function snapPosition(list, location, period) {
|
|
|
373
371
|
* With clones, a logical snap is a snap whose active slide is an original slide.
|
|
374
372
|
*
|
|
375
373
|
* @param {object} layout The result of readLayout().
|
|
376
|
-
* @param {{view: number, slides: Array<{start: number, end: number}>, gap: number}} rects The result of readRects().
|
|
377
|
-
*
|
|
374
|
+
* @param {{view: number, slides: Array<{start: number, end: number}>, gap: number, area: {start: number, size: number}}} rects The result of readRects().
|
|
375
|
+
* The slides after the first count slides are clones.
|
|
378
376
|
* @param {number} count The number of original slides.
|
|
379
377
|
* @param {object} options The options of the carousel.
|
|
380
378
|
* @returns {object} The geometry. If the carousel needs a different number of clones, only { copies }: the number of copies of all slides.
|
|
381
379
|
* map gives the logical snap of each snap.
|
|
382
380
|
*/
|
|
383
|
-
export function compute(layout, rects,
|
|
381
|
+
export function compute(layout, rects, count, options) {
|
|
384
382
|
const content = bounds(rects.slides.slice(0, count)),
|
|
385
|
-
|
|
383
|
+
// The auto scroll with the loop never fits, unless no slide shows, for example in a hidden tab
|
|
384
|
+
fit = content.size <= rects.view + 1 && !(options.autoScroll && options.loop && content.max),
|
|
386
385
|
loop = options.loop && !fit && count,
|
|
387
386
|
// The loop period: the distance from the first slide start to the same point in the next copy of the slides
|
|
388
387
|
period = loop ? bounds(rects.slides).size + rects.gap : 0,
|
|
389
|
-
copies = loop && options.clones ? cloneCount(content.size + rects.gap, area.size, content.max) : 0;
|
|
388
|
+
copies = loop && options.clones ? cloneCount(content.size + rects.gap, rects.area.size, content.max) : 0;
|
|
390
389
|
|
|
391
390
|
if (copies * count !== rects.slides.length - count) {
|
|
392
391
|
return { copies };
|
|
393
392
|
}
|
|
394
393
|
|
|
395
|
-
const geo = { ...layout, ...rects,
|
|
394
|
+
const geo = { ...layout, ...rects, fit, count, period, min: 0, max: 0 };
|
|
396
395
|
|
|
397
396
|
if (fit) {
|
|
398
397
|
geo.snaps = [{ location: 0, slide: Math.max(0, activeSlide(rects.slides, 0, rects.view, layout.align)) }];
|
|
@@ -417,16 +416,7 @@ export function compute(layout, rects, area, count, options) {
|
|
|
417
416
|
* @returns {number} The snap index.
|
|
418
417
|
*/
|
|
419
418
|
export function physicalSnap(geo, index, location) {
|
|
420
|
-
|
|
421
|
-
gap = Infinity;
|
|
422
|
-
|
|
423
|
-
geo.snaps.forEach((snap, item) => {
|
|
424
|
-
const value = Math.abs(distance(snap.location, location, geo.period));
|
|
425
|
-
if (geo.map[item] === index && value < gap) {
|
|
426
|
-
gap = value;
|
|
427
|
-
best = item;
|
|
428
|
-
}
|
|
429
|
-
});
|
|
419
|
+
const best = lowest(geo.snaps, (snap, item) => geo.map[item] === index ? abs(distance(snap.location, location, geo.period)) : Infinity);
|
|
430
420
|
|
|
431
421
|
return best < 0 ? clamp(index, 0, geo.snaps.length - 1) : best;
|
|
432
422
|
}
|