-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.js
284 lines (257 loc) · 9.33 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
import React from 'react';
import ReactNative from 'react-native';
const {View, Navigator, Text, TouchableWithoutFeedback, StyleSheet} = ReactNative;
import {Actions, CoreActions} from './actions';
import {INIT, PUSH, REPLACE, POP, DISMISS, RESET} from './actions';
import routerReducer from './reducer';
import Animations from './Animations';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
// schema class represents schema for routes and it is processed inside Router component
class Schema extends React.Component {
className(){
return "Schema";
}
render(){
return null;
}
}
// action class represents simple action which will be handled by user-defined stores
class Action extends React.Component {
className(){
return "Action";
}
render(){
return null;
}
}
// route class processed inside Router component
class Route extends React.Component {
className(){
return "Route";
}
render(){
return null;
}
}
class Router extends React.Component {
constructor(props){
super(props);
this.routes = {};
this.schemas = {};
var {dispatch} = props;
if (!dispatch){
throw new Error("No redux dispatch is provided to Router!");
}
var self = this;
this.initial = props.initial;
var RouterActions = props.actions || Actions;
React.Children.forEach(props.children, function (child, index){
var name = child.props.name;
if (!name){
throw new Error("Name is not defined for "+child.type.prototype.className())
}
if (child.type.prototype.className() == "Schema") {
self.schemas[name] = child.props;
} else if (child.type.prototype.className() == "Route") {
if (child.props.initial || !self.initial) {
self.initial = name;
}
if (!(RouterActions[name])) {
RouterActions[name] = function (data) {
if (typeof(data)!='object'){
data={data:data};
}
var args = {name: name, data:data};
var action = child.props.type || 'push';
dispatch(CoreActions[action](args));
};
}
self.routes[name] = 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() == "Action") {
//console.log("Added action: " + name);
if (!(RouterActions[name])) {
RouterActions[name] = function(data){
var props = self.extend({}, self.props);
props = self.extend(props, child.props);
dispatch(CoreActions.custom({name, props, data}));
};
}
}
});
let dispatched = bindActionCreators(CoreActions, dispatch);
for (var i in dispatched)
RouterActions[i] = dispatched[i];
this.routerActions = RouterActions;
this.initialRoute = this.routes[this.initial] || console.error("No initial route "+this.initial);
this.state = {initial: this.initial};
}
componentWillReceiveProps(props){
if (props.mode){
this.onChange(props)
}
}
onChange(page){
if (page.mode==PUSH || page.mode==REPLACE){
var route = this.routes[page.currentRoute];
if (!route){
console.error("No route is defined for name: "+page.currentRoute);
return;
}
// check if route is popup
if (route.schema=='popup'){
var element = React.createElement(route.component, Object.assign({}, route, page.data, {dispatch: this.props.dispatch, routes:this.routerActions}));
this.setState({modal: element});
} else {
if (page.mode == REPLACE){
this.refs.nav.replace(this.getRoute(route, page.data))
} else {
this.refs.nav.push(this.getRoute(route, page.data))
}
}
}
if (page.mode==POP){
var routes = this.refs.nav.getCurrentRoutes();
var num = page.num || (routes.length - page.routes.length);
// pop only existing routes!
if (num < routes.length) {
this.refs.nav.popToRoute(routes[routes.length - 1 - num]);
} else {
if (this.props.onExit){
this.props.onExit(routes[0], page.data || {});
}
}
}
if (page.mode==DISMISS) {
this.setState({modal: null});
}
if (page.mode==RESET){
// reset navigation stack
this.refs.nav.immediatelyResetRouteStack([this.getRoute(this.routes[page.initial], {})])
}
}
componentDidMount(){
this.props.dispatch(Actions.init(this.initial));
}
renderScene(route, navigator) {
var Component = route.component;
var navBar = route.navigationBar;
var footer = route.footer;
if (navBar) {
navBar = React.cloneElement(navBar, {
navigator: navigator,
route: route,
dispatch:this.props.dispatch,
routes:this.routerActions
});
}
if (footer){
footer = React.cloneElement(footer, {
navigator: navigator,
route: route,
dispatch:this.props.dispatch,
routes:this.routerActions
});
}
var child = null;
if (Component){
child = <Component key={route.name} navigator={navigator} route={route} {...route.passProps} routes={this.routerActions} dispatch={this.props.dispatch} />
} else {
child = React.Children.only(this.routes[route.name].children);
child = React.cloneElement(child, {schemas: this.schemas});
}
return (
<View style={styles.transparent}>
{navBar}
{child}
{footer}
</View>
)
}
extend(destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
}
getRoute(route, data) {
if (!route){
console.error("No route for data:"+JSON.stringify(data));
}
var schema = this.schemas[route.schema || 'default'] || {};
var sceneConfig = route.sceneConfig || schema.sceneConfig || Animations.None;
var NavBar = route.navBar || schema.navBar;
var Footer = route.footer || schema.footer;
var navBar;
if (NavBar){
navBar = <NavBar {...schema} {...route} {...data} routes={this.routerActions} dispatch={this.props.dispatch}/>
}
var footer;
if (Footer){
footer = <Footer {...schema} {...route} {...data} routes={this.routerActions} dispatch={this.props.dispatch} />
}
var props = this.extend({}, route);
props = this.extend(props, data);
return {
name: route.name,
component: route.component,
sceneConfig: {
...sceneConfig,
},
navigationBar: route.hideNavBar ? null : navBar,
footer: route.hideFooter ? null : footer,
passProps: props
}
}
render(){
if (!(this.props.initial || this.initial)){
console.error("No initial attribute!");
}
this.initialRoute = this.routes[this.props.initial || this.initial];
if (!this.initialRoute) {
console.error("No initial route!");
}
var modal = null;
if (this.state.modal){
modal = (<View style={styles.container}>
<TouchableWithoutFeedback onPress={()=>Actions.dismiss()}><View style={[styles.container,{backgroundColor:'black',opacity:0.5},this.props.popupStyle]}/></TouchableWithoutFeedback>
{this.state.modal}
</View>
);
}
return (
<View style={styles.transparent}>
<Navigator
renderScene={this.renderScene.bind(this)}
configureScene={(route) => { return route.sceneConfig;}}
ref="nav"
initialRoute={this.getRoute(this.initialRoute)}
/>
{modal}
</View>
);
}
}
var styles = StyleSheet.create({
container: {
position: 'absolute',
top:0,
bottom:0,
left:0,
right:0,
backgroundColor:'transparent',
justifyContent: 'center',
alignItems: 'center',
},
transparent: {
flex:1,
backgroundColor: "transparent"
}
});
module.exports = {Router: connect(state=>state.routerReducer)(Router), Actions, Action, Route, Animations, Schema, routerReducer}