react-native-navigation的迁移库

Navigation.ts 4.3KB

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