@@ -11,28 +11,18 @@ Object.defineProperty(exports, '__esModule', {
11
11
/*
12
12
react-native-swiper
13
13
14
- feaure:
15
- [x] loop
16
- [x] dir
17
- [x] custom style
18
- [x] title
19
- [x] multiple instances
20
- [x] custom size
21
- [x] control buttons
22
- [x] autoplay
23
- [ ] more switch effect
24
-
25
- params(props):
26
- - dir "x" || "y" @default: "x"
27
-
28
- -dot Optionally provide the dot object show in pagination
14
+ @author leecade<[email protected] >
29
15
*/
30
16
31
17
var _React$StyleSheet$Text$View$ScrollView$TouchableOpacity = require ( 'react-native' ) ;
32
18
33
19
var _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 = _interopRequireWildcard ( _React$StyleSheet$Text$View$ScrollView$TouchableOpacity ) ;
34
20
35
- // 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.
21
+ // Using bare setTimeout, setInterval, setImmediate
22
+ // and requestAnimationFrame calls is very dangerous
23
+ // because if you forget to cancel the request before
24
+ // the component is unmounted, you risk the callback
25
+ // throwing an exception.
36
26
37
27
var _TimerMixin = require ( 'react-timer-mixin' ) ;
38
28
@@ -173,15 +163,27 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
173
163
*/
174
164
getInitialState : function getInitialState ( ) {
175
165
var props = this . props ;
176
- var length = props . children ? props . children . length || 1 : 0 ;
177
- return {
178
- index : length > 1 ? props . index : 0 ,
179
- total : length ,
180
- dir : props . horizontal == false ? 'y' : 'x' ,
181
- width : props . width || width ,
182
- height : props . height || height ,
166
+
167
+ var initState = {
183
168
isScrolling : false ,
184
169
autoplayEnd : false } ;
170
+
171
+ initState . total = props . children ? props . children . length || 1 : 0 ;
172
+
173
+ initState . index = initState . total > 1 ? Math . min ( props . index , initState . total - 1 ) : 0 ;
174
+
175
+ // Default: horizontal
176
+ initState . dir = props . horizontal == false ? 'y' : 'x' ;
177
+ initState . width = props . width || width ;
178
+ initState . height = props . height || height ;
179
+ initState . offset = { } ;
180
+
181
+ if ( initState . total > 1 ) {
182
+ var setup = props . loop ? 1 : initState . index ;
183
+ initState . offset [ initState . dir ] = initState . dir == 'y' ? initState . height * setup : initState . width * setup ;
184
+ }
185
+
186
+ return initState ;
185
187
} ,
186
188
187
189
/**
@@ -190,14 +192,20 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
190
192
*/
191
193
autoplayTimer : null ,
192
194
193
- componentDidMount : function componentDidMount ( ) { } ,
195
+ componentDidMount : function componentDidMount ( ) {
196
+ this . autoplay ( ) ;
197
+ } ,
194
198
199
+ /**
200
+ * Automatic rolling
201
+ */
195
202
autoplay : function autoplay ( ) {
196
203
var _this = this ;
197
204
198
205
if ( ! this . props . autoplay || this . state . isScrolling || this . state . autoplayEnd ) {
199
206
return ;
200
207
} clearTimeout ( this . autoplayTimer ) ;
208
+
201
209
this . autoplayTimer = this . setTimeout ( function ( ) {
202
210
if ( ! _this . props . loop && ( _this . props . autoplayDirection ? _this . state . index == _this . state . total - 1 : _this . state . index == 0 ) ) return _this . setState ( {
203
211
autoplayEnd : true
@@ -206,22 +214,39 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
206
214
} , this . props . autoplayTimeout * 1000 ) ;
207
215
} ,
208
216
217
+ /**
218
+ * Scroll begin handle
219
+ * @param {object } e native event
220
+ */
221
+ onScrollBegin : function onScrollBegin ( e ) {
222
+ // update scroll state
223
+ this . setState ( {
224
+ isScrolling : true
225
+ } ) ;
226
+
227
+ this . props . onScrollBeginDrag && this . props . onScrollBeginDrag . call ( this , e ) ;
228
+ } ,
229
+
209
230
/**
210
231
* Scroll end handle
211
232
* @param {object } e native event
212
233
*/
213
234
onScrollEnd : function onScrollEnd ( e ) {
235
+ var _this2 = this ;
214
236
215
237
// update scroll state
216
238
this . setState ( {
217
239
isScrolling : false
218
240
} ) ;
219
241
220
- var offset = e . nativeEvent . contentOffset ;
221
- this . updateIndex ( offset , this . state . dir ) ;
242
+ this . updateIndex ( e . nativeEvent . contentOffset , this . state . dir ) ;
243
+
244
+ this . setTimeout ( function ( ) {
245
+ _this2 . autoplay ( ) ;
246
+ } ) ;
222
247
223
248
// if `onMomentumScrollEnd` registered will be called here
224
- this . props . onMomentumScrollEnd && this . props . onMomentumScrollEnd . call ( this ) ;
249
+ this . props . onMomentumScrollEnd && this . props . onMomentumScrollEnd . call ( this , e ) ;
225
250
} ,
226
251
227
252
/**
@@ -230,17 +255,31 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
230
255
* @param {string } dir 'x' || 'y'
231
256
*/
232
257
updateIndex : function updateIndex ( offset , dir ) {
233
- offset = offset [ dir ] ;
258
+
234
259
var state = this . state ;
235
260
var index = state . index ;
236
- var diff = dir == 'x' ? state . width : state . height ;
261
+ var diff = offset [ dir ] - state . offset [ dir ] ;
262
+ var step = dir == 'x' ? state . width : state . height ;
263
+
264
+ // Do nothing if offset no change.
265
+ if ( ! diff ) {
266
+ return ;
267
+ } // Note: if touch very very quickly and continuous,
268
+ // the variation of `index` more than 1.
269
+ index = index + diff / step ;
237
270
if ( this . props . loop ) {
238
- if ( offset > diff ) index ++ ; else if ( offset < diff ) index -- ;
239
- if ( index == - 1 ) index = state . total - 1 ; else if ( index == state . total ) index = 0 ;
240
- } else index = Math . floor ( ( offset - diff / 2 ) / diff ) + 1 ;
271
+ if ( index <= - 1 ) {
272
+ index = state . total - 1 ;
273
+ offset [ dir ] = step * state . total ;
274
+ } else if ( index >= state . total ) {
275
+ index = 0 ;
276
+ offset [ dir ] = step ;
277
+ }
278
+ }
279
+
241
280
this . setState ( {
242
- index : index
243
- } ) ;
281
+ index : index ,
282
+ offset : offset } ) ;
244
283
} ,
245
284
246
285
/**
@@ -251,7 +290,7 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
251
290
if ( this . state . isScrolling ) {
252
291
return ;
253
292
} var state = this . state ;
254
- var diff = ( this . props . loop ? 1 : this . state . index ) + index ;
293
+ var diff = ( this . props . loop ? 1 : 0 ) + index + this . state . index ;
255
294
var x = 0 ;
256
295
var y = 0 ;
257
296
if ( state . dir == 'x' ) x = diff * state . width ;
@@ -312,7 +351,7 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
312
351
} ,
313
352
314
353
renderButtons : function renderButtons ( ) {
315
- var _this2 = this ;
354
+ var _this3 = this ;
316
355
317
356
var nextButton = this . props . nextButton || _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
318
357
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . Text ,
@@ -332,7 +371,7 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
332
371
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
333
372
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . TouchableOpacity ,
334
373
{ onPress : function ( ) {
335
- return ! ( ! _this2 . props . loop && _this2 . state . index == 0 ) && _this2 . scrollTo . call ( _this2 , - 1 ) ;
374
+ return ! ( ! _this3 . props . loop && _this3 . state . index == 0 ) && _this3 . scrollTo . call ( _this3 , - 1 ) ;
336
375
} } ,
337
376
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
338
377
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . View ,
@@ -343,7 +382,7 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
343
382
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
344
383
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . TouchableOpacity ,
345
384
{ onPress : function ( ) {
346
- return ! ( ! _this2 . props . loop && _this2 . state . index == _this2 . state . total - 1 ) && _this2 . scrollTo . call ( _this2 , 1 ) ;
385
+ return ! ( ! _this3 . props . loop && _this3 . state . index == _this3 . state . total - 1 ) && _this3 . scrollTo . call ( _this3 , 1 ) ;
347
386
} } ,
348
387
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
349
388
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . View ,
@@ -366,38 +405,34 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
366
405
var total = state . total ;
367
406
var loop = props . loop ;
368
407
var dir = state . dir ;
369
- var initOffset = { } ;
370
408
var key = 0 ;
371
409
372
410
var pages = [ ] ;
373
411
var pageStyle = [ { width : state . width , height : state . height } , styles . slide ] ;
374
412
375
413
// For make infinite at least total > 1
376
414
if ( total > 1 ) {
415
+
416
+ // Re-design a loop model for avoid img flickering
417
+ pages = Object . keys ( children ) ;
377
418
if ( loop ) {
378
- pages . push ( index == 0 ? total - 1 : index - 1 ) ;
379
- pages . push ( index ) ;
380
- pages . push ( index == total - 1 ? 0 : index + 1 ) ;
381
- key = index ;
382
- } else pages = Object . keys ( children ) ;
419
+ pages . unshift ( total - 1 ) ;
420
+ pages . push ( 0 ) ;
421
+ }
422
+
383
423
pages = pages . map ( function ( page , i ) {
384
424
return _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
385
425
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . View ,
386
426
{ style : pageStyle , key : i } ,
387
427
children [ page ]
388
428
) ;
389
429
} ) ;
390
-
391
- var setup = loop ? 1 : index ;
392
- initOffset [ dir ] = dir == 'y' ? state . height * setup : state . width * setup ;
393
430
} else pages = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
394
431
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . View ,
395
432
{ style : pageStyle } ,
396
433
children
397
434
) ;
398
435
399
- this . autoplay ( ) ;
400
-
401
436
return _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2 [ 'default' ] . createElement (
402
437
_React$StyleSheet$Text$View$ScrollView$TouchableOpacity . View ,
403
438
{ style : [ styles . container , {
@@ -409,9 +444,9 @@ exports['default'] = _React$StyleSheet$Text$View$ScrollView$TouchableOpacity2['d
409
444
_extends ( { ref : 'scrollView'
410
445
} , props , {
411
446
contentContainerStyle : [ styles . wrapper , props . style ] ,
412
- contentOffset : initOffset ,
413
- onMomentumScrollEnd : this . onScrollEnd ,
414
- key : key } ) ,
447
+ contentOffset : state . offset ,
448
+ onScrollBeginDrag : this . onScrollBegin ,
449
+ onMomentumScrollEnd : this . onScrollEnd } ) ,
415
450
pages
416
451
) ,
417
452
props . showsPagination && ( props . renderPagination ? this . props . renderPagination . call ( this , state . index , state . total ) : this . renderPagination ( ) ) ,
0 commit comments