react-native-navigation的迁移库

LayoutTreeParser.ts 2.2KB

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