forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutTreeParser.ts
127 lines (116 loc) · 3.01 KB
/
LayoutTreeParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as _ from 'lodash';
import { LayoutType } from './LayoutType';
import { LayoutNode } from './LayoutTreeCrawler';
export class LayoutTreeParser {
constructor() {
this.parse = this.parse.bind(this);
}
parse(api): LayoutNode {
if (api.topTabs) {
return this._topTabs(api.topTabs);
} else if (api.sideMenu) {
return this._sideMenu(api.sideMenu);
} else if (api.bottomTabs) {
return this._bottomTabs(api.bottomTabs);
} else if (api.stack) {
return this._stack(api.stack);
} else if (api.component) {
return this._component(api.component);
} else if (api.externalComponent) {
return this._externalComponent(api.externalComponent);
} else if (api.splitView) {
return this._splitView(api.splitView);
}
throw new Error(`unknown LayoutType "${_.keys(api)}"`);
}
_topTabs(api): LayoutNode {
return {
id: api.id,
type: LayoutType.TopTabs,
data: { options: api.options },
children: _.map(api.children, this.parse)
};
}
_sideMenu(api): LayoutNode {
return {
id: api.id,
type: LayoutType.SideMenuRoot,
data: { options: api.options },
children: this._sideMenuChildren(api)
};
}
_sideMenuChildren(api): LayoutNode[] {
if (!api.center) {
throw new Error(`sideMenu.center is required`);
}
const children: LayoutNode[] = [];
if (api.left) {
children.push({
id: api.left.id,
type: LayoutType.SideMenuLeft,
data: {},
children: [this.parse(api.left)]
});
}
children.push({
id: api.center.id,
type: LayoutType.SideMenuCenter,
data: {},
children: [this.parse(api.center)]
});
if (api.right) {
children.push({
id: api.right.id,
type: LayoutType.SideMenuRight,
data: {},
children: [this.parse(api.right)]
});
}
return children;
}
_bottomTabs(api): LayoutNode {
return {
id: api.id,
type: LayoutType.BottomTabs,
data: { options: api.options },
children: _.map(api.children, this.parse)
};
}
_stack(api): LayoutNode {
return {
id: api.id,
type: LayoutType.Stack,
data: { name: api.name, options: api.options },
children: _.map(api.children, this.parse)
};
}
_component(api): LayoutNode {
return {
id: api.id,
type: LayoutType.Component,
data: { name: api.name, options: api.options, passProps: api.passProps },
children: []
};
}
_externalComponent(api): LayoutNode {
return {
id: api.id,
type: LayoutType.ExternalComponent,
data: { name: api.name, options: api.options, passProps: api.passProps },
children: []
};
}
_splitView(api): LayoutNode {
const master = this.parse(api.master);
const detail = this.parse(api.detail);
return {
id: api.id,
type: LayoutType.SplitView,
data: { name: api.name, options: api.options },
children: [
master,
detail,
],
};
}
}