react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as _ from 'lodash';
  2. import { CommandsObserver } from '../events/CommandsObserver';
  3. import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
  4. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  5. export class Commands {
  6. constructor(
  7. private readonly nativeCommandsSender: NativeCommandsSender,
  8. private readonly layoutTreeParser,
  9. private readonly layoutTreeCrawler,
  10. private readonly commandsObserver: CommandsObserver,
  11. private readonly uniqueIdProvider: UniqueIdProvider) {
  12. }
  13. public setRoot(simpleApi) {
  14. const input = _.cloneDeep(simpleApi);
  15. const root = this.layoutTreeParser.parse(input.root);
  16. this.layoutTreeCrawler.crawl(root);
  17. const modals = _.map(input.modals, (modal) => {
  18. const modalLayout = this.layoutTreeParser.parse(modal);
  19. this.layoutTreeCrawler.crawl(modalLayout);
  20. return modalLayout;
  21. });
  22. const overlays = _.map(input.overlays, (overlay) => {
  23. const overlayLayout = this.layoutTreeParser.parse(overlay);
  24. this.layoutTreeCrawler.crawl(overlayLayout);
  25. return overlayLayout;
  26. });
  27. const commandId = this.uniqueIdProvider.generate('setRoot');
  28. const result = this.nativeCommandsSender.setRoot(commandId, { root, modals, overlays });
  29. this.commandsObserver.notify('setRoot', { commandId, layout: { root, modals, overlays } });
  30. return result;
  31. }
  32. public setDefaultOptions(options) {
  33. const input = _.cloneDeep(options);
  34. this.layoutTreeCrawler.processOptions(input);
  35. this.nativeCommandsSender.setDefaultOptions(input);
  36. this.commandsObserver.notify('setDefaultOptions', { options });
  37. }
  38. public mergeOptions(componentId, options) {
  39. const input = _.cloneDeep(options);
  40. this.layoutTreeCrawler.processOptions(input);
  41. this.nativeCommandsSender.mergeOptions(componentId, input);
  42. this.commandsObserver.notify('mergeOptions', { componentId, options });
  43. }
  44. public showModal(simpleApi) {
  45. const input = _.cloneDeep(simpleApi);
  46. const layout = this.layoutTreeParser.parse(input);
  47. this.layoutTreeCrawler.crawl(layout);
  48. const commandId = this.uniqueIdProvider.generate('showModal');
  49. const result = this.nativeCommandsSender.showModal(commandId, layout);
  50. this.commandsObserver.notify('showModal', { commandId, layout });
  51. return result;
  52. }
  53. public dismissModal(componentId) {
  54. const commandId = this.uniqueIdProvider.generate('dismissModal');
  55. const result = this.nativeCommandsSender.dismissModal(commandId, componentId);
  56. this.commandsObserver.notify('dismissModal', { commandId, componentId });
  57. return result;
  58. }
  59. public dismissAllModals() {
  60. const commandId = this.uniqueIdProvider.generate('dismissAllModals');
  61. const result = this.nativeCommandsSender.dismissAllModals(commandId);
  62. this.commandsObserver.notify('dismissAllModals', { commandId });
  63. return result;
  64. }
  65. public push(componentId, simpleApi) {
  66. const input = _.cloneDeep(simpleApi);
  67. const layout = this.layoutTreeParser.parse(input);
  68. this.layoutTreeCrawler.crawl(layout);
  69. const commandId = this.uniqueIdProvider.generate('push');
  70. const result = this.nativeCommandsSender.push(commandId, componentId, layout);
  71. this.commandsObserver.notify('push', { commandId, componentId, layout });
  72. return result;
  73. }
  74. public pop(componentId, options) {
  75. const commandId = this.uniqueIdProvider.generate('pop');
  76. const result = this.nativeCommandsSender.pop(commandId, componentId, options);
  77. this.commandsObserver.notify('pop', { commandId, componentId, options });
  78. return result;
  79. }
  80. public popTo(componentId) {
  81. const commandId = this.uniqueIdProvider.generate('popTo');
  82. const result = this.nativeCommandsSender.popTo(commandId, componentId);
  83. this.commandsObserver.notify('popTo', { commandId, componentId });
  84. return result;
  85. }
  86. public popToRoot(componentId) {
  87. const commandId = this.uniqueIdProvider.generate('popToRoot');
  88. const result = this.nativeCommandsSender.popToRoot(commandId, componentId);
  89. this.commandsObserver.notify('popToRoot', { commandId, componentId });
  90. return result;
  91. }
  92. public setStackRoot(componentId, simpleApi) {
  93. const input = _.cloneDeep(simpleApi);
  94. const layout = this.layoutTreeParser.parse(input);
  95. this.layoutTreeCrawler.crawl(layout);
  96. const commandId = this.uniqueIdProvider.generate('setStackRoot');
  97. const result = this.nativeCommandsSender.setStackRoot(commandId, componentId, layout);
  98. this.commandsObserver.notify('setStackRoot', { commandId, componentId, layout });
  99. return result;
  100. }
  101. public showOverlay(simpleApi) {
  102. const input = _.cloneDeep(simpleApi);
  103. const layout = this.layoutTreeParser.parse(input);
  104. this.layoutTreeCrawler.crawl(layout);
  105. const commandId = this.uniqueIdProvider.generate('showOverlay');
  106. const result = this.nativeCommandsSender.showOverlay(commandId, layout);
  107. this.commandsObserver.notify('showOverlay', { commandId, layout });
  108. return result;
  109. }
  110. public dismissOverlay(componentId) {
  111. const commandId = this.uniqueIdProvider.generate('dismissOverlay');
  112. const result = this.nativeCommandsSender.dismissOverlay(commandId, componentId);
  113. this.commandsObserver.notify('dismissOverlay', { commandId, componentId });
  114. return result;
  115. }
  116. public getLaunchArgs() {
  117. const commandId = this.uniqueIdProvider.generate('getLaunchArgs');
  118. const result = this.nativeCommandsSender.getLaunchArgs(commandId);
  119. this.commandsObserver.notify('getLaunchArgs', { commandId });
  120. return result;
  121. }
  122. }