import { ComponentRef , Injectable , ViewContainerRef } from '@angular/core' ; import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; import { FormProperty } from './model/form.property' ; import { SFUISchemaItem } from './schema/ui' ; import type { Widget } from './widget' ; export class WidgetRegistry { private _widgets : { [ type ] : Widget < FormProperty , SFUISchemaItem > ; } = { } ; private defaultWidget ! : Widget < FormProperty , SFUISchemaItem > ; get widgets ( ) : { [ type ] : Widget < FormProperty , SFUISchemaItem > ; } { return this . _widgets ; } setDefault ( widget ) { this . defaultWidget = widget ; } register ( type , widget ) { this . _widgets [ type ] = widget ; } has ( type ) { return this . _widgets . hasOwnProperty ( type ) ; } getType ( type ) { if ( this . has ( type ) ) { return this . _widgets [ type ] ; } return this . defaultWidget ; } } @ Injectable ( ) export class WidgetFactory { constructor ( private registry ) { } createWidget ( container , type ) { if ( ! this . registry . has ( type ) ) { if ( typeof ngDevMode === 'undefined' || ngDevMode ) { console . warn ( `No widget for type "${ type }"` ) ; } } const componentClass = this . registry . getType ( type ) as NzSafeAny ; return container . createComponent ( componentClass ) ; } }