1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const lodash_1 = require("lodash");
|
4 | const Schema_1 = require("./parts/Schema");
|
5 | const DocumentList_1 = require("./DocumentList");
|
6 | const SerializeError_1 = require("./SerializeError");
|
7 | const List_1 = require("./List");
|
8 | const Editor_1 = require("./Editor");
|
9 | const Component_1 = require("./Component");
|
10 | class ListItemBuilder {
|
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 | icon(icon) {
|
27 | return this.clone({ icon });
|
28 | }
|
29 | showIcon(enabled) {
|
30 | return this.clone({
|
31 | displayOptions: Object.assign({}, (this.spec.displayOptions || {}), { showIcon: enabled })
|
32 | });
|
33 | }
|
34 | getShowIcon() {
|
35 | return this.spec.displayOptions ? this.spec.displayOptions.showIcon : undefined;
|
36 | }
|
37 | getIcon() {
|
38 | return this.spec.icon;
|
39 | }
|
40 | child(child) {
|
41 | return this.clone({ child });
|
42 | }
|
43 | getChild() {
|
44 | return this.spec.child;
|
45 | }
|
46 | schemaType(schemaType) {
|
47 | return this.clone({ schemaType });
|
48 | }
|
49 | getSchemaType() {
|
50 | return this.spec.schemaType;
|
51 | }
|
52 | serialize(options = { path: [] }) {
|
53 | const { id, title, child } = this.spec;
|
54 | if (typeof id !== 'string' || !id) {
|
55 | throw new SerializeError_1.SerializeError('`id` is required for list items', options.path, options.index).withHelpUrl(SerializeError_1.HELP_URL.ID_REQUIRED);
|
56 | }
|
57 | if (!options.titleIsOptional && (typeof title !== 'string' || !title)) {
|
58 | throw new SerializeError_1.SerializeError('`title` is required for list items', options.path, id).withHelpUrl(SerializeError_1.HELP_URL.TITLE_REQUIRED);
|
59 | }
|
60 | let schemaType = this.spec.schemaType;
|
61 | if (typeof schemaType === 'string') {
|
62 | const type = Schema_1.defaultSchema.get(schemaType);
|
63 | if (!type) {
|
64 | throw new SerializeError_1.SerializeError(`Could not find type "${schemaType}" in schema`, options.path, id).withHelpUrl(SerializeError_1.HELP_URL.SCHEMA_TYPE_NOT_FOUND);
|
65 | }
|
66 | schemaType = type;
|
67 | }
|
68 | const serializeOptions = { path: options.path.concat(id), hint: 'child' };
|
69 | let listChild = child instanceof Component_1.ComponentBuilder ||
|
70 | child instanceof DocumentList_1.DocumentListBuilder ||
|
71 | child instanceof List_1.ListBuilder ||
|
72 | child instanceof Editor_1.EditorBuilder
|
73 | ? child.serialize(serializeOptions)
|
74 | : child;
|
75 |
|
76 |
|
77 | if (typeof listChild === 'function') {
|
78 | const originalChild = listChild;
|
79 | listChild = (itemId, options) => {
|
80 | return originalChild(itemId, Object.assign({}, options, { serializeOptions }));
|
81 | };
|
82 | }
|
83 | return Object.assign({}, this.spec, { schemaType, child: listChild, id, title, type: 'listItem' });
|
84 | }
|
85 | clone(withSpec) {
|
86 | const builder = new ListItemBuilder();
|
87 | builder.spec = Object.assign({}, this.spec, (withSpec || {}));
|
88 | return builder;
|
89 | }
|
90 | }
|
91 | exports.ListItemBuilder = ListItemBuilder;
|
92 |
|
93 |
|