forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.ts
189 lines (168 loc) · 6.75 KB
/
Navigation.ts
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
import { isArray } from 'lodash';
import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
import { Store } from './components/Store';
import { ComponentRegistry } from './components/ComponentRegistry';
import { Commands } from './commands/Commands';
import { LayoutTreeParser } from './commands/LayoutTreeParser';
import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
import { EventsRegistry } from './events/EventsRegistry';
import { ComponentProvider } from 'react-native';
import { Element } from './adapters/Element';
import { CommandsObserver } from './events/CommandsObserver';
import { Constants } from './adapters/Constants';
import { ComponentEventsObserver } from './events/ComponentEventsObserver';
import { TouchablePreview } from './adapters/TouchablePreview';
import { LayoutRoot, Layout } from './interfaces/Layout';
import { Options } from './interfaces/Options';
import { ComponentWrapper } from './components/ComponentWrapper';
export class NavigationRoot {
public readonly Element: React.ComponentType<{ elementId: any; resizeMode?: any; }>;
public readonly TouchablePreview: React.ComponentType<any>;
public readonly store: Store;
private readonly nativeEventsReceiver: NativeEventsReceiver;
private readonly uniqueIdProvider: UniqueIdProvider;
private readonly componentRegistry: ComponentRegistry;
private readonly layoutTreeParser: LayoutTreeParser;
private readonly layoutTreeCrawler: LayoutTreeCrawler;
private readonly nativeCommandsSender: NativeCommandsSender;
private readonly commands: Commands;
private readonly eventsRegistry: EventsRegistry;
private readonly commandsObserver: CommandsObserver;
private readonly componentEventsObserver: ComponentEventsObserver;
private readonly componentWrapper: ComponentWrapper;
constructor() {
this.Element = Element;
this.TouchablePreview = TouchablePreview;
this.componentWrapper = new ComponentWrapper();
this.store = new Store();
this.nativeEventsReceiver = new NativeEventsReceiver();
this.uniqueIdProvider = new UniqueIdProvider();
this.componentEventsObserver = new ComponentEventsObserver(this.nativeEventsReceiver);
this.componentRegistry = new ComponentRegistry(this.store, this.componentEventsObserver);
this.layoutTreeParser = new LayoutTreeParser();
this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
this.nativeCommandsSender = new NativeCommandsSender();
this.commandsObserver = new CommandsObserver();
this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler, this.commandsObserver, this.uniqueIdProvider);
this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.commandsObserver, this.componentEventsObserver);
this.componentEventsObserver.registerOnceForAllComponentEvents();
}
/**
* Every navigation component in your app must be registered with a unique name.
* The component itself is a traditional React component extending React.Component.
*/
public registerComponent(componentName: string | number, componentProvider: ComponentProvider, concreteComponentProvider?: ComponentProvider): ComponentProvider {
return this.componentRegistry.registerComponent(componentName, componentProvider, this.componentWrapper, concreteComponentProvider);
}
/**
* Utility helper function like registerComponent,
* wraps the provided component with a react-redux Provider with the passed redux store
*/
public registerComponentWithRedux(
componentName: string | number,
getComponentClassFunc: ComponentProvider,
ReduxProvider: any,
reduxStore: any
): ComponentProvider {
return this.componentRegistry.registerComponent(componentName, getComponentClassFunc, this.componentWrapper, undefined, ReduxProvider, reduxStore);
}
/**
* Reset the app to a new layout
*/
public setRoot(layout: LayoutRoot): Promise<any> {
return this.commands.setRoot(layout);
}
/**
* Set default options to all screens. Useful for declaring a consistent style across the app.
*/
public setDefaultOptions(options: Options): void {
this.commands.setDefaultOptions(options);
}
/**
* Change a component's navigation options
*/
public mergeOptions(componentId: string, options: Options): void {
this.commands.mergeOptions(componentId, options);
}
/**
* Show a screen as a modal.
*/
public showModal(layout: Layout): Promise<any> {
return this.commands.showModal(layout);
}
/**
* Dismiss a modal by componentId. The dismissed modal can be anywhere in the stack.
*/
public dismissModal(componentId: string, mergeOptions?: Options): Promise<any> {
return this.commands.dismissModal(componentId, mergeOptions);
}
/**
* Dismiss all Modals
*/
public dismissAllModals(mergeOptions?: Options): Promise<any> {
return this.commands.dismissAllModals(mergeOptions);
}
/**
* Push a new layout into this screen's navigation stack.
*/
public push<P>(componentId: string, layout: Layout<P>): Promise<any> {
return this.commands.push(componentId, layout);
}
/**
* Pop a component from the stack, regardless of it's position.
*/
public pop(componentId: string, mergeOptions?: Options): Promise<any> {
return this.commands.pop(componentId, mergeOptions);
}
/**
* Pop the stack to a given component
*/
public popTo(componentId: string, mergeOptions?: Options): Promise<any> {
return this.commands.popTo(componentId, mergeOptions);
}
/**
* Pop the component's stack to root.
*/
public popToRoot(componentId: string, mergeOptions?: Options): Promise<any> {
return this.commands.popToRoot(componentId, mergeOptions);
}
/**
* Sets new root component to stack.
*/
public setStackRoot(componentId: string, layout: Layout | Layout[]): Promise<any> {
const children: Layout[] = isArray(layout) ? layout : [layout];
return this.commands.setStackRoot(componentId, children);
}
/**
* Show overlay on top of the entire app
*/
public showOverlay(layout: Layout): Promise<any> {
return this.commands.showOverlay(layout);
}
/**
* dismiss overlay by componentId
*/
public dismissOverlay(componentId: string): Promise<any> {
return this.commands.dismissOverlay(componentId);
}
/**
* Resolves arguments passed on launch
*/
public getLaunchArgs(): Promise<any> {
return this.commands.getLaunchArgs();
}
/**
* Obtain the events registry instance
*/
public events(): EventsRegistry {
return this.eventsRegistry;
}
/**
* Constants coming from native
*/
public async constants(): Promise<any> {
return await Constants.get();
}
}