-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhelpers.js
130 lines (102 loc) · 4.95 KB
/
helpers.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
const prefix = 'pf'
const refinePos = (arr, size) => arr.map(obj => { return { ...obj, index: obj.index % size } })
// Export all these FOLLOWING stuff before publish
if (!Array.prototype.includes) {
Array.prototype.includes = function(item) {
return this.indexOf(item) !== -1
}
}
export const SliderXClasses = {
wrapper: `${prefix}-slider-wrapper`,
inner: `${prefix}-slider-inner`,
slide: `${prefix}-slider-slide`,
indicators: `${prefix}-slider-pagination`,
controller: `${prefix}-slider-nav`,
disabledCtrl: `${prefix}-slider-nav-disabled`,
nextCtrl: `${prefix}-next-nav`,
prevCtrl: `${prefix}-prev-nav`,
mouseEventOff: `${prefix}-slider-mouse-event-off`
}
// The logic is compicated! Stay tune before reading this func
export const getSlidingData = (slider, direction, toIndex) => {
let { totalSlide, sliderWidth, $slides } = slider
totalSlide *= 3
const sliderOffsetLeft = slider.el.getBoundingClientRect().left
const slideWidth = getSlideWidth(slider)
const { curr, slidesToShow, slidesToScroll, gutter } = slider.opts
// Created this array to check if the next index is in the curr-showing-slides or not
const currIndexes = []
for (let i = 0; i < slidesToShow; i++) { currIndexes.push((curr + i) % totalSlide) }
let nextIndex
let nextSlidesReadyPos = [], nextSlidesNewPos = [], currSlidesNewPos = []
let slidesMove
if (direction === "next") {
// Calculate number of slides to move
if (toIndex !== undefined) {
if (currIndexes.includes(toIndex)) {
slidesMove = currIndexes.indexOf(toIndex) - currIndexes.indexOf(curr)
} else slidesMove = slidesToShow
} else slidesMove = slidesToScroll
nextIndex = toIndex !== undefined ? toIndex : (curr + slidesToScroll) % totalSlide
// Calculate next slides ready-position - where the next slides stay and be ready to move in
let firstX
if (currIndexes.includes(nextIndex)) {
///// Pausing here
firstX = $slides[nextIndex].getBoundingClientRect().left - sliderOffsetLeft
} else firstX = sliderWidth + gutter
for (let i = 0; i < slidesToShow; i++) {
const readyX = firstX + (slideWidth + gutter) * i
if (!currIndexes.includes((nextIndex + i) % totalSlide)) {
nextSlidesReadyPos.push({ index: nextIndex + i, readyX })
}
nextSlidesNewPos.push({ index: nextIndex + i, newX: readyX - (gutter + slideWidth) * slidesMove })
}
} else if (direction === "prev") {
// Calculate number of slides to move
if (toIndex !== undefined) {
const lastIndex = (toIndex + slidesToShow - 1) % totalSlide
if (currIndexes.includes(lastIndex)) {
slidesMove = currIndexes.indexOf((curr + slidesToShow - 1) % totalSlide) - currIndexes.indexOf(lastIndex)
} else slidesMove = slidesToShow
} else slidesMove = slidesToScroll
nextIndex = toIndex !== undefined ? toIndex : (totalSlide + (curr - slidesToScroll)) % totalSlide
// Calculate next slides ready-position - where the next slides stay and be ready to move in
let firstX // left position of the last slide in next slides
if (currIndexes.includes((nextIndex + slidesToShow - 1) % totalSlide)) {
firstX = $slides[(nextIndex + slidesToShow - 1) % totalSlide].getBoundingClientRect().left - sliderOffsetLeft
} else firstX = -(slideWidth + gutter)
for (let i = 0; i < slidesToShow; i++) {
const readyX = firstX - (slideWidth + gutter) * (slidesToShow - i - 1)
if (!currIndexes.includes((nextIndex + i) % totalSlide)) {
nextSlidesReadyPos.push({ index: nextIndex + i, readyX })
}
nextSlidesNewPos.push({ index: nextIndex + i, newX: readyX + (gutter + slideWidth) * slidesMove })
}
}
// Calculate new position for curr-showing-slides
for (let i = 0; i < slidesToShow; i++) {
const slideX = $slides[(curr + i) % totalSlide].getBoundingClientRect().left - sliderOffsetLeft
let newX
if (direction === 'next') newX = slideX - (gutter + slideWidth) * slidesMove
else if (direction === 'prev') newX = slideX + (gutter + slideWidth) * slidesMove
if (slider.moveByDrag) {
const currLeft = $slides[curr % totalSlide].getBoundingClientRect().left - sliderOffsetLeft
if (direction === 'prev') newX = slideX + (sliderWidth - currLeft) + gutter
else if (direction === 'next') newX = slideX - (sliderWidth + currLeft) - gutter
}
currSlidesNewPos.push({ index: curr + i, newX })
}
// debugger
////////////
nextSlidesReadyPos = refinePos(nextSlidesReadyPos, totalSlide)
nextSlidesNewPos = refinePos(nextSlidesNewPos, totalSlide)
currSlidesNewPos = refinePos(currSlidesNewPos, totalSlide)
// console.log(nextSlidesReadyPos, currSlidesNewPos, nextSlidesNewPos)
return { nextIndex, nextSlidesReadyPos, currSlidesNewPos, nextSlidesNewPos }
}
export const getSlideWidth = slider => {
const { gutter, slidesToShow } = slider.opts
const wrapperWidth = slider.sliderWidth
const slideWidth = (wrapperWidth - gutter * (slidesToShow - 1)) / slidesToShow
return slideWidth
}