UNPKG

4.05 kBPlain TextView Raw
1import memoizeOne from 'memoize-one'
2import {Schema, defaultSchema, SchemaType} from './parts/Schema'
3import {dataAspects, DataAspectsResolver} from './parts/DataAspects'
4import {getPlusIcon, getListIcon, getDetailsIcon} from './parts/Icon'
5import {MenuItemBuilder, getOrderingMenuItemsForSchemaType} from './MenuItem'
6import {DEFAULT_SELECTED_ORDERING_OPTION} from './Sort'
7import {DocumentListBuilder} from './DocumentList'
8import {ListItemBuilder} from './ListItem'
9import {EditorBuilder} from './Editor'
10import {isActionEnabled} from './parts/documentActionUtils'
11import {DocumentTypeListBuilder} from './DocumentTypeList'
12import {IntentChecker} from './Intent'
13
14const PlusIcon = getPlusIcon()
15const ListIcon = getListIcon()
16const DetailsIcon = getDetailsIcon()
17
18const getDataAspectsForSchema: (schema: Schema) => DataAspectsResolver = memoizeOne(dataAspects)
19
20export const DEFAULT_INTENT_HANDLER = Symbol('Document type list canHandleIntent')
21
22function shouldShowIcon(schemaType: SchemaType): boolean {
23 const preview = schemaType.preview
24 return Boolean(preview && (preview.prepare || (preview.select && preview.select.media)))
25}
26
27export 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
33export 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
47export 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 // Create new (from action button)
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 // Sort by <Y>
96 ...getOrderingMenuItemsForSchemaType(type),
97
98 // Display as <Z>
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 // Create new (from menu)
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}