2015-03-24 16:55:38 +00:00
|
|
|
/**
|
2015-03-26 16:28:14 +00:00
|
|
|
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Facebook, Inc. (“Facebook”) owns all right, title and interest, including
|
|
|
|
* all intellectual property and other proprietary rights, in and to the React
|
|
|
|
* Native CustomComponents software (the “Software”). Subject to your
|
|
|
|
* 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
|
|
|
|
* (“Your Software”). Facebook reserves all rights not expressly granted to
|
|
|
|
* 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.
|
2015-03-24 16:55:38 +00:00
|
|
|
*
|
2015-03-26 07:41:30 +00:00
|
|
|
* @providesModule NavigatorBreadcrumbNavigationBar
|
2015-03-24 16:55:38 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
var NavigatorBreadcrumbNavigationBarStyles = require('NavigatorBreadcrumbNavigationBarStyles');
|
|
|
|
var NavigatorNavigationBarStyles = require('NavigatorNavigationBarStyles');
|
2015-03-24 16:55:38 +00:00
|
|
|
var React = require('React');
|
|
|
|
var StaticContainer = require('StaticContainer.react');
|
|
|
|
var StyleSheet = require('StyleSheet');
|
|
|
|
var View = require('View');
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
var { Map } = require('immutable');
|
|
|
|
|
2015-04-28 04:13:19 +00:00
|
|
|
var invariant = require('invariant');
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
var Interpolators = NavigatorBreadcrumbNavigationBarStyles.Interpolators;
|
2015-03-24 16:55:38 +00:00
|
|
|
var PropTypes = React.PropTypes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reusable props objects.
|
|
|
|
*/
|
|
|
|
var CRUMB_PROPS = Interpolators.map(() => {return {style: {}};});
|
|
|
|
var ICON_PROPS = Interpolators.map(() => {return {style: {}};});
|
|
|
|
var SEPARATOR_PROPS = Interpolators.map(() => {return {style: {}};});
|
|
|
|
var TITLE_PROPS = Interpolators.map(() => {return {style: {}};});
|
|
|
|
var RIGHT_BUTTON_PROPS = Interpolators.map(() => {return {style: {}};});
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The first route is initially rendered using a different style than all
|
|
|
|
* future routes.
|
|
|
|
*
|
|
|
|
* @param {number} index Index of breadcrumb.
|
|
|
|
* @return {object} Style config for initial rendering of index.
|
|
|
|
*/
|
|
|
|
var initStyle = function(index, presentedIndex) {
|
2015-03-26 07:41:30 +00:00
|
|
|
return index === presentedIndex ? NavigatorBreadcrumbNavigationBarStyles.Center[index] :
|
|
|
|
index < presentedIndex ? NavigatorBreadcrumbNavigationBarStyles.Left[index] :
|
|
|
|
NavigatorBreadcrumbNavigationBarStyles.Right[index];
|
2015-03-24 16:55:38 +00:00
|
|
|
};
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
var NavigatorBreadcrumbNavigationBar = React.createClass({
|
2015-03-24 16:55:38 +00:00
|
|
|
propTypes: {
|
2015-03-24 22:40:48 +00:00
|
|
|
navigator: PropTypes.shape({
|
2015-03-24 16:55:38 +00:00
|
|
|
push: PropTypes.func,
|
|
|
|
pop: PropTypes.func,
|
|
|
|
replace: PropTypes.func,
|
|
|
|
popToRoute: PropTypes.func,
|
|
|
|
popToTop: PropTypes.func,
|
|
|
|
}),
|
2015-04-01 14:50:51 +00:00
|
|
|
routeMapper: PropTypes.shape({
|
2015-03-24 16:55:38 +00:00
|
|
|
rightContentForRoute: PropTypes.func,
|
|
|
|
titleContentForRoute: PropTypes.func,
|
|
|
|
iconForRoute: PropTypes.func,
|
|
|
|
}),
|
2015-03-30 22:35:04 +00:00
|
|
|
navState: React.PropTypes.shape({
|
|
|
|
routeStack: React.PropTypes.arrayOf(React.PropTypes.object),
|
|
|
|
presentedIndex: React.PropTypes.number,
|
|
|
|
}),
|
2015-04-01 14:50:51 +00:00
|
|
|
style: View.propTypes.style,
|
2015-03-24 16:55:38 +00:00
|
|
|
},
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
statics: {
|
|
|
|
Styles: NavigatorBreadcrumbNavigationBarStyles,
|
|
|
|
},
|
|
|
|
|
2015-03-24 16:55:38 +00:00
|
|
|
_updateIndexProgress: function(progress, index, fromIndex, toIndex) {
|
|
|
|
var amount = toIndex > fromIndex ? progress : (1 - progress);
|
|
|
|
var oldDistToCenter = index - fromIndex;
|
|
|
|
var newDistToCenter = index - toIndex;
|
|
|
|
var interpolate;
|
2015-04-28 04:13:19 +00:00
|
|
|
invariant(
|
|
|
|
Interpolators[index],
|
|
|
|
'Cannot find breadcrumb interpolators for ' + index
|
|
|
|
);
|
2015-03-24 16:55:38 +00:00
|
|
|
if (oldDistToCenter > 0 && newDistToCenter === 0 ||
|
|
|
|
newDistToCenter > 0 && oldDistToCenter === 0) {
|
|
|
|
interpolate = Interpolators[index].RightToCenter;
|
|
|
|
} else if (oldDistToCenter < 0 && newDistToCenter === 0 ||
|
|
|
|
newDistToCenter < 0 && oldDistToCenter === 0) {
|
|
|
|
interpolate = Interpolators[index].CenterToLeft;
|
|
|
|
} else if (oldDistToCenter === newDistToCenter) {
|
|
|
|
interpolate = Interpolators[index].RightToCenter;
|
|
|
|
} else {
|
|
|
|
interpolate = Interpolators[index].RightToLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interpolate.Crumb(CRUMB_PROPS[index].style, amount)) {
|
|
|
|
this.refs['crumb_' + index].setNativeProps(CRUMB_PROPS[index]);
|
|
|
|
}
|
|
|
|
if (interpolate.Icon(ICON_PROPS[index].style, amount)) {
|
|
|
|
this.refs['icon_' + index].setNativeProps(ICON_PROPS[index]);
|
|
|
|
}
|
|
|
|
if (interpolate.Separator(SEPARATOR_PROPS[index].style, amount)) {
|
|
|
|
this.refs['separator_' + index].setNativeProps(SEPARATOR_PROPS[index]);
|
|
|
|
}
|
|
|
|
if (interpolate.Title(TITLE_PROPS[index].style, amount)) {
|
|
|
|
this.refs['title_' + index].setNativeProps(TITLE_PROPS[index]);
|
|
|
|
}
|
|
|
|
var right = this.refs['right_' + index];
|
|
|
|
if (right &&
|
|
|
|
interpolate.RightItem(RIGHT_BUTTON_PROPS[index].style, amount)) {
|
|
|
|
right.setNativeProps(RIGHT_BUTTON_PROPS[index]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateProgress: function(progress, fromIndex, toIndex) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-04-21 11:14:17 +00:00
|
|
|
onAnimationStart: function(fromIndex, toIndex) {
|
|
|
|
var max = Math.max(fromIndex, toIndex);
|
|
|
|
var min = Math.min(fromIndex, toIndex);
|
|
|
|
for (var index = min; index <= max; index++) {
|
|
|
|
this._setRenderViewsToHardwareTextureAndroid(index, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-04-28 04:13:19 +00:00
|
|
|
onAnimationEnd: function() {
|
|
|
|
var max = this.props.navState.routeStack.length - 1;
|
|
|
|
for (var index = 0; index <= max; index++) {
|
2015-04-21 11:14:17 +00:00
|
|
|
this._setRenderViewsToHardwareTextureAndroid(index, false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setRenderViewsToHardwareTextureAndroid: function(index, renderToHardwareTexture) {
|
|
|
|
var props = {
|
|
|
|
renderToHardwareTextureAndroid: renderToHardwareTexture,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.refs['icon_' + index].setNativeProps(props);
|
|
|
|
this.refs['separator_' + index].setNativeProps(props);
|
|
|
|
this.refs['title_' + index].setNativeProps(props);
|
|
|
|
var right = this.refs['right_' + index];
|
|
|
|
if (right) {
|
|
|
|
right.setNativeProps(props);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
componentWillMount: function() {
|
|
|
|
this._descriptors = {
|
|
|
|
crumb: new Map(),
|
|
|
|
title: new Map(),
|
|
|
|
right: new Map(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-24 16:55:38 +00:00
|
|
|
render: function() {
|
|
|
|
var navState = this.props.navState;
|
2015-07-17 22:20:04 +00:00
|
|
|
var icons = navState && navState.routeStack.map(this._getBreadcrumb);
|
|
|
|
var titles = navState.routeStack.map(this._getTitle);
|
|
|
|
var buttons = navState.routeStack.map(this._getRightButton);
|
2015-03-24 16:55:38 +00:00
|
|
|
return (
|
2015-04-01 14:50:51 +00:00
|
|
|
<View style={[styles.breadCrumbContainer, this.props.style]}>
|
2015-03-24 16:55:38 +00:00
|
|
|
{titles}
|
|
|
|
{icons}
|
|
|
|
{buttons}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
_getBreadcrumb: function(route, index) {
|
|
|
|
if (this._descriptors.crumb.has(route)) {
|
|
|
|
return this._descriptors.crumb.get(route);
|
2015-03-24 16:55:38 +00:00
|
|
|
}
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
var navBarRouteMapper = this.props.routeMapper;
|
2015-03-24 16:55:38 +00:00
|
|
|
var firstStyles = initStyle(index, navStatePresentedIndex(this.props.navState));
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
var breadcrumbDescriptor = (
|
|
|
|
<View ref={'crumb_' + index} style={firstStyles.Crumb}>
|
|
|
|
<View ref={'icon_' + index} style={firstStyles.Icon}>
|
|
|
|
{navBarRouteMapper.iconForRoute(route, this.props.navigator)}
|
2015-03-24 16:55:38 +00:00
|
|
|
</View>
|
2015-07-17 22:20:04 +00:00
|
|
|
<View ref={'separator_' + index} style={firstStyles.Separator}>
|
|
|
|
{navBarRouteMapper.separatorForRoute(route, this.props.navigator)}
|
|
|
|
</View>
|
|
|
|
</View>
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
this._descriptors.crumb = this._descriptors.crumb.set(route, breadcrumbDescriptor);
|
|
|
|
return breadcrumbDescriptor;
|
2015-03-24 16:55:38 +00:00
|
|
|
},
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
_getTitle: function(route, index) {
|
|
|
|
if (this._descriptors.title.has(route)) {
|
|
|
|
return this._descriptors.title.get(route);
|
2015-03-24 16:55:38 +00:00
|
|
|
}
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
var titleContent = this.props.routeMapper.titleContentForRoute(
|
|
|
|
this.props.navState.routeStack[index],
|
2015-03-24 22:40:48 +00:00
|
|
|
this.props.navigator
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
|
|
|
var firstStyles = initStyle(index, navStatePresentedIndex(this.props.navState));
|
2015-07-17 22:20:04 +00:00
|
|
|
|
|
|
|
var titleDescriptor = (
|
|
|
|
<View ref={'title_' + index} style={firstStyles.Title}>
|
|
|
|
{titleContent}
|
|
|
|
</View>
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
2015-07-17 22:20:04 +00:00
|
|
|
this._descriptors.title = this._descriptors.title.set(route, titleDescriptor);
|
|
|
|
return titleDescriptor;
|
2015-03-24 16:55:38 +00:00
|
|
|
},
|
|
|
|
|
2015-07-17 22:20:04 +00:00
|
|
|
_getRightButton: function(route, index) {
|
|
|
|
if (this._descriptors.right.has(route)) {
|
|
|
|
return this._descriptors.right.get(route);
|
2015-03-24 16:55:38 +00:00
|
|
|
}
|
2015-07-17 22:20:04 +00:00
|
|
|
var rightContent = this.props.routeMapper.rightContentForRoute(
|
|
|
|
this.props.navState.routeStack[index],
|
2015-03-24 22:40:48 +00:00
|
|
|
this.props.navigator
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
|
|
|
if (!rightContent) {
|
2015-07-17 22:20:04 +00:00
|
|
|
this._descriptors.right = this._descriptors.right.set(route, null);
|
2015-03-24 16:55:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var firstStyles = initStyle(index, navStatePresentedIndex(this.props.navState));
|
2015-07-17 22:20:04 +00:00
|
|
|
var rightButtonDescriptor = (
|
|
|
|
<View ref={'right_' + index} style={firstStyles.RightItem}>
|
|
|
|
{rightContent}
|
|
|
|
</View>
|
2015-03-24 16:55:38 +00:00
|
|
|
);
|
2015-07-17 22:20:04 +00:00
|
|
|
this._descriptors.right = this._descriptors.right.set(route, rightButtonDescriptor);
|
|
|
|
return rightButtonDescriptor;
|
2015-03-24 16:55:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
breadCrumbContainer: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
position: 'absolute',
|
2015-03-26 07:41:30 +00:00
|
|
|
height: NavigatorNavigationBarStyles.General.TotalNavHeight,
|
2015-03-24 16:55:38 +00:00
|
|
|
top: 0,
|
|
|
|
left: 0,
|
2015-04-17 22:56:05 +00:00
|
|
|
right: 0,
|
2015-03-24 16:55:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-03-26 07:41:30 +00:00
|
|
|
module.exports = NavigatorBreadcrumbNavigationBar;
|