UNPKG

2.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const lodash_1 = require("lodash");
4const Layout_1 = require("./Layout");
5const MenuItem_1 = require("./MenuItem");
6const MenuItemGroup_1 = require("./MenuItemGroup");
7const SerializeError_1 = require("./SerializeError");
8function noChildResolver() {
9 return undefined;
10}
11class GenericListBuilder {
12 constructor() {
13 this.spec = {};
14 }
15 id(id) {
16 return this.clone({ id });
17 }
18 getId() {
19 return this.spec.id;
20 }
21 title(title) {
22 return this.clone({ title, id: this.spec.id || lodash_1.camelCase(title) });
23 }
24 getTitle() {
25 return this.spec.title;
26 }
27 defaultLayout(defaultLayout) {
28 return this.clone({ defaultLayout });
29 }
30 getDefaultLayout() {
31 return this.spec.defaultLayout;
32 }
33 menuItems(menuItems) {
34 return this.clone({ menuItems });
35 }
36 getMenuItems() {
37 return this.spec.menuItems;
38 }
39 menuItemGroups(menuItemGroups) {
40 return this.clone({ menuItemGroups });
41 }
42 getMenuItemGroups() {
43 return this.spec.menuItemGroups;
44 }
45 child(child) {
46 return this.clone({ child });
47 }
48 getChild() {
49 return this.spec.child;
50 }
51 canHandleIntent(canHandleIntent) {
52 return this.clone({ canHandleIntent });
53 }
54 getCanHandleIntent() {
55 return this.spec.canHandleIntent;
56 }
57 showIcons(enabled) {
58 return this.clone({
59 displayOptions: Object.assign({}, (this.spec.displayOptions || {}), { showIcons: enabled })
60 });
61 }
62 getShowIcons() {
63 return this.spec.displayOptions ? this.spec.displayOptions.showIcons : undefined;
64 }
65 serialize(options = { path: [] }) {
66 const id = this.spec.id || '';
67 const path = options.path;
68 const defaultLayout = this.spec.defaultLayout;
69 if (defaultLayout && !Layout_1.layoutOptions.includes(defaultLayout)) {
70 throw new SerializeError_1.SerializeError(`\`layout\` must be one of ${Layout_1.layoutOptions.map(item => `"${item}"`).join(', ')}`, path, id || options.index, this.spec.title);
71 }
72 return {
73 id,
74 title: this.spec.title,
75 type: 'genericList',
76 defaultLayout,
77 child: this.spec.child || noChildResolver,
78 canHandleIntent: this.spec.canHandleIntent,
79 displayOptions: this.spec.displayOptions,
80 menuItems: (this.spec.menuItems || []).map((item, i) => MenuItem_1.maybeSerializeMenuItem(item, i, path)),
81 menuItemGroups: (this.spec.menuItemGroups || []).map((item, i) => MenuItemGroup_1.maybeSerializeMenuItemGroup(item, i, path))
82 };
83 }
84 clone(withSpec) {
85 const builder = new this.constructor();
86 return builder;
87 }
88}
89exports.GenericListBuilder = GenericListBuilder;
90
91//# sourceMappingURL=GenericList.js.map