-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarousel.js
More file actions
326 lines (269 loc) · 7.38 KB
/
Copy pathcarousel.js
File metadata and controls
326 lines (269 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
let observer;
export default class Carousel extends HTMLElement {
static get observedAttributes() {
return ["index"];
}
constructor() {
super();
this.scrollBehavior = "smooth";
this.history = "push";
}
connectedCallback() {
//To calculate the offset of slides relative to the document
if (getStyleValue(this, "position") === "static") {
this.style.position = "relative";
}
this.addEventListener("keydown", (e) => {
switch (e.keyCode) {
case 37: //left
this.index -= 1;
e.preventDefault();
break;
case 39: //right
this.index += 1;
e.preventDefault();
break;
}
});
checkScrollSupport(this);
checkA11y(this);
//Handle navigation if children have ids
if (this.querySelector(":scope > [id]")) {
window.addEventListener(
"popstate",
(event) => handleTarget(this, document.location.hash),
);
if (document.location.hash) {
handleTarget(this, document.location.hash);
} else if (this.children[0]) {
handleHistory(this.children[0], this.history);
}
let scrolling;
function handleScroll() {
clearTimeout(scrolling);
scrolling = setTimeout(() => {
const target = this.target;
handleHistory(target, this.history);
this.lastTarget = target;
}, 50);
}
this.addEventListener("scroll", handleScroll, false);
}
//Resize observer
if (window.ResizeObserver) {
if (!observer) {
observer = new ResizeObserver((entries) => {
for (let entry of entries) {
const element = entry.target;
if (element.lastTarget) {
const restored = element.scrollBehavior;
element.scrollBehavior = "auto";
element.target = element.lastTarget;
element.scrollBehavior = restored;
}
}
});
}
observer.observe(this);
}
}
disconnectedCallback() {
if (observer) {
observer.unobserver(this);
}
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "index") {
this.index = parseInt(newValue);
return;
}
}
next(amount = 1) {
this.scrollFromLeft += this.clientWidth * amount;
}
prev(amount = 1) {
this.scrollFromLeft -= this.clientWidth * amount;
}
get index() {
const total = this.children.length - 1;
const slides = this.children;
for (let index = 0; index < total; index++) {
const slide = slides[index];
const scroll = Math.round(slide.offsetLeft - this.clientWidth / 2);
if (
this.scrollLeft >= scroll &&
this.scrollLeft <= scroll + slide.clientWidth
) {
return index;
}
}
return total;
}
set index(index) {
if (typeof index !== "number" || Math.round(index) !== index) {
throw new Error("Invalid index value. It must be an integer");
}
const slides = this.children;
index = Math.min(Math.max(index, 0), slides.length - 1);
this.target = slides[index];
}
get target() {
return this.children[this.index];
}
set target(target) {
if (target.parentElement !== this) {
throw new Error("The target must be a direct child of this element");
}
const scroll = Math.round(
target.offsetLeft - this.clientWidth / 2 + target.clientWidth / 2,
);
this.scrollFromLeft = Math.max(0, scroll);
this.lastTarget = target;
}
get scrollFromLeft() {
return this.scrollLeft;
}
set scrollFromLeft(scroll) {
try {
this.scroll({
left: scroll,
behavior: this.scrollBehavior,
});
} catch (err) {
this.scrollLeft = scroll;
}
}
get scrollFromRight() {
return this.scrollWidth - this.clientWidth - this.scrollLeft;
}
set scrollFromRight(scroll) {
this.scrollFromLeft = this.scrollWidth - this.clientWidth - scroll;
}
}
export class Navigation extends HTMLElement {
static get observedAttributes() {
return ["data-target"];
}
get target() {
const { target } = this.dataset;
if (target) {
return this.ownerDocument.getElementById(target);
}
console.info(
"@oom/carusel [navigation]:",
'Missing data-target="id" attribute in the carousel navigation element',
);
}
connectedCallback() {
const carousel = this.target;
if (!carousel) {
return;
}
const buttons = this.querySelectorAll("button");
buttons.forEach((button) => {
button.addEventListener("click", (ev) => {
const value = button.value;
if (!value) {
return;
}
if (value === "next") {
carousel.next();
} else if (value === "prev") {
carousel.prev();
} else if (value.startsWith("+")) {
carousel.index += parseInt(value.slice(1));
} else if (value.startsWith("-")) {
carousel.index -= parseInt(value.slice(1));
} else if (value.match(/^\d+$/)) {
carousel.index = parseInt(value);
} else {
const target = carousel.ownerDocument.getElementById(value);
if (slide) {
carousel.target = target;
}
}
});
});
let isScrolling;
carousel.addEventListener(
"scroll",
() => {
clearTimeout(isScrolling);
isScrolling = setTimeout(showHideButtons, 10);
},
false,
);
carousel.addEventListener("mouseenter", showHideButtons);
function showHideButtons() {
buttons.forEach((button) => {
const value = button.value;
if (!value) {
return;
}
if (value === "next" || value.startsWith("+")) {
button.disabled = carousel.scrollFromRight < 5;
} else if (value === "prev" || value.startsWith("-")) {
button.disabled = carousel.scrollFromLeft < 5;
}
});
}
showHideButtons();
}
}
function handleTarget(el, hash) {
if (!hash) {
return;
}
const target = el.querySelector(`:scope > ${hash}`);
if (target) {
el.target = target;
}
}
function handleHistory(el, mode) {
if (!el.id || document.location.hash === `#${el.id}`) {
return;
}
switch (mode) {
case "push":
history.pushState({}, null, `#${el.id}`);
break;
case "replace":
history.replaceState({}, null, `#${el.id}`);
break;
}
}
function getStyleValue(el, name) {
const value = getComputedStyle(el)[name];
if (value && value.replace(/none/g, "").trim()) {
return value;
}
}
function checkScrollSupport(element) {
const support = "scroll" in element && "scrollBehavior" in element.style;
if (!support) {
console.info(
"@oom/carusel [compatibility]:",
'Missing smooth scrolling support. Consider using a polyfill like "smoothscroll-polyfill"',
);
}
}
function checkA11y(element) {
if (element.getAttribute("role") !== "region") {
console.info(
"@oom/carusel [accesibility]:",
'Missing role="region" attribute in the carousel element',
);
}
if (!element.hasAttribute("aria-label")) {
console.info(
"@oom/carusel [accesibility]:",
"Missing aria-label attribute in the carousel element",
);
}
if (!element.hasAttribute("tabindex")) {
console.info(
"@oom/carusel [accesibility]:",
'Missing tabindex="0" attribute in the carousel element',
);
}
}