-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathNavigation.js
133 lines (110 loc) · 4.48 KB
/
Navigation.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
import { NativeModules, NativeEventEmitter } from 'react-native';
import React, { useEffect, useRef } from 'react';
import {AppRegistry} from 'react-native';
const { NavigatorModule } = NativeModules;
function processButton(button) {
button = Object.assign({}, button);
if (button.title && typeof(button.title) == "function") {
button.title = button.title();
}
return button;
}
var uniqueId = 1000;
class Navigator {
constructor(navigatorId, screenInstanceId) {
this.navigatorId = navigatorId;
this.screenInstanceId = screenInstanceId;
}
push(screenId, passProps) {
if ("id" in passProps || "navigator" in passProps ||
"navigatorId" in passProps || "screenInstanceId" in passProps) {
throw "props can't include id|navigator|navigatorId|screenInstanceId property";
}
var id = ++uniqueId;
var props = {id:id};
for (let k in passProps) {
let v = passProps[k];
if (typeof(v) == "string" || typeof(v) == "number" || typeof(v) == "boolean") {
props[k] = v;
}
}
Navigation.screenProps[id] = {component:screenId, passProps:passProps};
NavigatorModule.push(this.navigatorId, screenId, props);
}
pop() {
NavigatorModule.pop();
}
setTitle(title) {
NavigatorModule.setTitle(this.screenInstanceId, title);
}
}
export function Title(props) {
useEffect(() => {
Navigator.setTitle(props.screenInstanceId, props.title);
}, [props.title]);
return null;
}
var Navigation = {
screenProps:{},
screens:{},
eventEmitter:new NativeEventEmitter(NavigatorModule),
registerComponent: function(screenId, getComponentFunc) {
var NavigatorApp = function(props) {
const internalRef = useRef(null);
var screen = {};
if (props.id in Navigation.screenProps) {
//delete 为了避免内存泄漏
screen = Navigation.screenProps[props.id];
delete(Navigation.screenProps[props.id]);
} else if (props.id) {
console.warn("can't get screen props, screen id:", screenId, " instance id:", props.id);
}
useEffect(() => {
console.log("register screen:", props.screenInstanceId);
Navigation.screens[props.screenInstanceId] = internalRef.current;
return () => {
console.log("delete screen:", props.screenInstanceId);
delete(Navigation.screens[props.screenInstanceId]);
}
}, []);
var navigator = new Navigator(props.navigatorId, props.screenInstanceId);
var passProps = screen.passProps;
var screenProps = {...props, ...passProps};
delete(screenProps.id);
delete(screenProps.navigatorId);
delete(screenProps.screenInstanceId);
const InternalComponent = getComponentFunc();
var passProps = screen.passProps;
return (<InternalComponent ref={internalRef} navigator={navigator} {...screenProps}></InternalComponent>);
}
const InternalComponent = getComponentFunc();
if (InternalComponent.navigatorButtons) {
let navigatorButtons = Object.assign({}, InternalComponent.navigatorButtons);
if (navigatorButtons.leftButtons) {
navigatorButtons.leftButtons = navigatorButtons.leftButtons.map((button) => {
return processButton(button);
});
}
if(navigatorButtons.rightButtons) {
navigatorButtons.rightButtons = navigatorButtons.rightButtons.map((button) => {
return processButton(button);
});
}
this.registerNavigatorButtons(screenId, navigatorButtons);
}
AppRegistry.registerComponent(screenId, () => NavigatorApp);
},
registerNavigatorButtons: function(screenId, navigatorButtons) {
NavigatorModule.registerNavigatorButtons(screenId, navigatorButtons);
},
}
Navigation.eventEmitter.addListener('NavBarButtonPress', (event) => {
var screenInstanceId = event.screenInstanceId;
if (screenInstanceId in Navigation.screens) {
let screenRef = Navigation.screens[screenInstanceId];
if ("onNavigatorEvent" in screenRef) {
screenRef.onNavigatorEvent(event);
}
}
});
export default Navigation;