react-native-navigation的迁移库

LayoutTreeParser.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const _ = require('lodash');
  2. const LayoutTypes = require('./LayoutTypes');
  3. class LayoutTreeParser {
  4. /**
  5. * returns correct layout tree of {type, children, data?}
  6. */
  7. parseFromSimpleJSON(simpleJsonApi) {
  8. if (simpleJsonApi.sideMenu) {
  9. return this._createSideMenu(simpleJsonApi);
  10. }
  11. if (simpleJsonApi.bottomTabs) {
  12. return this._createTabs(simpleJsonApi.bottomTabs);
  13. }
  14. return this._createContainerStackWithContainerData(simpleJsonApi.container);
  15. }
  16. createContainer(data) {
  17. return {
  18. type: LayoutTypes.Container,
  19. data,
  20. children: []
  21. };
  22. }
  23. _createTabs(tabs) {
  24. return {
  25. type: LayoutTypes.BottomTabs,
  26. children: _.map(tabs, (t) => this._createContainerStackWithContainerData(t.container))
  27. };
  28. }
  29. _createContainerStackWithContainerData(containerData) {
  30. return {
  31. type: LayoutTypes.ContainerStack,
  32. children: [this.createContainer(containerData)]
  33. };
  34. }
  35. _createSideMenu(layout) {
  36. return {
  37. type: LayoutTypes.SideMenuRoot,
  38. children: this._createSideMenuChildren(layout)
  39. };
  40. }
  41. _createSideMenuChildren(layout) {
  42. const children = [];
  43. if (layout.sideMenu.left) {
  44. children.push({
  45. type: LayoutTypes.SideMenuLeft,
  46. children: [this.createContainer(layout.sideMenu.left.container)]
  47. });
  48. }
  49. children.push({
  50. type: LayoutTypes.SideMenuCenter,
  51. children: [
  52. layout.bottomTabs ? this._createTabs(layout.bottomTabs) : this._createContainerStackWithContainerData(layout.container)
  53. ]
  54. });
  55. if (layout.sideMenu.right) {
  56. children.push({
  57. type: LayoutTypes.SideMenuRight,
  58. children: [this.createContainer(layout.sideMenu.right.container)]
  59. });
  60. }
  61. return children;
  62. }
  63. }
  64. module.exports = LayoutTreeParser;