-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.js
167 lines (138 loc) · 4.91 KB
/
carousel.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
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
/**
* @classdesc Represents a FabrCarousel component.
* @extends FabrCoreComponent
*/
fbr.FabrCarousel = class extends fbr.FabrCoreComponent {
constructor() {
super();
this.componentName = "Carousel";
this.componentStyleClass = "fabr-carousel";
this.selector = "[fabr-carousel]";
this.eventMap = {};
this.iconHelper = new fbr.FabrHelperIcon();
this.iconHelper.init(this);
this.iconHelper.setIconsLibraryMaterial();
this.animateHelper = new fbr.FabrHelperAnimate();
this.animateHelper.init(this);
}
/**
* Render the carousel.
* @param {Object} carousel - The carousel to render.
*/
render(carousel) {
const delay = carousel.getAttribute("fabr-carousel-delay");
const wrapper = document.createElement("div");
const slides = carousel.querySelectorAll("[fabr-carousel-slide]");
carousel.style.height = `${carousel.offsetHeight}px`;
carousel.delay = delay ? parseInt(delay) : 5000;
wrapper.classList.add("fabr-carousel-wrapper");
carousel.parentNode.insertBefore(wrapper, carousel);
wrapper.appendChild(carousel);
slides.forEach((slide) => {
slide.classList.add("fabr-carousel-slide");
});
this.#renderControls(carousel);
this.#renderIndicators(carousel);
carousel.activeSlide = 0;
this.#rotateSlides(carousel);
}
/**
* Render the controls for the given carousel.
* @param {Object} carousel - The carousel to render the controls for.
* @private
*/
#renderControls(carousel) {
const controls = document.createElement("div");
const prev = document.createElement("div");
const next = document.createElement("div");
controls.classList.add("fabr-carousel-controls");
prev.classList.add("fabr-carousel-control-prev");
next.classList.add("fabr-carousel-control-next");
const prevIcon = this.iconHelper.new("chevron_left");
const nextIcon = this.iconHelper.new("chevron_right");
prev.appendChild(prevIcon);
next.appendChild(nextIcon);
controls.appendChild(prev);
controls.appendChild(next);
carousel.appendChild(controls);
this.addInternalEventListener(prev, "click", "setSlideEvent", carousel, -2);
this.addInternalEventListener(next, "click", "setSlideEvent", carousel, -1);
}
/**
* Render the indicators for the given carousel.
* @param {Object} carousel - The carousel to render the indicators for.
* @private
*/
#renderIndicators(carousel) {
const indicators = document.createElement("div");
const slides = carousel.querySelectorAll("[fabr-carousel-slide]");
indicators.classList.add("fabr-carousel-indicators");
slides.forEach((_, index) => {
const indicator = document.createElement("div");
indicator.classList.add("fabr-carousel-indicator");
indicator.setAttribute("fabr-carousel-indicator", "");
indicator.setAttribute("fabr-carousel-indicator-index", index);
indicators.appendChild(indicator);
this.addInternalEventListener(
indicator,
"click",
"setSlideEvent",
carousel,
index
);
});
carousel.appendChild(indicators);
}
/**
* Rotate the slides of the given carousel.
* @param {Object} carousel - The carousel to rotate the slides for.
* @private
*/
#rotateSlides(carousel) {
const slides = carousel.querySelectorAll("[fabr-carousel-slide]");
const indicators = carousel.querySelectorAll("[fabr-carousel-indicator]");
slides.forEach((slide) => {
slide.classList.remove("fabr-carousel-slide-active");
});
indicators.forEach((indicator) => {
indicator.classList.remove("fabr-carousel-indicator-active");
});
slides[carousel.activeSlide].classList.add("fabr-carousel-slide-active");
indicators[carousel.activeSlide].classList.add(
"fabr-carousel-indicator-active"
);
carousel.activeSlideTimeout = setTimeout(() => {
this.setSlide(carousel, -1);
}, carousel.delay);
}
/**
* Event Function: Set the slide of the given carousel to the given index.
* @param {Event} event - The event object.
* @param {Object} carousel - The carousel to set the slide for.
* @param {Number} index - The index of the slide to set.
*/
setSlideEvent(_, carousel, index) {
this.setSlide(carousel, index);
}
/**
* Set the slide of the given carousel to the given index.
* @param {Object} carousel - The carousel to set the slide for.
* @param {Number} index - The index of the slide to set.
*/
setSlide(carousel, index) {
const slides = carousel.querySelectorAll("[fabr-carousel-slide]");
if (index === -1) {
index = carousel.activeSlide + 1;
} else if (index === -2) {
index = carousel.activeSlide - 1;
}
if (index >= slides.length) {
index = 0;
} else if (index < 0) {
index = slides.length - 1;
}
clearTimeout(carousel.activeSlideTimeout);
carousel.activeSlide = index;
this.#rotateSlides(carousel);
}
};