瀏覽代碼

parser works

Daniel Zlotin 7 年之前
父節點
當前提交
0ed93e25ed
共有 3 個文件被更改,包括 207 次插入63 次删除
  1. 59
    54
      src/commands/LayoutTreeParser.js
  2. 136
    0
      src/commands/LayoutTreeParser.test.js
  3. 12
    9
      src/commands/SimpleLayouts.js

+ 59
- 54
src/commands/LayoutTreeParser.js 查看文件

@@ -1,5 +1,15 @@
1 1
 import * as _ from 'lodash';
2 2
 
3
+const Types = {
4
+  Container: 'Container',
5
+  ContainerStack: 'ContainerStack',
6
+  Tabs: 'Tabs',
7
+  SideMenuRoot: 'SideMenuRoot',
8
+  SideMenuCenter: 'SideMenuCenter',
9
+  SideMenuLeft: 'SideMenuLeft',
10
+  SideMenuRight: 'SideMenuRight'
11
+};
12
+
3 13
 export default class LayoutTreeParser {
4 14
   constructor(uniqueIdProvider) {
5 15
     this.uniqueIdProvider = uniqueIdProvider;
@@ -8,73 +18,68 @@ export default class LayoutTreeParser {
8 18
   parseFromSimpleJSON(simpleJsonApi) {
9 19
     // TOOD deepclone
10 20
     if (simpleJsonApi.sideMenu) {
11
-      return {
12
-        type: 'SideMenuRoot',
13
-        id: this.uniqueIdProvider.generate('SideMenuRoot'),
14
-        children: this.createSideMenuChildren(simpleJsonApi)
15
-      };
21
+      return this._createSideMenu(simpleJsonApi);
16 22
     }
17 23
     if (simpleJsonApi.tabs) {
18
-      return this.createTabs(simpleJsonApi.tabs);
24
+      return this._createTabs(simpleJsonApi.tabs);
19 25
     }
26
+    return this._createContainerStackWithContainer(simpleJsonApi.container);
27
+  }
28
+
29
+  _node(node) {
30
+    node.id = this.uniqueIdProvider.generate(node.type);
31
+    node.children = node.children || [];
32
+    return node;
33
+  }
34
+
35
+  _createTabs(tabs) {
36
+    return this._node({
37
+      type: Types.Tabs,
38
+      children: _.map(tabs, (t) => this._createContainerStackWithContainer(t.container))
39
+    });
40
+  }
41
+
42
+  _createContainerStackWithContainer(container) {
43
+    return this._node({
44
+      type: Types.ContainerStack,
45
+      children: [this._createContainer(container)]
46
+    });
47
+  }
20 48
 
21
-    return this.createContainerStackWithContainer(simpleJsonApi.container);
49
+  _createContainer(container) {
50
+    return this._node({
51
+      type: Types.Container,
52
+      data: container
53
+    });
22 54
   }
23 55
 
24
-  createSideMenuChildren(layout) {
56
+  _createSideMenu(layout) {
57
+    return this._node({
58
+      type: Types.SideMenuRoot,
59
+      children: this._createSideMenuChildren(layout)
60
+    });
61
+  }
62
+
63
+  _createSideMenuChildren(layout) {
25 64
     const children = [];
26 65
     if (layout.sideMenu.left) {
27
-      children.push({
28
-        type: 'SideMenuLeft',
29
-        id: this.uniqueIdProvider.generate('SideMenuLeft'),
30
-        children: [
31
-          this.createContainer(layout.sideMenu.left.container)
32
-        ]
33
-      });
66
+      children.push(this._node({
67
+        type: Types.SideMenuLeft,
68
+        children: [this._createContainer(layout.sideMenu.left.container)]
69
+      }));
34 70
     }
35
-    children.push({
36
-      type: 'SideMenuCenter',
37
-      id: this.uniqueIdProvider.generate('SideMenuCenter'),
71
+    children.push(this._node({
72
+      type: Types.SideMenuCenter,
38 73
       children: [
39
-        this.createContainerStackWithContainer(layout.container)
74
+        layout.tabs ? this._createTabs(layout.tabs) : this._createContainerStackWithContainer(layout.container)
40 75
       ]
41
-    });
76
+    }));
42 77
     if (layout.sideMenu.right) {
43
-      children.push({
44
-        type: 'SideMenuRight',
45
-        id: this.uniqueIdProvider.generate('SideMenuRight'),
46
-        children: [
47
-          this.createContainer(layout.sideMenu.right.container)
48
-        ]
49
-      });
78
+      children.push(this._node({
79
+        type: Types.SideMenuRight,
80
+        children: [this._createContainer(layout.sideMenu.right.container)]
81
+      }));
50 82
     }
51 83
     return children;
52 84
   }
53
-
54
-  createTabs(tabs) {
55
-    return {
56
-      type: 'Tabs',
57
-      id: this.uniqueIdProvider.generate(`Tabs`),
58
-      children: _.map(tabs, (t) => this.createContainerStackWithContainer(t.container))
59
-    };
60
-  }
61
-
62
-  createContainerStackWithContainer(container) {
63
-    return {
64
-      type: 'ContainerStack',
65
-      id: this.uniqueIdProvider.generate(`ContainerStack`),
66
-      children: [
67
-        this.createContainer(container)
68
-      ]
69
-    };
70
-  }
71
-
72
-  createContainer(container) {
73
-    return {
74
-      data: container,
75
-      type: 'Container',
76
-      id: this.uniqueIdProvider.generate(`Container`),
77
-      children: []
78
-    };
79
-  }
80 85
 }

+ 136
- 0
src/commands/LayoutTreeParser.test.js 查看文件

@@ -211,4 +211,140 @@ describe('LayoutTreeParser', () => {
211 211
         ]
212 212
       });
213 213
   });
