react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { NativeCommandsSender } from './adapters/NativeCommandsSender';
  2. import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
  3. import { UniqueIdProvider } from './adapters/UniqueIdProvider';
  4. import { Store } from './components/Store';
  5. import { ComponentRegistry } from './components/ComponentRegistry';
  6. import { Commands } from './commands/Commands';
  7. import { LayoutTreeParser } from './commands/LayoutTreeParser';
  8. import { LayoutTreeCrawler } from './commands/LayoutTreeCrawler';
  9. import { EventsRegistry } from './events/EventsRegistry';
  10. import { ComponentProvider } from 'react-native';
  11. import { Element } from './adapters/Element';
  12. import { ComponentEventsObserver } from './events/ComponentEventsObserver';
  13. import { CommandsObserver } from './events/CommandsObserver';
  14. import { Constants } from './adapters/Constants';
  15. import { ComponentType } from 'react';
  16. export class Navigation {
  17. public readonly Element: React.ComponentType<{ elementId: any; resizeMode: any; }>;
  18. public readonly store: Store;
  19. private readonly nativeEventsReceiver: NativeEventsReceiver;
  20. private readonly uniqueIdProvider: UniqueIdProvider;
  21. private readonly componentRegistry: ComponentRegistry;
  22. private readonly layoutTreeParser: LayoutTreeParser;
  23. private readonly layoutTreeCrawler: LayoutTreeCrawler;
  24. private readonly nativeCommandsSender: NativeCommandsSender;
  25. private readonly commands: Commands;
  26. private readonly eventsRegistry: EventsRegistry;
  27. private readonly commandsObserver: CommandsObserver;
  28. private readonly componentEventsObserver: ComponentEventsObserver;
  29. constructor() {
  30. this.Element = Element;
  31. this.store = new Store();
  32. this.nativeEventsReceiver = new NativeEventsReceiver();
  33. this.uniqueIdProvider = new UniqueIdProvider();
  34. this.componentRegistry = new ComponentRegistry(this.store);
  35. this.layoutTreeParser = new LayoutTreeParser();
  36. this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
  37. this.nativeCommandsSender = new NativeCommandsSender();
  38. this.commandsObserver = new CommandsObserver();
  39. this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler, this.commandsObserver, this.uniqueIdProvider);
  40. this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver, this.commandsObserver);
  41. this.componentEventsObserver = new ComponentEventsObserver(this.eventsRegistry, this.store);
  42. this.componentEventsObserver.registerForAllComponents();
  43. }
  44. /**
  45. * Every navigation component in your app must be registered with a unique name.
  46. * The component itself is a traditional React component extending React.Component.
  47. */
  48. public registerComponent(componentName: string, getComponentClassFunc: ComponentProvider): ComponentType<any> {
  49. return this.componentRegistry.registerComponent(componentName, getComponentClassFunc);
  50. }
  51. /**
  52. * Reset the app to a new layout
  53. */
  54. public setRoot(layout): Promise<any> {
  55. return this.commands.setRoot(layout);
  56. }
  57. /**
  58. * Set default options to all screens. Useful for declaring a consistent style across the app.
  59. */
  60. public setDefaultOptions(options): void {
  61. this.commands.setDefaultOptions(options);
  62. }
  63. /**
  64. * Change a component's navigation options
  65. */
  66. public mergeOptions(componentId: string, options): void {
  67. this.commands.mergeOptions(componentId, options);
  68. }
  69. /**
  70. * Show a screen as a modal.
  71. */
  72. public showModal(layout): Promise<any> {
  73. return this.commands.showModal(layout);
  74. }
  75. /**
  76. * Dismiss a modal by componentId. The dismissed modal can be anywhere in the stack.
  77. */
  78. public dismissModal(componentId: string): Promise<any> {
  79. return this.commands.dismissModal(componentId);
  80. }
  81. /**
  82. * Dismiss all Modals
  83. */
  84. public dismissAllModals(): Promise<any> {
  85. return this.commands.dismissAllModals();
  86. }
  87. /**
  88. * Push a new layout into this screen's navigation stack.
  89. */
  90. public push(componentId: string, layout): Promise<any> {
  91. return this.commands.push(componentId, layout);
  92. }
  93. /**
  94. * Pop a component from the stack, regardless of it's position.
  95. */
  96. public pop(componentId: string, params): Promise<any> {
  97. return this.commands.pop(componentId, params);
  98. }
  99. /**
  100. * Pop the stack to a given component
  101. */
  102. public popTo(componentId: string): Promise<any> {
  103. return this.commands.popTo(componentId);
  104. }
  105. /**
  106. * Pop the component's stack to root.
  107. */
  108. public popToRoot(componentId: string): Promise<any> {
  109. return this.commands.popToRoot(componentId);
  110. }
  111. /**
  112. * Sets new root component to stack.
  113. */
  114. public setStackRoot(componentId: string, layout): Promise<any> {
  115. return this.commands.setStackRoot(componentId, layout);
  116. }
  117. /**
  118. * Show overlay on top of the entire app
  119. */
  120. public showOverlay(layout): Promise<any> {
  121. return this.commands.showOverlay(layout);
  122. }
  123. /**
  124. * dismiss overlay by componentId
  125. */
  126. public dismissOverlay(componentId: string): Promise<any> {
  127. return this.commands.dismissOverlay(componentId);
  128. }
  129. /**
  130. *
  131. * Resolves arguments passed on launch
  132. */
  133. public getLaunchArgs(): Promise<any> {
  134. return this.commands.getLaunchArgs();
  135. }
  136. /**
  137. * Obtain the events registry instance
  138. */
  139. public events(): EventsRegistry {
  140. return this.eventsRegistry;
  141. }
  142. /**
  143. * Constants coming from native
  144. */
  145. public constants(): Constants {
  146. return Constants.get();
  147. }
  148. }