@@ -46,6 +46,32 @@ export default {
46
46
}
47
47
}
48
48
}
49
+
50
+ const frameCount = this . frameCount
51
+ if ( ! this . _preFrameCount ) {
52
+ this . _preFrameCount = frameCount
53
+ }
54
+ else if ( this . _preFrameCount !== frameCount ) {
55
+ this . _resetNodes ( )
56
+ this . _preFrameCount = frameCount
57
+ const resetBlankFrame = ( ) => {
58
+ if ( this . currentIndex >= frameCount ) {
59
+ // reset blank page.
60
+ this . _stopAutoPlay ( )
61
+ this . _slideTo ( 0 )
62
+ }
63
+ }
64
+ if ( this . _sliding ) {
65
+ // If it's sliding, then the currentIndex is the last frame. The actual currentIndex
66
+ // should be the next index.
67
+ // That is to say, this updating happens Between _parepareNodes and _rearrangeNodes,
68
+ // and the sliding is not yet finished, and the state is not updated yet.
69
+ setTimeout ( resetBlankFrame , TRANSITION_TIME + NEIGHBOR_SCALE_TIME )
70
+ }
71
+ else {
72
+ resetBlankFrame ( )
73
+ }
74
+ }
49
75
weex . utils . fireLazyload ( this . $el , true )
50
76
if ( this . _preIndex !== this . currentIndex ) {
51
77
this . _slideTo ( this . currentIndex )
@@ -156,7 +182,6 @@ export default {
156
182
return
157
183
}
158
184
}
159
-
160
185
if ( ! this . _preIndex && this . _preIndex !== 0 ) {
161
186
if ( this . _showNodes && this . _showNodes [ 0 ] ) {
162
187
this . _preIndex = this . _showNodes [ 0 ] . index
@@ -282,23 +307,44 @@ export default {
282
307
node . _showIndex = 0
283
308
return
284
309
}
310
+
285
311
const showCount = this . _showCount = Math . abs ( step ) + 3
286
312
this . _showStartIdx = step <= 0 ? - 1 : 2 - showCount
287
313
this . _showEndIdx = step <= 0 ? showCount - 2 : 1
288
314
this . _clearNodesOffset ( )
289
315
this . _positionNodes ( this . _showStartIdx , this . _showEndIdx , step )
290
316
} ,
291
317
318
+ _clearClones ( ) {
319
+ // clear all clones.
320
+ Object . keys ( this . _clones ) . forEach ( key => {
321
+ this . _clones [ key ] . forEach ( cloneNode => {
322
+ cloneNode . parentNode . removeChild ( cloneNode )
323
+ } )
324
+ this . _clones [ key ] = [ ]
325
+ } )
326
+ } ,
327
+
328
+ // reset nodes' index and _inShow state. But leave the styles
329
+ // as they are to prevent dom rerendering.
330
+ _resetNodes ( ) {
331
+ this . _clearClones ( )
332
+ // reset status.
333
+ this . _cells . forEach ( ( cell , idx ) => {
334
+ const elm = cell . elm
335
+ elm . index = idx
336
+ elm . _inShow = false
337
+ } )
338
+ } ,
339
+
292
340
_initNodes ( ) {
293
- const total = this . frameCount
294
- const cells = this . _cells
295
- for ( let i = 0 ; i < total ; i ++ ) {
296
- const node = cells [ i ] . elm
297
- node . index = i
341
+ this . _cells . forEach ( ( cell , idx ) => {
342
+ const node = cell . elm
343
+ node . index = idx
298
344
node . _inShow = false
299
345
node . style . zIndex = 0
300
346
node . style . opacity = 0
301
- }
347
+ } )
302
348
} ,
303
349
304
350
_positionNodes ( begin , end , step , anim ) {
@@ -319,11 +365,11 @@ export default {
319
365
*/
320
366
_positionNode ( node , index ) {
321
367
const holder = this . _showNodes [ index ]
322
- if ( node . _inShow && holder !== node ) {
323
- if ( holder ) { this . _removeClone ( holder ) }
368
+ if ( node . _inShow && ( holder !== node || holder . _showIndex !== index ) ) {
369
+ if ( holder && holder . _isClone ) { this . _removeClone ( holder ) }
324
370
node = this . _getClone ( node . index )
325
371
}
326
- else if ( node . _inShow ) {
372
+ else if ( node . _inShow ) { // holder === node
327
373
return
328
374
}
329
375
@@ -339,30 +385,49 @@ export default {
339
385
} ,
340
386
341
387
_getClone ( index ) {
342
- let arr = this . _clones [ index ]
343
- if ( ! arr ) {
344
- this . _clones [ index ] = arr = [ ]
345
- }
346
- if ( arr . length <= 0 ) {
347
- const origNode = this . _cells [ index ] . elm
348
- const clone = origNode . cloneNode ( true )
349
- clone . _isClone = true
350
- clone . _inShow = origNode . _inShow
351
- clone . index = origNode . index
352
- clone . style . opacity = 0
353
- clone . style . zIndex = 0
354
- const ct = this . $refs . inner
355
- ct . appendChild ( clone )
356
- arr . push ( clone )
357
- }
358
- return arr . pop ( )
388
+ const arr = this . _clones [ index ] || ( this . _clones [ index ] = [ ] )
389
+ const origNode = this . _cells [ index ] . elm
390
+ const clone = origNode . cloneNode ( true )
391
+ clone . _isClone = true
392
+ clone . _inShow = true
393
+ // clone._inShow = origNode._inShow
394
+ clone . index = origNode . index
395
+ clone . style . opacity = 0
396
+ clone . style . zIndex = 0
397
+ this . $refs . inner . appendChild ( clone )
398
+ arr . push ( clone )
399
+ return clone
400
+ // try {
401
+ // let arr = this._clones[index]
402
+ // if (!arr) {
403
+ // this._clones[index] = arr = []
404
+ // }
405
+ // if (arr.length <= 0) {
406
+
407
+ // }
408
+ // return arr.pop()
409
+ // } catch (err) {
410
+ // console.error('this._cells -> ', this._cells)
411
+ // }
359
412
} ,
360
413
361
414
_removeClone ( node ) {
362
- const idx = node . index
363
- this . _hideNode ( node )
364
- const arr = this . _clones [ idx ]
365
- arr . push ( node )
415
+ const cloneArr = this . _clones [ node . index ]
416
+ let i
417
+ if ( cloneArr && ( i = cloneArr . indexOf ( node ) ) > - 1 ) {
418
+ cloneArr . splice ( i , 1 )
419
+ }
420
+ try {
421
+ node . parentNode . removeChild ( node )
422
+ }
423
+ catch ( err ) {
424
+ // maybe cells has been updated and this clone node is already removed from the dom tree
425
+ // throught _clearClones method.
426
+ }
427
+ // const idx = node.index
428
+ // this._hideNode(node)
429
+ // const arr = this._clones[idx]
430
+ // arr.push(node)
366
431
} ,
367
432
368
433
_hideNode ( node ) {
@@ -410,7 +475,11 @@ export default {
410
475
* replace a clone node with the original node if it's not in use.
411
476
*/
412
477
_replaceClone ( clone , pos ) {
413
- const origNode = this . _cells [ clone . index ] . elm
478
+ const origCell = this . _cells [ clone . index ]
479
+ if ( ! origCell ) {
480
+ return
481
+ }
482
+ const origNode = origCell . elm
414
483
if ( origNode . _inShow ) {
415
484
return
416
485
}
0 commit comments