-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.js
346 lines (292 loc) · 8.58 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import React, { Navigator, StyleSheet, View } from 'react-native';
import Animations from './src/Animations';
import { NavBar } from './src/NavBar';
import TabBar from './src/TabBar';
import * as actions from './src/actions';
import reducer from './src/reducer';
const actionTypes = actions.actionTypes;
const actionMap = {
push: actionTypes.ROUTER_PUSH,
replace: actionTypes.ROUTER_REPLACE,
reset: actionTypes.ROUTER_RESET,
};
class Schema extends React.Component {
className() {
return 'Schema';
}
render() {
return null;
}
}
class Route extends React.Component {
className() {
return 'Route';
}
render() {
return null;
}
}
class TabRoute extends React.Component {
className() {
return 'TabRoute';
}
render() {
return null;
}
}
class Router extends React.Component {
constructor(props) {
super(props);
const { actions = {}, dispatch } = props;
actions.routes = {};
this.routes = {};
this.schemas = {
default: {
actions: props.actions,
sceneConfig: Animations.FlatFloatFromRight,
},
};
this.schemas.tabs = Object.assign({}, this.schemas.default);
this.tabStyles = {};
this.initial = { name: props.initial };
React.Children.forEach(props.children, (child, index) => {
var name = child.props.name;
if (child.type.prototype.className() == 'Schema') {
this.schemas[name] = Object.assign({}, this.schemas[name], child.props);
if (name === 'default') {
this.schemas.tabs = Object.assign({}, this.schemas.tabs, {
footer: child.props.tabBar,
});
Object.keys(this.schemas).forEach(key => {
this.schemas[key] = Object.assign(
{}, this.schemas.default, this.schemas[key]);
});
}
} else if (child.type.prototype.className() == 'TabRoute') {
const tabBarName = child.props.name;
this.routes[tabBarName] = {};
this.tabStyles[tabBarName] = {
barTint: child.props.barTint,
tint: child.props.tint,
};
actions.routes[tabBarName] = {};
React.Children.forEach(child.props.children, (tabChild, tabIndex) => {
const tabName = tabChild.props.name;
this.routes[tabBarName][tabName] = tabChild.props;
this.routes[tabName] = tabChild.props;
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 }
});
};
});
} else if (child.type.prototype.className() == 'Route') {
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',
payload: { name, data },
});
};
this.routes[name] = child.props;
if (!child.props.component && !child.props.children) {
console.error('No route component is defined for name: ' + name);
return;
}
}
});
this.initialRoute = this.routes[this.initial.name]
|| console.error('No initial route ' + this.initial.name);
}
componentDidMount() {
this.props.actions.init(this.initial);
}
componentWillReceiveProps(nextProps) {
if (this.props.router.currentRoute !== nextProps.router.currentRoute) {
this.handleRouteChange(nextProps.router);
}
}
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);
let footer = currentRoute.footer;
if (footer) {
footer = React.addons.cloneWithProps(footer, {
navigator: this.refs.nav,
});
}
return (
<View style={styles.transparent}>
<Navigator
configureScene={(route) => route.sceneConfig}
initialRoute={this.getRoute(this.initialRoute, this.props.router)}
ref="nav"
renderScene={this.renderScene.bind(this)}
/>
{footer}
</View>
);
}
renderScene(route, navigator) {
var Component = route.component;
var navBar = route.navigationBar;
if (navBar) {
navBar = React.addons.cloneWithProps(navBar, {
navigator: navigator,
route: route,
router: this.props.router,
});
}
var child = null;
if (Component) {
child = (
<Component
key={route.name}
navigator={navigator}
route={route}
{...route.passProps}
/>
);
} else {
child = React.Children.only(this.routes[route.name].children);
child = React.addons.cloneWithProps(child, {schemas: this.schemas});
}
return (
<View style={styles.transparent}>
{navBar}
{child}
</View>
)
}
getRoute(routeProps, router = { data: {} }) {
const { data } = router;
if (!routeProps) {
return {};
}
var schema = this.schemas[routeProps.schema || 'default'] || {};
if (router.activeTabBar && this.schemas.tabs) {
schema = this.schemas.tabs
var tabs = this.routes[router.activeTabBar];
var tabStyles = this.tabStyles[router.activeTabBar];
}
var sceneConfig = routeProps.sceneConfig || schema.sceneConfig || Animations.None;
var NavBar = routeProps.navBar || schema.navBar;
var Footer = routeProps.footer || schema.footer;
var navBar;
if (NavBar) {
navBar = (
<NavBar {...schema} {...routeProps} {...data} />
);
}
var footer;
if (Footer){
footer = (
<Footer {...schema} {...routeProps} {...data}
activeTab={router.activeTab}
tabs={tabs}
tabStyles={tabStyles}
/>
);
}
var routerProps = Object.assign({}, this.props);
delete routerProps.children;
var props = Object.assign({}, routeProps, data, routerProps);
return {
component: routeProps.component,
footer: routeProps.hideFooter ? null : footer,
name: routeProps.name,
navigationBar: routeProps.hideNavBar ? null : navBar,
passProps: props,
sceneConfig: { ...sceneConfig },
}
}
handleRouteChange(router) {
const { data = {}, mode } = router;
if (mode === actionTypes.ROUTER_CHANGE_TAB) {
let routes = [];
if (router.routeStacks[router.activeTabBar][router.activeTab]) {
routes = router.routeStacks[router.activeTabBar][router.activeTab];
} else {
routes = router.routes.map(route => (
this.getRoute(this.routes[route], router)
));
}
this.refs.nav.immediatelyResetRouteStack(routes);
}
if (mode === actionTypes.ROUTER_POP) {
const num = data.num || 1;
const routes = this.refs.nav.getCurrentRoutes();
if (num < routes.length) {
this.refs.nav.popToRoute(routes[routes.length - 1 - num]);
} else {
this.refs.nav.popToTop();
}
}
if (mode === actionTypes.ROUTER_PUSH) {
this.refs.nav.push(this.getRoute(
this.routes[router.currentRoute], router
));
}
if (mode === actionTypes.ROUTER_REPLACE) {
this.refs.nav.replace(this.getRoute(
this.routes[router.currentRoute], router
));
}
if (mode === actionTypes.ROUTER_RESET) {
this.refs.nav.immediatelyResetRouteStack([
this.getRoute(this.routes[router.currentRoute], router)
]);
}
}
}
var styles = StyleSheet.create({
container: {
alignItems: 'center',
backgroundColor: 'transparent',
bottom: 0,
justifyContent: 'center',
left: 0,
position: 'absolute',
right: 0,
top: 0,
},
transparent: {
backgroundColor: 'transparent',
flex: 1,
},
});
module.exports = {
Animations,
NavBar,
Route,
Router,
Schema,
TabBar,
TabRoute,
actions,
reducer,
}