react-native-navigation的迁移库

Navigation.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const NativeCommandsSender = require('./adapters/NativeCommandsSender');
  2. const NativeEventsReceiver = require('./adapters/NativeEventsReceiver');
  3. const UniqueIdProvider = require('./adapters/UniqueIdProvider');
  4. const Store = require('./containers/Store');
  5. const ContainerRegistry = require('./containers/ContainerRegistry');
  6. const Commands = require('./commands/Commands');
  7. const LayoutTreeParser = require('./commands/LayoutTreeParser');
  8. const LayoutTreeCrawler = require('./commands/LayoutTreeCrawler');
  9. const PrivateEventsListener = require('./events/PrivateEventsListener');
  10. const PublicEventsRegistry = require('./events/PublicEventsRegistry');
  11. const Element = require('./adapters/Element');
  12. const Root = require('./params/Root');
  13. const NavigationOptions = require('./params/NavigationOptions');
  14. /** @constructor */
  15. class Navigation {
  16. constructor() {
  17. this.store = new Store();
  18. this.nativeEventsReceiver = new NativeEventsReceiver();
  19. this.uniqueIdProvider = new UniqueIdProvider();
  20. this.containerRegistry = new ContainerRegistry(this.store);
  21. this.layoutTreeParser = new LayoutTreeParser();
  22. this.layoutTreeCrawler = new LayoutTreeCrawler(this.uniqueIdProvider, this.store);
  23. this.nativeCommandsSender = new NativeCommandsSender();
  24. this.commands = new Commands(this.nativeCommandsSender, this.layoutTreeParser, this.layoutTreeCrawler);
  25. this.publicEventsRegistry = new PublicEventsRegistry(this.nativeEventsReceiver);
  26. this.privateEventsListener = new PrivateEventsListener(this.nativeEventsReceiver, this.store);
  27. this.privateEventsListener.listenAndHandlePrivateEvents();
  28. this.Element = Element;
  29. }
  30. /**
  31. * Every screen component in your app must be registered with a unique name. The component itself is a traditional React component extending React.Component.
  32. * @param {string} containerName Unique container name
  33. * @param {function} getContainerFunc generator function, typically `() => require('./myContainer')`
  34. */
  35. registerContainer(containerName, getContainerFunc) {
  36. this.containerRegistry.registerContainer(containerName, getContainerFunc);
  37. }
  38. /**
  39. * Reset the navigation stack to a new screen (the stack root is changed).
  40. * @param {Root} root
  41. */
  42. setRoot(params) {
  43. return this.commands.setRoot(new Root(params));
  44. }
  45. /**
  46. * Set default options to all screens. Useful for declaring a consistent style across the app.
  47. * @param {NavigationOptions} options
  48. */
  49. setDefaultOptions(options) {
  50. this.commands.setDefaultOptions(options);
  51. }
  52. /**
  53. * Change a containers navigation options
  54. * @param {string} containerId The container's id.
  55. * @param {NavigationOptions} options
  56. */
  57. setOptions(containerId, options) {
  58. this.commands.setOptions(containerId, new NavigationOptions(options));
  59. }
  60. /**
  61. * Show a screen as a modal.
  62. * @param {Object} params
  63. */
  64. showModal(params) {
  65. return this.commands.showModal(params);
  66. }
  67. /**
  68. * Dismiss a modal by containerId. The dismissed modal can be anywhere in the stack.
  69. * @param {String} containerId The container's id.
  70. */
  71. dismissModal(containerId) {
  72. return this.commands.dismissModal(containerId);
  73. }
  74. /**
  75. * Dismiss all Modals
  76. */
  77. dismissAllModals() {
  78. return this.commands.dismissAllModals();
  79. }
  80. /**
  81. * Push a new screen into this screen's navigation stack.
  82. * @param {String} containerId The container's id.
  83. * @param {*} params
  84. */
  85. push(containerId, params) {
  86. return this.commands.push(containerId, params);
  87. }
  88. /**
  89. * Pop a container from the stack, regardless of it's position.
  90. * @param {String} containerId The container's id.
  91. * @param {*} params
  92. */
  93. pop(containerId, params) {
  94. return this.commands.pop(containerId, params);
  95. }
  96. /**
  97. * Pop the stack to a given container
  98. * @param {String} containerId The container's id.
  99. */
  100. popTo(containerId) {
  101. return this.commands.popTo(containerId);
  102. }
  103. /**
  104. * Pop the container's stack to root.
  105. * @param {*} containerId
  106. */
  107. popToRoot(containerId) {
  108. return this.commands.popToRoot(containerId);
  109. }
  110. /**
  111. * Obtain the events registery instance
  112. */
  113. events() {
  114. return this.publicEventsRegistry;
  115. }
  116. showOverlay(type, options) {
  117. return this.commands.showOverlay(type, options);
  118. }
  119. dismissOverlay() {
  120. return this.commands.dismissOverlay();
  121. }
  122. }
  123. const singleton = new Navigation();
  124. module.exports = singleton;