UNPKG

1.89 kBPlain TextView Raw
1import {DocumentListBuilder, DocumentListInput, PartialDocumentList} from './DocumentList'
2import {SchemaType} from './parts/Schema'
3import {Child} from './StructureNodes'
4import {DEFAULT_INTENT_HANDLER} from './documentTypeListItems'
5
6// 1:1 with document list builder, but when modifying key parameters (filter, params, child)
7// remove canHandleIntent function since we can't guarantee child editor can handle intent
8export class DocumentTypeListBuilder extends DocumentListBuilder {
9 protected spec: PartialDocumentList
10
11 constructor(spec?: DocumentListInput) {
12 super()
13 this.spec = spec ? spec : {}
14 }
15
16 filter(filter: string): DocumentListBuilder {
17 return this.cloneWithoutDefaultIntentHandler({
18 options: {...(this.spec.options || {}), filter}
19 })
20 }
21
22 params(params: {}): DocumentListBuilder {
23 return this.cloneWithoutDefaultIntentHandler({
24 options: {...(this.spec.options || {filter: ''}), params}
25 })
26 }
27
28 schemaType(type: SchemaType | string): DocumentListBuilder {
29 return this.cloneWithoutDefaultIntentHandler({
30 schemaTypeName: typeof type === 'string' ? type : type.name
31 })
32 }
33
34 child(child: Child) {
35 return this.cloneWithoutDefaultIntentHandler({child})
36 }
37
38 clone(withSpec?: PartialDocumentList): DocumentTypeListBuilder {
39 const builder = new DocumentTypeListBuilder()
40 builder.spec = {...this.spec, ...(withSpec || {})}
41 return builder
42 }
43
44 cloneWithoutDefaultIntentHandler(withSpec?: PartialDocumentList): DocumentTypeListBuilder {
45 const builder = new DocumentTypeListBuilder()
46 const canHandleIntent = this.spec.canHandleIntent
47 const shouldOverride = canHandleIntent && canHandleIntent.identity === DEFAULT_INTENT_HANDLER
48 const override = shouldOverride ? {canHandleIntent: undefined} : {}
49 builder.spec = {...this.spec, ...(withSpec || {}), ...override}
50 return builder
51 }
52}