This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRouter.wix.js
110 lines (92 loc) · 3.23 KB
/
Router.wix.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
import { Navigation, ScreenVisibilityListener } from 'react-native-navigation'
import platformSpecific from 'react-native-navigation/src/deprecated/platformSpecificDeprecated'
import { keys, findKey } from 'lodash'
import RouterBase from './RouterBase'
export default class TipsiRouter extends RouterBase {
constructor(initialRoute, routes) {
super(initialRoute, routes)
this.registerScreens(routes)
this.stackNavigationProvider()
this.registerScreenVisibilityListener()
this.routes = routes
this.title = ''
}
registerScreens = (routes) => {
keys(routes).forEach((key) => {
Navigation.registerComponent(key, () => routes[key].component)
})
}
registerScreenVisibilityListener() {
new ScreenVisibilityListener({
didAppear: event => this.parseVisibilityEvent(event),
}).register()
}
stackNavigationProvider = () => {
Navigation.startSingleScreenApp({
screen: {
screen: 'home', // unique ID registered with Navigation.registerScreen
title: 'Home', // title of the screen as appears in the nav bar (optional)
},
})
}
parseVisibilityEvent = async (event) => {
if (event.commandType === 'InitialScreen' || event.commandType === 'ShowModal') {
const { screenId } = await Navigation.getCurrentlyVisibleScreenId()
this[event.commandType === 'InitialScreen' ? 'navigatorID' : 'modalNavigatorID'] = screenId
}
}
getNavigatorID = () => this.modalNavigatorID || this.navigatorID
setTitle(title) {
super.setTitle(title)
platformSpecific.navigatorSetTitle({ navigatorID: this.getNavigatorID() }, { title })
}
updateTitle(title) {
this.setTitle(title)
}
push = (e, route, paramsOrOptions = {}) => {
const { config = {}, ...params } = paramsOrOptions
const { navigationBar = {} } = config
platformSpecific.navigatorPush({ navigatorID: this.getNavigatorID() }, {
screen: this.routeName(route),
title: navigationBar.title,
passProps: params,
})
}
pop = () => platformSpecific.navigatorPop({ navigatorID: this.getNavigatorID() }, {})
popToTop = () => platformSpecific.navigatorPopToRoot({ navigatorID: this.getNavigatorID() }, {})
replace(e, route, paramsOrOptions = {}) {
const { config = {}, ...params } = paramsOrOptions
const { navigationBar = {} } = config
platformSpecific.navigatorPop({ navigatorID: this.getNavigatorID() }, { animated: false })
platformSpecific.navigatorPush({ navigatorID: this.getNavigatorID() }, {
screen: this.routeName(route),
title: navigationBar.title,
animated: false,
passProps: params,
})
}
showModal = (e, route, paramsOrOptions) => {
const { config, ...params } = paramsOrOptions
const { navigationBar = {} } = config
Navigation.showModal({
screen: this.routeName(route),
title: navigationBar.title,
passProps: params,
navigatorButtons: {
leftButtons: [{
title: 'Cancel',
id: 'dismissModal',
}],
},
})
}
dismissModal = () => {
this.modalNavigatorID = null
Navigation.dismissModal()
}
/* eslint-disable-next-line */
redirect(url) {
window.location.replace(url)
}
routeName = route => findKey(this.routes, { path: route.path })
}