214
+
215
+  it('parses both side menus', () => {
216
+    expect(uut.parseFromSimpleJSON(SimpleLayouts.singleWithBothMenus))
217
+      .toEqual({
218
+        type: 'SideMenuRoot',
219
+        id: 'SideMenuRoot+UNIQUE_ID',
220
+        children: [
221
+          {
222
+            type: 'SideMenuLeft',
223
+            id: 'SideMenuLeft+UNIQUE_ID',
224
+            children: [
225
+              {
226
+                type: 'Container',
227
+                id: 'Container+UNIQUE_ID',
228
+                data: {
229
+                  name: 'com.example.Menu1'
230
+                },
231
+                children: []
232
+              }
233
+            ]
234
+          },
235
+          {
236
+            type: 'SideMenuCenter',
237
+            id: 'SideMenuCenter+UNIQUE_ID',
238
+            children: [
239
+              {
240
+                type: 'ContainerStack',
241
+                id: 'ContainerStack+UNIQUE_ID',
242
+                children: [
243
+                  {
244
+                    type: 'Container',
245
+                    id: 'Container+UNIQUE_ID',
246
+                    data: {
247
+                      name: 'com.example.MyScreen'
248
+                    },
249
+                    children: []
250
+                  }
251
+                ]
252
+              }
253
+            ]
254
+          },
255
+          {
256
+            type: 'SideMenuRight',
257
+            id: 'SideMenuRight+UNIQUE_ID',
258
+            children: [
259
+              {
260
+                type: 'Container',
261
+                id: 'Container+UNIQUE_ID',
262
+                data: {
263
+                  name: 'com.example.Menu2'
264
+                },
265
+                children: []
266
+              }
267
+            ]
268
+          }
269
+        ]
270
+      });
271
+  });
272
+
273
+  it('parses tabs with side menus', () => {
274
+    expect(uut.parseFromSimpleJSON(SimpleLayouts.tabBasedWithBothSideMenus))
275
+      .toEqual({
276
+        type: 'SideMenuRoot',
277
+        id: 'SideMenuRoot+UNIQUE_ID',
278
+        children: [
279
+          {
280
+            type: 'SideMenuLeft',
281
+            id: 'SideMenuLeft+UNIQUE_ID',
282
+            children: [
283
+              {
284
+                type: 'Container',
285
+                id: 'Container+UNIQUE_ID',
286
+                data: {
287
+                  name: 'com.example.Menu1'
288
+                },
289
+                children: []
290
+              }
291
+            ]
292
+          },
293
+          {
294
+            type: 'SideMenuCenter',
295
+            id: 'SideMenuCenter+UNIQUE_ID',
296
+            children: [
297
+              {
298
+                type: 'Tabs',
299
+                id: 'Tabs+UNIQUE_ID',
300
+                children: [
301
+                  {
302
+                    type: 'ContainerStack',
303
+                    id: 'ContainerStack+UNIQUE_ID',
304
+                    children: [
305
+                      {
306
+                        type: 'Container',
307
+                        id: 'Container+UNIQUE_ID',
308
+                        data: {
309
+                          name: 'com.example.FirstTab'
310
+                        },
311
+                        children: []
312
+                      }
313
+                    ]
314
+                  },
315
+                  {
316
+                    type: 'ContainerStack',
317
+                    id: 'ContainerStack+UNIQUE_ID',
318
+                    children: [
319
+                      {
320
+                        type: 'Container',
321
+                        id: 'Container+UNIQUE_ID',
322
+                        data: {
323
+                          name: 'com.example.SecondTab'
324
+                        },
325
+                        children: []
326
+                      }
327
+                    ]
328
+                  }
329
+                ]
330
+              }
331
+            ]
332
+          },
333
+          {
334
+            type: 'SideMenuRight',
335
+            id: 'SideMenuRight+UNIQUE_ID',
336
+            children: [
337
+              {
338
+                type: 'Container',
339
+                id: 'Container+UNIQUE_ID',
340
+                data: {
341
+                  name: 'com.example.Menu2'
342
+                },
343
+                children: []
344
+              }
345
+            ]
346
+          }
347
+        ]
348
+      });
349
+  });
214 350
 });

+ 12
- 9
src/commands/SimpleLayouts.js 查看文件

@@ -73,10 +73,14 @@ export const singleWithBothMenus = {
73 73
   },
74 74
   sideMenu: {
75 75
     left: {
76
-      name: 'com.example.Menu1'
76
+      container: {
77
+        name: 'com.example.Menu1'
78
+      }
77 79
     },
78 80
     right: {
79
-      name: 'com.example.Menu2'
81
+      container: {
82
+        name: 'com.example.Menu2'
83
+      }
80 84
     }
81 85
   }
82 86
 };
@@ -92,19 +96,18 @@ export const tabBasedWithBothSideMenus = {
92 96
       container: {
93 97
         name: 'com.example.SecondTab'
94 98
       }
95
-    },
96
-    {
97
-      container: {
98
-        name: 'com.example.FirstTab'
99
-      }
100 99
     }
101 100
   ],
102 101
   sideMenu: {
103 102
     left: {
104
-      name: 'com.example.Menu1'
103
+      container: {
104
+        name: 'com.example.Menu1'
105
+      }
105 106
     },
106 107
     right: {
107
-      name: 'com.example.Menu2'
108
+      container: {
109
+        name: 'com.example.Menu2'
110
+      }
108 111
     }
109 112
   }
110 113
 };