react-native-navigation的迁移库

LayoutTreeParser.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as _ from 'lodash';
  2. import LayoutTypes from './LayoutTypes';
  3. export default 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.tabs) {
  12. return this._createTabs(simpleJsonApi.tabs);
  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.tabs ? this._createTabs(layout.tabs) : 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. }