import { Input , Directive } from "@angular/core" ; import { AbstractControl } from "@angular/forms" ; import { defaultContainer } from "../core/defaultContainer" ; import { OBJECT_PROPERTY , PROPERTY } from "../const" ; import { InstanceContainer , PropertyInfo } from '../core/validator.interface' ; import { ApplicationUtil } from "../util/app-util" ; import { TEMPLATE_VALIDATION_CONFIG } from '../const/app.const' ; @ Directive ( ) export abstract class BaseDirective { @ Input ( ) model : any ; applyValidations ( controls , model = null ) { if ( this . model ) { let modelConfig = defaultContainer . get ( model || this . model . constructor ) ; if ( modelConfig ) { modelConfig . properties . forEach ( property => { if ( controls [ property . name ] ) { switch ( property . propertyType ) { case PROPERTY : this . setValidatorConfig ( controls [ property . name ] , modelConfig , property ) ; break ; case OBJECT_PROPERTY : this . applyValidations ( controls [ property . name ] . controls , property . entity ) ; break ; } } } ) ; } } } private setValidatorConfig ( control , modelConfig , property ) { let annotations = modelConfig . propertyAnnotations . filter ( t => t . propertyName == property . name ) ; annotations . forEach ( annotation => { if ( ! control [ TEMPLATE_VALIDATION_CONFIG ] ) control [ TEMPLATE_VALIDATION_CONFIG ] = { } ; ApplicationUtil . configureControl ( control , annotation . config ? annotation . config : "" , annotation . annotationType ) ; } ) ; } }