1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const memoize_one_1 = require("memoize-one");
|
4 | const Schema_1 = require("./parts/Schema");
|
5 | const DataAspects_1 = require("./parts/DataAspects");
|
6 | const Icon_1 = require("./parts/Icon");
|
7 | const MenuItem_1 = require("./MenuItem");
|
8 | const Sort_1 = require("./Sort");
|
9 | const ListItem_1 = require("./ListItem");
|
10 | const Editor_1 = require("./Editor");
|
11 | const documentActionUtils_1 = require("./parts/documentActionUtils");
|
12 | const DocumentTypeList_1 = require("./DocumentTypeList");
|
13 | const PlusIcon = Icon_1.getPlusIcon();
|
14 | const ListIcon = Icon_1.getListIcon();
|
15 | const DetailsIcon = Icon_1.getDetailsIcon();
|
16 | const getDataAspectsForSchema = memoize_one_1.default(DataAspects_1.dataAspects);
|
17 | exports.DEFAULT_INTENT_HANDLER = Symbol('Document type list canHandleIntent');
|
18 | function shouldShowIcon(schemaType) {
|
19 | const preview = schemaType.preview;
|
20 | return Boolean(preview && (preview.prepare || (preview.select && preview.select.media)));
|
21 | }
|
22 | function getDocumentTypeListItems(schema = Schema_1.defaultSchema) {
|
23 | const resolver = getDataAspectsForSchema(schema);
|
24 | const types = resolver.getDocumentTypes();
|
25 | return types.map(typeName => getDocumentTypeListItem(typeName, schema));
|
26 | }
|
27 | exports.getDocumentTypeListItems = getDocumentTypeListItems;
|
28 | function getDocumentTypeListItem(typeName, schema = Schema_1.defaultSchema) {
|
29 | const type = schema.get(typeName);
|
30 | if (!type) {
|
31 | throw new Error(`Schema type with name "${typeName}" not found`);
|
32 | }
|
33 | const resolver = getDataAspectsForSchema(schema);
|
34 | const title = resolver.getDisplayName(typeName);
|
35 | return new ListItem_1.ListItemBuilder()
|
36 | .id(typeName)
|
37 | .title(title)
|
38 | .schemaType(type)
|
39 | .child(getDocumentTypeList(typeName, schema));
|
40 | }
|
41 | exports.getDocumentTypeListItem = getDocumentTypeListItem;
|
42 | function getDocumentTypeList(typeName, schema = Schema_1.defaultSchema) {
|
43 | const type = schema.get(typeName);
|
44 | if (!type) {
|
45 | throw new Error(`Schema type with name "${typeName}" not found`);
|
46 | }
|
47 | const resolver = getDataAspectsForSchema(schema);
|
48 | const title = resolver.getDisplayName(typeName);
|
49 | const showIcons = shouldShowIcon(type);
|
50 | const canCreate = documentActionUtils_1.isActionEnabled(type, 'create');
|
51 | const intentChecker = (intentName, params) => Boolean(intentName === 'edit' && params && params.id && params.type === typeName) ||
|
52 | Boolean(intentName === 'create' && params && params.type === typeName);
|
53 | intentChecker.identity = exports.DEFAULT_INTENT_HANDLER;
|
54 | return new DocumentTypeList_1.DocumentTypeListBuilder()
|
55 | .id(typeName)
|
56 | .title(title)
|
57 | .filter('_type == $type')
|
58 | .params({ type: typeName })
|
59 | .schemaType(type)
|
60 | .showIcons(showIcons)
|
61 | .defaultOrdering(Sort_1.DEFAULT_SELECTED_ORDERING_OPTION.by)
|
62 | .menuItemGroups([
|
63 | { id: 'sorting', title: 'Sort' },
|
64 | { id: 'layout', title: 'Layout' },
|
65 | { id: 'actions', title: 'Actions' }
|
66 | ])
|
67 | .child((documentId) => new Editor_1.EditorBuilder()
|
68 | .id('editor')
|
69 | .schemaType(type)
|
70 | .documentId(documentId))
|
71 | .canHandleIntent(intentChecker)
|
72 | .menuItems([
|
73 |
|
74 | ...(canCreate
|
75 | ? [
|
76 | new MenuItem_1.MenuItemBuilder()
|
77 | .title(`Create new ${title}`)
|
78 | .icon(PlusIcon)
|
79 | .intent({ type: 'create', params: { type: typeName } })
|
80 | .showAsAction({ whenCollapsed: true })
|
81 | ]
|
82 | : []),
|
83 |
|
84 | ...MenuItem_1.getOrderingMenuItemsForSchemaType(type),
|
85 |
|
86 | new MenuItem_1.MenuItemBuilder()
|
87 | .group('layout')
|
88 | .title('List')
|
89 | .icon(ListIcon)
|
90 | .action('setLayout')
|
91 | .params({ layout: 'default' }),
|
92 | new MenuItem_1.MenuItemBuilder()
|
93 | .group('layout')
|
94 | .title('Details')
|
95 | .icon(DetailsIcon)
|
96 | .action('setLayout')
|
97 | .params({ layout: 'detail' }),
|
98 |
|
99 | ...(canCreate
|
100 | ? [
|
101 | new MenuItem_1.MenuItemBuilder()
|
102 | .group('actions')
|
103 | .title('Create new…')
|
104 | .icon(PlusIcon)
|
105 | .intent({ type: 'create', params: { type: typeName } })
|
106 | ]
|
107 | : [])
|
108 | ]);
|
109 | }
|
110 | exports.getDocumentTypeList = getDocumentTypeList;
|
111 |
|
112 |
|