This repository was archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathNavigatorNavigationBar.js
234 lines (207 loc) · 7.41 KB
/
NavigatorNavigationBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* 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.
*
*/
'use strict';
var React = require('react');
var NavigatorNavigationBarStylesAndroid = require('./NavigatorNavigationBarStylesAndroid');
var NavigatorNavigationBarStylesIOS = require('./NavigatorNavigationBarStylesIOS');
import {
Platform,
StyleSheet,
View,
ViewPropTypes,
} from 'react-native';
var PropTypes = require('prop-types');
var guid = require('./guid');
var { Map } = require('immutable');
var COMPONENT_NAMES = ['Title', 'LeftButton', 'RightButton'];
var NavigatorNavigationBarStyles = Platform.OS === 'android' ?
NavigatorNavigationBarStylesAndroid : NavigatorNavigationBarStylesIOS;
var navStatePresentedIndex = function(navState) {
if (navState.presentedIndex !== undefined) {
return navState.presentedIndex;
}
// TODO: rename `observedTopOfStack` to `presentedIndex` in `NavigatorIOS`
return navState.observedTopOfStack;
};
class NavigatorNavigationBar extends React.Component {
static propTypes = {
navigator: PropTypes.object,
routeMapper: PropTypes.shape({
Title: PropTypes.func.isRequired,
LeftButton: PropTypes.func.isRequired,
RightButton: PropTypes.func.isRequired,
}).isRequired,
navState: PropTypes.shape({
routeStack: PropTypes.arrayOf(PropTypes.object),
presentedIndex: PropTypes.number,
}),
navigationStyles: PropTypes.object,
style: ViewPropTypes.style,
};
static Styles = NavigatorNavigationBarStyles;
static StylesAndroid = NavigatorNavigationBarStylesAndroid;
static StylesIOS = NavigatorNavigationBarStylesIOS;
static defaultProps = {
navigationStyles: NavigatorNavigationBarStyles,
};
componentWillMount() {
this._reset();
}
/**
* Stop transtion, immediately resets the cached state and re-render the
* whole view.
*/
immediatelyRefresh = () => {
this._reset();
this.forceUpdate();
};
_reset = () => {
this._key = guid();
this._reusableProps = {};
this._components = {};
this._descriptors = {};
COMPONENT_NAMES.forEach(componentName => {
this._components[componentName] = new Map();
this._descriptors[componentName] = new Map();
});
};
_getReusableProps = (/*string*/componentName, /*number*/index) => /*object*/ {
var propStack = this._reusableProps[componentName];
if (!propStack) {
propStack = this._reusableProps[componentName] = [];
}
var props = propStack[index];
if (!props) {
props = propStack[index] = {style:{}};
}
return props;
};
_updateIndexProgress = (
/*number*/progress,
/*number*/index,
/*number*/fromIndex,
/*number*/toIndex,
) => {
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) {
interpolate = this.props.navigationStyles.Interpolators.RightToCenter;
} else if (oldDistToCenter < 0 && newDistToCenter === 0 ||
newDistToCenter < 0 && oldDistToCenter === 0) {
interpolate = this.props.navigationStyles.Interpolators.CenterToLeft;
} else if (oldDistToCenter === newDistToCenter) {
interpolate = this.props.navigationStyles.Interpolators.RightToCenter;
} else {
interpolate = this.props.navigationStyles.Interpolators.RightToLeft;
}
COMPONENT_NAMES.forEach(function (componentName) {
var component = this._components[componentName].get(this.props.navState.routeStack[index]);
var props = this._getReusableProps(componentName, index);
if (component && interpolate[componentName](props.style, amount)) {
props.pointerEvents = props.style.opacity === 0 ? 'none' : 'box-none';
component.setNativeProps(props);
}
}, this);
};
updateProgress = (/*number*/progress, /*number*/fromIndex, /*number*/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);
}
};
render() {
var navBarStyle = {
height: this.props.navigationStyles.General.TotalNavHeight,
};
var navState = this.props.navState;
var components = navState.routeStack.map((route, index) =>
COMPONENT_NAMES.map(componentName =>
this._getComponent(componentName, route, index)
)
);
return (
<View
key={this._key}
style={[
styles.navBarContainer,
!this.getShouldDisplay() && styles.navBarHidden,
navBarStyle,
this.props.style
]}
>
{components}
</View>
);
}
_getComponent = (/*string*/componentName, /*object*/route, /*number*/index) => /*?Object*/ {
if (this._descriptors[componentName].includes(route)) {
return this._descriptors[componentName].get(route);
}
var rendered = null;
var content = this.props.routeMapper[componentName](
this.props.navState.routeStack[index],
this.props.navigator,
index,
this.props.navState
);
if (!content) {
return null;
}
var componentIsActive = index === navStatePresentedIndex(this.props.navState);
var initialStage = componentIsActive ?
this.props.navigationStyles.Stages.Center :
this.props.navigationStyles.Stages.Left;
rendered = (
<View
ref={(ref) => {
this._components[componentName] = this._components[componentName].set(route, ref);
}}
pointerEvents={componentIsActive ? 'box-none' : 'none'}
style={initialStage[componentName]}>
{content}
</View>
);
this._descriptors[componentName] = this._descriptors[componentName].set(route, rendered);
return rendered;
};
}
var styles = StyleSheet.create({
navBarContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
backgroundColor: 'transparent',
},
navBarHidden: {
zIndex: -1,
},
});
module.exports = NavigatorNavigationBar;