react-native-navigation的迁移库

LayoutTreeParser.ts 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  22. throw new Error(`unknown LayoutType "${_.keys(api)}"`);
  23. }
  24. _topTabs(api): LayoutNode {
  25. return {
  26. id: api.id,
  27. type: LayoutType.TopTabs,
  28. data: { options: api.options },
  29. children: _.map(api.children, this.parse)
  30. };
  31. }
  32. _sideMenu(api): LayoutNode {
  33. return {
  34. id: api.id,
  35. type: LayoutType.SideMenuRoot,
  36. data: { options: api.options },
  37. children: this._sideMenuChildren(api)
  38. };
  39. }
  40. _sideMenuChildren(api): LayoutNode[] {
  41. if (!api.center) {
  42. throw new Error(`sideMenu.center is required`);
  43. }
  44. const children: LayoutNode[] = [];
  45. if (api.left) {
  46. children.push({
  47. id: api.left.id,
  48. type: LayoutType.SideMenuLeft,
  49. data: {},
  50. children: [this.parse(api.left)]
  51. });
  52. }
  53. children.push({
  54. id: api.center.id,
  55. type: LayoutType.SideMenuCenter,
  56. data: {},
  57. children: [this.parse(api.center)]
  58. });
  59. if (api.right) {
  60. children.push({
  61. id: api.right.id,
  62. type: LayoutType.SideMenuRight,
  63. data: {},
  64. children: [this.parse(api.right)]
  65. });
  66. }
  67. return children;
  68. }
  69. _bottomTabs(api): LayoutNode {
  70. return {
  71. id: api.id,
  72. type: LayoutType.BottomTabs,
  73. data: { options: api.options },
  74. children: _.map(api.children, this.parse)
  75. };
  76. }
  77. _stack(api): LayoutNode {
  78. return {
  79. id: api.id,
  80. type: LayoutType.Stack,
  81. data: { name: api.name, options: api.options },
  82. children: _.map(api.children, this.parse)
  83. };
  84. }
  85. _component(api): LayoutNode {
  86. return {
  87. id: api.id,
  88. type: LayoutType.Component,
  89. data: { name: api.name, options: api.options, passProps: api.passProps },
  90. children: []
  91. };
  92. }
  93. _externalComponent(api): LayoutNode {
  94. return {
  95. id: api.id,
  96. type: LayoutType.ExternalComponent,
  97. data: { name: api.name, options: api.options, passProps: api.passProps },
  98. children: []
  99. };
  100. }
  101. }