import { AfterViewInit , ChangeDetectorRef , Directive , HostBinding , Inject , Injector } from '@angular/core' ; import { DomSanitizer } from '@angular/platform-browser' ; import { takeUntil } from 'rxjs/operators' ; import { LocaleData } from '@delon/theme' ; import { NgClassType , NzSafeAny } from 'ng-zorro-antd/core/types' ; import { ErrorData } from './errors' ; import { SFValue } from './interface' ; import { ArrayProperty } from './model/array.property' ; import { FormProperty } from './model/form.property' ; import { ObjectProperty } from './model/object.property' ; import { SFSchema } from './schema' ; import { SFOptionalHelp , SFUISchemaItem } from './schema/ui' ; import { SFItemComponent } from './sf-item.component' ; import { SFComponent } from './sf.component' ; import { di } from './utils' ; import { SFArrayWidgetSchema , SFObjectWidgetSchema } from './widgets' ; @ Directive ( ) export abstract class Widget < T extends FormProperty , UIT extends SFUISchemaItem > implements AfterViewInit { formProperty ! : T ; error ? : string ; showError = false ; id = '' ; schema ! : SFSchema ; ui ! : UIT ; firstVisual = false ; @ HostBinding ( 'class' ) get cls ( ) : NgClassType { return this . ui . class || '' ; } get disabled ( ) : boolean { if ( this . schema . readOnly === true || this . sfComp ! . disabled ) { return true ; } return false ; } get l ( ) : LocaleData { return this . formProperty . root . widget . sfComp ! . locale ; } get oh ( ) : SFOptionalHelp { return this . ui . optionalHelp as SFOptionalHelp ; } get dom ( ) : DomSanitizer { return this . injector . get ( DomSanitizer ) ; } get cleanValue ( ) : boolean { return this . sfComp ?. cleanValue ! ; } constructor ( @ Inject ( ChangeDetectorRef ) public readonly cd , @ Inject ( Injector ) public readonly injector , @ Inject ( SFItemComponent ) public readonly sfItemComp ? , @ Inject ( SFComponent ) public readonly sfComp ? ) { } ngAfterViewInit ( ) { this . formProperty . errorsChanges . pipe ( takeUntil ( this . sfItemComp ! . unsubscribe$ ) ) . subscribe ( ( errors ) => { if ( errors == null ) return ; di ( this . ui , 'errorsChanges' , this . formProperty . path , errors ) ; if ( this . firstVisual ) { this . showError = errors . length > 0 ; this . error = this . showError ? ( errors [ 0 ] . message as string ) : '' ; this . cd . detectChanges ( ) ; } this . firstVisual = true ; } ) ; this . afterViewInit ( ) ; } setValue ( value ) { this . formProperty . setValue ( value , false ) ; di ( this . ui , 'valueChanges' , this . formProperty . path , this . formProperty ) ; } get value ( ) : NzSafeAny { return this . formProperty . value ; } detectChanges ( onlySelf = false ) { if ( onlySelf ) { this . cd . markForCheck ( ) ; } else { this . formProperty . root . widget . cd . markForCheck ( ) ; } } abstract reset ( value ) ; abstract afterViewInit ( ) ; } @ Directive ( ) export class ControlWidget extends Widget < FormProperty , SFUISchemaItem > { reset ( _value ) { } afterViewInit ( ) { } } @ Directive ( ) export class ControlUIWidget < UIT extends SFUISchemaItem > extends Widget < FormProperty , UIT > { reset ( _value ) { } afterViewInit ( ) { } } @ Directive ( ) export class ArrayLayoutWidget extends Widget < ArrayProperty , SFArrayWidgetSchema > implements AfterViewInit { reset ( _value ) { } afterViewInit ( ) { } ngAfterViewInit ( ) { this . formProperty . errorsChanges . pipe ( takeUntil ( this . sfItemComp ! . unsubscribe$ ) ) . subscribe ( ( ) => this . cd . detectChanges ( ) ) ; } } @ Directive ( ) export class ObjectLayoutWidget extends Widget < ObjectProperty , SFObjectWidgetSchema > implements AfterViewInit { reset ( _value ) { } afterViewInit ( ) { } ngAfterViewInit ( ) { this . formProperty . errorsChanges . pipe ( takeUntil ( this . sfItemComp ! . unsubscribe$ ) ) . subscribe ( ( ) => this . cd . detectChanges ( ) ) ; } }