@twistedandy/better-carousel 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +839 -2
- package/dist/better-carousel.css +1 -0
- package/dist/better-carousel.js +3373 -0
- package/dist/better-carousel.min.js +1 -0
- package/package.json +55 -5
- package/src/index.js +49 -0
- package/src/scripts/autoplay.js +322 -0
- package/src/scripts/carousel.js +870 -0
- package/src/scripts/geometry.js +432 -0
- package/src/scripts/input.js +484 -0
- package/src/scripts/layout.js +258 -0
- package/src/scripts/loop.js +83 -0
- package/src/scripts/markup.js +288 -0
- package/src/scripts/motion.js +232 -0
- package/src/scripts/navigation.js +209 -0
- package/src/scripts/options.js +153 -0
- package/src/styles/better-carousel.scss +323 -0
- package/src/types/better-carousel.d.ts +309 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Layout: the layout reads, and the observers that all carousels share.
|
|
3
|
+
*
|
|
4
|
+
* The reads run in the ResizeObserver callback, when the layout is clean.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Read the axis, the alignment, the direction and the gap from the computed style of the container.
|
|
9
|
+
*
|
|
10
|
+
* @param {HTMLElement} container
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @returns {{axis: string, align: string|Function, rtl: boolean, sign: number, gap: number}} sign is -1 for a right-to-left carousel, else 1.
|
|
13
|
+
*/
|
|
14
|
+
export function readLayout(container, options) {
|
|
15
|
+
const style = getComputedStyle(container),
|
|
16
|
+
display = style.display,
|
|
17
|
+
content = style.justifyContent;
|
|
18
|
+
|
|
19
|
+
let axis = options.axis;
|
|
20
|
+
|
|
21
|
+
if (axis === 'auto') {
|
|
22
|
+
axis = 'x';
|
|
23
|
+
if (display.includes('grid')) {
|
|
24
|
+
axis = style.gridAutoFlow.startsWith('column') ? 'x' : 'y';
|
|
25
|
+
} else if (display.includes('flex') && style.flexDirection.startsWith('column') && style.flexWrap === 'nowrap') {
|
|
26
|
+
axis = 'y';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let align = options.align;
|
|
31
|
+
|
|
32
|
+
if (align === 'auto') {
|
|
33
|
+
align = content === 'center' ? 'center' : content === 'flex-end' || content === 'end' || (axis === 'x' && content === 'right') ? 'end' : 'start';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const rtl = axis === 'x' && (options.dir === 'auto' ? style.direction === 'rtl' : options.dir === 'rtl');
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
axis,
|
|
40
|
+
align,
|
|
41
|
+
rtl,
|
|
42
|
+
sign: rtl ? -1 : 1,
|
|
43
|
+
gap: parseFloat(axis === 'x' ? style.columnGap : style.rowGap) || 0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 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.
|
|
49
|
+
* 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
|
|
50
|
+
* values do not change with a scale or a rotation of a parent.
|
|
51
|
+
* 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.
|
|
52
|
+
*
|
|
53
|
+
* @param {HTMLElement} viewport
|
|
54
|
+
* @param {HTMLElement[]} nodes All slides, clones included.
|
|
55
|
+
* @param {object} layout The result of readLayout().
|
|
56
|
+
* @param {{axis: string, container: number, slides: number[]}} written The translate values that the library wrote, in px on the screen.
|
|
57
|
+
* @returns {{view: number, slides: Array<{start: number, end: number, cross: number, hidden: boolean}>, gap: number, scale: number, pad: {start: number, end: number}}}
|
|
58
|
+
* 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.
|
|
59
|
+
* scale is the screen px of one px of the carousel. pad is the viewport padding at the start and at the end.
|
|
60
|
+
*/
|
|
61
|
+
export function readRects(viewport, nodes, layout, written) {
|
|
62
|
+
const box = viewport.getBoundingClientRect(),
|
|
63
|
+
x = layout.axis === 'x',
|
|
64
|
+
sign = layout.sign,
|
|
65
|
+
known = written.axis === layout.axis,
|
|
66
|
+
shift = known ? written.container : 0,
|
|
67
|
+
width = viewport.offsetWidth,
|
|
68
|
+
height = viewport.offsetHeight,
|
|
69
|
+
// offsetWidth is a whole number: a difference of less than 1 px is not a transform
|
|
70
|
+
transformed = width && height && (Math.abs(box.width - width) >= 1 || Math.abs(box.height - height) >= 1),
|
|
71
|
+
// 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.
|
|
72
|
+
scale = !transformed ? 1 : Math.abs(width - height) > 1 ? (width * box.width - height * box.height) / (width * width - height * height) : box.width / width,
|
|
73
|
+
view = !transformed ? (x ? box.width : box.height) : x ? width : height,
|
|
74
|
+
style = getComputedStyle(viewport),
|
|
75
|
+
[before, after] = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'],
|
|
76
|
+
pad = { start: parseFloat(style['padding' + before]) || 0, end: parseFloat(style['padding' + after]) || 0 };
|
|
77
|
+
|
|
78
|
+
const slides = nodes.map((node, index) => {
|
|
79
|
+
const rect = node.getBoundingClientRect(),
|
|
80
|
+
offset = sign * (shift + (known && written.slides[index] || 0)),
|
|
81
|
+
start = (x ? (layout.rtl ? box.right - rect.right : rect.left - box.left) : rect.top - box.top) / scale - pad.start - offset,
|
|
82
|
+
size = (x ? rect.width : rect.height) / scale;
|
|
83
|
+
|
|
84
|
+
return { start, end: start + size, cross: (x ? rect.height : rect.width) / scale, hidden: !rect.width && !rect.height };
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
let edge = slides.find((slide) => !slide.hidden)?.start || 0;
|
|
88
|
+
|
|
89
|
+
for (const slide of slides) {
|
|
90
|
+
if (slide.hidden) {
|
|
91
|
+
slide.start = slide.end = edge;
|
|
92
|
+
} else {
|
|
93
|
+
edge = slide.end;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let first = Math.max(0, slides.findIndex((slide) => !slide.hidden)),
|
|
98
|
+
last = first;
|
|
99
|
+
|
|
100
|
+
slides.forEach((slide, index) => {
|
|
101
|
+
if (slide.hidden) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (slide.start < slides[first].start) {
|
|
105
|
+
first = index;
|
|
106
|
+
}
|
|
107
|
+
if (slide.end > slides[last].end) {
|
|
108
|
+
last = index;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
let gap = layout.gap;
|
|
113
|
+
|
|
114
|
+
if (slides.length) {
|
|
115
|
+
const side = x ? (layout.rtl ? ['Right', 'Left'] : ['Left', 'Right']) : ['Top', 'Bottom'];
|
|
116
|
+
gap += (parseFloat(getComputedStyle(nodes[first])['margin' + side[0]]) || 0) + (parseFloat(getComputedStyle(nodes[last])['margin' + side[1]]) || 0);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
view: view - pad.start - pad.end,
|
|
121
|
+
slides,
|
|
122
|
+
gap,
|
|
123
|
+
scale,
|
|
124
|
+
pad
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get the window size on the main axis.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} axis 'x' or 'y'.
|
|
132
|
+
* @returns {number}
|
|
133
|
+
*/
|
|
134
|
+
export function windowSize(axis) {
|
|
135
|
+
return axis === 'x' ? document.documentElement.clientWidth : innerHeight;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get the part of the main axis where the loop must show slides, relative to the inner edge of the viewport padding.
|
|
140
|
+
* 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.
|
|
141
|
+
*
|
|
142
|
+
* @param {HTMLElement} viewport
|
|
143
|
+
* @param {{axis: string, rtl: boolean}} layout
|
|
144
|
+
* @param {number} view The viewport size without the padding.
|
|
145
|
+
* @param {{start: number, end: number}} pad The viewport padding.
|
|
146
|
+
* @param {number} [size] The window size on the main axis, for clones: 'fill'. Without it, the area is the viewport.
|
|
147
|
+
* @returns {{start: number, size: number}}
|
|
148
|
+
*/
|
|
149
|
+
export function visibleArea(viewport, layout, view, pad, size) {
|
|
150
|
+
if (!size) {
|
|
151
|
+
return { start: -pad.start, size: pad.start + view + pad.end };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const box = viewport.getBoundingClientRect(),
|
|
155
|
+
before = Math.max(0, Math.round(layout.axis === 'x' ? (layout.rtl ? size - box.right : box.left) : box.top)) + pad.start;
|
|
156
|
+
|
|
157
|
+
return { start: -before, size: before + Math.max(view + pad.end, Math.round(size - before)) };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 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
|
|
161
|
+
// come after the writes of another carousel, and the browser does one layout for all of them.
|
|
162
|
+
const owners = new WeakMap(),
|
|
163
|
+
// The elements of each owner, and the last size of each element
|
|
164
|
+
watched = new WeakMap(),
|
|
165
|
+
sizes = new WeakMap();
|
|
166
|
+
|
|
167
|
+
let resizer;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 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.
|
|
171
|
+
* 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:
|
|
172
|
+
* a new watch of an element after a write sends such an entry. observeSize() clears the last size, so its first entry always counts.
|
|
173
|
+
*
|
|
174
|
+
* @param {Element[]} nodes
|
|
175
|
+
* @param {{read: () => boolean, write: () => void, skip: (entry: ResizeObserverEntry) => boolean}} [owner] Without an owner, stop the watch.
|
|
176
|
+
*/
|
|
177
|
+
export function observeSize(nodes, owner) {
|
|
178
|
+
resizer ||= new ResizeObserver((entries) => {
|
|
179
|
+
const list = new Set(entries.filter((entry) => {
|
|
180
|
+
const { inlineSize, blockSize } = entry.borderBoxSize[0],
|
|
181
|
+
size = inlineSize + ',' + blockSize,
|
|
182
|
+
same = sizes.get(entry.target) === size;
|
|
183
|
+
sizes.set(entry.target, size);
|
|
184
|
+
return !same && !owners.get(entry.target)?.skip(entry);
|
|
185
|
+
}).map((entry) => owners.get(entry.target)));
|
|
186
|
+
const changed = [...list].filter((item) => item && item.read());
|
|
187
|
+
changed.forEach((item) => item.write());
|
|
188
|
+
// 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
|
|
189
|
+
// a change to report after its callback, the browser sends the "ResizeObserver loop" error to window.onerror. Stop the watch of these
|
|
190
|
+
// elements, and watch them again in the next frame. Then the first callback measures again and finds no change.
|
|
191
|
+
if (changed.length) {
|
|
192
|
+
changed.forEach((item) => watched.get(item)?.forEach((node) => resizer.unobserve(node)));
|
|
193
|
+
requestAnimationFrame(() => changed.forEach((item) => watched.get(item)?.forEach((node) => owners.get(node) === item && resizer.observe(node))));
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
if (owner) {
|
|
198
|
+
watched.set(owner, nodes);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (const node of nodes) {
|
|
202
|
+
sizes.delete(node);
|
|
203
|
+
if (owner) {
|
|
204
|
+
owners.set(node, owner);
|
|
205
|
+
resizer.observe(node);
|
|
206
|
+
} else {
|
|
207
|
+
owners.delete(node);
|
|
208
|
+
resizer.unobserve(node);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// The lazy option: one IntersectionObserver for each margin, the callback of each element that waits, and the boxes that readBoxes() read
|
|
214
|
+
const watchers = new Map(),
|
|
215
|
+
waiters = new WeakMap(),
|
|
216
|
+
boxes = new WeakMap();
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Read the boxes of elements before a carousel writes to the DOM. Then the reads of all carousels cause one layout, not one for each carousel.
|
|
220
|
+
* waitNear() uses each box one time.
|
|
221
|
+
*
|
|
222
|
+
* @param {Element[]} elements
|
|
223
|
+
*/
|
|
224
|
+
export function readBoxes(elements) {
|
|
225
|
+
elements.forEach((element) => boxes.set(element, element.getBoundingClientRect()));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Wait until an element comes near the screen.
|
|
230
|
+
*
|
|
231
|
+
* @param {Element} node
|
|
232
|
+
* @param {true|number} lazy The "lazy" option: true for one screen height, or a distance in px.
|
|
233
|
+
* @param {Function} callback
|
|
234
|
+
* @returns {Function|null} A function that stops the wait, or null if the element is near the screen now.
|
|
235
|
+
*/
|
|
236
|
+
export function waitNear(node, lazy, callback) {
|
|
237
|
+
const near = lazy === true ? innerHeight : lazy,
|
|
238
|
+
rect = boxes.get(node) || node.getBoundingClientRect(),
|
|
239
|
+
margin = lazy === true ? '100%' : lazy + 'px';
|
|
240
|
+
|
|
241
|
+
boxes.delete(node);
|
|
242
|
+
|
|
243
|
+
if (rect.height && rect.bottom >= -near && rect.top <= innerHeight + near) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
let observer = watchers.get(margin);
|
|
248
|
+
if (!observer) {
|
|
249
|
+
observer = new IntersectionObserver((entries) => entries.forEach((entry) => entry.isIntersecting && waiters.get(entry.target)?.()), { rootMargin: margin });
|
|
250
|
+
watchers.set(margin, observer);
|
|
251
|
+
}
|
|
252
|
+
waiters.set(node, callback);
|
|
253
|
+
observer.observe(node);
|
|
254
|
+
return () => {
|
|
255
|
+
observer.unobserve(node);
|
|
256
|
+
waiters.delete(node);
|
|
257
|
+
};
|
|
258
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Loop math: the period, the clones and the place of each slide. This module does not use the DOM.
|
|
3
|
+
*
|
|
4
|
+
* The values and the slides are the same as in geometry.js. In loop mode, each slide is at start + k * period.
|
|
5
|
+
* The library writes k * period as the translate of the slide.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get the number of full copies of the slides that the loop needs. With 0, the slides can loop with no clones:
|
|
10
|
+
* no slide is necessary at two places at the same time.
|
|
11
|
+
*
|
|
12
|
+
* @param {number} length The loop period of the original slides.
|
|
13
|
+
* @param {number} view
|
|
14
|
+
* @param {number} max
|
|
15
|
+
* @returns {number}
|
|
16
|
+
*/
|
|
17
|
+
export function cloneCount(length, view, max) {
|
|
18
|
+
return length > 0 ? Math.max(0, Math.ceil((view + max) / length) - 1) : 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get the k of each slide for a motion from one location to another. With from = to, it is the placement at rest.
|
|
23
|
+
* The slide center goes into a window of one period. The window has the same center as the part of the content that shows during the motion.
|
|
24
|
+
*
|
|
25
|
+
* @param {Array<{start: number, end: number}>} slides
|
|
26
|
+
* @param {number} from
|
|
27
|
+
* @param {number} to
|
|
28
|
+
* @param {number} view The size of the visible area.
|
|
29
|
+
* @param {number} length The loop period.
|
|
30
|
+
* @param {number} [offset] The start of the visible area, relative to the viewport start.
|
|
31
|
+
* @returns {number[]}
|
|
32
|
+
*/
|
|
33
|
+
export function placement(slides, from, to, view, length, offset = 0) {
|
|
34
|
+
const center = offset + (view - from - to) / 2;
|
|
35
|
+
return slides.map((slide) => Math.round((center - (slide.start + slide.end) / 2) / length) || 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Find if one placement is correct for the full motion. If not, the motion must change the placement during the motion.
|
|
40
|
+
*
|
|
41
|
+
* @param {Array<{start: number, end: number}>} slides
|
|
42
|
+
* @param {number} from
|
|
43
|
+
* @param {number} to
|
|
44
|
+
* @param {number} view
|
|
45
|
+
* @param {number} length
|
|
46
|
+
* @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
export function pathFits(slides, from, to, view, length) {
|
|
49
|
+
const path = view + Math.abs(to - from);
|
|
50
|
+
return slides.every((slide) => length >= path + slide.end - slide.start);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the steps of each slide for an auto scroll iteration. The container moves by one period, from `from` to `from - length` (forward)
|
|
55
|
+
* or to `from + length` (back). Each slide moves by one period at one time in the iteration.
|
|
56
|
+
*
|
|
57
|
+
* @param {Array<{start: number, end: number}>} slides
|
|
58
|
+
* @param {number} from
|
|
59
|
+
* @param {number} view
|
|
60
|
+
* @param {number} length
|
|
61
|
+
* @param {number} direction 1 for forward, -1 for back.
|
|
62
|
+
* @param {number} [offset] The start of the visible area, relative to the viewport start.
|
|
63
|
+
* @returns {Array<{k: number, offset: number}>} k at the start of the iteration, and the time of the step, from 0 to 1.
|
|
64
|
+
*/
|
|
65
|
+
export function steps(slides, from, view, length, direction, offset = 0) {
|
|
66
|
+
return slides.map((slide) => {
|
|
67
|
+
const value = (offset + view / 2 - from - (slide.start + slide.end) / 2) / length,
|
|
68
|
+
k = Math.round(value);
|
|
69
|
+
|
|
70
|
+
return { k, offset: direction > 0 ? k + 0.5 - value : value - k + 0.5 };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Put a location into the range (-length, 0].
|
|
76
|
+
*
|
|
77
|
+
* @param {number} location
|
|
78
|
+
* @param {number} length
|
|
79
|
+
* @returns {number}
|
|
80
|
+
*/
|
|
81
|
+
export function normalize(location, length) {
|
|
82
|
+
return location - Math.ceil(location / length) * length;
|
|
83
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Markup: the wrapper, the slides, the clones, the ARIA attributes, and the helpers that write to the DOM.
|
|
3
|
+
*
|
|
4
|
+
* The helpers that set a value write only when the value changes. restore() removes all that the library added.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Add or remove a class.
|
|
9
|
+
*
|
|
10
|
+
* @param {Element} element
|
|
11
|
+
* @param {string} name An empty name does nothing.
|
|
12
|
+
* @param {boolean} on
|
|
13
|
+
*/
|
|
14
|
+
export function setClass(element, name, on) {
|
|
15
|
+
if (name && element.classList.contains(name) !== !!on) {
|
|
16
|
+
element.classList.toggle(name, !!on);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Set an attribute, or remove it if the value is false.
|
|
22
|
+
*
|
|
23
|
+
* @param {Element} element
|
|
24
|
+
* @param {string} name
|
|
25
|
+
* @param {string|false} value
|
|
26
|
+
*/
|
|
27
|
+
export function setAttribute(element, name, value) {
|
|
28
|
+
if (element.getAttribute(name) !== (value || null)) {
|
|
29
|
+
value ? element.setAttribute(name, value) : element.removeAttribute(name);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Set an inline style property.
|
|
35
|
+
*
|
|
36
|
+
* @param {HTMLElement} element
|
|
37
|
+
* @param {string} name
|
|
38
|
+
* @param {string} value An empty string removes the property.
|
|
39
|
+
*/
|
|
40
|
+
export function setStyle(element, name, value) {
|
|
41
|
+
if (element.style[name] !== value) {
|
|
42
|
+
element.style[name] = value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Remove an empty style attribute that the library left.
|
|
48
|
+
*
|
|
49
|
+
* @param {Element} element
|
|
50
|
+
*/
|
|
51
|
+
export function cleanStyle(element) {
|
|
52
|
+
if (element.getAttribute('style') === '') {
|
|
53
|
+
element.removeAttribute('style');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Make an element.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} tag
|
|
61
|
+
* @param {string} className
|
|
62
|
+
* @returns {HTMLElement}
|
|
63
|
+
*/
|
|
64
|
+
export function create(tag, className) {
|
|
65
|
+
const element = document.createElement(tag);
|
|
66
|
+
if (className) {
|
|
67
|
+
element.className = className;
|
|
68
|
+
}
|
|
69
|
+
return element;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Find the element of an option, such as "arrows", "dots" or "sync". true gives the root, and false gives null.
|
|
74
|
+
* The library searches for a selector in the root and then in each parent of the root, so that it finds a heading in the section of the carousel.
|
|
75
|
+
*
|
|
76
|
+
* @param {boolean|string|Element} value The option value.
|
|
77
|
+
* @param {HTMLElement} root
|
|
78
|
+
* @returns {Element|null}
|
|
79
|
+
*/
|
|
80
|
+
export function optionElement(value, root) {
|
|
81
|
+
if (typeof value === 'string') {
|
|
82
|
+
for (let node = root; node; node = node.parentElement) {
|
|
83
|
+
const found = node.querySelector(value);
|
|
84
|
+
if (found) {
|
|
85
|
+
return found;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return value === true ? root : value || null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The attributes and the classes that the library added: element => names
|
|
94
|
+
const marked = new WeakMap(),
|
|
95
|
+
added = new WeakMap();
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Set an attribute that the page did not set. restore() removes it.
|
|
99
|
+
*
|
|
100
|
+
* @param {Element} node
|
|
101
|
+
* @param {string} name
|
|
102
|
+
* @param {string} value
|
|
103
|
+
*/
|
|
104
|
+
export function own(node, name, value) {
|
|
105
|
+
const list = marked.get(node) || new Set();
|
|
106
|
+
if (list.has(name) || !node.hasAttribute(name)) {
|
|
107
|
+
list.add(name);
|
|
108
|
+
marked.set(node, list);
|
|
109
|
+
if (node.getAttribute(name) !== value) {
|
|
110
|
+
node.setAttribute(name, value);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Add a class that the element does not have. restore() removes it.
|
|
117
|
+
*
|
|
118
|
+
* @param {Element} node
|
|
119
|
+
* @param {string} name An empty name does nothing.
|
|
120
|
+
*/
|
|
121
|
+
export function addClass(node, name) {
|
|
122
|
+
if (name && !node.classList.contains(name)) {
|
|
123
|
+
const list = added.get(node) || new Set();
|
|
124
|
+
node.classList.add(name);
|
|
125
|
+
list.add(name);
|
|
126
|
+
added.set(node, list);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// The elements that the library made: the wrapper and the clones
|
|
131
|
+
const made = new WeakSet();
|
|
132
|
+
|
|
133
|
+
// A counter for container ids
|
|
134
|
+
let uid = 0;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Find or make the root, the viewport and the container of a carousel. Then add their classes and the ARIA attributes of the
|
|
138
|
+
* carousel pattern of the WAI-ARIA Authoring Practices. A new setup after reinit() uses the wrapper that the library made before.
|
|
139
|
+
*
|
|
140
|
+
* @param {HTMLElement} element The container of the slides, or its viewport.
|
|
141
|
+
* @param {object} options
|
|
142
|
+
* @param {object} cls The class names.
|
|
143
|
+
* @returns {{root: HTMLElement, viewport: HTMLElement, container: HTMLElement}}
|
|
144
|
+
*/
|
|
145
|
+
export function build(element, options, cls) {
|
|
146
|
+
let container = options.container ? element.querySelector(options.container) : element;
|
|
147
|
+
|
|
148
|
+
if (container.classList.contains(cls.viewport)) {
|
|
149
|
+
container = container.firstElementChild;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let viewport = container.parentElement,
|
|
153
|
+
root = viewport.parentElement;
|
|
154
|
+
|
|
155
|
+
if (!made.has(viewport) && !viewport.classList.contains(cls.viewport) && options.wrapper) {
|
|
156
|
+
root = create('div');
|
|
157
|
+
viewport = create('div');
|
|
158
|
+
made.add(root).add(viewport);
|
|
159
|
+
container.before(root);
|
|
160
|
+
root.append(viewport);
|
|
161
|
+
viewport.append(container);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
addClass(root, cls.root);
|
|
165
|
+
addClass(viewport, cls.viewport);
|
|
166
|
+
addClass(container, cls.container);
|
|
167
|
+
|
|
168
|
+
own(root, 'role', 'region');
|
|
169
|
+
own(root, 'aria-roledescription', 'carousel');
|
|
170
|
+
if (!root.hasAttribute('aria-labelledby')) {
|
|
171
|
+
own(root, 'aria-label', options.labels.carousel);
|
|
172
|
+
}
|
|
173
|
+
own(container, 'id', container.id || 'carousel-' + ++uid);
|
|
174
|
+
own(container, 'aria-live', 'polite');
|
|
175
|
+
|
|
176
|
+
return { root, viewport, container };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Remove the classes and the attributes that own() and addClass() added, an empty class attribute, and the wrapper that build() made.
|
|
181
|
+
*
|
|
182
|
+
* @param {HTMLElement} root
|
|
183
|
+
* @param {HTMLElement} viewport
|
|
184
|
+
* @param {HTMLElement} container
|
|
185
|
+
* @param {HTMLElement[]} slides
|
|
186
|
+
* @param {boolean} keep Keep the wrapper for a new setup.
|
|
187
|
+
*/
|
|
188
|
+
export function restore(root, viewport, container, slides, keep) {
|
|
189
|
+
for (const node of [root, viewport, container, ...slides]) {
|
|
190
|
+
marked.get(node)?.forEach((name) => node.removeAttribute(name));
|
|
191
|
+
added.get(node)?.forEach((name) => node.classList.remove(name));
|
|
192
|
+
marked.delete(node);
|
|
193
|
+
added.delete(node);
|
|
194
|
+
if (node.getAttribute('class') === '') {
|
|
195
|
+
node.removeAttribute('class');
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (!keep && made.has(root)) {
|
|
200
|
+
root.before(container);
|
|
201
|
+
root.remove();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Slides that are a control keep their own role and name
|
|
206
|
+
const controls = 'a[href],button,input,select,textarea,[tabindex],[role]';
|
|
207
|
+
|
|
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
|
+
const generic = 'div,span,section';
|
|
210
|
+
|
|
211
|
+
// The elements that never show, so they are not slides
|
|
212
|
+
const skipped = new Set(['template', 'script', 'style']);
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Get the slides of a container. The clones that the library made, and the template, script and style elements, are not slides.
|
|
216
|
+
*
|
|
217
|
+
* @param {HTMLElement} container
|
|
218
|
+
* @param {string} [selector] The "slides" option.
|
|
219
|
+
* @returns {HTMLElement[]}
|
|
220
|
+
*/
|
|
221
|
+
export function readSlides(container, selector) {
|
|
222
|
+
return [...(selector ? container.querySelectorAll(selector) : container.children)].filter((node) => !made.has(node) && !skipped.has(node.localName));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Make the clones: full copies of the slides, which the screen reader and the keyboard do not see.
|
|
227
|
+
*
|
|
228
|
+
* @param {HTMLElement[]} slides
|
|
229
|
+
* @param {number} count The number of copies of all slides.
|
|
230
|
+
* @param {string} className The class of a clone.
|
|
231
|
+
* @returns {HTMLElement[]}
|
|
232
|
+
*/
|
|
233
|
+
export function copySlides(slides, count, className) {
|
|
234
|
+
const list = [];
|
|
235
|
+
|
|
236
|
+
for (let copy = 0; copy < count; copy++) {
|
|
237
|
+
for (const node of slides) {
|
|
238
|
+
const clone = node.cloneNode(true);
|
|
239
|
+
removeIds(clone);
|
|
240
|
+
clone.setAttribute('aria-hidden', 'true');
|
|
241
|
+
clone.inert = true;
|
|
242
|
+
clone.style.translate = '';
|
|
243
|
+
setClass(clone, className, true);
|
|
244
|
+
cleanStyle(clone);
|
|
245
|
+
made.add(clone);
|
|
246
|
+
list.push(clone);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return list;
|
|
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
|
+
export function copyContent(clone, node) {
|
|
267
|
+
const copy = node.cloneNode(true);
|
|
268
|
+
removeIds(copy);
|
|
269
|
+
clone.replaceChildren(...copy.childNodes);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
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.
|
|
274
|
+
*
|
|
275
|
+
* @param {HTMLElement[]} slides
|
|
276
|
+
* @param {string} text The "slide" label, with %d for the slide number and %n for the number of slides.
|
|
277
|
+
*/
|
|
278
|
+
export function labelSlides(slides, text) {
|
|
279
|
+
slides.forEach((node, index) => {
|
|
280
|
+
if (!node.matches(controls) || marked.get(node)?.has('role')) {
|
|
281
|
+
if (node.matches(generic)) {
|
|
282
|
+
own(node, 'role', 'group');
|
|
283
|
+
}
|
|
284
|
+
own(node, 'aria-roledescription', 'slide');
|
|
285
|
+
own(node, 'aria-label', text.replace('%d', index + 1).replace('%n', slides.length));
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|