forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayoutTreeParser.ts
139 lines (128 loc) · 4.05 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
128
129
130
131
132
133
134
135
136
137
138
139
import { LayoutType } from './LayoutType';
import { LayoutNode } from './LayoutTreeCrawler';
import {
Layout,
LayoutTopTabs,
LayoutComponent,
LayoutStack,
LayoutBottomTabs,
LayoutSideMenu,
LayoutSplitView,
ExternalComponent,
} from '../interfaces/Layout';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
export class LayoutTreeParser {
constructor(private uniqueIdProvider: UniqueIdProvider) {
this.parse = this.parse.bind(this);
}
public parse(api: Layout): 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 "${Object.keys(api)}"`);
}
private topTabs(api: LayoutTopTabs): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.TopTabs),
type: LayoutType.TopTabs,
data: { options: api.options },
children: api.children ? api.children.map(this.parse) : [],
};
}
private sideMenu(api: LayoutSideMenu): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.SideMenuRoot),
type: LayoutType.SideMenuRoot,
data: { options: api.options },
children: this.sideMenuChildren(api),
};
}
private sideMenuChildren(api: LayoutSideMenu): LayoutNode[] {
const children: LayoutNode[] = [];
if (api.left) {
children.push({
id: this.uniqueIdProvider.generate(LayoutType.SideMenuLeft),
type: LayoutType.SideMenuLeft,
data: {},
children: [this.parse(api.left)],
});
}
children.push({
id: this.uniqueIdProvider.generate(LayoutType.SideMenuCenter),
type: LayoutType.SideMenuCenter,
data: {},
children: [this.parse(api.center)],
});
if (api.right) {
children.push({
id: this.uniqueIdProvider.generate(LayoutType.SideMenuRight),
type: LayoutType.SideMenuRight,
data: {},
children: [this.parse(api.right)],
});
}
return children;
}
private bottomTabs(api: LayoutBottomTabs): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.BottomTabs),
type: LayoutType.BottomTabs,
data: { options: api.options },
children: api.children ? api.children.map(this.parse) : [],
};
}
private stack(api: LayoutStack): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.Stack),
type: LayoutType.Stack,
data: { options: api.options },
children: api.children ? api.children.map(this.parse) : [],
};
}
private component(api: LayoutComponent): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.Component),
type: LayoutType.Component,
data: {
name: api.name.toString(),
options: api.options,
passProps: api.passProps,
},
children: [],
};
}
private externalComponent(api: ExternalComponent): LayoutNode {
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.ExternalComponent),
type: LayoutType.ExternalComponent,
data: {
name: api.name.toString(),
options: api.options,
passProps: api.passProps,
},
children: [],
};
}
private splitView(api: LayoutSplitView): LayoutNode {
const master = api.master ? this.parse(api.master) : undefined;
const detail = api.detail ? this.parse(api.detail) : undefined;
return {
id: api.id || this.uniqueIdProvider.generate(LayoutType.SplitView),
type: LayoutType.SplitView,
data: { options: api.options },
children: master && detail ? [master, detail] : [],
};
}
}