-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRouteNavigator.js
113 lines (99 loc) · 2.66 KB
/
RouteNavigator.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
/**
* Created by andrewhurst on 9/26/15.
*/
var React = require('react-native');
var { Navigator } = React;
function NavigationError (msg, route) {
this.message = msg;
this.type = 'Navigation';
this.routeName = route.name;
this.route = route;
return this;
}
NavigationError.prototype = Object.create(Error);
/**
* Navigates and manages a set of pages.
*/
class RouteNavigator extends React.Component {
render() {
return (
<Navigator
initialRouteStack={this.initialRouteStack}
configureScene={(raw) => {
return raw.animation || this.routeFor(raw).route.defaultAnimation;
}}
renderScene={(raw, nav) => {
var { body } = raw;
var { route, query }= this.routeFor(raw);
if ( !route )
throw new NavigationError('Failed to find route', route);
return this.getPage(
route,
nav,
raw,
ref => {
if ( !ref ) return;
ref.props = { ...(ref.props || {}), ...(route.props || {}) };
ref.setState({ query: query, body: body });
}
);
}}
/>
);
}
get router() {
if ( !this.props.router )
throw new Error('Route navigator must have a router.');
return this.props.router;
}
routeFor(raw) {
if ( typeof raw === 'string' )
return this.router.getRoute(raw);
var base = this.router.getRoute(raw.name || raw.URI);
for ( var k in raw )
base[k] = raw[k];
return base;
}
/**
* Gets the initial route stack.
* @returns {[]}
*/
get initialRouteStack() {
return this.props.initialRouteStack || [ this.props.initialRoute ];
}
/**
* Our Component map and cache, you should define your pages and props here.
* @returns {Object}
*/
get pages () {
if ( !this._pages ) this._pages = {};
return this._pages
}
getPage(route, nav, raw, refCallback) {
var { name, props, component } = route;
// Construct if needed
this.pages[name] = this.pages[name] || {};
var page = this.pages[name];
// Try to use cached page
if (page.cached && !props )
return page.cached;
// Construct Component
if (component) {
// Build Props
var _props = {
app: this.props.app,
nav,
ref: (c) => { page.ref = c; (refCallback || (()=>{}))(c); },
...(props),
...(raw.props || {}),
};
// Create element
var com = React.createElement( component, _props );
// Cache element
if (page.useCache)
page.cached = com;
}
return com;
}
}
module.exports = RouteNavigator;