This repository was archived by the owner on May 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathNavigatorNavigationBar.js
More file actions
226 lines (195 loc) · 6.29 KB
/
Copy pathNavigatorNavigationBar.js
File metadata and controls
226 lines (195 loc) · 6.29 KB
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
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* @providesModule ReactNavigatorNavigationBar
*/
'use strict';
import React, { Component, PropTypes } from 'react';
import NavigatorNavigationBarStylesAndroid from 'ReactNavigatorNavigationBarStylesAndroid';
import NavigatorNavigationBarStylesIOS from 'ReactNavigatorNavigationBarStylesIOS';
import Platform from 'ReactStyleSheet';
import StyleSheet from 'ReactStyleSheet';
import View from 'ReactView';
import Dimensions from 'ReactDimensions';
import { Map } from 'immutable';
import autobind from 'autobind-decorator';
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 Component {
constructor(props) {
super(props);
this._components = {};
this._descriptors = {};
COMPONENT_NAMES.forEach(componentName => {
this._components[componentName] = new Map();
this._descriptors[componentName] = new Map();
});
this._handleResize = this._handleResize.bind(this)
}
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: View.propTypes.style,
}
static statics = {
Styles: NavigatorNavigationBarStyles,
StylesAndroid: NavigatorNavigationBarStylesAndroid,
StylesIOS: NavigatorNavigationBarStylesIOS,
}
static defaultProps = {
navigationStyles: NavigatorNavigationBarStyles,
}
_getReusableProps(
/* string */componentName,
/* number */index
) /* object */ {
if (!this._reusableProps) {
this._reusableProps = {};
}
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)) {
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);
}
}
componentWillMount() {
window.addEventListener('resize', this._handleResize)
}
componentWillUnmount() {
window.removeEventListener('resize', this._handleResize)
clearTimeout(this._resizeTimeout)
}
_handleResize() {
const self = this
clearTimeout(this._resizeTimeout)
// debounce
this._resizeTimeout = setTimeout(function () {
self.setState({ dimensions: Dimensions.get('window') })
}, 100)
}
render() {
var navBarStyle = {
height: this.props.navigationStyles.General.TotalNavHeight,
};
var navState = this.props.navState;
var components = COMPONENT_NAMES.map(function(componentName) {
return navState.routeStack.map(
this._getComponent.bind(this, componentName)
);
}, this);
return (
<View style={[styles.navBarContainer, 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 initialStage = index === navStatePresentedIndex(this.props.navState) ?
this.props.navigationStyles.Stages.Center :
this.props.navigationStyles.Stages.Left;
rendered = (
<View
ref={(ref) => {
this._components[componentName] = this._components[componentName].set(route, ref);
}}
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',
},
});
autobind(NavigatorNavigationBar);
module.exports = NavigatorNavigationBar;