react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*eslint-disable*/
  2. import React, {Component} from 'react';
  3. import {
  4. NativeAppEventEmitter,
  5. DeviceEventEmitter,
  6. Platform
  7. } from 'react-native';
  8. import platformSpecific from './deprecated/platformSpecificDeprecated';
  9. import Navigation from './Navigation';
  10. const _allNavigatorEventHandlers = {};
  11. const NavigationSpecific = {
  12. push: platformSpecific.navigatorPush,
  13. pop: platformSpecific.navigatorPop,
  14. popToRoot: platformSpecific.navigatorPopToRoot,
  15. resetTo: platformSpecific.navigatorResetTo
  16. };
  17. class Navigator {
  18. constructor(navigatorID, navigatorEventID, screenInstanceID) {
  19. this.navigatorID = navigatorID;
  20. this.screenInstanceID = screenInstanceID;
  21. this.navigatorEventID = navigatorEventID;
  22. this.navigatorEventHandler = null;
  23. this.navigatorEventSubscription = null;
  24. }
  25. push(params = {}) {
  26. return NavigationSpecific.push(this, params);
  27. }
  28. pop(params = {}) {
  29. return NavigationSpecific.pop(this, params);
  30. }
  31. popToRoot(params = {}) {
  32. return NavigationSpecific.popToRoot(this, params);
  33. }
  34. resetTo(params = {}) {
  35. return NavigationSpecific.resetTo(this, params);
  36. }
  37. showModal(params = {}) {
  38. return Navigation.showModal(params);
  39. }
  40. dismissModal(params = {}) {
  41. return Navigation.dismissModal(params);
  42. }
  43. dismissAllModals(params = {}) {
  44. return Navigation.dismissAllModals(params);
  45. }
  46. showLightBox(params = {}) {
  47. return Navigation.showLightBox(params);
  48. }
  49. dismissLightBox(params = {}) {
  50. return Navigation.dismissLightBox(params);
  51. }
  52. showInAppNotification(params = {}) {
  53. return Navigation.showInAppNotification(params);
  54. }
  55. dismissInAppNotification(params = {}) {
  56. return Navigation.dismissInAppNotification(params);
  57. }
  58. setButtons(params = {}) {
  59. return platformSpecific.navigatorSetButtons(this, this.navigatorEventID, params);
  60. }
  61. setTitle(params = {}) {
  62. return platformSpecific.navigatorSetTitle(this, params);
  63. }
  64. setSubTitle(params = {}) {
  65. return platformSpecific.navigatorSetSubtitle(this, params);
  66. }
  67. setTitleImage(params = {}) {
  68. return platformSpecific.navigatorSetTitleImage(this, params);
  69. }
  70. toggleDrawer(params = {}) {
  71. return platformSpecific.navigatorToggleDrawer(this, params);
  72. }
  73. toggleTabs(params = {}) {
  74. return platformSpecific.navigatorToggleTabs(this, params);
  75. }
  76. toggleNavBar(params = {}) {
  77. return platformSpecific.navigatorToggleNavBar(this, params);
  78. }
  79. setTabBadge(params = {}) {
  80. return platformSpecific.navigatorSetTabBadge(this, params);
  81. }
  82. switchToTab(params = {}) {
  83. return platformSpecific.navigatorSwitchToTab(this, params);
  84. }
  85. showSnackbar(params = {}) {
  86. return platformSpecific.showSnackbar(this, params);
  87. }
  88. showContextualMenu(params, onButtonPressed) {
  89. return platformSpecific.showContextualMenu(this, params, onButtonPressed);
  90. }
  91. dismissContextualMenu() {
  92. return platformSpecific.dismissContextualMenu();
  93. }
  94. setOnNavigatorEvent(callback) {
  95. this.navigatorEventHandler = callback;
  96. if (!this.navigatorEventSubscription) {
  97. let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
  98. this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
  99. _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
  100. }
  101. }
  102. handleDeepLink(params = {}) {
  103. if (!params.link) return;
  104. const event = {
  105. type: 'DeepLink',
  106. link: params.link
  107. };
  108. for (let i in _allNavigatorEventHandlers) {
  109. _allNavigatorEventHandlers[i](event);
  110. }
  111. }
  112. onNavigatorEvent(event) {
  113. if (this.navigatorEventHandler) {
  114. this.navigatorEventHandler(event);
  115. }
  116. }
  117. cleanup() {
  118. if (this.navigatorEventSubscription) {
  119. this.navigatorEventSubscription.remove();
  120. delete _allNavigatorEventHandlers[this.navigatorEventID];
  121. }
  122. }
  123. }
  124. export default class Screen extends Component {
  125. static navigatorStyle = {};
  126. static navigatorButtons = {};
  127. constructor(props) {
  128. super(props);
  129. if (props.navigatorID) {
  130. this.navigator = new Navigator(props.navigatorID, props.navigatorEventID, props.screenInstanceID);
  131. }
  132. }
  133. componentWillUnmount() {
  134. if (this.navigator) {
  135. this.navigator.cleanup();
  136. this.navigator = undefined;
  137. }
  138. }
  139. }