react-native-navigation的迁移库

LayoutTreeParser.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as _ from 'lodash';
  2. import { LayoutType } from './LayoutType';
  3. import { LayoutNode } from './LayoutTreeCrawler';
  4. export class LayoutTreeParser {
  5. constructor() {
  6. this.parse = this.parse.bind(this);
  7. }
  8. parse(api): LayoutNode {
  9. if (api.topTabs) {
  10. return this._topTabs(api.topTabs);
  11. } else if (api.sideMenu) {
  12. return this._sideMenu(api.sideMenu);
  13. } else if (api.bottomTabs) {
  14. return this._bottomTabs(api.bottomTabs);
  15. } else if (api.stack) {
  16. return this._stack(api.stack);
  17. } else if (api.component) {
  18. return this._component(api.component);
  19. } else if (api.externalComponent) {
  20. return this._externalComponent(api.externalComponent);
  21. } else if (api.splitView) {
  22. return this._splitView(api.splitView);
  23. }
  24. throw new Error(`unknown LayoutType "${_.keys(api)}"`);
  25. }
  26. _topTabs(api): LayoutNode {
  27. return {
  28. id: api.id,
  29. type: LayoutType.TopTabs,
  30. data: { options: api.options },
  31. children: _.map(api.children, this.parse)
  32. };
  33. }
  34. _sideMenu(api): LayoutNode {
  35. return {
  36. id: api.id,
  37. type: LayoutType.SideMenuRoot,
  38. data: { options: api.options },
  39. children: this._sideMenuChildren(api)
  40. };
  41. }
  42. _sideMenuChildren(api): LayoutNode[] {
  43. if (!api.center) {
  44. throw new Error(`sideMenu.center is required`);
  45. }
  46. const children: LayoutNode[] = [];
  47. if (api.left) {
  48. children.push({
  49. id: api.left.id,
  50. type: LayoutType.SideMenuLeft,
  51. data: {},
  52. children: [this.parse(api.left)]
  53. });
  54. }
  55. children.push({
  56. id: api.center.id,
  57. type: LayoutType.SideMenuCenter,
  58. data: {},
  59. children: [this.parse(api.center)]
  60. });
  61. if (api.right) {
  62. children.push({
  63. id: api.right.id,
  64. type: LayoutType.SideMenuRight,
  65. data: {},
  66. children: [this.parse(api.right)]
  67. });
  68. }
  69. return children;
  70. }
  71. _bottomTabs(api): LayoutNode {
  72. return {
  73. id: api.id,
  74. type: LayoutType.BottomTabs,
  75. data: { options: api.options },
  76. children: _.map(api.children, this.parse)
  77. };
  78. }
  79. _stack(api): LayoutNode {
  80. return {
  81. id: api.id,
  82. type: LayoutType.Stack,
  83. data: { name: api.name, options: api.options },
  84. children: _.map(api.children, this.parse)
  85. };
  86. }
  87. _component(api): LayoutNode {
  88. return {
  89. id: api.id,
  90. type: LayoutType.Component,
  91. data: { name: api.name, options: api.options, passProps: api.passProps },
  92. children: []
  93. };
  94. }
  95. _externalComponent(api): LayoutNode {
  96. return {
  97. id: api.id,
  98. type: LayoutType.ExternalComponent,
  99. data: { name: api.name, options: api.options, passProps: api.passProps },
  100. children: []
  101. };
  102. }
  103. _splitView(api): LayoutNode {
  104. const master = this.parse(api.master);
  105. const detail = this.parse(api.detail);
  106. return {
  107. id: api.id,
  108. type: LayoutType.SplitView,
  109. data: { name: api.name, options: api.options },
  110. children: [
  111. master,
  112. detail,
  113. ],
  114. };
  115. }
  116. }