import { AlainSFConfig } from '@delon/util/config' ; import type { NzSafeAny } from 'ng-zorro-antd/core/types' ; import { SF_SEQ } from '../const' ; import { SFValue } from '../interface' ; import { SFSchema } from '../schema/index' ; import { SFUISchema , SFUISchemaItem } from '../schema/ui' ; import { SchemaValidatorFactory } from '../validator.factory' ; import { FormProperty , PropertyGroup } from './form.property' ; import { FormPropertyFactory } from './form.property.factory' ; import { ObjectProperty } from './object.property' ; export class ArrayProperty extends PropertyGroup { constructor ( private formPropertyFactory , schemaValidatorFactory , schema , ui , formData , parent , path , options ) { super ( schemaValidatorFactory , schema , ui , formData , parent , path , options ) ; this . properties = [ ] ; } getProperty ( path ) { const subPathIdx = path . indexOf ( SF_SEQ ) ; const pos = + ( subPathIdx !== - 1 ? path . substring ( 0 , subPathIdx ) : path ) ; const list = this . properties as PropertyGroup [ ] ; if ( isNaN ( pos ) || pos >= list . length ) { return undefined ; } const subPath = path . substring ( subPathIdx + 1 ) ; return list [ pos ] . getProperty ( subPath ) ; } setValue ( value , onlySelf ) { this . properties = [ ] ; this . clearErrors ( ) ; this . resetProperties ( value ) ; this . updateValueAndValidity ( { onlySelf , emitValueEvent : true } ) ; } resetValue ( value , onlySelf ) { this . _value = value || this . schema . default || [ ] ; this . setValue ( this . _value , onlySelf ) ; } _hasValue ( ) { return true ; } _updateValue ( ) { const value = [ ] ; this . forEachChild ( ( property ) => { if ( property . visible ) { value . push ( { ... ( this . widget ?. cleanValue ? null : property . formData ) , ... property . value } ) ; } } ) ; this . _value = value ; } private addProperty ( formData ) { const newProperty = this . formPropertyFactory . createProperty ( this . schema . items ! , this . ui . $items , formData , this as PropertyGroup ) as ObjectProperty ; ( this . properties as FormProperty [ ] ) . push ( newProperty ) ; return newProperty ; } private resetProperties ( formDatas ) { for ( const item of formDatas ) { const property = this . addProperty ( item ) ; property . resetValue ( item , true ) ; } } private clearErrors ( property ? ) { ( property || this ) . _objErrors = { } ; } add ( formData ) { const newProperty = this . addProperty ( formData ) ; newProperty . resetValue ( formData , false ) ; return newProperty ; } remove ( index ) { const list = this . properties as FormProperty [ ] ; this . clearErrors ( ) ; list . splice ( index , 1 ) ; list . forEach ( ( property , idx ) => { property . path = [ property . parent ! . path , idx ] . join ( SF_SEQ ) ; this . clearErrors ( property ) ; if ( property instanceof ObjectProperty ) { property . forEachChild ( p => { p . updateValueAndValidity ( ) ; } ) ; } } ) ; if ( list . length === 0 ) { this . updateValueAndValidity ( ) ; } } }