-
Notifications
You must be signed in to change notification settings - Fork 1
/
Registry.ts
36 lines (30 loc) · 1.4 KB
/
Registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { CoreComponent } from '../types/CoreComponent'
import { ElementInstance } from '../types/ElementInstance'
import { RdfForm } from '..'
import BaseFields from '../plugins'
export class Registry extends EventTarget implements CoreComponent {
public ready: boolean = false
private registeredFieldClasses = {}
private form: RdfForm
constructor (rdfForm: RdfForm) {
super()
this.form = rdfForm
this.init()
}
async init () {
const event = new CustomEvent('register-elements', { detail: { fields: [] } })
this.form.dispatchEvent(event)
Object.assign(this.registeredFieldClasses, BaseFields, event.detail.fields)
this.ready = true
this.dispatchEvent(new CustomEvent('ready'))
}
async setupElement (definition, bindings: Array<string>, value = null, itemValues = {}, parentValues = null, render: any = () => null, parent, index: number | null = null, children = []): Promise<ElementInstance> {
const widget = definition['form:widget']?._ && this.registeredFieldClasses[definition['form:widget']?._] ? definition['form:widget']._ : 'unknown'
let elementClass = this.registeredFieldClasses[widget]
if (!elementClass) {
console.log(this.registeredFieldClasses)
throw new Error('Unknown widget type: ' + definition['form:widget']?._)
}
return new elementClass(definition, bindings, value, itemValues, parentValues, render, parent, index, children)
}
}