UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const lodash_1 = require("lodash");
4const SerializeError_1 = require("./SerializeError");
5const MenuItem_1 = require("./MenuItem");
6const MenuItemGroup_1 = require("./MenuItemGroup");
7function getFunctionName(fn) {
8 return typeof fn.displayName === 'string' ? fn.displayName : fn.name;
9}
10class ComponentBuilder {
11 constructor(spec) {
12 this.spec = spec ? spec : {};
13 }
14 id(id) {
15 return this.clone({ id });
16 }
17 getId() {
18 return this.spec.id;
19 }
20 title(title) {
21 return this.clone({ title, id: this.spec.id || lodash_1.camelCase(title) });
22 }
23 getTitle() {
24 return this.spec.title;
25 }
26 component(component) {
27 return this.clone({ component, id: this.spec.id || getFunctionName(component) });
28 }
29 getComponent() {
30 return this.spec.component;
31 }
32 menuItems(menuItems) {
33 return this.clone({ menuItems });
34 }
35 getMenuItems() {
36 return this.spec.menuItems;
37 }
38 menuItemGroups(menuItemGroups) {
39 return this.clone({ menuItemGroups });
40 }
41 getMenuItemGroups() {
42 return this.spec.menuItemGroups;
43 }
44 serialize(options = { path: [] }) {
45 const { id, title, component } = this.spec;
46 if (!id) {
47 throw new SerializeError_1.SerializeError('`id` is required for `component` structure item', options.path, options.index).withHelpUrl(SerializeError_1.HELP_URL.ID_REQUIRED);
48 }
49 if (!component) {
50 throw new SerializeError_1.SerializeError('`component` is required for `component` structure item', options.path, options.index).withHelpUrl(SerializeError_1.HELP_URL.ID_REQUIRED);
51 }
52 return {
53 id,
54 title,
55 type: 'component',
56 component,
57 menuItems: (this.spec.menuItems || []).map((item, i) => MenuItem_1.maybeSerializeMenuItem(item, i, options.path)),
58 menuItemGroups: (this.spec.menuItemGroups || []).map((item, i) => MenuItemGroup_1.maybeSerializeMenuItemGroup(item, i, options.path))
59 };
60 }
61 clone(withSpec) {
62 const builder = new ComponentBuilder();
63 builder.spec = Object.assign({}, this.spec, (withSpec || {}));
64 return builder;
65 }
66}
67exports.ComponentBuilder = ComponentBuilder;
68
69//# sourceMappingURL=Component.js.map