react-native-navigation的迁移库

Commands.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as _ from 'lodash';
  2. import { CommandsObserver } from '../events/CommandsObserver';
  3. import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
  4. export class Commands {
  5. constructor(
  6. private readonly nativeCommandsSender: NativeCommandsSender,
  7. private readonly layoutTreeParser,
  8. private readonly layoutTreeCrawler,
  9. private readonly commandsObserver: CommandsObserver) {
  10. }
  11. public setRoot(simpleApi) {
  12. const input = _.cloneDeep(simpleApi);
  13. const layout = this.layoutTreeParser.parse(input);
  14. this.layoutTreeCrawler.crawl(layout);
  15. const result = this.nativeCommandsSender.setRoot(layout);
  16. this.commandsObserver.notify('setRoot', { layout });
  17. return result;
  18. }
  19. public setDefaultOptions(options) {
  20. const input = _.cloneDeep(options);
  21. this.layoutTreeCrawler.processOptions(input);
  22. this.nativeCommandsSender.setDefaultOptions(input);
  23. this.commandsObserver.notify('setDefaultOptions', { options });
  24. }
  25. public mergeOptions(componentId, options) {
  26. const input = _.cloneDeep(options);
  27. this.layoutTreeCrawler.processOptions(input);
  28. this.nativeCommandsSender.mergeOptions(componentId, input);
  29. this.commandsObserver.notify('mergeOptions', { componentId, options });
  30. }
  31. public showModal(simpleApi) {
  32. const input = _.cloneDeep(simpleApi);
  33. const layout = this.layoutTreeParser.parse(input);
  34. this.layoutTreeCrawler.crawl(layout);
  35. const result = this.nativeCommandsSender.showModal(layout);
  36. this.commandsObserver.notify('showModal', { layout });
  37. return result;
  38. }
  39. public dismissModal(componentId) {
  40. const result = this.nativeCommandsSender.dismissModal(componentId);
  41. this.commandsObserver.notify('dismissModal', { componentId });
  42. return result;
  43. }
  44. public dismissAllModals() {
  45. const result = this.nativeCommandsSender.dismissAllModals();
  46. this.commandsObserver.notify('dismissAllModals', {});
  47. return result;
  48. }
  49. public push(componentId, simpleApi) {
  50. const input = _.cloneDeep(simpleApi);
  51. const layout = this.layoutTreeParser.parse(input);
  52. this.layoutTreeCrawler.crawl(layout);
  53. const result = this.nativeCommandsSender.push(componentId, layout);
  54. this.commandsObserver.notify('push', { componentId, layout });
  55. return result;
  56. }
  57. public pop(componentId, options) {
  58. const result = this.nativeCommandsSender.pop(componentId, options);
  59. this.commandsObserver.notify('pop', { componentId, options });
  60. return result;
  61. }
  62. public popTo(componentId) {
  63. const result = this.nativeCommandsSender.popTo(componentId);
  64. this.commandsObserver.notify('popTo', { componentId });
  65. return result;
  66. }
  67. public popToRoot(componentId) {
  68. const result = this.nativeCommandsSender.popToRoot(componentId);
  69. this.commandsObserver.notify('popToRoot', { componentId });
  70. return result;
  71. }
  72. public setStackRoot(componentId, simpleApi) {
  73. const input = _.cloneDeep(simpleApi);
  74. const layout = this.layoutTreeParser.parse(input);
  75. this.layoutTreeCrawler.crawl(layout);
  76. const result = this.nativeCommandsSender.setStackRoot(componentId, layout);
  77. this.commandsObserver.notify('setStackRoot', { componentId, layout });
  78. return result;
  79. }
  80. public showOverlay(simpleApi) {
  81. const input = _.cloneDeep(simpleApi);
  82. const layout = this.layoutTreeParser.parse(input);
  83. this.layoutTreeCrawler.crawl(layout);
  84. const result = this.nativeCommandsSender.showOverlay(layout);
  85. this.commandsObserver.notify('showOverlay', { layout });
  86. return result;
  87. }
  88. public dismissOverlay(componentId) {
  89. const result = this.nativeCommandsSender.dismissOverlay(componentId);
  90. this.commandsObserver.notify('dismissOverlay', { componentId });
  91. return result;
  92. }
  93. }