import { Input , Output , EventEmitter , OnChanges , SimpleChanges , ChangeDetectorRef , Directive } from '@angular/core' ; import { SchemaValidatorService } from './schema-validator.service' ; import { JSONEditorSchema } from './json-editor.helper' ; import { debounceable } from '../../decorators/debounceable/debounceable.decorator' ; @ Directive ( ) export class JsonEditor implements OnChanges { @ Input ( ) model : any ; @ Input ( ) schema : JSONEditorSchema ; @ Input ( ) label : string ; @ Input ( ) typeCheckOverrides ? : any ; @ Input ( ) schemaValidator ? : ( schema : any , ... args : any [ ] ) => any [ ] ; @ Input ( ) showKnownProperties = false ; @ Output ( ) modelChange : EventEmitter < any > = new EventEmitter ( ) ; @ Output ( ) schemaUpdate : EventEmitter < JSONEditorSchema > = new EventEmitter ( ) ; errors : any [ ] ; constructor ( protected schemaValidatorService , protected cdr ) { } ngOnChanges ( changes ) { if ( changes . schema ) { this . schema = JSON . parse ( JSON . stringify ( this . schema ) ) ; } if ( ! this . schema ) { this . schema = { type : 'object' } ; } if ( ! this . schema . type ) { this . schema . type = 'object' ; } } param model modelChangedCallback ( model ) { this . validate ( this . schema , model ) ; this . modelChange . emit ( model ) ; } param schema param model @ debounceable ( 120 ) validate ( schema , model ) { this . errors = this . schemaValidator ? this . schemaValidator ( schema , model ) : this . schemaValidatorService . validate ( schema , model ) ; this . cdr . markForCheck ( ) ; return this . errors && this . errors . length > 0 ; } }