-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
226 lines (182 loc) · 5.99 KB
/
main.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
const { wrapper, inner, slide, indicators, indicatorItem, controller, nextCtrl, prevCtrl, disabledCtrl } = DomClasses
class SliderController {
constructor(ele, opts) {
this.el = ele
this.originalStyles = {}
this.$el = $(this.el) // The original element
this.$slider = '' // The .inner div that wrap all slide
this.totalSlide = this.$el.children().length
this.opts = Object.assign({}, opts) // Each slider's opts is a specific instance of opts argument
this.autoTimeoutId = ''
this.moveLock = false
// Update DOM + set event handler
this.initialize()
this.$el.on('click', this.handleClick.bind(this))
console.log(this)
}
initialize() {
this.verifyOptions()
this.setupSliderDOM()
this.setAutoPlay()
return this
}
setupSliderDOM() {
this.$el.addClass(wrapper)
// Create an inner div to wrap all slide item
const $inner = $(`<div class=${inner}></div>`)
this.$el.children().each((i, child) => {
$(child).addClass(slide)
if (i === this.opts.curr) {
$(child).css('left', '0')
}
$inner.append(child)
})
// Save the inner DOM
this.$slider = $inner
// Append controllers
const $nextCtrl = $(`<a class='${nextCtrl} ${controller}' data-action='next'>Next</a>`)
const $prevCtrl = $(`<a class='${prevCtrl} ${controller}' data-action='prev'>Prev</a>`)
// Append indicators
const $indicators = $(`<ol class='${indicators}'></ol>`)
for (let i = 0; i < this.totalSlide; i++) {
const $indItem = $(`<li data-goto-slide=${i} data-action='goto'></li>`)
$indicators.append($indItem)
}
this.$el.append($inner).append($indicators).append($prevCtrl).append($nextCtrl)
// Set the .active class
this.updateSliderDOM()
}
verifyOptions() {
Object.entries(this.constructor.defaultOptions).forEach(([key, value]) => {
if (typeof this.opts[key] !== typeof value) {
this.opts[key] = value
}
})
// Turn off autoPlay if loop is false
if (!this.opts.loop) this.opts.autoPlay = false
}
updateOptions(newOtps) {
const { defaultOptions } = this.constructor
for (let key in newOtps) {
// The type of newOpts values must be legal
// And update 'curr' key is forbidden
if (typeof newOtps[key] === typeof defaultOptions[key] && key !== 'curr') {
this.opts[key] = newOtps[key]
}
}
// Turn off autoPlay if loop is false
if (!this.opts.loop) this.opts.autoPlay = false
this.updateSliderCtrlDOM(this.opts.curr)
this.setAutoPlay()
}
updateSliderCtrlDOM(index) {
this.$el.find('a').removeClass('pf-slider-nav-disabled')
if (index === 0 && !this.opts.loop) {
this.$el.find(`.${prevCtrl}`).addClass('pf-slider-nav-disabled')
}
else if (index === this.totalSlide - 1 && !this.opts.loop) {
this.$el.find(`.${nextCtrl}`).addClass('pf-slider-nav-disabled')
}
}
handleClick(e) {
const action = e.target.dataset.action
switch (action) {
case 'next': this.next(); break
case 'prev': this.prev(); break
case 'goto':
// DOMStringMap convert dataset from hyphen style to upperCase (goto-slide => gotoSlide)
const index = parseInt(e.target.dataset.gotoSlide) || 0
this.goto(index)
break
default: console.log('Slider clicked')
}
}
destroy() {
// Save all items, remove all classes on them
const items = []
this.$slider.children().each((i, child) => {
$(child).removeClass(slide)
items.push($(child))
})
// Remove class, event handler, data-instance and all children
this.$el.removeClass(wrapper)
this.$el.off('click', this.handleClick)
this.$el.data('slider', '')
this.$el.empty()
// Append the original item
for (let item of items) {
this.$el.append(item)
}
// !TODO!: remove all inline css (transition, left...)
}
setAutoPlay() {
if (this.opts.autoPlay) {
const delay = this.opts.autoPlayDelay
this.clearAutoPlay()
this.autoTimeoutId = setTimeout(() => {
this.moveSlide('next')
this.setAutoPlay()
}, delay)
}
}
clearAutoPlay() {
clearTimeout(this.autoTimeoutId)
}
moveSlide(direction, toIndex) {
let currIndex = this.opts.curr
let { nextIndex, nextSlidePos, currSlidePos } = getSlideMovementData(direction, currIndex, toIndex, this.totalSlide)
let $curr = this.$slider.children().eq(currIndex)
let $next = this.$slider.children().eq(nextIndex)
$next.css({ 'transition': '' })
$next.css('left', nextSlidePos)
// $next.css('transform', `translate3d(${nextSlidePos}px, 0, 0)`)
const duration = this.opts.duration
this.updateSliderCtrlDOM(nextIndex)
setTimeout(() => {
$curr.css({ 'transition': `left ${duration}ms ease-in-out`, 'left': currSlidePos })
$next.css({ 'transition': `left ${duration}ms ease-in-out`, 'left': '0' })
setTimeout(() => {
this.setAutoPlay()
this.moveLock = false
}, duration)
}, 20)
this.opts.curr = nextIndex
this.updateSliderDOM()
}
updateSliderDOM() {
// Add class .active for current active slide n indicator
const curr = this.opts.curr
this.$slider.children(`.${slide}.active`).removeClass('active')
this.$slider.children().eq(curr).addClass('active')
this.$el.find('li.active').removeClass('active')
this.$el.find('ol').children().eq(curr).addClass('active')
this.updateSliderCtrlDOM(curr)
}
next() {
if (this.moveLock) return
this.clearAutoPlay()
this.moveLock = true
this.moveSlide("next")
}
prev() {
if (this.moveLock) return
this.clearAutoPlay()
this.moveLock = true
this.moveSlide("prev")
}
goto(index) {
this.clearAutoPlay()
if (index > this.opts.curr) {
this.moveSlide("next", index)
} else if (index < this.opts.curr) {
this.moveSlide("prev", index)
}
}
}
SliderController.defaultOptions = {
curr: 0,
autoPlay: false,
autoPlayDelay: 3000,
duration: 450,
loop: true
}