-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·273 lines (252 loc) · 8.18 KB
/
index.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React, { Component } from 'react';
import { Navigator, View, Text, BackAndroid} from 'react-native';
import Container from './src/container';
import Navbar from './src/navbar';
import Tabbar from './src/tabbar';
import * as actions from './src/actions';
import reducer from './src/reducer';
const emptyFn = function(){};
const actionTypes = actions.actionTypes;
const actionMap = {
push: actionTypes.ROUTER_PUSH,
replace: actionTypes.ROUTER_REPLACE,
reset: actionTypes.ROUTER_RESET,
};
const schemaDefault = {
sceneConfig: Navigator.SceneConfigs.FloatFromRight,
container: Container
}
class Route extends Component {
className() {
return 'Route';
}
render() {
return null;
}
}
class TabRoute extends React.Component {
className() {
return 'TabRoute';
}
render() {
return null;
}
}
class Router extends Component {
constructor(props) {
super(props);
const { actions = {}, dispatch, onDidFocus = emptyFn, onWillFocus = emptyFn} = this.props;
actions.routes = {};
this.routes = {};
this.initial = { name: this.props.initial };
React.Children.forEach(props.children, (child, index) => {
let name = child.props.name;
if (child.type.prototype.className() === 'Route') {
//if props.initial is undefined in Router, it can be defined in Route's props
if (child.props.initial && !this.initial.name) {
this.initial.name = name;
}
actions.routes[name] = (data = {}) => e => {
if (typeof(data) !== 'object') {
data = { data }
}
dispatch({
type: actionMap[data.type || child.props.type] || 'ROUTER_PUSH', //default actionTypes is push
payload: { name:name, data },
});
};
this.routes[name] = Object.assign({}, {onDidFocus}, {onWillFocus}, child.props);
if (!child.props.component && !child.props.children) {
console.error('No route component is defined for name: ' + name);
return;
}
}
else if (child.type.prototype.className() == 'TabRoute') {
let {name, chidren, onDidFocus: onTabDidFocus = emptyFn, onWillFocus:onTabWillFocus = emptyFn, ...passProps} = child.props;
const tabBarName = child.props.name;
this.routes[tabBarName] = {passProps};
actions.routes[tabBarName] = {};
React.Children.forEach(child.props.children, (tabChild, tabIndex) => {
const tabName = tabChild.props.name;
let newprops = Object.assign({}, {onDidFocus}, {onDidFocus: onTabDidFocus}, {onWillFocus}, {onWillFocus: onTabWillFocus}, tabChild.props);
this.routes[tabBarName][tabName] = newprops;
this.routes[tabName] = Object.assign({}, newprops);
this.routes[tabName].tabBarName=tabBarName;
if (tabChild.props.initial || !this.initial.name) {
this.initial.name = tabName;
this.initial.tabBarName = tabBarName;
}
if (props.initial === tabName) {
this.initial.tabBarName = tabBarName;
}
actions.routes[tabBarName][tabName] = (data = {}) => e => {
if (typeof(data) !== 'object') {
data = { data }
}
dispatch({
type: actionMap[data.type || tabChild.props.type] || 'ROUTER_PUSH',
payload: { name: tabName, tabBarName, data }
});
};
});
}
});
this.initialRoute = this.routes[this.initial.name]
|| console.error('No initial route ' + this.initial.name);
}
componentDidMount() {
this.props.actions.initRouter(this.initial);
BackAndroid.addEventListener('hardwareBackPress', this.handleBackButton.bind(this));
}
componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress', this.handleBackButton.bind(this));
}
componentWillReceiveProps(nextProps) {
if (this.props.router.currentRoute !== nextProps.router.currentRoute) {
this.handleRouteChange(nextProps.router);
}
}
handleBackButton() {
let routes=this.nav.getCurrentRoutes();
if (routes.length>1) {
return this.props.actions.popRouter();
}
return false;
}
render() {
if (!(this.props.initial || this.initial)) {
console.error('No initial attribute!');
}
this.initialRoute = this.routes[this.props.initial || this.initial.name];
if (!this.initialRoute) {
console.error('No initial route!');
}
const currentRoute = this.getRoute(
this.routes[this.props.router.currentRoute],
this.props.router
);
return (
<View style={{flex:1}}>
<Navigator
configureScene={(route) => route.sceneConfig}
initialRoute={this.getRoute(this.initialRoute, this.props.router)}
renderScene={this.renderScene.bind(this)}
ref={(nav) => this.nav = nav}
onDidFocus = {this.onDidFocus.bind(this)}
onWillFocus = {this.onWillFocus.bind(this)}
/>
</View>
);
}
onDidFocus(route){
this.routes[route.name].onDidFocus && this.routes[route.name].onDidFocus(route);
}
onWillFocus(route){
this.routes[route.name].onWillFocus && this.routes[route.name].onWillFocus(route);
}
renderScene(route, navigator) {
let Component = route.component;
let props = Object.assign({}, this.props);
let actions = props.actions;
let componentProps = this.routes[route.name];
delete props.children;
delete props.initial;
let child = null;
let that=this;
if (Component) {
child = (
<Component
key={route.name}
{...props}
{...route.passProps}
/>
);
} else {
child = React.Children.only(componentProps.children);
child = React.cloneElement(child);
}
function _renderNavbar(){
if(componentProps.navbar===true){
return <Navbar {...componentProps} {...props}/>
}
else if(componentProps.navbar){
let Nav = componentProps.navbar;
return <Nav {...componentProps} {...props}/>
}
else{
return null;
}
}
function _renderTabbar(){
let tabBarRoutes;
if(componentProps.tabBarName) {
tabBarRoutes = that.routes[componentProps.tabBarName];
if (tabBarRoutes.visible===false){
return null;
}
else{
let TabComponent = tabBarRoutes.passProps.component ? tabBarRoutes.passProps.component : Tabbar;
return <TabComponent tabs={that.routes[componentProps.tabBarName]} selectedTab={route.name} {...props} {...that.routes[componentProps.tabBarName].passProps}/>
}
} else {
return null;
}
}
return (
<Container navbar={componentProps.navbar} style={componentProps.containerStyle || {}}>
{_renderNavbar()}
{child}
{_renderTabbar()}
</Container>
);
}
getRoute(routeProps, router = { data: {} }) {
const { data = {} } = router;
if (!routeProps) {
return {};
}
let sceneConfig = routeProps.sceneConfig ? (typeof routeProps.sceneConfig==='string' ? Navigator.SceneConfigs[routeProps.sceneConfig] : routeProps.sceneConfig) : schemaDefault.sceneConfig;
return {
component: routeProps.component,
name: routeProps.name,
passProps: { routerData: data },
sceneConfig: { ...sceneConfig },
}
}
handleRouteChange(router) {
const { data = {}, mode } = router;
if (mode ===actionTypes.ROUTER_POP) {
const num = data.num || 1;
const routes = this.nav.getCurrentRoutes();
if (num < routes.length) {
this.nav.popToRoute(routes[routes.length - 1 - num]);
} else {
this.nav.popToTop();
}
}
if (mode === actionTypes.ROUTER_PUSH) {
this.nav.push(this.getRoute(
this.routes[router.currentRoute], router
));
}
if (mode === actionTypes.ROUTER_REPLACE) {
this.nav.replace(this.getRoute(
this.routes[router.currentRoute], router
));
}
if (mode === actionTypes.ROUTER_RESET) {
this.nav.immediatelyResetRouteStack([
this.getRoute(this.routes[router.currentRoute], router)
]);
}
}
}
module.exports = {
Router,
Route,
TabRoute,
Navbar,
Container,
actions,
reducer,
};