-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (200 loc) · 6.26 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
import React, { Component } from 'react'
import { View, StyleSheet, NativeModules, NativeEventEmitter, requireNativeComponent, UIManager, findNodeHandle, processColor, Platform, DeviceEventEmitter } from 'react-native'
import HTNavigationBar from './HTNavigationBar'
const HTRouteEventManagerEmitter = new NativeEventEmitter(NativeModules.HTRouteEventManager)
const HTRouteView = requireNativeComponent('HTRouteView')
const HTNaviveRouteManager = NativeModules.HTRouteManager
const globalValue = {
registerList: {},
count: 1000,
componentList: {}
}
const sureComponentItem = (id, defaultValue = {}) => {
let componentItem = globalValue.componentList[id]
if (componentItem == null) {
globalValue.componentList[id] = defaultValue
}
}
const encodeRouteData = (routeData, navigation) => {
let reloadRouteData = { ...routeData }
let componentName = reloadRouteData?.componentName
let componentPropList = reloadRouteData?.componentPropList ?? {}
let componentRouteOptionList = reloadRouteData?.componentRouteOptionList ?? {}
componentRouteOptionList = {
...HTRouteManager.defaultRouteOption({ ...routeData, navigation }),
...componentRouteOptionList,
}
if (typeof(componentName) == 'string' && !globalValue.registerList[componentName]) {
return null
}
let id = componentRouteOptionList['id']
if (id == null) {
globalValue.count += 1
id = `${globalValue.count}`
}
componentRouteOptionList['id'] = id
reloadRouteData['componentRouteOptionList'] = componentRouteOptionList
sureComponentItem(id, { componentPropList })
return reloadRouteData
}
const decodeRouteData = (props) => {
let reloadProps = { ...props }
let componentRouteOptionList = reloadProps?.componentRouteOptionList ?? {}
let id = componentRouteOptionList?.id
sureComponentItem(id)
let componentValueList = globalValue.componentList[id] ?? {}
let componentPropList = componentValueList['componentPropList'] ?? {}
reloadProps.componentPropList = componentPropList
return reloadProps
}
const routeListener = HTRouteEventManagerEmitter.addListener('onHTRouteEventChange', (value) => {
let id = value['id']
let actionName = value['actionName']
let componentRef = globalValue.componentList[id]?.ref
if (componentRef == null) {
return
}
DeviceEventEmitter.emit('onHTRouteEvent', { title: 'controller', value: value })
if (value?.actionName == 'dealloc') {
globalValue.componentList[id].ref = null
return
}
let componentFunction = componentRef[actionName]
if (componentFunction == null) {
return
}
componentFunction.call(componentRef, value)
})
const createNavigationWithRootRefFunction = (rootRefFunction, props) => {
const actionList = ['push', 'pop', 'navigate', 'replace', 'popToRoot', 'present', 'dismiss']
let navigation = {}
for (let action of actionList) {
navigation[action] = (componentName, componentPropList, componentRouteOptionList, animated) => {
let routeData = {
action,
componentName,
componentPropList,
componentRouteOptionList,
animated
}
if (typeof(routeData.componentName) == 'object') {
routeData.componentName = undefined
}
DeviceEventEmitter.emit('onHTRouteEvent', { title: 'navigation', value: routeData })
routeData = encodeRouteData(routeData, navigation)
if (!routeData) {
return
}
if (rootRefFunction == null) {
HTNaviveRouteManager.route(routeData)
} else {
UIManager.dispatchViewManagerCommand(
findNodeHandle(rootRefFunction()),
'touchRouteData',
[routeData]
)
}
}
}
navigation['goBack'] = navigation['pop']
navigation['back'] = navigation['pop']
navigation['popToTop'] = navigation['popToRoot']
navigation['createRouteData'] = (action, componentName, componentPropList, componentRouteOptionList, animated) => {
let routeData = {
action,
componentName,
componentPropList,
animated,
componentRouteOptionList,
}
routeData = encodeRouteData(routeData, navigation)
if (!routeData) {
return
}
return routeData
}
var componentPropList = decodeRouteData(props).componentPropList ?? {}
componentPropList = {...componentPropList}
navigation['state'] = { params: componentPropList }
navigation['getParam'] = (key, defaultValue) => {
let value = componentPropList[key]
return value ?? defaultValue
}
navigation['setParams'] = (keyValueList = {}) => {
Object.keys(keyValueList).map(key => {
componentPropList[key] = keyValueList[key]
})
}
return navigation
}
class HTRouteManager {
static defaultRouteOption = (props) => ({
backgroundColor: processColor('white'),
lazyRender: false,
})
static defaultRouteNavigationRender = (props) => {
return null
}
static defaultNavigation = createNavigationWithRootRefFunction(null)
static register(registerList) {
globalValue.registerList = registerList
}
static readRegisterFunction = (props) => {
let registerFunction = globalValue.registerList[props?.componentName]
if (registerFunction == null) {
registerFunction = () => {
return {}
}
}
return registerFunction
}
}
class HTRouteComponent extends Component {
constructor(props) {
super(props)
let navigation = createNavigationWithRootRefFunction(() => this.rootRef, props)
let originSetParams = navigation.setParams
navigation.setParams = (keyValueList) => {
originSetParams(keyValueList)
this.forceUpdate()
}
this.reloadProps = { ...props, navigation }
}
render() {
let bindRef = (ref) => {
if (ref?.renderWrappedComponent) {
return
}
let id = this.reloadProps.componentRouteOptionList['id']
let componentItem = globalValue.componentList[id]
if (componentItem) {
componentItem.ref = ref
globalValue.componentList[id] = componentItem
}
}
let ComponentClass = HTRouteManager.readRegisterFunction(this.reloadProps)()
let pointerEvents = this.reloadProps.componentRouteOptionList['pointerEvents'] ?? null
return (
<HTRouteView
style={{ flex: 1 }}
pointerEvents={ pointerEvents }
ref={ref => this.rootRef = ref}>
{
HTRouteManager.defaultRouteNavigationRender(this.reloadProps)
}
<ComponentClass
ref={bindRef}
getRef={bindRef}
navigation={this.reloadProps.navigation}
route={this.reloadProps.navigation['state']}
/>
</HTRouteView>
)
}
}
module.exports = {
HTRouteManager,
HTRouteComponent,
HTRouteView,
HTNavigationBar
}