import { Input , EventEmitter , Output , OnChanges , SimpleChanges , Directive } from '@angular/core' ; import { createValueForSchema , jsonSchemaDataTypes , dataTypeMap , inferType , getIcon , getCurrentType , JsonSchemaDataType , JSONEditorSchema , jsonSchemaDataFormats } from '../json-editor.helper' ; @ Directive ( ) export class ArrayNode implements OnChanges { @ Input ( ) schema : JSONEditorSchema ; @ Input ( ) model : any [ ] ; @ Input ( ) required = false ; @ Input ( ) expanded : boolean ; @ Input ( ) path : string ; @ Input ( ) errors : any [ ] ; @ Input ( ) typeCheckOverrides ? : any ; @ Input ( ) schemaRef : JSONEditorSchema ; @ Input ( ) showKnownProperties = false ; @ Output ( ) modelChange : EventEmitter < any [ ] > = new EventEmitter ( ) ; @ Output ( ) schemaUpdate : EventEmitter < JSONEditorSchema > = new EventEmitter ( ) ; requiredCache : any = { } ; schemas : JSONEditorSchema [ ] = [ ] ; dataTypes : JsonSchemaDataType [ ] = [ ... jsonSchemaDataTypes , ... jsonSchemaDataFormats ] ; dataTypeMap = dataTypeMap ; _array = Array ; ngOnChanges ( changes ) { if ( changes . schema ) { if ( this . schema && this . schema . required ) { for ( const prop of this . schema . required ) { this . requiredCache [ prop ] = true ; } } } this . initSchemasTypeByModelValue ( ) ; this . updateIcons ( ) ; } param index param value updateArrayItem ( index , value ) { this . model [ index ] = value ; this . modelChange . emit ( this . model ) ; } /**
     * Adds a new item to the model
     */ addArrayItem ( dataType ? ) { let schema ; if ( dataType ) { schema = JSON . parse ( JSON . stringify ( { ... ( this . schema . items as any ) , ... dataType . schema } ) ) ; } else { schema = JSON . parse ( JSON . stringify ( this . schema . items ) ) ; } if ( ! schema . type ) { schema . type = 'object' ; } if ( ! schema . $meta ) { schema . $meta = { } ; } if ( Array . isArray ( schema . type ) ) { schema . $meta . type = [ ... schema . type ] ; schema . type = schema . type [ 0 ] ; schema . $meta . currentType = getCurrentType ( schema ) ; } const value = createValueForSchema ( schema ) ; if ( value !== undefined ) { this . model . push ( value ) ; this . schemas . push ( schema ) ; } this . modelChange . emit ( this . model ) ; this . updateIcons ( ) ; } param index deleteArrayItem ( index ) { this . model . splice ( index , 1 ) ; this . schemas . splice ( index , 1 ) ; this . model = [ ... this . model ] ; this . schemas = [ ... this . schemas ] ; this . modelChange . emit ( this . model ) ; } param index param value arrayTrackBy ( index ) { return index ; } param property param type changeItemType ( index , type ) { const schema = this . schemas [ index ] ; const dataType = this . dataTypeMap [ type ] ; if ( dataType ) { delete schema . format ; schema . type = dataType . schema . type ; if ( dataType . schema . format ) { schema . format = dataType . schema . format ; } schema . $meta . currentType = getCurrentType ( schema ) ; } const value = createValueForSchema ( schema ) ; this . model [ index ] = value ; this . modelChange . emit ( this . model ) ; this . updateIcons ( ) ; } /**
     * Infers the schema type for each item in the array
     */ private initSchemasTypeByModelValue ( ) { this . schemas = [ ] ; if ( Array . isArray ( this . model ) ) { this . model . forEach ( value => { let schema = inferType ( value , this . typeCheckOverrides ) ; if ( this . schema . items ) { schema = JSON . parse ( JSON . stringify ( { ... ( this . schema . items as any ) , ... schema } ) ) ; } this . schemas . push ( schema ) ; } ) ; } } /**
     * Updates the icons in the schemas
     */ private updateIcons ( ) { for ( const schema of this . schemas ) { if ( ! schema . $meta ) { schema . $meta = { } ; } schema . $meta . icon = getIcon ( schema ) ; } } }