react-native-navigation的迁移库

Navigation.ts 5.1KB

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