123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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';
- import { OptionsProcessor } from './commands/OptionsProcessor';
- import { ColorService } from './adapters/ColorService';
- import { AssetService } from './adapters/AssetResolver';
- import { AppRegistryService } from './adapters/AppRegistryService';
-
- 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);
- const appRegistryService = new AppRegistryService();
- this.componentRegistry = new ComponentRegistry(
- this.store,
- this.componentEventsObserver,
- this.componentWrapper,
- appRegistryService
- );
- this.layoutTreeParser = new LayoutTreeParser();
- const optionsProcessor = new OptionsProcessor(this.store, this.uniqueIdProvider, new ColorService(), new AssetService());
- this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store, optionsProcessor);
- this.nativeCommandsSender = new NativeCommandsSender();
- this.commandsObserver = new CommandsObserver(this.uniqueIdProvider);
- this.commands = new Commands(
- this.nativeCommandsSender,
- this.layoutTreeParser,
- this.layoutTreeCrawler,
- this.commandsObserver,
- this.uniqueIdProvider,
- optionsProcessor
- );
- 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, 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, 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();
- }
- }
|