-
Notifications
You must be signed in to change notification settings - Fork 878
/
react-native-swiper.js
480 lines (418 loc) · 12.1 KB
/
react-native-swiper.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
'use strict'
/*
react-native-swiper
@原作者 leecade<[email protected]>
@修改成标准RN-Module
*/
var React = require('react-native');
var {
StyleSheet,
Text,
View,
ScrollView,
TouchableOpacity,
} = React;
// Using bare setTimeout, setInterval, setImmediate
// and requestAnimationFrame calls is very dangerous
// because if you forget to cancel the request before
// the component is unmounted, you risk the callback
// throwing an exception.
var Dimensions = require('Dimensions');
var TimerMixin = require('react-timer-mixin');
var { width, height } = Dimensions.get('window');
/**
* Default styles
* @type {StyleSheetPropType}
*/
var styles = StyleSheet.create({
container: {
backgroundColor: 'transparent',
position: 'relative',
},
wrapper: {
backgroundColor: 'transparent',
},
slide: {
backgroundColor: 'transparent',
},
pagination_x: {
position: 'absolute',
bottom: 25,
left: 0,
right: 0,
flexDirection: 'row',
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor:'transparent',
},
pagination_y: {
position: 'absolute',
right: 15,
top: 0,
bottom: 0,
flexDirection: 'column',
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor:'transparent',
},
title: {
height: 30,
justifyContent: 'center',
position: 'absolute',
paddingLeft: 10,
bottom: -30,
left: 0,
flexWrap: 'nowrap',
width: 250,
backgroundColor: 'transparent',
},
buttonWrapper: {
backgroundColor: 'transparent',
flexDirection: 'row',
position: 'absolute',
top: 0,
left: 0,
flex: 1,
paddingHorizontal: 10,
paddingVertical: 10,
justifyContent: 'space-between',
alignItems: 'center'
},
buttonText: {
fontSize: 50,
color: '#007aff',
fontFamily: 'Arial',
},
})
module.exports = React.createClass({
/**
* Props Validation
* @type {Object}
*/
propTypes: {
horizontal : React.PropTypes.bool,
children : React.PropTypes.node.isRequired,
style : View.propTypes.style,
pagingEnabled : React.PropTypes.bool,
showsHorizontalScrollIndicator : React.PropTypes.bool,
showsVerticalScrollIndicator : React.PropTypes.bool,
bounces : React.PropTypes.bool,
scrollsToTop : React.PropTypes.bool,
removeClippedSubviews : React.PropTypes.bool,
automaticallyAdjustContentInsets : React.PropTypes.bool,
showsPagination : React.PropTypes.bool,
showsButtons : React.PropTypes.bool,
loop : React.PropTypes.bool,
autoplay : React.PropTypes.bool,
autoplayTimeout : React.PropTypes.number,
autoplayDirection : React.PropTypes.bool,
index : React.PropTypes.number,
renderPagination : React.PropTypes.func,
},
mixins: [TimerMixin],
/**
* Default props
* @return {object} props
* @see http://facebook.github.io/react-native/docs/scrollview.html
*/
getDefaultProps() {
return {
horizontal : true,
pagingEnabled : true,
showsHorizontalScrollIndicator : false,
showsVerticalScrollIndicator : false,
bounces : false,
scrollsToTop : false,
removeClippedSubviews : true,
automaticallyAdjustContentInsets : false,
showsPagination : true,
showsButtons : false,
loop : true,
autoplay : false,
autoplayTimeout : 2.5,
autoplayDirection : true,
index : 0,
}
},
/**
* Init states
* @return {object} states
*/
getInitialState() {
let props = this.props
let initState = {
isScrolling: false,
autoplayEnd: false,
}
initState.total = props.children
? (props.children.length || 1)
: 0
initState.index = initState.total > 1
? Math.min(props.index, initState.total - 1)
: 0
// Default: horizontal
initState.dir = props.horizontal == false ? 'y' : 'x'
initState.width = props.width || width
initState.height = props.height || height
initState.offset = {}
if(initState.total > 1) {
let setup = props.loop ? 1 : initState.index
initState.offset[initState.dir] = initState.dir == 'y'
? initState.height * setup
: initState.width * setup
}
return initState
},
/**
* autoplay timer
* @type {null}
*/
autoplayTimer: null,
componentWillMount() {
this.props = this.injectState(this.props)
},
componentDidMount() {
this.autoplay()
},
/**
* Automatic rolling
*/
autoplay() {
if(!this.props.autoplay
|| this.state.isScrolling
|| this.state.autoplayEnd) return
clearTimeout(this.autoplayTimer)
this.autoplayTimer = this.setTimeout(() => {
if(!this.props.loop && (this.props.autoplayDirection
? this.state.index == this.state.total - 1
: this.state.index == 0)) return this.setState({
autoplayEnd: true
})
this.scrollTo(this.props.autoplayDirection ? 1 : -1)
}, this.props.autoplayTimeout * 1000)
},
/**
* Scroll begin handle
* @param {object} e native event
*/
onScrollBegin(e) {
// update scroll state
this.setState({
isScrolling: true
})
this.setTimeout(() => {
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e, this.state, this)
})
},
/**
* Scroll end handle
* @param {object} e native event
*/
onScrollEnd(e) {
// update scroll state
this.setState({
isScrolling: false
})
this.updateIndex(e.nativeEvent.contentOffset, this.state.dir)
// Note: `this.setState` is async, so I call the `onMomentumScrollEnd`
// in setTimeout to ensure synchronous update `index`
this.setTimeout(() => {
this.autoplay()
// if `onMomentumScrollEnd` registered will be called here
this.props.onMomentumScrollEnd && this.props.onMomentumScrollEnd(e, this.state, this)
})
},
/**
* Update index after scroll
* @param {object} offset content offset
* @param {string} dir 'x' || 'y'
*/
updateIndex(offset, dir) {
let state = this.state
let index = state.index
let diff = offset[dir] - state.offset[dir]
let step = dir == 'x' ? state.width : state.height
// Do nothing if offset no change.
if(!diff) return
// Note: if touch very very quickly and continuous,
// the variation of `index` more than 1.
index = index + diff / step
if(this.props.loop) {
if(index <= -1) {
index = state.total - 1
offset[dir] = step * state.total
}
else if(index >= state.total) {
index = 0
offset[dir] = step
}
}
this.setState({
index: index,
offset: offset,
})
},
/**
* Scroll by index
* @param {number} index offset index
*/
scrollTo(index) {
if(this.state.isScrolling) return
let state = this.state
let diff = (this.props.loop ? 1 : 0) + index + this.state.index
let x = 0
let y = 0
if(state.dir == 'x') x = diff * state.width
if(state.dir == 'y') y = diff * state.height
this.refs.scrollView && this.refs.scrollView.scrollTo(y, x)
// update scroll state
this.setState({
isScrolling: true,
autoplayEnd: false,
})
},
/**
* Render pagination
* @return {object} react-dom
*/
renderPagination() {
// By default, dots only show when `total` > 2
if(this.state.total <= 1) return null
let dots = []
for(let i = 0; i < this.state.total; i++) {
dots.push(i === this.state.index
? (this.props.activeDot || <View style={{
backgroundColor: '#007aff',
width: 8,
height: 8,
borderRadius: 4,
marginLeft: 3,
marginRight: 3,
marginTop: 3,
marginBottom: 3,
}} />)
: (this.props.dot || <View style={{
backgroundColor:'rgba(0,0,0,.2)',
width: 8,
height: 8,
borderRadius: 4,
marginLeft: 3,
marginRight: 3,
marginTop: 3,
marginBottom: 3,
}} />)
)
}
return (
<View pointerEvents='none' style={[styles['pagination_' + this.state.dir], this.props.paginationStyle]}>
{dots}
</View>
)
},
renderTitle() {
let child = this.props.children[this.state.index]
let title = child && child.props.title
return title
? (
<View style={styles.title}>
{this.props.children[this.state.index].props.title}
</View>
)
: null
},
renderButtons() {
let nextButton = this.props.nextButton || <Text style={[styles.buttonText, {color: !this.props.loop && this.state.index == this.state.total - 1 ? 'rgba(0,0,0,0)' : '#007aff'}]}>›</Text>
let prevButton = this.props.prevButton || <Text style={[styles.buttonText, {color: !this.props.loop && this.state.index == 0 ? 'rgba(0,0,0,0)' : '#007aff'}]}>‹</Text>
return (
<View pointerEvents='box-none' style={[styles.buttonWrapper, {width: this.state.width, height: this.state.height}, this.props.buttonWrapperStyle]}>
<TouchableOpacity onPress={() => !(!this.props.loop && this.state.index == 0) && this.scrollTo.call(this, -1)}>
<View>
{prevButton}
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => !(!this.props.loop && this.state.index == this.state.total - 1) && this.scrollTo.call(this, 1)}>
<View>
{nextButton}
</View>
</TouchableOpacity>
</View>
)
},
/**
* Inject state to ScrollResponder
* @param {object} props origin props
* @return {object} props injected props
*/
injectState(props) {
/* const scrollResponders = [
'onMomentumScrollBegin',
'onTouchStartCapture',
'onTouchStart',
'onTouchEnd',
'onResponderRelease',
]*/
for(let prop in props) {
// if(~scrollResponders.indexOf(prop)
if(typeof props[prop] === 'function'
&& prop !== 'onMomentumScrollEnd'
&& prop !== 'renderPagination'
&& prop !== 'onScrollBeginDrag'
) {
let originResponder = props[prop]
props[prop] = (e) => originResponder(e, this.state, this)
}
}
return props
},
/**
* Default render
* @return {object} react-dom
*/
render() {
let state = this.state
let props = this.props
let children = props.children
let index = state.index
let total = state.total
let loop = props.loop
let dir = state.dir
let key = 0
let pages = []
let pageStyle = [{width: state.width, height: state.height}, styles.slide]
// For make infinite at least total > 1
if(total > 1) {
// Re-design a loop model for avoid img flickering
pages = Object.keys(children)
if(loop) {
pages.unshift(total - 1)
pages.push(0)
}
pages = pages.map((page, i) =>
<View style={pageStyle} key={i}>{children[page]}</View>
)
}
else pages = <View style={pageStyle}>{children}</View>
return (
<View style={[styles.container, {
width: state.width,
height: state.height
}]}>
<ScrollView ref="scrollView"
{...props}
contentContainerStyle={[styles.wrapper, props.style]}
contentOffset={state.offset}
onScrollBeginDrag={this.onScrollBegin}
onMomentumScrollEnd={this.onScrollEnd}>
{pages}
</ScrollView>
{props.showsPagination && (props.renderPagination
? this.props.renderPagination(state.index, state.total, this)
: this.renderPagination())}
{this.renderTitle()}
{this.props.showsButtons && this.renderButtons()}
</View>
)
}
})