react-native-navigation的迁移库

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