react-native-navigation的迁移库

LayoutTreeParser.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const _ = require('lodash');
  2. const LayoutTypes = require('./LayoutTypes');
  3. class LayoutTreeParser {
  4. constructor() {
  5. this.parse = this.parse.bind(this);
  6. }
  7. /**
  8. * returns correct layout tree of nodes which are { type, data, children }
  9. */
  10. parse(api) {
  11. if (api.topTabs) {
  12. return this._topTabs(api.topTabs);
  13. } else if (api.sideMenu) {
  14. return this._sideMenu(api.sideMenu);
  15. } else if (api.bottomTabs) {
  16. return this._bottomTabs(api.bottomTabs);
  17. } else if (api.stack) {
  18. return this._stack(api.stack);
  19. } else if (api.component) {
  20. return this._component(api.component);
  21. }
  22. throw new Error(`unknown LayoutType "${_.keys(api)}"`);
  23. }
  24. _topTabs(api) {
  25. return {
  26. type: LayoutTypes.TopTabs,
  27. data: { options: api.options },
  28. children: _.map(api.children, this.parse)
  29. };
  30. }
  31. _sideMenu(api) {
  32. return {
  33. type: LayoutTypes.SideMenuRoot,
  34. data: { options: api.options },
  35. children: this._sideMenuChildren(api)
  36. };
  37. }
  38. _sideMenuChildren(api) {
  39. if (!api.center) {
  40. throw new Error(`sideMenu.center is required`);
  41. }
  42. const children = [];
  43. if (api.left) {
  44. children.push({
  45. type: LayoutTypes.SideMenuLeft,
  46. data: {},
  47. children: [this.parse(api.left)]
  48. });
  49. }
  50. children.push({
  51. type: LayoutTypes.SideMenuCenter,
  52. data: {},
  53. children: [this.parse(api.center)]
  54. });
  55. if (api.right) {
  56. children.push({
  57. type: LayoutTypes.SideMenuRight,
  58. data: {},
  59. children: [this.parse(api.right)]
  60. });
  61. }
  62. return children;
  63. }
  64. _bottomTabs(api) {
  65. return {
  66. type: LayoutTypes.BottomTabs,
  67. data: { options: api.options },
  68. children: _.map(api.children, this.parse)
  69. };
  70. }
  71. _stack(api) {
  72. return {
  73. type: LayoutTypes.Stack,
  74. data: { name: api.name, options: api.options },
  75. children: _.map(api.children, this.parse)
  76. };
  77. }
  78. _component(api) {
  79. return {
  80. type: LayoutTypes.Component,
  81. data: { name: api.name, options: api.options, passProps: api.passProps },
  82. children: []
  83. };
  84. }
  85. }
  86. module.exports = LayoutTreeParser;