2016-02-05 22:25:17 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule NavigationAnimatedView
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Animated = require('Animated');
|
|
|
|
var Map = require('Map');
|
2016-02-19 09:24:07 +00:00
|
|
|
var NavigationStateUtils = require('NavigationStateUtils');
|
2016-02-05 22:25:17 +00:00
|
|
|
var NavigationContainer = require('NavigationContainer');
|
2016-02-19 09:24:07 +00:00
|
|
|
var React = require('React');
|
2016-02-05 22:25:17 +00:00
|
|
|
var View = require('View');
|
|
|
|
|
|
|
|
import type {
|
|
|
|
NavigationState,
|
|
|
|
NavigationParentState,
|
2016-03-02 20:20:56 +00:00
|
|
|
NavigationScene,
|
2016-02-19 09:24:07 +00:00
|
|
|
} from 'NavigationStateUtils';
|
2016-02-05 22:25:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to compare route keys (e.g. "9", "11").
|
|
|
|
*/
|
|
|
|
function compareKey(one: string, two: string): number {
|
|
|
|
var delta = one.length - two.length;
|
|
|
|
if (delta > 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (delta < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return one > two ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to sort scenes based on their index and view key.
|
|
|
|
*/
|
|
|
|
function compareScenes(
|
|
|
|
one: NavigationScene,
|
|
|
|
two: NavigationScene
|
|
|
|
): number {
|
|
|
|
if (one.index > two.index) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (one.index < two.index) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareKey(
|
|
|
|
one.state.key,
|
|
|
|
two.state.key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type Layout = {
|
|
|
|
initWidth: number,
|
|
|
|
initHeight: number,
|
|
|
|
width: Animated.Value;
|
|
|
|
height: Animated.Value;
|
|
|
|
};
|
|
|
|
|
2016-03-02 20:20:56 +00:00
|
|
|
type OverlayRenderer = (
|
|
|
|
scenes: Array<NavigationScene>,
|
|
|
|
index: number,
|
|
|
|
position: Animated.Value,
|
|
|
|
layout: Layout
|
|
|
|
) => ReactElement;
|
|
|
|
|
2016-02-25 02:51:09 +00:00
|
|
|
type Position = Animated.Value;
|
|
|
|
|
2016-02-29 21:21:43 +00:00
|
|
|
/**
|
|
|
|
* Definition of the props object that is passed to the functions
|
|
|
|
* that render the overlay and the scene.
|
|
|
|
*/
|
|
|
|
type NavigationStateRendererProps = {
|
|
|
|
// The state of the child view.
|
|
|
|
navigationState: NavigationState,
|
|
|
|
// The index of the child view.
|
2016-02-05 22:25:17 +00:00
|
|
|
index: number,
|
2016-02-29 21:21:43 +00:00
|
|
|
// The "progressive index" of the containing navigation state.
|
2016-02-25 02:51:09 +00:00
|
|
|
position: Position,
|
2016-02-29 21:21:43 +00:00
|
|
|
// The layout of the the containing navigation view.
|
|
|
|
layout: Layout,
|
|
|
|
// The state of the the containing navigation view.
|
|
|
|
navigationParentState: NavigationParentState,
|
2016-03-02 02:44:58 +00:00
|
|
|
|
|
|
|
onNavigate: (action: any) => void,
|
2016-02-29 21:21:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type NavigationStateRenderer = (
|
|
|
|
props: NavigationStateRendererProps,
|
2016-02-23 00:15:43 +00:00
|
|
|
) => ReactElement;
|
|
|
|
|
|
|
|
type TimingSetter = (
|
|
|
|
position: Animated.Value,
|
|
|
|
newState: NavigationParentState,
|
2016-02-29 21:21:43 +00:00
|
|
|
lastState: NavigationParentState,
|
2016-02-23 00:15:43 +00:00
|
|
|
) => void;
|
2016-02-05 22:25:17 +00:00
|
|
|
|
|
|
|
type Props = {
|
2016-03-02 02:44:58 +00:00
|
|
|
navigationState: NavigationParentState,
|
|
|
|
onNavigate: (action: any) => void,
|
|
|
|
renderScene: NavigationStateRenderer,
|
|
|
|
renderOverlay: ?NavigationStateRenderer,
|
|
|
|
style: any,
|
|
|
|
setTiming: ?TimingSetter,
|
2016-02-05 22:25:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NavigationAnimatedView extends React.Component {
|
|
|
|
_animatedHeight: Animated.Value;
|
|
|
|
_animatedWidth: Animated.Value;
|
|
|
|
_lastHeight: number;
|
|
|
|
_lastWidth: number;
|
|
|
|
props: Props;
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-03-01 17:43:35 +00:00
|
|
|
this._lastWidth = 0;
|
|
|
|
this._lastHeight = 0;
|
|
|
|
this._animatedHeight = new Animated.Value(this._lastHeight);
|
|
|
|
this._animatedWidth = new Animated.Value(this._lastWidth);
|
|
|
|
|
2016-02-05 22:25:17 +00:00
|
|
|
this.state = {
|
|
|
|
position: new Animated.Value(this.props.navigationState.index),
|
|
|
|
scenes: new Map(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
componentWillMount() {
|
|
|
|
this.setState({
|
|
|
|
scenes: this._reduceScenes(this.state.scenes, this.props.navigationState),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
componentDidMount() {
|
|
|
|
this.postionListener = this.state.position.addListener(this._onProgressChange.bind(this));
|
|
|
|
}
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.navigationState !== this.props.navigationState) {
|
|
|
|
this.setState({
|
|
|
|
scenes: this._reduceScenes(this.state.scenes, nextProps.navigationState, this.props.navigationState),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
componentDidUpdate(lastProps) {
|
2016-02-23 00:15:43 +00:00
|
|
|
if (lastProps.navigationState.index !== this.props.navigationState.index && this.props.setTiming) {
|
|
|
|
this.props.setTiming(this.state.position, this.props.navigationState, lastProps.navigationState);
|
2016-02-05 22:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.postionListener) {
|
|
|
|
this.state.position.removeListener(this.postionListener);
|
|
|
|
this.postionListener = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_onProgressChange(data: Object): void {
|
|
|
|
if (Math.abs(data.value - this.props.navigationState.index) > Number.EPSILON) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.state.scenes.forEach((scene, index) => {
|
|
|
|
if (scene.isStale) {
|
|
|
|
const scenes = this.state.scenes.slice();
|
|
|
|
scenes.splice(index, 1);
|
|
|
|
this.setState({ scenes, });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_reduceScenes(
|
|
|
|
scenes: Array<NavigationScene>,
|
|
|
|
nextState: NavigationParentState,
|
|
|
|
lastState: ?NavigationParentState
|
|
|
|
): Array<NavigationScene> {
|
|
|
|
let nextScenes = nextState.children.map((child, index) => {
|
|
|
|
return {
|
|
|
|
index,
|
|
|
|
state: child,
|
|
|
|
isStale: false,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (lastState) {
|
|
|
|
lastState.children.forEach((child, index) => {
|
2016-02-23 00:15:44 +00:00
|
|
|
if (!NavigationStateUtils.get(nextState, child.key) && index !== nextState.index) {
|
2016-02-05 22:25:17 +00:00
|
|
|
nextScenes.push({
|
|
|
|
index,
|
|
|
|
state: child,
|
|
|
|
isStale: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
nextScenes = nextScenes.sort(compareScenes);
|
|
|
|
|
|
|
|
return nextScenes;
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
onLayout={(e) => {
|
|
|
|
const {height, width} = e.nativeEvent.layout;
|
|
|
|
this._animatedHeight &&
|
|
|
|
this._animatedHeight.setValue(height);
|
|
|
|
this._animatedWidth &&
|
|
|
|
this._animatedWidth.setValue(width);
|
|
|
|
this._lastHeight = height;
|
|
|
|
this._lastWidth = width;
|
|
|
|
}}
|
|
|
|
style={this.props.style}>
|
|
|
|
{this.state.scenes.map(this._renderScene, this)}
|
2016-02-29 21:21:43 +00:00
|
|
|
{this._renderOverlay()}
|
2016-02-05 22:25:17 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_getLayout() {
|
|
|
|
return {
|
|
|
|
height: this._animatedHeight,
|
|
|
|
width: this._animatedWidth,
|
|
|
|
initWidth: this._lastWidth,
|
|
|
|
initHeight: this._lastHeight,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_renderScene(scene: NavigationScene) {
|
2016-02-29 21:21:43 +00:00
|
|
|
return this.props.renderScene({
|
|
|
|
index: scene.index,
|
|
|
|
layout: this._getLayout(),
|
|
|
|
navigationParentState: this.props.navigationState,
|
|
|
|
navigationState: scene.state,
|
2016-03-02 02:44:58 +00:00
|
|
|
onNavigate: this.props.onNavigate,
|
2016-02-29 21:21:43 +00:00
|
|
|
position: this.state.position,
|
|
|
|
});
|
2016-02-05 22:25:17 +00:00
|
|
|
}
|
|
|
|
_renderOverlay() {
|
2016-03-02 02:44:58 +00:00
|
|
|
const {
|
|
|
|
onNavigate,
|
|
|
|
renderOverlay,
|
|
|
|
navigationState,
|
|
|
|
} = this.props;
|
2016-02-05 22:25:17 +00:00
|
|
|
if (renderOverlay) {
|
2016-02-29 21:21:43 +00:00
|
|
|
return renderOverlay({
|
2016-03-02 02:44:58 +00:00
|
|
|
index: navigationState.index,
|
2016-02-29 21:21:43 +00:00
|
|
|
layout: this._getLayout(),
|
2016-03-02 02:44:58 +00:00
|
|
|
navigationParentState: navigationState,
|
|
|
|
navigationState: navigationState.children[navigationState.index],
|
|
|
|
onNavigate: onNavigate,
|
2016-02-29 21:21:43 +00:00
|
|
|
position: this.state.position,
|
|
|
|
});
|
2016-02-05 22:25:17 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 20:20:56 +00:00
|
|
|
NavigationAnimatedView.propTypes = {
|
|
|
|
navigationState: React.PropTypes.instanceOf(NavigationStateUtils),
|
|
|
|
style: View.propTypes.style,
|
|
|
|
};
|
|
|
|
|
2016-02-23 00:15:43 +00:00
|
|
|
function setDefaultTiming(position, navigationState) {
|
|
|
|
Animated.spring(
|
|
|
|
position,
|
|
|
|
{
|
|
|
|
bounciness: 0,
|
|
|
|
toValue: navigationState.index,
|
|
|
|
}
|
|
|
|
).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
NavigationAnimatedView.defaultProps = {
|
|
|
|
setTiming: setDefaultTiming,
|
|
|
|
};
|
|
|
|
|
2016-02-05 22:25:17 +00:00
|
|
|
NavigationAnimatedView = NavigationContainer.create(NavigationAnimatedView);
|
|
|
|
|
|
|
|
module.exports = NavigationAnimatedView;
|