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