react-native-navigation的迁移库

LayoutTreeParser.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { LayoutType } from './LayoutType';
  2. import { LayoutNode } from './LayoutTreeCrawler';
  3. import {
  4. Layout,
  5. LayoutTopTabs,
  6. LayoutComponent,
  7. LayoutStack,
  8. LayoutBottomTabs,
  9. LayoutSideMenu,
  10. LayoutSplitView,
  11. ExternalComponent
  12. } from '../interfaces/Layout';
  13. import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
  14. export class LayoutTreeParser {
  15. constructor(private uniqueIdProvider: UniqueIdProvider) {
  16. this.parse = this.parse.bind(this);
  17. }
  18. public parse(api: Layout): LayoutNode {
  19. if (api.topTabs) {
  20. return this.topTabs(api.topTabs);
  21. } else if (api.sideMenu) {
  22. return this.sideMenu(api.sideMenu);
  23. } else if (api.bottomTabs) {
  24. return this.bottomTabs(api.bottomTabs);
  25. } else if (api.stack) {
  26. return this.stack(api.stack);
  27. } else if (api.component) {
  28. return this.component(api.component);
  29. } else if (api.externalComponent) {
  30. return this.externalComponent(api.externalComponent);
  31. } else if (api.splitView) {
  32. return this.splitView(api.splitView);
  33. }
  34. throw new Error(`unknown LayoutType "${Object.keys(api)}"`);
  35. }
  36. private topTabs(api: LayoutTopTabs): LayoutNode {
  37. return {
  38. id: api.id || this.uniqueIdProvider.generate(LayoutType.TopTabs),
  39. type: LayoutType.TopTabs,
  40. data: { options: api.options },
  41. children: api.children ? api.children.map(this.parse) : []
  42. };
  43. }
  44. private sideMenu(api: LayoutSideMenu): LayoutNode {
  45. return {
  46. id: api.id || this.uniqueIdProvider.generate(LayoutType.SideMenuRoot),
  47. type: LayoutType.SideMenuRoot,
  48. data: { options: api.options },
  49. children: this.sideMenuChildren(api)
  50. };
  51. }
  52. private sideMenuChildren(api: LayoutSideMenu): LayoutNode[] {
  53. const children: LayoutNode[] = [];
  54. if (api.left) {
  55. children.push({
  56. id: this.uniqueIdProvider.generate(LayoutType.SideMenuLeft),
  57. type: LayoutType.SideMenuLeft,
  58. data: {},
  59. children: [this.parse(api.left)]
  60. });
  61. }
  62. children.push({
  63. id: this.uniqueIdProvider.generate(LayoutType.SideMenuCenter),
  64. type: LayoutType.SideMenuCenter,
  65. data: {},
  66. children: [this.parse(api.center)]
  67. });
  68. if (api.right) {
  69. children.push({
  70. id: this.uniqueIdProvider.generate(LayoutType.SideMenuRight),
  71. type: LayoutType.SideMenuRight,
  72. data: {},
  73. children: [this.parse(api.right)]
  74. });
  75. }
  76. return children;
  77. }
  78. private bottomTabs(api: LayoutBottomTabs): LayoutNode {
  79. return {
  80. id: api.id || this.uniqueIdProvider.generate(LayoutType.BottomTabs),
  81. type: LayoutType.BottomTabs,
  82. data: { options: api.options },
  83. children: api.children ? api.children.map(this.parse) : []
  84. };
  85. }
  86. private stack(api: LayoutStack): LayoutNode {
  87. return {
  88. id: api.id || this.uniqueIdProvider.generate(LayoutType.Stack),
  89. type: LayoutType.Stack,
  90. data: { options: api.options },
  91. children: api.children ? api.children.map(this.parse) : []
  92. };
  93. }
  94. private component(api: LayoutComponent): LayoutNode {
  95. return {
  96. id: api.id || this.uniqueIdProvider.generate(LayoutType.Component),
  97. type: LayoutType.Component,
  98. data: {
  99. name: api.name.toString(),
  100. options: api.options,
  101. passProps: api.passProps
  102. },
  103. children: []
  104. };
  105. }
  106. private externalComponent(api: ExternalComponent): LayoutNode {
  107. return {
  108. id: api.id || this.uniqueIdProvider.generate(LayoutType.ExternalComponent),
  109. type: LayoutType.ExternalComponent,
  110. data: {
  111. name: api.name.toString(),
  112. options: api.options,
  113. passProps: api.passProps
  114. },
  115. children: []
  116. };
  117. }
  118. private splitView(api: LayoutSplitView): LayoutNode {
  119. const master = api.master ? this.parse(api.master) : undefined;
  120. const detail = api.detail ? this.parse(api.detail) : undefined;
  121. return {
  122. id: api.id || this.uniqueIdProvider.generate(LayoutType.SplitView),
  123. type: LayoutType.SplitView,
  124. data: { options: api.options },
  125. children: master && detail ? [master, detail] : []
  126. };
  127. }
  128. }