Skip to content
This repository was archived by the owner on Apr 9, 2019. It is now read-only.

Commit aa0a6ad

Browse files
committed
Implement popToRootView
1 parent 75a0a40 commit aa0a6ad

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

examples/src/view.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class View extends React.Component {
4848
transition: Transition.type.COVER_UP
4949
});
5050
}
51+
onPopToRoot() {
52+
this.props.navigationController.popToRootView({
53+
transition: this.props.modal ? Transition.type.REVEAL_DOWN : Transition.type.PUSH_RIGHT
54+
});
55+
}
5156
render() {
5257
return (
5358
<div
@@ -65,6 +70,7 @@ class View extends React.Component {
6570
<button onClick={this.onModal.bind(this)}>
6671
Show Modal
6772
</button>
73+
{this.renderPopToRootButton()}
6874
</section>
6975
</div>
7076
);
@@ -80,6 +86,11 @@ class View extends React.Component {
8086
? <div />
8187
: <button onClick={this.onNext.bind(this)}>Next</button>;
8288
}
89+
renderPopToRootButton() {
90+
return this.props.index === 1
91+
? <div />
92+
: <button onClick={this.onPopToRoot.bind(this)}>Pop To Root</button>;
93+
}
8394
}
8495

8596
View.defaultProps ={

src/navigation-controller.jsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)