2015-03-24 16:55:38 +00:00
|
|
|
/**
|
2015-03-26 07:41:30 +00:00
|
|
|
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
|
|
|
*
|
2015-07-24 19:58:12 +00:00
|
|
|
* Facebook, Inc. ("Facebook") owns all right, title and interest, including
|
2015-03-26 07:41:30 +00:00
|
|
|
* all intellectual property and other proprietary rights, in and to the React
|
2015-07-24 19:58:12 +00:00
|
|
|
* Native CustomComponents software (the "Software"). Subject to your
|
2015-03-26 07:41:30 +00:00
|
|
|
* compliance with these terms, you are hereby granted a non-exclusive,
|
|
|
|
* worldwide, royalty-free copyright license to (1) use and copy the Software;
|
|
|
|
* and (2) reproduce and distribute the Software as part of your own software
|
2015-07-24 19:58:12 +00:00
|
|
|
* ("Your Software"). Facebook reserves all rights not expressly granted to
|
2015-03-26 07:41:30 +00:00
|
|
|
* you in this license agreement.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE AND DOCUMENTATION, IF ANY, ARE PROVIDED "AS IS" AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES (INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE) ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL FACEBOOK OR ITS AFFILIATES, OFFICERS, DIRECTORS OR
|
|
|
|
* EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @providesModule NavigatorNavigationBar
|
2015-03-24 16:55:38 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('React');
|
2015-10-10 22:30:23 +00:00
|
|
|
var NavigatorNavigationBarStylesAndroid = require('NavigatorNavigationBarStylesAndroid');
|
|
|
|
var NavigatorNavigationBarStylesIOS = require('NavigatorNavigationBarStylesIOS');
|
|
|
|
var Platform = require('Platform');
|
2015-03-24 16:55:38 +00:00
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var View = require('View');
|
|
|
|
|
2015-12-15 23:38:43 +00:00
|
|
|
var guid = require('guid');
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
var { Map } = require('immutable');
|
|
|
|
|
2015-03-24 16:55:38 +00:00
|
|
|
var COMPONENT_NAMES = ['Title', 'LeftButton', 'RightButton'];
|
|
|
|
|
2015-10-10 22:30:23 +00:00
|
|
|
var NavigatorNavigationBarStyles = Platform.OS === 'android' ?
|
|
|
|
NavigatorNavigationBarStylesAndroid : NavigatorNavigationBarStylesIOS;
|
|
|
|
|
2015-03-24 16:55:38 +00:00
|
|
|
var navStatePresentedIndex = function(navState) {
|
|
|
|
if (navState.presentedIndex !== undefined) {
|
|
|
|
return navState.presentedIndex;
|
|
|
|
}
|
2015-03-30 22:35:04 +00:00
|
|
|
// TODO: rename `observedTopOfStack` to `presentedIndex` in `NavigatorIOS`
|
|
|
|
return navState.observedTopOfStack;
|
2015-03-24 16:55:38 +00:00
|
|
|
};
|
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class NavigatorNavigationBar extends React.Component {
|
|
|
|
static propTypes = {
|
2015-03-30 22:35:04 +00:00
|
|
|
navigator: React.PropTypes.object,
|
2015-04-01 14:50:51 +00:00
|
|
|
routeMapper: React.PropTypes.shape({
|
2015-03-30 22:35:04 +00:00
|
|
|
Title: React.PropTypes.func.isRequired,
|
|
|
|
LeftButton: React.PropTypes.func.isRequired,
|
|
|
|
RightButton: React.PropTypes.func.isRequired,
|
2015-09-22 21:41:01 +00:00
|
|
|
}).isRequired,
|
2015-03-30 22:35:04 +00:00
|
|
|
navState: React.PropTypes.shape({
|
|
|
|
routeStack: React.PropTypes.arrayOf(React.PropTypes.object),
|
|
|
|
presentedIndex: React.PropTypes.number,
|
|
|
|
}),
|
2015-10-10 22:30:23 +00:00
|
|
|
navigationStyles: React.PropTypes.object,
|
2015-04-01 14:50:51 +00:00
|
|
|
style: View.propTypes.style,
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-30 22:35:04 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
static Styles = NavigatorNavigationBarStyles;
|
|
|
|
static StylesAndroid = NavigatorNavigationBarStylesAndroid;
|
|
|
|
static StylesIOS = NavigatorNavigationBarStylesIOS;
|
2015-10-10 22:30:23 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
static defaultProps = {
|
|
|
|
navigationStyles: NavigatorNavigationBarStyles,
|
|
|
|
};
|
2015-03-24 16:55:38 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
componentWillMount() {
|
2015-12-15 23:38:43 +00:00
|
|
|
this._reset();
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-12-15 23:38:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop transtion, immediately resets the cached state and re-render the
|
|
|
|
* whole view.
|
|
|
|
*/
|
2016-07-26 08:00:02 +00:00
|
|
|
immediatelyRefresh = () => {
|
2015-12-15 23:38:43 +00:00
|
|
|
this._reset();
|
|
|
|
this.forceUpdate();
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-12-15 23:38:43 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_reset = () => {
|
2015-12-15 23:38:43 +00:00
|
|
|
this._key = guid();
|
|
|
|
this._reusableProps = {};
|
2015-07-17 22:20:04 +00:00
|
|
|
this._components = {};
|
|
|
|
this._descriptors = {};
|
|
|
|
|
|
|
|
COMPONENT_NAMES.forEach(componentName => {
|
|
|
|
this._components[componentName] = new Map();
|
|
|
|
this._descriptors[componentName] = new Map();
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-07-17 22:20:04 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_getReusableProps = (/*string*/componentName, /*number*/index) => /*object*/ {
|
2015-03-24 16:55:38 +00:00
|
|
|
var propStack = this._reusableProps[componentName];
|
|
|
|
if (!propStack) {
|
|
|
|
propStack = this._reusableProps[componentName] = [];
|
|
|
|
}
|
|
|
|
var props = propStack[index];
|
|
|
|
if (!props) {
|
|
|
|
props = propStack[index] = {style:{}};
|
|
|
|
}
|
|
|
|
return props;
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-24 16:55:38 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_updateIndexProgress = (
|
2015-03-24 16:55:38 +00:00
|
|
|
/*number*/progress,
|
|
|
|
/*number*/index,
|
|
|
|
/*number*/fromIndex,
|
2016-07-26 08:00:02 +00:00
|
|
|
/*number*/toIndex,
|
|
|
|
) => {
|
2015-03-24 16:55:38 +00:00
|
|
|
var amount = toIndex > fromIndex ? progress : (1 - progress);
|
|
|
|
var oldDistToCenter = index - fromIndex;
|
|
|
|
var newDistToCenter = index - toIndex;
|
|
|
|
var interpolate;
|
|
|
|
if (oldDistToCenter > 0 && newDistToCenter === 0 ||
|
|
|
|
newDistToCenter > 0 && oldDistToCenter === 0) {
|
2015-10-10 22:30:23 +00:00
|
|
|
interpolate = this.props.navigationStyles.Interpolators.RightToCenter;
|
2015-03-24 16:55:38 +00:00
|
|
|
} else if (oldDistToCenter < 0 && newDistToCenter === 0 ||
|
|
|
|
newDistToCenter < 0 && oldDistToCenter === 0) {
|
2015-10-10 22:30:23 +00:00
|
|
|
interpolate = this.props.navigationStyles.Interpolators.CenterToLeft;
|
2015-03-24 16:55:38 +00:00
|
|
|
} else if (oldDistToCenter === newDistToCenter) {
|
2015-10-10 22:30:23 +00:00
|
|
|
interpolate = this.props.navigationStyles.Interpolators.RightToCenter;
|
2015-03-24 16:55:38 +00:00
|
|
|
} else {
|
2015-10-10 22:30:23 +00:00
|
|
|
interpolate = this.props.navigationStyles.Interpolators.RightToLeft;
|
2015-03-24 16:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
COMPONENT_NAMES.forEach(function (componentName) {
|
2015-07-17 22:20:04 +00:00
|
|
|
var component = this._components[componentName].get(this.props.navState.routeStack[index]);
|
2015-03-24 16:55:38 +00:00
|
|
|
var props = this._getReusableProps(componentName, index);
|
|
|
|
if (component && interpolate[componentName](props.style, amount)) {
|
2016-04-05 18:32:15 +00:00
|
|
|
props.pointerEvents = props.style.opacity === 0 ? 'none' : 'box-none';
|
2015-03-24 16:55:38 +00:00
|
|
|
component.setNativeProps(props);
|
|
|
|
}
|
|
|
|
}, this);
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-24 16:55:38 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
updateProgress = (/*number*/progress, /*number*/fromIndex, /*number*/toIndex) => {
|
2015-03-24 16:55:38 +00:00
|
|
|
var max = Math.max(fromIndex, toIndex);
|
|
|
|
var min = Math.min(fromIndex, toIndex);
|
|
|
|
for (var index = min; index <= max; index++) {
|
|
|
|
this._updateIndexProgress(progress, index, fromIndex, toIndex);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
2015-03-24 16:55:38 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
render() {
|
2015-10-10 22:30:23 +00:00
|
|
|
var navBarStyle = {
|
|
|
|
height: this.props.navigationStyles.General.TotalNavHeight,
|
|
|
|
};
|
2015-03-24 16:55:38 +00:00
|
|
|
var navState = this.props.navState;
|
2015-12-15 20:01:27 +00:00
|
|
|
var components = navState.routeStack.map((route, index) =>
|
|
|
|
COMPONENT_NAMES.map(componentName =>
|
|
|
|
this._getComponent(componentName, route, index)
|
|
|
|
)
|
|
|
|
);
|
2015-03-24 16:55:38 +00:00
|
|
|
|
|
|
|
return (
|
2015-12-15 23:38:43 +00:00
|
|
|
<View
|
|
|
|
key={this._key}
|
|
|
|
style={[styles.navBarContainer, navBarStyle, this.props.style]}>
|
2015-03-24 16:55:38 +00:00
|
|
|
{components}
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-24 16:55:38 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
_getComponent = (/*string*/componentName, /*object*/route, /*number*/index) => /*?Object*/ {
|
2015-07-17 22:20:04 +00:00
|
|
|
if (this._descriptors[componentName].includes(route)) {
|
|
|
|
return this._descriptors[componentName].get(route);
|
2015-03-24 16:55:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
var rendered = null;
|
|
|
|
|
2015-04-01 14:50:51 +00:00
|
|
|
var content = this.props.routeMapper[componentName](
|
2015-07-17 22:20:04 +00:00
|
|
|
this.props.navState.routeStack[index],
|
2015-03-24 22:40:48 +00:00
|
|
|
this.props.navigator,
|
2015-03-24 16:55:38 +00:00
|
|
|
index,
|
|
|
|
this.props.navState
|
|
|
|
);
|
|
|
|
if (!content) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:32:15 +00:00
|
|
|
var componentIsActive = index === navStatePresentedIndex(this.props.navState);
|
|
|
|
var initialStage = componentIsActive ?
|
2015-10-10 22:30:23 +00:00
|
|
|
this.props.navigationStyles.Stages.Center :
|
|
|
|
this.props.navigationStyles.Stages.Left;
|
2015-07-17 22:20:04 +00:00
|
|
|
rendered = (
|
|
|
|
<View
|
|
|
|
ref={(ref) => {
|
|
|
|
this._components[componentName] = this._components[componentName].set(route, ref);
|
|
|
|
}}
|
2016-04-05 18:32:15 +00:00
|
|
|
pointerEvents={componentIsActive ? 'box-none' : 'none'}
|
2015-07-17 22:20:04 +00:00
|
|
|
style={initialStage[componentName]}>
|
|
|
|
{content}
|
|
|
|
</View>
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
this._descriptors[componentName] = this._descriptors[componentName].set(route, rendered);
|
|
|
|
return rendered;
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
}
|
2015-03-24 16:55:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
navBarContainer: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
2015-04-17 22:56:05 +00:00
|
|
|
right: 0,
|
2015-03-24 16:55:38 +00:00
|
|
|
backgroundColor: 'transparent',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
module.exports = NavigatorNavigationBar;
|