UNPKG

4.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const memoize_one_1 = require("memoize-one");
4const Schema_1 = require("./parts/Schema");
5const DataAspects_1 = require("./parts/DataAspects");
6const Icon_1 = require("./parts/Icon");
7const MenuItem_1 = require("./MenuItem");
8const Sort_1 = require("./Sort");
9const ListItem_1 = require("./ListItem");
10const Editor_1 = require("./Editor");
11const documentActionUtils_1 = require("./parts/documentActionUtils");
12const DocumentTypeList_1 = require("./DocumentTypeList");
13const PlusIcon = Icon_1.getPlusIcon();
14const ListIcon = Icon_1.getListIcon();
15const DetailsIcon = Icon_1.getDetailsIcon();
16const getDataAspectsForSchema = memoize_one_1.default(DataAspects_1.dataAspects);
17exports.DEFAULT_INTENT_HANDLER = Symbol('Document type list canHandleIntent');
18function shouldShowIcon(schemaType) {
19 const preview = schemaType.preview;
20 return Boolean(preview && (preview.prepare || (preview.select && preview.select.media)));
21}
22function getDocumentTypeListItems(schema = Schema_1.defaultSchema) {
23 const resolver = getDataAspectsForSchema(schema);
24 const types = resolver.getDocumentTypes();
25 return types.map(typeName => getDocumentTypeListItem(typeName, schema));
26}
27exports.getDocumentTypeListItems = getDocumentTypeListItems;
28function 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}
41exports.getDocumentTypeListItem = getDocumentTypeListItem;
42function 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 // Create new (from action button)
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 // Sort by <Y>
84 ...MenuItem_1.getOrderingMenuItemsForSchemaType(type),
85 // Display as <Z>
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 // Create new (from menu)
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}
110exports.getDocumentTypeList = getDocumentTypeList;
111
112//# sourceMappingURL=documentTypeListItems.js.map