-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (238 loc) · 8.29 KB
/
index.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
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
/**
* Creates a Swiper instance on the specified element.
* @param {HTMLElement} element - The element to attach the Swiper to.
* @param {Object} options - The options for configuring the Swiper behavior.
*
* @param {Function} options.leftEvent - The function to be called when the user swipes left.
*
* @param {Function} options.rightEvent - The function to be called when the user swipes right.
*
* @param {Function} options.topEvent - The function to be called when the user swipes up.
*
* @param {Function} options.bottomEvent - The function to be called when the user swipes down.
*
* @param {Boolean} options.horizontal - Whether the Swiper should listen for horizontal swipes.
*
* @param {Boolean} options.vertical - Whether the Swiper should listen for vertical swipes.
*
* @param {Boolean} options.mobileOnly - Whether the Swiper should only listen for swipes on
* mobile devices.
*
* @param {Boolean} options.step - Whether the Swiper should only listen for swipes that are a
* single step.
*
* @returns {void}
*
* */
import './index.scss';
let setSwiperContainer;
let isContainerInvalid;
let setOptions;
let setContainerChildrenClass;
let defaltCardAnimation;
let defaultOptions;
let eventHandler;
let optionsInUse;
let coordinates;
export default function Swiper(element, options = {}) {
const swiperContainer = setSwiperContainer(element);
if (isContainerInvalid(swiperContainer)) return false;
if (!setContainerChildrenClass(swiperContainer)) return false;
optionsInUse = setOptions(options);
function handleTouchEnd() {
coordinates.resetCoordinates(false);
defaultOptions.slideStep = 0;
}
function handleMouseUp() {
coordinates.resetCoordinates(false);
defaultOptions.slideStep = 0;
}
function handleTouchStart(evt) {
coordinates.xDown = evt.touches[0].clientX;
coordinates.yDown = evt.touches[0].clientY;
defaultOptions.slideSpeed = 25;
if (optionsInUse.slideble) {
swiperContainer.addEventListener('touchend', handleTouchEnd, false);
}
}
function handleMouseDown(evt) {
coordinates.xDown = evt.clientX;
coordinates.yDown = evt.clientY;
defaultOptions.slideSpeed = 50;
if (optionsInUse.slideble) {
swiperContainer.addEventListener('mouseup', handleMouseUp, false);
}
}
function handleTouchMove(evt) {
if (!coordinates.xDown || !coordinates.yDown) return;
coordinates.xUp = evt.touches[0].clientX;
coordinates.yUp = evt.touches[0].clientY;
eventHandler();
}
function handleMouseMove(evt) {
if (!coordinates.xDown || !coordinates.yDown) return;
coordinates.xUp = evt.clientX;
coordinates.yUp = evt.clientY;
eventHandler();
}
swiperContainer.addEventListener('touchstart', handleTouchStart, false);
swiperContainer.addEventListener('touchmove', handleTouchMove, false);
swiperContainer.addEventListener(
'touchmove',
(e) => {
e.preventDefault();
},
{ passive: false },
);
if (!optionsInUse.mobileOnly) {
swiperContainer.addEventListener('mousedown', handleMouseDown, false);
swiperContainer.addEventListener('mousemove', handleMouseMove, false);
swiperContainer.addEventListener(
'mousemove',
(e) => {
e.preventDefault();
},
{ passive: false },
);
}
return () => {
swiperContainer.removeEventListener('touchstart', handleTouchStart, false);
swiperContainer.removeEventListener('touchmove', handleTouchMove, false);
swiperContainer.removeEventListener('touchend', handleTouchEnd, false);
swiperContainer.removeEventListener('mousedown', handleMouseDown, false);
swiperContainer.removeEventListener('mousemove', handleMouseMove, false);
swiperContainer.removeEventListener('mouseup', handleMouseUp, false);
};
}
defaultOptions = {
leftEvent: () => defaltCardAnimation.leftEvent(),
rightEvent: () => defaltCardAnimation.rightEvent(),
topEvent: () => defaltCardAnimation.topEvent(),
bottomEvent: () => defaltCardAnimation.bottomEvent(),
horizontal: true,
vertical: false,
mobileOnly: false,
slideble: false,
slideStep: 0,
slideSpeed: 50,
};
coordinates = {
xDown: null,
yDown: null,
xUp: null,
yUp: null,
xDiff: () => coordinates.xDown - coordinates.xUp,
yDiff: () => coordinates.yDown - coordinates.yUp,
isHorizontal: () => Math.abs(coordinates.xDiff()) > Math.abs(coordinates.yDiff()),
resetCoordinates: (isHolding = true) => {
if (isHolding && defaultOptions.slideble) {
coordinates.xDown = coordinates.xUp;
coordinates.yDown = coordinates.yUp;
coordinates.xUp = null;
coordinates.yUp = null;
defaultOptions.slideStep = 0;
} else {
coordinates.xDown = null;
coordinates.yDown = null;
coordinates.xUp = null;
coordinates.yUp = null;
}
},
};
eventHandler = () => {
if (
!defaultOptions.slideble
|| (defaultOptions.slideble
&& defaultOptions.slideStep === defaultOptions.slideSpeed)
) {
if (optionsInUse.horizontal && coordinates.isHorizontal()) {
if (coordinates.xDiff() > 0) {
if (optionsInUse.leftEvent) optionsInUse.leftEvent();
} else if (optionsInUse.rightEvent) optionsInUse.rightEvent();
} else if (optionsInUse.vertical) {
if (coordinates.yDiff() > 0) {
if (optionsInUse.topEvent) optionsInUse.topEvent();
} else if (optionsInUse.bottomEvent) optionsInUse.bottomEvent();
}
coordinates.resetCoordinates();
} else {
defaultOptions.slideStep += 1;
}
};
setOptions = (options) => ({ ...defaultOptions, ...options });
setSwiperContainer = (element) => {
if (Array.isArray(element)) {
if (element.length === 1 && element[0] instanceof HTMLElement) {
element[0].classList.add('swiper-container');
return element[0];
}
return element.find((el) => el.classList.contains('swiper-container'));
}
if (element instanceof HTMLElement) {
element.classList.add('swiper-container');
return element;
}
return false;
};
setContainerChildrenClass = (container) => {
if (container.childElementCount === 0) return false;
[...container.children].forEach((child, index) => {
child.classList.add('swiper-slide-item');
if (index === 0) child.classList.add('swiper-slide-item--active');
if (index === 1) child.classList.add('swiper-slide-item--next');
if (index > 1) child.classList.add('swiper-slide-item--hidden-next');
});
return true;
};
isContainerInvalid = (container) => !container;
defaltCardAnimation = {
leftEvent: () => {
const prev = document.querySelector('.swiper-slide-item--prev');
const active = document.querySelector('.swiper-slide-item--active');
const next = document.querySelector('.swiper-slide-item--next');
const hiddenNext = document.querySelector(
'.swiper-slide-item--hidden-next',
);
if (!next) return;
if (prev) {
prev.classList.remove('swiper-slide-item--prev');
prev.classList.add('swiper-slide-item--hidden-prev');
}
active.classList.remove('swiper-slide-item--active');
active.classList.add('swiper-slide-item--prev');
next.classList.remove('swiper-slide-item--next');
next.classList.add('swiper-slide-item--active');
if (hiddenNext) {
hiddenNext.classList.remove('swiper-slide-item--hidden-next');
hiddenNext.classList.add('swiper-slide-item--next');
}
},
rightEvent: () => {
const hiddenPrevArr = document.querySelectorAll(
'.swiper-slide-item--hidden-prev',
);
const hiddenPrev = hiddenPrevArr[hiddenPrevArr.length - 1];
const prev = document.querySelector('.swiper-slide-item--prev');
const active = document.querySelector('.swiper-slide-item--active');
const next = document.querySelector('.swiper-slide-item--next');
if (!prev) return;
if (next) {
next.classList.remove('swiper-slide-item--next');
next.classList.add('swiper-slide-item--hidden-next');
}
active.classList.remove('swiper-slide-item--active');
active.classList.add('swiper-slide-item--next');
prev.classList.remove('swiper-slide-item--prev');
prev.classList.add('swiper-slide-item--active');
if (hiddenPrev) {
hiddenPrev.classList.remove('swiper-slide-item--hidden-prev');
hiddenPrev.classList.add('swiper-slide-item--prev');
}
},
topEvent: () => {
defaltCardAnimation.leftEvent();
},
bottomEvent: () => {
defaltCardAnimation.rightEvent();
},
};