import { Component , ElementRef , EventEmitter , Input , OnInit , Output } from '@angular/core' ; import { ControlValueAccessor , FormControl , NG_VALIDATORS , NG_VALUE_ACCESSOR , ValidationErrors , Validator , Validators } from '@angular/forms' ; import { RunService , OnExecSuccess } from '../run.service' ; import { startWith } from 'rxjs/operators' ; @ Component ( { selector : 'dv-input-id' , templateUrl : './input-id.component.html' , providers : [ { provide : NG_VALUE_ACCESSOR , useExisting : InputIdComponent , multi : true } , { provide : NG_VALIDATORS , useExisting : InputIdComponent , multi : true } ] } ) export class InputIdComponent implements OnInit , ControlValueAccessor , Validator , OnExecSuccess { @ Input ( ) entityName = 'Id' ; idControl = new FormControl ( '' , [ Validators . required ] ) ; @ Output ( ) id = new EventEmitter < string > ( ) ; constructor ( private elem , private rs ) { } ngOnInit ( ) { this . rs . register ( this . elem , this ) ; this . idControl . valueChanges . subscribe ( ( value ) => { this . id . emit ( value ) ; } ) ; this . idControl . valueChanges . pipe ( startWith ( this . idControl . value ) ) ; } writeValue ( value ) { if ( value === null ) { this . reset ( ) ; } else { this . idControl . setValue ( value ) ; } } registerOnChange ( fn ) { this . id . subscribe ( fn ) ; } registerOnTouched ( ) { } validate ( _c ) { return this . idControl . errors ; } dvOnExecSuccess ( ) { this . reset ( ) ; } reset ( ) { this . idControl . reset ( ) ; this . idControl . markAsUntouched ( ) ; this . idControl . markAsPristine ( ) ; } }