-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathScrollGallery.js
89 lines (72 loc) · 2.45 KB
/
ScrollGallery.js
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
import ScrollCarousel from './ScrollCarouselGrab.js';
export default class ScrollGallery {
constructor(domNode) {
this.gallery = domNode;
this.slider = this.gallery.querySelector('[data-elem-slider]');
this.thumbs = this.gallery.querySelector('[data-elem-thumbs]');
this.thumbsList = this.gallery.querySelectorAll('[data-elem-thumb]');
this.stylesClass = {
current: '_current'
}
}
init() {
if (!this.slider || !this.thumbs) {
throw new Error('Not initialized. Carousel or Slider is missed!')
}
this.sliderCarousel = new ScrollCarousel(this.slider);
this.thumbsCarousel = new ScrollCarousel(this.thumbs);
this.sliderCarousel.init();
this.thumbsCarousel.init();
this.initEventListeners();
this.initThumbs();
}
initEventListeners() {
this.onSliderChange = this.onSliderChange.bind(this);
this.onThumbClick = this.onThumbClick.bind(this);
this.slider.addEventListener('scrollCarousel:pageChanged', this.onSliderChange);
this.thumbsList.forEach(item => item.addEventListener('click', this.onThumbClick));
}
onSliderChange(event) {
event.stopPropagation();
const currentPageIndex = event.detail;
const currentPageNode = this.thumbsList[currentPageIndex];
this.setActiveThumb(currentPageNode);
this.scrollActivePaginationIntoView(currentPageIndex, currentPageNode);
}
onThumbClick(event) {
const currentPageIndex = event.target.getAttribute('data-page');
if (!currentPageIndex) {
return;
}
this.sliderCarousel.scrollToPage(currentPageIndex);
}
initThumbs() {
this.setActiveThumb(this.thumbsList[0]);
}
setActiveThumb(current) {
if (this.activeThumb) {
this.activeThumb.classList.remove(this.stylesClass.current);
}
current.classList.add(this.stylesClass.current);
this.activeThumb = current;
}
scrollActivePaginationIntoView(currentPageIndex, currentPageNode) {
if (!this.thumbsCarousel.hasScroll) {
return;
}
if (currentPageNode.offsetTop > this.thumbsCarousel.carouselTrack.clientHeight) {
this.thumbsCarousel.scrollToPage(currentPageIndex);
} else if (currentPageNode.offsetTop < this.thumbsCarousel.carouselTrack.scrollTop) {
this.thumbsCarousel.scrollToPage(0);
}
}
destroyEventListeners() {
this.slider.removeEventListener('scrollCarousel:pageChanged', this.onSliderChange);
this.thumbsList.forEach(item => item.removeEventListener('click', this.onThumbClick));
}
destroy() {
this.sliderCarousel.destroy();
this.thumbsCarousel.destroy();
this.destroyEventListeners();
}
}