react-native-navigation的迁移库

Navigation.ts 4.9KB

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