-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-slider.mjs
66 lines (55 loc) · 1.73 KB
/
image-slider.mjs
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
(async function (window) {
if (typeof window === 'undefined') { return null; }
const customElementName = 'image-slider';
const { render, updateSlideBy, slotChangedHandler } = await import('./partials/helpers.mjs');
class ImageSlider extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({ mode: 'open'});
this.state = {
count: 0,
isLocked: false,
selectedIndex: 0,
selectedImage: null
};
this.listenersAttached = false;
this.width = 0;
this.position = 0;
this.mounted = false;
}
get isLocked() { return this.state.isLocked; }
get selectedImage() { return this.state.selectedImage; }
get selectedIndex() { return this.state.selectedIndex; }
get count() { return this.state.count; }
connectedCallback(){
if (!this.mounted) {
render(this);
this.width = this.getBoundingClientRect().width;
this.root.querySelector('#main').addEventListener('slotchange', slotChangedHandler.bind(this));
this.mounted = true;
}
}
next(){
if (this.selectedIndex < this.count - 1) {
updateSlideBy(1, this);
}
}
prev(){
if (this.selectedIndex <= 0) {
updateSlideBy(-1, this);
}
}
slideTo(index) {
if(!index || typeof index !== 'number') { return; }
const idx = Math.round(index);
if(idx > 0 && idx <= this.count) {
updateSlideBy(idx - 1, this);
}
}
first() { this.slideTo(1); }
last() { this.slideTo(this.count); }
};
if (!!window.customElements && !window.customElements.get(customElementName)) {
window.customElements.define(customElementName, ImageSlider);
}
})(window);