@twistedandy/better-carousel 1.0.0 → 1.1.0
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 +12 -11
- package/dist/better-carousel.js +459 -261
- package/dist/better-carousel.min.js +1 -1
- package/package.json +1 -1
- package/src/scripts/autoplay.js +39 -5
- package/src/scripts/carousel.js +44 -8
- package/src/scripts/geometry.js +9 -6
- package/src/scripts/input.js +61 -9
- package/src/scripts/layout.js +76 -23
- package/src/scripts/markup.js +25 -5
- package/src/scripts/navigation.js +209 -209
- package/src/types/better-carousel.d.ts +232 -232
package/dist/better-carousel.js
CHANGED
|
@@ -208,15 +208,18 @@ const controls = 'a[href],button,input,select,textarea,[tabindex],[role]';
|
|
|
208
208
|
// The slides that can get the "group" role. An element with its own role, such as an article or a list item, does not permit "group".
|
|
209
209
|
const generic = 'div,span,section';
|
|
210
210
|
|
|
211
|
+
// The elements that never show, so they are not slides
|
|
212
|
+
const skipped = new Set(['template', 'script', 'style']);
|
|
213
|
+
|
|
211
214
|
/**
|
|
212
|
-
* Get the slides of a container. The clones that the library made are not slides.
|
|
215
|
+
* Get the slides of a container. The clones that the library made, and the template, script and style elements, are not slides.
|
|
213
216
|
*
|
|
214
217
|
* @param {HTMLElement} container
|
|
215
218
|
* @param {string} [selector] The "slides" option.
|
|
216
219
|
* @returns {HTMLElement[]}
|
|
217
220
|
*/
|
|
218
221
|
function readSlides(container, selector) {
|
|
219
|
-
return [...(selector ? container.querySelectorAll(selector) : container.children)].filter((node) => !made.has(node));
|
|
222
|
+
return [...(selector ? container.querySelectorAll(selector) : container.children)].filter((node) => !made.has(node) && !skipped.has(node.localName));
|
|
220
223
|
}
|
|
221
224
|
|
|
222
225
|
/**
|
|
@@ -233,9 +236,7 @@ function copySlides(slides, count, className) {
|
|
|
233
236
|
for (let copy = 0; copy < count; copy++) {
|
|
234
237
|
for (const node of slides) {
|
|
235
238
|
const clone = node.cloneNode(true);
|
|
236
|
-
|
|
237
|
-
item.removeAttribute('id');
|
|
238
|
-
}
|
|
239
|
+
removeIds(clone);
|
|
239
240
|
clone.setAttribute('aria-hidden', 'true');
|
|
240
241
|
clone.inert = true;
|
|
241
242
|
clone.style.translate = '';
|
|
@@ -249,6 +250,25 @@ function copySlides(slides, count, className) {
|
|
|
249
250
|
return list;
|
|
250
251
|
}
|
|
251
252
|
|
|
253
|
+
// An id must be unique in the page, so a copy has no ids
|
|
254
|
+
function removeIds(node) {
|
|
255
|
+
for (const item of [node, ...node.querySelectorAll('[id]')]) {
|
|
256
|
+
item.removeAttribute('id');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Copy the content of a slide into its clone again.
|
|
262
|
+
*
|
|
263
|
+
* @param {HTMLElement} clone
|
|
264
|
+
* @param {HTMLElement} node The slide.
|
|
265
|
+
*/
|
|
266
|
+
function copyContent(clone, node) {
|
|
267
|
+
const copy = node.cloneNode(true);
|
|
268
|
+
removeIds(copy);
|
|
269
|
+
clone.replaceChildren(...copy.childNodes);
|
|
270
|
+
}
|
|
271
|
+
|
|
252
272
|
/**
|
|
253
273
|
* Give each slide the slide role and a name such as "2 of 8". A slide with its own role, such as an article, keeps it.
|
|
254
274
|
*
|
|
@@ -370,10 +390,13 @@ function controller(context, key, options, hooks) {
|
|
|
370
390
|
} else if (!button.isConnected) {
|
|
371
391
|
root.append(button);
|
|
372
392
|
}
|
|
373
|
-
// Start at the first init, unless the user prefers less motion
|
|
393
|
+
// Start at the first init, unless the user prefers less motion. After a breakpoint change, the play state stays.
|
|
394
|
+
// A play() before the first init starts now, because the carousel had no size before.
|
|
374
395
|
if (first) {
|
|
375
396
|
first = false;
|
|
376
|
-
if (
|
|
397
|
+
if (playing) {
|
|
398
|
+
hooks.play();
|
|
399
|
+
} else if (context.playing?.[key] ?? (options.playOnInit && !matchMedia('(prefers-reduced-motion: reduce)').matches)) {
|
|
377
400
|
control.play();
|
|
378
401
|
}
|
|
379
402
|
}
|
|
@@ -501,8 +524,9 @@ function autoScroll(context) {
|
|
|
501
524
|
|
|
502
525
|
const options = settings(autoScrollDefaults, 'speed', context.options.autoScroll);
|
|
503
526
|
|
|
504
|
-
// The start delay
|
|
505
|
-
let timer = 0
|
|
527
|
+
// The start delay, and true while a pointer presses the viewport
|
|
528
|
+
let timer = 0,
|
|
529
|
+
pressed = false;
|
|
506
530
|
|
|
507
531
|
const control = controller(context, 'autoScroll', options, {
|
|
508
532
|
play: wait,
|
|
@@ -547,11 +571,41 @@ function autoScroll(context) {
|
|
|
547
571
|
function run() {
|
|
548
572
|
timer = 0;
|
|
549
573
|
const geo = context.geo();
|
|
550
|
-
if (geo && !geo.fit && !context.dragging() && !control.paused()) {
|
|
574
|
+
if (geo && !geo.fit && !pressed && !context.dragging() && !control.paused()) {
|
|
551
575
|
context.scroll(options.direction === 'prev' ? -1 : 1, options.speed / 1000);
|
|
552
576
|
}
|
|
553
577
|
}
|
|
554
578
|
|
|
579
|
+
// A press stops the slides under the pointer. A release without a drag starts them again after the delay. A drag is an interaction.
|
|
580
|
+
const listen = (method) => ['pointerup', 'pointercancel'].forEach((name) => window[method](name, release));
|
|
581
|
+
|
|
582
|
+
function press(event) {
|
|
583
|
+
if (!event.button && control.isPlaying() && !pressed) {
|
|
584
|
+
pressed = true;
|
|
585
|
+
wait(-1);
|
|
586
|
+
context.halt();
|
|
587
|
+
listen('addEventListener');
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function release() {
|
|
592
|
+
pressed = false;
|
|
593
|
+
listen('removeEventListener');
|
|
594
|
+
if (control.isPlaying() && !context.dragging()) {
|
|
595
|
+
wait();
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const { viewport } = context,
|
|
600
|
+
remove = control.remove;
|
|
601
|
+
|
|
602
|
+
viewport.addEventListener('pointerdown', press);
|
|
603
|
+
control.remove = () => {
|
|
604
|
+
viewport.removeEventListener('pointerdown', press);
|
|
605
|
+
listen('removeEventListener');
|
|
606
|
+
remove();
|
|
607
|
+
};
|
|
608
|
+
|
|
555
609
|
return control;
|
|
556
610
|
}
|
|
557
611
|
|
|
@@ -700,6 +754,9 @@ function bounds(slides) {
|
|
|
700
754
|
max = 0;
|
|
701
755
|
|
|
702
756
|
for (const slide of slides) {
|
|
757
|
+
if (slide.hidden) {
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
703
760
|
start = Math.min(start, slide.start);
|
|
704
761
|
end = Math.max(end, slide.end);
|
|
705
762
|
max = Math.max(max, slide.end - slide.start);
|
|
@@ -709,14 +766,14 @@ function bounds(slides) {
|
|
|
709
766
|
}
|
|
710
767
|
|
|
711
768
|
/**
|
|
712
|
-
* Put the slides that start at the same position (1 px or less) into one column.
|
|
769
|
+
* Put the slides that start at the same position (1 px or less) into one column. A hidden slide is in no column.
|
|
713
770
|
* In a flex row, one column has one slide. In a grid, a column can have more slides.
|
|
714
771
|
*
|
|
715
772
|
* @param {Array<{start: number, end: number}>} slides
|
|
716
773
|
* @returns {Array<{start: number, end: number}>} The columns, sorted by start.
|
|
717
774
|
*/
|
|
718
775
|
function columns(slides) {
|
|
719
|
-
const order = slides.map((slide, index) => index).sort((a, b) => slides[a].start - slides[b].start || a - b),
|
|
776
|
+
const order = slides.map((slide, index) => index).filter((index) => !slides[index].hidden).sort((a, b) => slides[a].start - slides[b].start || a - b),
|
|
720
777
|
list = [];
|
|
721
778
|
|
|
722
779
|
let column;
|
|
@@ -842,7 +899,7 @@ function snaps({ slides, view, align, slidesPerSnap, containScroll, period = 0 }
|
|
|
842
899
|
/**
|
|
843
900
|
* Find the slide whose anchor is nearest to the align point at a location.
|
|
844
901
|
* The anchor is the start for 'start', the center for 'center' and the end for 'end'.
|
|
845
|
-
* If two slides have the same distance, the lower index wins.
|
|
902
|
+
* If two slides have the same distance, the lower index wins. A hidden slide is never the active slide.
|
|
846
903
|
*
|
|
847
904
|
* @param {Array<{start: number, end: number}>} slides
|
|
848
905
|
* @param {number} location
|
|
@@ -859,7 +916,7 @@ function activeSlide(slides, location, view, align, period, from = 0, to = slide
|
|
|
859
916
|
|
|
860
917
|
slides.forEach((slide, index) => {
|
|
861
918
|
const value = Math.abs(distance(slide.start + location, alignOffset(align, view, slide.end - slide.start, index), period));
|
|
862
|
-
if (index >= from && index < to && value < gap - 0.5) {
|
|
919
|
+
if (index >= from && index < to && !slide.hidden && value < gap - 0.5) {
|
|
863
920
|
gap = value;
|
|
864
921
|
best = index;
|
|
865
922
|
}
|
|
@@ -971,7 +1028,7 @@ function inView(slides, location, view, threshold, period) {
|
|
|
971
1028
|
start = period ? distance(slide.start + location + size / 2, view / 2, period) + view / 2 - size / 2 : slide.start + location,
|
|
972
1029
|
visible = Math.min(start + size, view) - Math.max(start, 0);
|
|
973
1030
|
|
|
974
|
-
if (visible > 0 && visible >= threshold * size - 0.5) {
|
|
1031
|
+
if (!slide.hidden && visible > 0 && visible >= threshold * size - 0.5) {
|
|
975
1032
|
result.push(index);
|
|
976
1033
|
}
|
|
977
1034
|
});
|
|
@@ -1032,7 +1089,7 @@ function compute(layout, rects, area, count, options) {
|
|
|
1032
1089
|
const geo = { ...layout, ...rects, area, fit, count, period, min: 0, max: 0 };
|
|
1033
1090
|
|
|
1034
1091
|
if (fit) {
|
|
1035
|
-
geo.snaps = [{ location: 0, slide: activeSlide(rects.slides, 0, rects.view, layout.align) }];
|
|
1092
|
+
geo.snaps = [{ location: 0, slide: Math.max(0, activeSlide(rects.slides, 0, rects.view, layout.align)) }];
|
|
1036
1093
|
} else {
|
|
1037
1094
|
Object.assign(geo, snaps({ ...rects, align: layout.align, slidesPerSnap: options.slidesPerSnap, containScroll: options.containScroll, period }));
|
|
1038
1095
|
}
|
|
@@ -1115,35 +1172,62 @@ function readLayout(container, options) {
|
|
|
1115
1172
|
}
|
|
1116
1173
|
|
|
1117
1174
|
/**
|
|
1118
|
-
* Read the slide boxes on the main axis, relative to the viewport
|
|
1175
|
+
* Read the slide boxes on the main axis, relative to the inner edge of the viewport padding at location 0. The snaps align to this edge.
|
|
1176
|
+
* The values are in the px of the carousel: in a parent with a CSS scale, such as a modal window that opens with a scale animation, the
|
|
1177
|
+
* values do not change with a scale or a rotation of a parent.
|
|
1178
|
+
* A slide with no box, such as a slide with display: none, is hidden. It gets no snap, and it stays at the end of the slide before it.
|
|
1119
1179
|
*
|
|
1120
1180
|
* @param {HTMLElement} viewport
|
|
1121
1181
|
* @param {HTMLElement[]} nodes All slides, clones included.
|
|
1122
1182
|
* @param {object} layout The result of readLayout().
|
|
1123
1183
|
* @param {{axis: string, container: number, slides: number[]}} written The translate values that the library wrote, in px on the screen.
|
|
1124
|
-
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number}>, gap: number}}
|
|
1125
|
-
* gap is the loop gap: the space between the last slide and the next copy of the first slide.
|
|
1184
|
+
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number, hidden: boolean}>, gap: number, scale: number, pad: {start: number, end: number}}}
|
|
1185
|
+
* view is the viewport size without the padding. gap is the loop gap: the space between the last slide and the next copy of the first slide.
|
|
1186
|
+
* scale is the screen px of one px of the carousel. pad is the viewport padding at the start and at the end.
|
|
1126
1187
|
*/
|
|
1127
1188
|
function readRects(viewport, nodes, layout, written) {
|
|
1128
1189
|
const box = viewport.getBoundingClientRect(),
|
|
1129
1190
|
x = layout.axis === 'x',
|
|
1130
1191
|
sign = layout.sign,
|
|
1131
1192
|
known = written.axis === layout.axis,
|
|
1132
|
-
shift = known ? written.container : 0
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1193
|
+
shift = known ? written.container : 0,
|
|
1194
|
+
width = viewport.offsetWidth,
|
|
1195
|
+
height = viewport.offsetHeight,
|
|
1196
|
+
// offsetWidth is a whole number: a difference of less than 1 px is not a transform
|
|
1197
|
+
transformed = width && height && (Math.abs(box.width - width) >= 1 || Math.abs(box.height - height) >= 1),
|
|
1198
|
+
// A scale s and a rotation by an angle a give a box of s(w cos a + h sin a) by s(w sin a + h cos a). The main axis gets s cos a.
|
|
1199
|
+
scale = !transformed ? 1 : Math.abs(width - height) > 1 ? (width * box.width - height * box.height) / (width * width - height * height) : box.width / width,
|
|
1200
|
+
view = !transformed ? (x ? box.width : box.height) : x ? width : height,
|
|
1201
|
+
style = getComputedStyle(viewport),
|
|
1202
|
+
[before, after] = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'],
|
|
1203
|
+
pad = { start: parseFloat(style['padding' + before]) || 0, end: parseFloat(style['padding' + after]) || 0 };
|
|
1136
1204
|
|
|
1137
1205
|
const slides = nodes.map((node, index) => {
|
|
1138
1206
|
const rect = node.getBoundingClientRect(),
|
|
1139
1207
|
offset = sign * (shift + (known && written.slides[index] || 0)),
|
|
1140
|
-
start = (x ? (layout.rtl ? box.right - rect.right : rect.left - box.left) : rect.top - box.top) - offset,
|
|
1141
|
-
size = x ? rect.width : rect.height;
|
|
1208
|
+
start = (x ? (layout.rtl ? box.right - rect.right : rect.left - box.left) : rect.top - box.top) / scale - pad.start - offset,
|
|
1209
|
+
size = (x ? rect.width : rect.height) / scale;
|
|
1142
1210
|
|
|
1143
|
-
return { start, end: start + size, cross: x ? rect.height : rect.width };
|
|
1211
|
+
return { start, end: start + size, cross: (x ? rect.height : rect.width) / scale, hidden: !rect.width && !rect.height };
|
|
1144
1212
|
});
|
|
1145
1213
|
|
|
1214
|
+
let edge = slides.find((slide) => !slide.hidden)?.start || 0;
|
|
1215
|
+
|
|
1216
|
+
for (const slide of slides) {
|
|
1217
|
+
if (slide.hidden) {
|
|
1218
|
+
slide.start = slide.end = edge;
|
|
1219
|
+
} else {
|
|
1220
|
+
edge = slide.end;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
let first = Math.max(0, slides.findIndex((slide) => !slide.hidden)),
|
|
1225
|
+
last = first;
|
|
1226
|
+
|
|
1146
1227
|
slides.forEach((slide, index) => {
|
|
1228
|
+
if (slide.hidden) {
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1147
1231
|
if (slide.start < slides[first].start) {
|
|
1148
1232
|
first = index;
|
|
1149
1233
|
}
|
|
@@ -1160,9 +1244,11 @@ function readRects(viewport, nodes, layout, written) {
|
|
|
1160
1244
|
}
|
|
1161
1245
|
|
|
1162
1246
|
return {
|
|
1163
|
-
view:
|
|
1247
|
+
view: view - pad.start - pad.end,
|
|
1164
1248
|
slides,
|
|
1165
|
-
gap
|
|
1249
|
+
gap,
|
|
1250
|
+
scale,
|
|
1251
|
+
pad
|
|
1166
1252
|
};
|
|
1167
1253
|
}
|
|
1168
1254
|
|
|
@@ -1177,46 +1263,70 @@ function windowSize(axis) {
|
|
|
1177
1263
|
}
|
|
1178
1264
|
|
|
1179
1265
|
/**
|
|
1180
|
-
* Get the part of the main axis where the loop must show slides, relative to the viewport
|
|
1181
|
-
* It is the viewport. With clones: 'fill', it is the window, so that the slides fill it when the viewport overflow shows.
|
|
1266
|
+
* Get the part of the main axis where the loop must show slides, relative to the inner edge of the viewport padding.
|
|
1267
|
+
* It is the viewport with its padding. With clones: 'fill', it is the window, so that the slides fill it when the viewport overflow shows.
|
|
1182
1268
|
*
|
|
1183
1269
|
* @param {HTMLElement} viewport
|
|
1184
1270
|
* @param {{axis: string, rtl: boolean}} layout
|
|
1185
|
-
* @param {number} view The viewport size.
|
|
1271
|
+
* @param {number} view The viewport size without the padding.
|
|
1272
|
+
* @param {{start: number, end: number}} pad The viewport padding.
|
|
1186
1273
|
* @param {number} [size] The window size on the main axis, for clones: 'fill'. Without it, the area is the viewport.
|
|
1187
1274
|
* @returns {{start: number, size: number}}
|
|
1188
1275
|
*/
|
|
1189
|
-
function visibleArea(viewport, layout, view, size) {
|
|
1276
|
+
function visibleArea(viewport, layout, view, pad, size) {
|
|
1190
1277
|
if (!size) {
|
|
1191
|
-
return { start:
|
|
1278
|
+
return { start: -pad.start, size: pad.start + view + pad.end };
|
|
1192
1279
|
}
|
|
1193
1280
|
|
|
1194
1281
|
const box = viewport.getBoundingClientRect(),
|
|
1195
|
-
before = Math.max(0, Math.round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top));
|
|
1282
|
+
before = Math.max(0, Math.round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top)) + pad.start;
|
|
1196
1283
|
|
|
1197
|
-
return { start: -before, size: before + Math.max(view, Math.round(size - before)) };
|
|
1284
|
+
return { start: -before, size: before + Math.max(view + pad.end, Math.round(size - before)) };
|
|
1198
1285
|
}
|
|
1199
1286
|
|
|
1200
1287
|
// One ResizeObserver for all carousels. Each callback first reads the layout of all carousels, then writes. In this way, the reads of one carousel do not
|
|
1201
1288
|
// come after the writes of another carousel, and the browser does one layout for all of them.
|
|
1202
|
-
const owners = new WeakMap()
|
|
1289
|
+
const owners = new WeakMap(),
|
|
1290
|
+
// The elements of each owner, and the last size of each element
|
|
1291
|
+
watched = new WeakMap(),
|
|
1292
|
+
sizes = new WeakMap();
|
|
1203
1293
|
|
|
1204
1294
|
let resizer;
|
|
1205
1295
|
|
|
1206
1296
|
/**
|
|
1207
1297
|
* Watch the size of elements. The ResizeObserver callback calls owner.read() for each owner, and then owner.write() for each owner whose read() returned true.
|
|
1208
|
-
* An entry for which owner.skip() returns true does not count.
|
|
1298
|
+
* An entry for which owner.skip() returns true does not count. An entry with the same size as the last entry of its element does not count:
|
|
1299
|
+
* a new watch of an element after a write sends such an entry. observeSize() clears the last size, so its first entry always counts.
|
|
1209
1300
|
*
|
|
1210
1301
|
* @param {Element[]} nodes
|
|
1211
1302
|
* @param {{read: () => boolean, write: () => void, skip: (entry: ResizeObserverEntry) => boolean}} [owner] Without an owner, stop the watch.
|
|
1212
1303
|
*/
|
|
1213
1304
|
function observeSize(nodes, owner) {
|
|
1214
1305
|
resizer ||= new ResizeObserver((entries) => {
|
|
1215
|
-
const list = new Set(entries.filter((entry) =>
|
|
1216
|
-
|
|
1306
|
+
const list = new Set(entries.filter((entry) => {
|
|
1307
|
+
const { inlineSize, blockSize } = entry.borderBoxSize[0],
|
|
1308
|
+
size = inlineSize + ',' + blockSize,
|
|
1309
|
+
same = sizes.get(entry.target) === size;
|
|
1310
|
+
sizes.set(entry.target, size);
|
|
1311
|
+
return !same && !owners.get(entry.target)?.skip(entry);
|
|
1312
|
+
}).map((entry) => owners.get(entry.target)));
|
|
1313
|
+
const changed = [...list].filter((item) => item && item.read());
|
|
1314
|
+
changed.forEach((item) => item.write());
|
|
1315
|
+
// A write can change the size of an element that the observer watches, for example the auto height or the clones. If the observer has
|
|
1316
|
+
// a change to report after its callback, the browser sends the "ResizeObserver loop" error to window.onerror. Stop the watch of these
|
|
1317
|
+
// elements, and watch them again in the next frame. Then the first callback measures again and finds no change.
|
|
1318
|
+
if (changed.length) {
|
|
1319
|
+
changed.forEach((item) => watched.get(item)?.forEach((node) => resizer.unobserve(node)));
|
|
1320
|
+
requestAnimationFrame(() => changed.forEach((item) => watched.get(item)?.forEach((node) => owners.get(node) === item && resizer.observe(node))));
|
|
1321
|
+
}
|
|
1217
1322
|
});
|
|
1218
1323
|
|
|
1324
|
+
if (owner) {
|
|
1325
|
+
watched.set(owner, nodes);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1219
1328
|
for (const node of nodes) {
|
|
1329
|
+
sizes.delete(node);
|
|
1220
1330
|
if (owner) {
|
|
1221
1331
|
owners.set(node, owner);
|
|
1222
1332
|
resizer.observe(node);
|
|
@@ -1741,6 +1851,8 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1741
1851
|
// The observers and the measure state
|
|
1742
1852
|
let observed = [],
|
|
1743
1853
|
mutationObserver,
|
|
1854
|
+
// Watches the content of the slides while the carousel has clones
|
|
1855
|
+
copyObserver,
|
|
1744
1856
|
media = null,
|
|
1745
1857
|
ready = false,
|
|
1746
1858
|
started = false,
|
|
@@ -1767,8 +1879,9 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1767
1879
|
|
|
1768
1880
|
const api = {
|
|
1769
1881
|
goToSnap: (index, instant) => go(physical(index), instant),
|
|
1770
|
-
|
|
1771
|
-
|
|
1882
|
+
// Before the first measure pass, keep the step in the selected snap
|
|
1883
|
+
goToNext: (instant) => geo ? api.canGoToNext() && step(selected, 1, instant) : go(selected + 1, instant),
|
|
1884
|
+
goToPrev: (instant) => geo ? api.canGoToPrev() && step(selected, -1, instant) : go(selected - 1, instant),
|
|
1772
1885
|
goToSlide: (index, instant) => go(physical(api.snapOfSlide(index)), instant, index),
|
|
1773
1886
|
canGoToNext: () => !!geo && !geo.fit && (!!geo.period || selected < geo.snaps.length - 1),
|
|
1774
1887
|
canGoToPrev: () => !!geo && !geo.fit && (!!geo.period || selected > 0),
|
|
@@ -1799,7 +1912,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1799
1912
|
each = { node: null, index: 0, offset: 0, distance: 0, size: 0, visible: 0 },
|
|
1800
1913
|
/**
|
|
1801
1914
|
* What the features share: the API, the events, and the state and the actions of the carousel.
|
|
1802
|
-
* setup() adds root, viewport, container, cls and options.
|
|
1915
|
+
* setup() adds root, viewport, container, cls and options. reinit() adds playing: the play state of the autoplay and the auto scroll before a breakpoint change.
|
|
1803
1916
|
*/
|
|
1804
1917
|
context = {
|
|
1805
1918
|
api,
|
|
@@ -1977,6 +2090,8 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1977
2090
|
resolved = null;
|
|
1978
2091
|
}
|
|
1979
2092
|
const keep = geo ? selectedSlide() : requested;
|
|
2093
|
+
// After a breakpoint change, the new autoplay and auto scroll keep the play state. New options start them again.
|
|
2094
|
+
context.playing = newOptions ? null : { autoplay: api.autoplay?.isPlaying(), autoScroll: api.autoScroll?.isPlaying() };
|
|
1980
2095
|
teardown(true);
|
|
1981
2096
|
setup();
|
|
1982
2097
|
requested = keep;
|
|
@@ -1998,6 +2113,27 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
1998
2113
|
nodes = slides.concat(clones);
|
|
1999
2114
|
written.slides.length = slides.length;
|
|
2000
2115
|
mutationObserver?.takeRecords();
|
|
2116
|
+
|
|
2117
|
+
copyObserver?.disconnect();
|
|
2118
|
+
if (clones.length) {
|
|
2119
|
+
copyObserver ||= new MutationObserver(onCopy);
|
|
2120
|
+
slides.forEach((node) => copyObserver.observe(node, { childList: true, characterData: true, subtree: true, attributeFilter: ['class', 'src', 'srcset', 'href', 'value'] }));
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
/**
|
|
2125
|
+
* Copy a changed slide into its clones. The carousel writes the classes of the slides, so a change of a slide's own attributes does not count.
|
|
2126
|
+
*
|
|
2127
|
+
* @param {MutationRecord[]} records
|
|
2128
|
+
*/
|
|
2129
|
+
function onCopy(records) {
|
|
2130
|
+
const changed = new Set(records.filter((record) => record.type !== 'attributes' || !slides.includes(record.target)).map(({ target }) => slides.find((node) => node.contains(target))));
|
|
2131
|
+
|
|
2132
|
+
changed.forEach((node) => {
|
|
2133
|
+
for (let index = slides.indexOf(node); node && index < clones.length; index += slides.length) {
|
|
2134
|
+
copyContent(clones[index], node);
|
|
2135
|
+
}
|
|
2136
|
+
});
|
|
2001
2137
|
}
|
|
2002
2138
|
|
|
2003
2139
|
/**
|
|
@@ -2027,8 +2163,15 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2027
2163
|
return same && options.autoHeight && geo?.axis === 'x';
|
|
2028
2164
|
}
|
|
2029
2165
|
|
|
2166
|
+
// Keep the selected slide. If the page removed it, select the slide that is now at its index, or the last slide.
|
|
2030
2167
|
function onMutation() {
|
|
2168
|
+
const old = ready ? selectedSlide() : -1,
|
|
2169
|
+
node = slides[old];
|
|
2031
2170
|
slides = readSlides(container, options.slides);
|
|
2171
|
+
if (old > -1) {
|
|
2172
|
+
const index = slides.indexOf(node);
|
|
2173
|
+
requested = index > -1 ? index : Math.min(old, slides.length - 1);
|
|
2174
|
+
}
|
|
2032
2175
|
setClones(0);
|
|
2033
2176
|
nodes = slides;
|
|
2034
2177
|
changed = true;
|
|
@@ -2081,7 +2224,7 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2081
2224
|
// With clones: 'fill', the loop fills the window
|
|
2082
2225
|
lastWindow = options.loop && options.clones === 'fill' ? windowSize(layout.axis) : 0;
|
|
2083
2226
|
|
|
2084
|
-
const area = visibleArea(viewport, layout, rects.view, lastWindow),
|
|
2227
|
+
const area = visibleArea(viewport, layout, rects.view, rects.pad, lastWindow),
|
|
2085
2228
|
values = [layout.axis, layout.rtl, typeof layout.align === 'string' && layout.align, rects.view, rects.gap, area.start, area.size, ...rects.slides.flatMap((slide) => [slide.start, slide.end, slide.cross])];
|
|
2086
2229
|
|
|
2087
2230
|
// 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.
|
|
@@ -2091,7 +2234,8 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2091
2234
|
|
|
2092
2235
|
signature = values;
|
|
2093
2236
|
|
|
2094
|
-
|
|
2237
|
+
// After a change of the slides, onMutation() gives the slide to keep
|
|
2238
|
+
const keep = ready && !changed ? selectedSlide() : requested,
|
|
2095
2239
|
next = compute(layout, rects, area, slides.length, options);
|
|
2096
2240
|
|
|
2097
2241
|
if (!next.snaps) {
|
|
@@ -2103,8 +2247,10 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2103
2247
|
|
|
2104
2248
|
const total = geo.snaps.length;
|
|
2105
2249
|
|
|
2106
|
-
// Before the first measure pass, selected is the logical snap of the startSnap option or of
|
|
2107
|
-
|
|
2250
|
+
// Before the first measure pass, selected is the logical snap of the startSnap option, of goToSnap() or of goToNext().
|
|
2251
|
+
// In loop mode, a step before the first snap goes to the last snap.
|
|
2252
|
+
const count = geo.logical.length;
|
|
2253
|
+
selected = keep >= 0 ? physical(api.snapOfSlide(keep)) : Math.min(total - 1, ready ? selected : physical(geo.period ? (selected % count + count) % count : Math.max(selected, 0)));
|
|
2108
2254
|
previous = Math.min(previous, total - 1);
|
|
2109
2255
|
requested = keep;
|
|
2110
2256
|
if (!dragging) {
|
|
@@ -2499,6 +2645,9 @@ function carousel(element, userOptions, defaults, features) {
|
|
|
2499
2645
|
// The targets that keep their own pointer behavior
|
|
2500
2646
|
const fields = 'input,textarea,select,[contenteditable]';
|
|
2501
2647
|
|
|
2648
|
+
// The pointerdown events that a carousel took for a drag
|
|
2649
|
+
const claimed = new WeakSet();
|
|
2650
|
+
|
|
2502
2651
|
// The duration of the motion for one wheel event, in ms
|
|
2503
2652
|
const WHEEL = 180;
|
|
2504
2653
|
|
|
@@ -2627,17 +2776,21 @@ function drag(context) {
|
|
|
2627
2776
|
|
|
2628
2777
|
const geo = context.geo();
|
|
2629
2778
|
|
|
2630
|
-
|
|
2779
|
+
// In nested carousels, the inner carousel gets the press first and claims it
|
|
2780
|
+
if (pointer !== null || event.button || !geo || geo.fit || claimed.has(event) || event.target.closest(fields)) {
|
|
2631
2781
|
return;
|
|
2632
2782
|
}
|
|
2633
2783
|
|
|
2784
|
+
claimed.add(event);
|
|
2785
|
+
|
|
2634
2786
|
// Stop the text selection and the native image drag
|
|
2635
2787
|
if (event.pointerType === 'mouse') {
|
|
2636
2788
|
event.preventDefault();
|
|
2637
2789
|
}
|
|
2638
2790
|
|
|
2639
2791
|
pointer = event.pointerId;
|
|
2640
|
-
|
|
2792
|
+
// In a parent with a CSS scale, one px of the pointer is not one px of the carousel
|
|
2793
|
+
axis = { x: geo.axis === 'x', sign: geo.sign, scale: geo.scale };
|
|
2641
2794
|
start = [event.clientX, event.clientY];
|
|
2642
2795
|
samples = [];
|
|
2643
2796
|
listen('addEventListener');
|
|
@@ -2656,8 +2809,8 @@ function drag(context) {
|
|
|
2656
2809
|
return end(event.type === 'pointerup');
|
|
2657
2810
|
}
|
|
2658
2811
|
|
|
2659
|
-
const dx = event.clientX - start[0],
|
|
2660
|
-
dy = event.clientY - start[1],
|
|
2812
|
+
const dx = (event.clientX - start[0]) / axis.scale,
|
|
2813
|
+
dy = (event.clientY - start[1]) / axis.scale,
|
|
2661
2814
|
main = axis.x ? dx * axis.sign : dy,
|
|
2662
2815
|
cross = axis.x ? dy : dx;
|
|
2663
2816
|
|
|
@@ -2743,6 +2896,31 @@ function drag(context) {
|
|
|
2743
2896
|
};
|
|
2744
2897
|
}
|
|
2745
2898
|
|
|
2899
|
+
/**
|
|
2900
|
+
* Find an element between a node and the viewport that can scroll in a direction.
|
|
2901
|
+
*
|
|
2902
|
+
* @param {Element} node The target of the wheel event.
|
|
2903
|
+
* @param {Element} viewport
|
|
2904
|
+
* @param {string} axis 'x' or 'y'.
|
|
2905
|
+
* @param {number} delta The wheel delta on the axis.
|
|
2906
|
+
* @returns {boolean}
|
|
2907
|
+
*/
|
|
2908
|
+
function scrolls(node, viewport, axis, delta) {
|
|
2909
|
+
const [size, client, position, overflow] = axis === 'x' ? ['scrollWidth', 'clientWidth', 'scrollLeft', 'overflowX'] : ['scrollHeight', 'clientHeight', 'scrollTop', 'overflowY'];
|
|
2910
|
+
|
|
2911
|
+
for (; node && node !== viewport; node = node.parentElement) {
|
|
2912
|
+
// Read the style only for an element with more content than space
|
|
2913
|
+
if (node[size] > node[client] && /auto|scroll/.test(getComputedStyle(node)[overflow])) {
|
|
2914
|
+
const at = Math.abs(node[position]);
|
|
2915
|
+
if (delta > 0 ? at + node[client] < node[size] - 1 : at > 0) {
|
|
2916
|
+
return true;
|
|
2917
|
+
}
|
|
2918
|
+
}
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
return false;
|
|
2922
|
+
}
|
|
2923
|
+
|
|
2746
2924
|
/**
|
|
2747
2925
|
* Make the wheel input of a viewport: in a vertical carousel, and with wheel: 'both' also in a horizontal carousel. The carousel follows
|
|
2748
2926
|
* the wheel distance with a short motion on the compositor. When the wheel stops, the carousel goes to a snap. At the first and the last
|
|
@@ -2760,9 +2938,12 @@ function wheel(context) {
|
|
|
2760
2938
|
return;
|
|
2761
2939
|
}
|
|
2762
2940
|
|
|
2763
|
-
// The time of the last wheel event, and
|
|
2941
|
+
// The time and the delta size of the last wheel event, and true if the gesture moved the carousel
|
|
2764
2942
|
let time = 0,
|
|
2765
|
-
|
|
2943
|
+
last = 0,
|
|
2944
|
+
held = false,
|
|
2945
|
+
// true while a gesture scrolls an element in a slide
|
|
2946
|
+
inner = false,
|
|
2766
2947
|
// The timer that finds the end of a gesture
|
|
2767
2948
|
timer = 0,
|
|
2768
2949
|
// The selected snap at the gesture start, and the direction of the last step
|
|
@@ -2780,7 +2961,24 @@ function wheel(context) {
|
|
|
2780
2961
|
|
|
2781
2962
|
const now = event.timeStamp,
|
|
2782
2963
|
gesture = now - time < 200,
|
|
2783
|
-
|
|
2964
|
+
size = Math.abs(delta),
|
|
2965
|
+
// A delta that does not grow is the inertia of the gesture, not a new push
|
|
2966
|
+
inertia = gesture && size <= last;
|
|
2967
|
+
|
|
2968
|
+
last = size;
|
|
2969
|
+
if (!gesture) {
|
|
2970
|
+
held = false;
|
|
2971
|
+
}
|
|
2972
|
+
|
|
2973
|
+
// An element in a slide that can scroll in the wheel direction scrolls, also for the rest of its gesture
|
|
2974
|
+
if ((gesture && inner) || scrolls(event.target, viewport, side ? 'x' : 'y', side ? event.deltaX : delta)) {
|
|
2975
|
+
inner = true;
|
|
2976
|
+
time = now;
|
|
2977
|
+
return;
|
|
2978
|
+
}
|
|
2979
|
+
inner = false;
|
|
2980
|
+
|
|
2981
|
+
const base = context.location(),
|
|
2784
2982
|
path = delta * (event.deltaMode === 1 ? 40 : event.deltaMode ? geo.view : 1);
|
|
2785
2983
|
|
|
2786
2984
|
let target = base - path;
|
|
@@ -2792,7 +2990,7 @@ function wheel(context) {
|
|
|
2792
2990
|
|
|
2793
2991
|
// At the end, the page scrolls. The inertia of a gesture that moved the carousel does not scroll the page.
|
|
2794
2992
|
if (Math.abs(target - base) < 0.5) {
|
|
2795
|
-
if (
|
|
2993
|
+
if (held && inertia) {
|
|
2796
2994
|
event.preventDefault();
|
|
2797
2995
|
}
|
|
2798
2996
|
return;
|
|
@@ -2804,7 +3002,7 @@ function wheel(context) {
|
|
|
2804
3002
|
if (!timer) {
|
|
2805
3003
|
snap = context.selected();
|
|
2806
3004
|
}
|
|
2807
|
-
|
|
3005
|
+
held = true;
|
|
2808
3006
|
direction = path > 0 ? 1 : -1;
|
|
2809
3007
|
context.move(nearestSnap(geo.snaps, target, geo.period), target, WHEEL, EASE);
|
|
2810
3008
|
|
|
@@ -2920,212 +3118,212 @@ function focus(context) {
|
|
|
2920
3118
|
};
|
|
2921
3119
|
}
|
|
2922
3120
|
|
|
2923
|
-
/*
|
|
2924
|
-
* Navigation: the arrows, the dots, and the link to another carousel (the sync option).
|
|
2925
|
-
*
|
|
2926
|
-
* Each feature takes the context of a carousel (see carousel.js). It returns nothing if its option is off, or an object with remove().
|
|
2927
|
-
*/
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
/**
|
|
2931
|
-
* Make the arrows and the dots of a carousel. They update on the carousel events.
|
|
2932
|
-
*
|
|
2933
|
-
* @param {object} context What the features share. See carousel.js.
|
|
2934
|
-
* @returns {{remove: () => void}|undefined} remove() removes the arrows and the dots.
|
|
2935
|
-
*/
|
|
2936
|
-
function nav(context) {
|
|
2937
|
-
const { api, root, cls, options } = context,
|
|
2938
|
-
labels = options.labels,
|
|
2939
|
-
arrows = optionElement(options.arrows, root),
|
|
2940
|
-
box = optionElement(options.dots, root),
|
|
2941
|
-
buttons = [],
|
|
2942
|
-
dots = [],
|
|
2943
|
-
// The window of 5 dots, or all dots with dotsStyle: 'all'
|
|
2944
|
-
windowed = options.dotsStyle !== 'all',
|
|
2945
|
-
// The window of the dots, the track that slides in it, and the marker of the selected dot
|
|
2946
|
-
list = box && create('div', windowed ? cls.dots : cls.dots + ' ' + cls.dotsAll),
|
|
2947
|
-
track = list && create('div', cls.dotsTrack);
|
|
2948
|
-
|
|
2949
|
-
let marks = new Map();
|
|
2950
|
-
|
|
2951
|
-
if (!arrows && !box) {
|
|
2952
|
-
return;
|
|
2953
|
-
}
|
|
2954
|
-
|
|
2955
|
-
// Add the window of the dots now: its CSS height is fixed, so the dots that the first measure adds do not shift the page
|
|
2956
|
-
if (list) {
|
|
2957
|
-
track.append(create('span', cls.dotsMarker));
|
|
2958
|
-
list.append(track);
|
|
2959
|
-
box.append(list);
|
|
2960
|
-
}
|
|
2961
|
-
|
|
2962
|
-
if (arrows) {
|
|
2963
|
-
[['prev', 'goToPrev'], ['next', 'goToNext']].forEach(([key, method]) => {
|
|
2964
|
-
const button = create('button', cls.button + ' ' + cls[key + 'Button']);
|
|
2965
|
-
button.type = 'button';
|
|
2966
|
-
button.setAttribute('aria-label', labels[key]);
|
|
2967
|
-
button.setAttribute('aria-controls', context.container.id);
|
|
2968
|
-
button.addEventListener('click', () => api[method]());
|
|
2969
|
-
buttons.push(button);
|
|
2970
|
-
});
|
|
2971
|
-
}
|
|
2972
|
-
|
|
2973
|
-
function update() {
|
|
2974
|
-
const fit = api.isFit(),
|
|
2975
|
-
count = fit ? 0 : api.snapProgress().length,
|
|
2976
|
-
selected = api.selectedSnap();
|
|
2977
|
-
|
|
2978
|
-
buttons.forEach((button, index) => {
|
|
2979
|
-
if (fit) {
|
|
2980
|
-
button.remove();
|
|
2981
|
-
} else {
|
|
2982
|
-
if (!button.isConnected) {
|
|
2983
|
-
// Before the dots, for the tab order
|
|
2984
|
-
list?.parentNode === arrows ? list.before(button) : arrows.append(button);
|
|
2985
|
-
}
|
|
2986
|
-
const disabled = !(index ? api.canGoToNext() : api.canGoToPrev());
|
|
2987
|
-
if (button.disabled !== disabled) {
|
|
2988
|
-
button.disabled = disabled;
|
|
2989
|
-
}
|
|
2990
|
-
}
|
|
2991
|
-
});
|
|
2992
|
-
|
|
2993
|
-
if (!list) {
|
|
2994
|
-
return;
|
|
2995
|
-
}
|
|
2996
|
-
|
|
2997
|
-
if (count < 2) {
|
|
2998
|
-
list.remove();
|
|
2999
|
-
return;
|
|
3000
|
-
}
|
|
3001
|
-
|
|
3002
|
-
while (dots.length > count) {
|
|
3003
|
-
dots.pop().remove();
|
|
3004
|
-
marks.delete(dots.length);
|
|
3005
|
-
}
|
|
3006
|
-
|
|
3007
|
-
while (dots.length < count) {
|
|
3008
|
-
const dot = create('button', cls.dot),
|
|
3009
|
-
index = dots.length;
|
|
3010
|
-
dot.type = 'button';
|
|
3011
|
-
dot.setAttribute('aria-label', labels.dot.replace('%d', index + 1));
|
|
3012
|
-
dot.setAttribute('aria-controls', context.container.id);
|
|
3013
|
-
// The window of the dots hides the dots out of the zoom: keep them out of the tab order
|
|
3014
|
-
if (windowed) {
|
|
3015
|
-
dot.tabIndex = -1;
|
|
3016
|
-
}
|
|
3017
|
-
dot.addEventListener('click', () => api.goToSnap(index));
|
|
3018
|
-
dots.push(dot);
|
|
3019
|
-
track.append(dot);
|
|
3020
|
-
}
|
|
3021
|
-
|
|
3022
|
-
if (!list.isConnected) {
|
|
3023
|
-
box.append(list);
|
|
3024
|
-
}
|
|
3025
|
-
|
|
3026
|
-
// The zoom effect: the 5 dots around the selected dot get state classes
|
|
3027
|
-
const next = new Map();
|
|
3028
|
-
|
|
3029
|
-
['first', 'prev', 'current', 'next', 'last'].forEach((key, offset) => {
|
|
3030
|
-
if (dots[selected + offset - 2]) {
|
|
3031
|
-
next.set(selected + offset - 2, cls[key]);
|
|
3032
|
-
}
|
|
3033
|
-
});
|
|
3034
|
-
|
|
3035
|
-
marks.forEach((name, index) => {
|
|
3036
|
-
if (next.get(index) !== name) {
|
|
3037
|
-
setClass(dots[index], name, false);
|
|
3038
|
-
setAttribute(dots[index], 'aria-current', false);
|
|
3039
|
-
dots[index].tabIndex = windowed ? -1 : 0;
|
|
3040
|
-
}
|
|
3041
|
-
});
|
|
3042
|
-
|
|
3043
|
-
next.forEach((name, index) => {
|
|
3044
|
-
setClass(dots[index], name, true);
|
|
3045
|
-
setAttribute(dots[index], 'aria-current', index === selected && 'true');
|
|
3046
|
-
dots[index].tabIndex = 0;
|
|
3047
|
-
});
|
|
3048
|
-
|
|
3049
|
-
marks = next;
|
|
3050
|
-
|
|
3051
|
-
// Center the group of up to 5 dots in the window of the dots, and move the marker to the selected dot. The CSS moves the track and the marker with "translate".
|
|
3052
|
-
if (windowed) {
|
|
3053
|
-
variable('--better-shift', 2 - (Math.max(0, selected - 2) + Math.min(count - 1, selected + 2)) / 2);
|
|
3054
|
-
}
|
|
3055
|
-
variable('--better-dot', selected);
|
|
3056
|
-
}
|
|
3057
|
-
|
|
3058
|
-
// Set a CSS variable on the window of the dots, if its value changes
|
|
3059
|
-
function variable(name, value) {
|
|
3060
|
-
if (list.style.getPropertyValue(name) !== String(value)) {
|
|
3061
|
-
list.style.setProperty(name, value);
|
|
3062
|
-
}
|
|
3063
|
-
}
|
|
3064
|
-
|
|
3065
|
-
['init', 'reinit', 'resize', 'select', 'slideschanged'].forEach((name) => context.on(name, update));
|
|
3066
|
-
|
|
3067
|
-
return {
|
|
3068
|
-
remove() {
|
|
3069
|
-
buttons.forEach((button) => button.remove());
|
|
3070
|
-
list?.remove();
|
|
3071
|
-
}
|
|
3072
|
-
};
|
|
3073
|
-
}
|
|
3074
|
-
|
|
3075
|
-
/**
|
|
3076
|
-
* Connect a carousel to another carousel with the same slides, such as thumbnails to a main gallery. This carousel follows the slide
|
|
3077
|
-
* that the other carousel selects. Only a click on a slide of this carousel selects that slide in the other carousel: a drag does not.
|
|
3078
|
-
*
|
|
3079
|
-
* @param {object} context What the features share. See carousel.js. The "sync" option is the other carousel, its container or a selector.
|
|
3080
|
-
* @returns {{remove: () => void}|undefined} remove() disconnects the carousels.
|
|
3081
|
-
*/
|
|
3082
|
-
function sync(context) {
|
|
3083
|
-
const { api, container } = context,
|
|
3084
|
-
target = context.options.sync;
|
|
3085
|
-
|
|
3086
|
-
let other = null;
|
|
3087
|
-
|
|
3088
|
-
if (!target) {
|
|
3089
|
-
return;
|
|
3090
|
-
}
|
|
3091
|
-
|
|
3092
|
-
// Show the slide that the other carousel selects
|
|
3093
|
-
function show(instant) {
|
|
3094
|
-
const index = other.selectedSlide();
|
|
3095
|
-
context.follow(index);
|
|
3096
|
-
api.goToSlide(index, instant === true);
|
|
3097
|
-
}
|
|
3098
|
-
|
|
3099
|
-
function click(event) {
|
|
3100
|
-
// The slide or the clone that gets the click, and then its original slide
|
|
3101
|
-
const index = context.nodes().findIndex((node) => node.contains(event.target));
|
|
3102
|
-
if (other && index > -1) {
|
|
3103
|
-
other.goToSlide(index % api.slideNodes().length);
|
|
3104
|
-
}
|
|
3105
|
-
}
|
|
3106
|
-
|
|
3107
|
-
// The other carousel can come later than this one, so find it at the init
|
|
3108
|
-
function connect() {
|
|
3109
|
-
if (other) {
|
|
3110
|
-
return;
|
|
3111
|
-
}
|
|
3112
|
-
other = target.goToSnap ? target : find(typeof target === 'string' ? optionElement(target, context.root) : target);
|
|
3113
|
-
if (other) {
|
|
3114
|
-
other.on('select', show);
|
|
3115
|
-
show(true);
|
|
3116
|
-
}
|
|
3117
|
-
}
|
|
3118
|
-
|
|
3119
|
-
context.on('init', connect);
|
|
3120
|
-
context.on('reinit', connect);
|
|
3121
|
-
container.addEventListener('click', click);
|
|
3122
|
-
|
|
3123
|
-
return {
|
|
3124
|
-
remove() {
|
|
3125
|
-
other?.off('select', show);
|
|
3126
|
-
container.removeEventListener('click', click);
|
|
3127
|
-
}
|
|
3128
|
-
};
|
|
3121
|
+
/*
|
|
3122
|
+
* Navigation: the arrows, the dots, and the link to another carousel (the sync option).
|
|
3123
|
+
*
|
|
3124
|
+
* Each feature takes the context of a carousel (see carousel.js). It returns nothing if its option is off, or an object with remove().
|
|
3125
|
+
*/
|
|
3126
|
+
|
|
3127
|
+
|
|
3128
|
+
/**
|
|
3129
|
+
* Make the arrows and the dots of a carousel. They update on the carousel events.
|
|
3130
|
+
*
|
|
3131
|
+
* @param {object} context What the features share. See carousel.js.
|
|
3132
|
+
* @returns {{remove: () => void}|undefined} remove() removes the arrows and the dots.
|
|
3133
|
+
*/
|
|
3134
|
+
function nav(context) {
|
|
3135
|
+
const { api, root, cls, options } = context,
|
|
3136
|
+
labels = options.labels,
|
|
3137
|
+
arrows = optionElement(options.arrows, root),
|
|
3138
|
+
box = optionElement(options.dots, root),
|
|
3139
|
+
buttons = [],
|
|
3140
|
+
dots = [],
|
|
3141
|
+
// The window of 5 dots, or all dots with dotsStyle: 'all'
|
|
3142
|
+
windowed = options.dotsStyle !== 'all',
|
|
3143
|
+
// The window of the dots, the track that slides in it, and the marker of the selected dot
|
|
3144
|
+
list = box && create('div', windowed ? cls.dots : cls.dots + ' ' + cls.dotsAll),
|
|
3145
|
+
track = list && create('div', cls.dotsTrack);
|
|
3146
|
+
|
|
3147
|
+
let marks = new Map();
|
|
3148
|
+
|
|
3149
|
+
if (!arrows && !box) {
|
|
3150
|
+
return;
|
|
3151
|
+
}
|
|
3152
|
+
|
|
3153
|
+
// Add the window of the dots now: its CSS height is fixed, so the dots that the first measure adds do not shift the page
|
|
3154
|
+
if (list) {
|
|
3155
|
+
track.append(create('span', cls.dotsMarker));
|
|
3156
|
+
list.append(track);
|
|
3157
|
+
box.append(list);
|
|
3158
|
+
}
|
|
3159
|
+
|
|
3160
|
+
if (arrows) {
|
|
3161
|
+
[['prev', 'goToPrev'], ['next', 'goToNext']].forEach(([key, method]) => {
|
|
3162
|
+
const button = create('button', cls.button + ' ' + cls[key + 'Button']);
|
|
3163
|
+
button.type = 'button';
|
|
3164
|
+
button.setAttribute('aria-label', labels[key]);
|
|
3165
|
+
button.setAttribute('aria-controls', context.container.id);
|
|
3166
|
+
button.addEventListener('click', () => api[method]());
|
|
3167
|
+
buttons.push(button);
|
|
3168
|
+
});
|
|
3169
|
+
}
|
|
3170
|
+
|
|
3171
|
+
function update() {
|
|
3172
|
+
const fit = api.isFit(),
|
|
3173
|
+
count = fit ? 0 : api.snapProgress().length,
|
|
3174
|
+
selected = api.selectedSnap();
|
|
3175
|
+
|
|
3176
|
+
buttons.forEach((button, index) => {
|
|
3177
|
+
if (fit) {
|
|
3178
|
+
button.remove();
|
|
3179
|
+
} else {
|
|
3180
|
+
if (!button.isConnected) {
|
|
3181
|
+
// Before the dots, for the tab order
|
|
3182
|
+
list?.parentNode === arrows ? list.before(button) : arrows.append(button);
|
|
3183
|
+
}
|
|
3184
|
+
const disabled = !(index ? api.canGoToNext() : api.canGoToPrev());
|
|
3185
|
+
if (button.disabled !== disabled) {
|
|
3186
|
+
button.disabled = disabled;
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
});
|
|
3190
|
+
|
|
3191
|
+
if (!list) {
|
|
3192
|
+
return;
|
|
3193
|
+
}
|
|
3194
|
+
|
|
3195
|
+
if (count < 2) {
|
|
3196
|
+
list.remove();
|
|
3197
|
+
return;
|
|
3198
|
+
}
|
|
3199
|
+
|
|
3200
|
+
while (dots.length > count) {
|
|
3201
|
+
dots.pop().remove();
|
|
3202
|
+
marks.delete(dots.length);
|
|
3203
|
+
}
|
|
3204
|
+
|
|
3205
|
+
while (dots.length < count) {
|
|
3206
|
+
const dot = create('button', cls.dot),
|
|
3207
|
+
index = dots.length;
|
|
3208
|
+
dot.type = 'button';
|
|
3209
|
+
dot.setAttribute('aria-label', labels.dot.replace('%d', index + 1));
|
|
3210
|
+
dot.setAttribute('aria-controls', context.container.id);
|
|
3211
|
+
// The window of the dots hides the dots out of the zoom: keep them out of the tab order
|
|
3212
|
+
if (windowed) {
|
|
3213
|
+
dot.tabIndex = -1;
|
|
3214
|
+
}
|
|
3215
|
+
dot.addEventListener('click', () => api.goToSnap(index));
|
|
3216
|
+
dots.push(dot);
|
|
3217
|
+
track.append(dot);
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
if (!list.isConnected) {
|
|
3221
|
+
box.append(list);
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
// The zoom effect: the 5 dots around the selected dot get state classes
|
|
3225
|
+
const next = new Map();
|
|
3226
|
+
|
|
3227
|
+
['first', 'prev', 'current', 'next', 'last'].forEach((key, offset) => {
|
|
3228
|
+
if (dots[selected + offset - 2]) {
|
|
3229
|
+
next.set(selected + offset - 2, cls[key]);
|
|
3230
|
+
}
|
|
3231
|
+
});
|
|
3232
|
+
|
|
3233
|
+
marks.forEach((name, index) => {
|
|
3234
|
+
if (next.get(index) !== name) {
|
|
3235
|
+
setClass(dots[index], name, false);
|
|
3236
|
+
setAttribute(dots[index], 'aria-current', false);
|
|
3237
|
+
dots[index].tabIndex = windowed ? -1 : 0;
|
|
3238
|
+
}
|
|
3239
|
+
});
|
|
3240
|
+
|
|
3241
|
+
next.forEach((name, index) => {
|
|
3242
|
+
setClass(dots[index], name, true);
|
|
3243
|
+
setAttribute(dots[index], 'aria-current', index === selected && 'true');
|
|
3244
|
+
dots[index].tabIndex = 0;
|
|
3245
|
+
});
|
|
3246
|
+
|
|
3247
|
+
marks = next;
|
|
3248
|
+
|
|
3249
|
+
// Center the group of up to 5 dots in the window of the dots, and move the marker to the selected dot. The CSS moves the track and the marker with "translate".
|
|
3250
|
+
if (windowed) {
|
|
3251
|
+
variable('--better-shift', 2 - (Math.max(0, selected - 2) + Math.min(count - 1, selected + 2)) / 2);
|
|
3252
|
+
}
|
|
3253
|
+
variable('--better-dot', selected);
|
|
3254
|
+
}
|
|
3255
|
+
|
|
3256
|
+
// Set a CSS variable on the window of the dots, if its value changes
|
|
3257
|
+
function variable(name, value) {
|
|
3258
|
+
if (list.style.getPropertyValue(name) !== String(value)) {
|
|
3259
|
+
list.style.setProperty(name, value);
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
|
|
3263
|
+
['init', 'reinit', 'resize', 'select', 'slideschanged'].forEach((name) => context.on(name, update));
|
|
3264
|
+
|
|
3265
|
+
return {
|
|
3266
|
+
remove() {
|
|
3267
|
+
buttons.forEach((button) => button.remove());
|
|
3268
|
+
list?.remove();
|
|
3269
|
+
}
|
|
3270
|
+
};
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
/**
|
|
3274
|
+
* Connect a carousel to another carousel with the same slides, such as thumbnails to a main gallery. This carousel follows the slide
|
|
3275
|
+
* that the other carousel selects. Only a click on a slide of this carousel selects that slide in the other carousel: a drag does not.
|
|
3276
|
+
*
|
|
3277
|
+
* @param {object} context What the features share. See carousel.js. The "sync" option is the other carousel, its container or a selector.
|
|
3278
|
+
* @returns {{remove: () => void}|undefined} remove() disconnects the carousels.
|
|
3279
|
+
*/
|
|
3280
|
+
function sync(context) {
|
|
3281
|
+
const { api, container } = context,
|
|
3282
|
+
target = context.options.sync;
|
|
3283
|
+
|
|
3284
|
+
let other = null;
|
|
3285
|
+
|
|
3286
|
+
if (!target) {
|
|
3287
|
+
return;
|
|
3288
|
+
}
|
|
3289
|
+
|
|
3290
|
+
// Show the slide that the other carousel selects
|
|
3291
|
+
function show(instant) {
|
|
3292
|
+
const index = other.selectedSlide();
|
|
3293
|
+
context.follow(index);
|
|
3294
|
+
api.goToSlide(index, instant === true);
|
|
3295
|
+
}
|
|
3296
|
+
|
|
3297
|
+
function click(event) {
|
|
3298
|
+
// The slide or the clone that gets the click, and then its original slide
|
|
3299
|
+
const index = context.nodes().findIndex((node) => node.contains(event.target));
|
|
3300
|
+
if (other && index > -1) {
|
|
3301
|
+
other.goToSlide(index % api.slideNodes().length);
|
|
3302
|
+
}
|
|
3303
|
+
}
|
|
3304
|
+
|
|
3305
|
+
// The other carousel can come later than this one, so find it at the init
|
|
3306
|
+
function connect() {
|
|
3307
|
+
if (other) {
|
|
3308
|
+
return;
|
|
3309
|
+
}
|
|
3310
|
+
other = target.goToSnap ? target : find(typeof target === 'string' ? optionElement(target, context.root) : target);
|
|
3311
|
+
if (other) {
|
|
3312
|
+
other.on('select', show);
|
|
3313
|
+
show(true);
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
|
|
3317
|
+
context.on('init', connect);
|
|
3318
|
+
context.on('reinit', connect);
|
|
3319
|
+
container.addEventListener('click', click);
|
|
3320
|
+
|
|
3321
|
+
return {
|
|
3322
|
+
remove() {
|
|
3323
|
+
other?.off('select', show);
|
|
3324
|
+
container.removeEventListener('click', click);
|
|
3325
|
+
}
|
|
3326
|
+
};
|
|
3129
3327
|
}
|
|
3130
3328
|
|
|
3131
3329
|
/*
|