@@ -45,6 +45,14 @@ const optionTypes = {
4545 ] ) ,
4646 onComplete : React . PropTypes . func
4747 } ,
48+ popToRootView : {
49+ preserveState : React . PropTypes . bool ,
50+ transition : React . PropTypes . oneOfType ( [
51+ React . PropTypes . func ,
52+ React . PropTypes . number
53+ ] ) ,
54+ onComplete : React . PropTypes . func
55+ } ,
4856 setViews : {
4957 views : React . PropTypes . arrayOf (
5058 React . PropTypes . element
@@ -427,6 +435,53 @@ class NavigationController extends React.Component {
427435 this . __pushView ( last ( views ) , options ) ;
428436 }
429437
438+ __popToRootView ( options ) {
439+ options = typeof options === 'object' ? options : { } ;
440+ const defaults = {
441+ transition : Transition . type . PUSH_RIGHT
442+ } ;
443+ options = assign ( { } , defaults , options ) ;
444+ checkOptions ( 'popToRootView' , options ) ;
445+ if ( this . state . views . length === 1 ) {
446+ throw new Error ( 'popToRootView() can only be called with two or more views in the stack' )
447+ } ;
448+ if ( this . __isTransitioning ) return ;
449+ const { transition} = options ;
450+ const [ prev , next ] = this . __viewIndexes ;
451+ const rootView = this . state . views [ 0 ] ;
452+ const topView = last ( this . state . views ) ;
453+ const mountedViews = [ ] ;
454+ mountedViews [ prev ] = topView ;
455+ mountedViews [ next ] = rootView ;
456+ console . log ( 'prev' , topView . props . index )
457+ console . log ( 'next' , rootView . props . index )
458+ // Display only the root view
459+ const views = [ rootView ] ;
460+ // Show the wrappers
461+ this . __displayViews ( 'block' ) ;
462+ // Pop from the top view, all the way to the root view
463+ this . setState ( {
464+ transition,
465+ views,
466+ mountedViews
467+ } , ( ) => {
468+ // The view that will be shown
469+ const rootView = this . refs [ `view-1` ] ;
470+ if ( rootView && this . state . preserveState ) {
471+ const state = this . __viewStates [ 0 ] ;
472+ // Rehydrate the state
473+ if ( state ) {
474+ rootView . setState ( state ) ;
475+ }
476+ }
477+ // Clear view states
478+ this . __viewStates . length = 0 ;
479+ // Transition
480+ this . __transitionViews ( options ) ;
481+ } ) ;
482+ this . __isTransitioning = true ;
483+ }
484+
430485 pushView ( ) {
431486 this . __pushView . apply ( this , arguments ) ;
432487 }
@@ -435,6 +490,10 @@ class NavigationController extends React.Component {
435490 this . __popView . apply ( this , arguments ) ;
436491 }
437492
493+ popToRootView ( ) {
494+ this . __popToRootView . apply ( this , arguments ) ;
495+ }
496+
438497 setViews ( ) {
439498 this . __setViews . apply ( this , arguments ) ;
440499 }
0 commit comments