import { RxFormArray } from "../services/rx-form-array" ; import { NumericValueType } from '../enums' ; import { ReactiveFormConfig } from "./reactive-form-config" ; import { AbstractControl } from "../abstract/abstract-control" ; const MODEL_INSTANCE_VALUE = "modelInstanceValue" ; export class ApplicationUtil { static getParentObjectValue ( control ) { if ( control . parent ) { let parent = this . parentObjectValue ( control . parent ) ; return parent . value ; } return { } ; } static getParentModelInstanceValue ( control ) { if ( control . parent ) { let parent = this . parentObjectValue ( control . parent ) ; return parent [ MODEL_INSTANCE_VALUE ] ; } return { } ; } static getRootFormGroup ( control ) { if ( control . parent ) { return this . getRootFormGroup ( control . parent ) ; } return control ; } private static getParentControl ( control ) { if ( control . parent ) { let parent = this . parentObjectValue ( control . parent ) ; return parent ; } return control ; } static getFormControlName ( control ) { let controlName = '' ; if ( control . parent ) { for ( var formControlName in control . parent . controls ) { if ( control . parent . controls [ formControlName ] == control ) { controlName = formControlName ; break ; } } } return controlName ; } static getParentFormArray ( control ) { if ( control . parent && ! ( control . parent instanceof RxFormArray || control . parent instanceof RxFormArray ) ) { let parent = this . getParentFormArray ( control . parent ) ; return parent ; } return control . parent ; } static toLower ( value ) { if ( value ) return String ( value ) . toLowerCase ( ) ; return value ; } static getControl ( fieldName , formGroup ) { let splitText = fieldName . split ( '.' ) ; if ( splitText . length > 1 ) { var formControl = formGroup ; splitText . forEach ( ( name , index ) => { formControl = formControl . controls [ name ] ; } ) ; return formControl ; } else return formGroup . controls [ fieldName ] ; } static getFormControl ( fieldName , control ) { let splitText = fieldName . split ( '.' ) ; if ( splitText . length > 1 && control . parent ) { var formControl = this . getParentControl ( control ) ; splitText . forEach ( ( name , index ) => { formControl = formControl . controls [ name ] ; } ) ; return formControl ; } return ( control . parent ) ? control . parent . get ( [ fieldName ] ) : undefined ; } private static parentObjectValue ( control ) { if ( ! control . parent ) return control ; else control = this . parentObjectValue ( control . parent ) ; return control ; } static isNumeric ( value ) { return ( value - parseFloat ( value ) + 1 ) >= 0 ; } static notEqualTo ( primaryValue , secondaryValue ) { let firstValue = ( primaryValue === undefined || primaryValue === null ) ? "" : primaryValue ; let secondValue = ( secondaryValue === undefined || secondaryValue === null ) ? "" : secondaryValue ; if ( firstValue instanceof Date && secondValue instanceof Date ) return + firstValue != + secondValue ; return ( firstValue != secondValue ) ; } static numericValidation ( allowDecimal , acceptValue ) { let decimalSymbol ; if ( ReactiveFormConfig && ReactiveFormConfig . number ) { decimalSymbol = ( ReactiveFormConfig . json && ReactiveFormConfig . json . allowDecimalSymbol ) ? ReactiveFormConfig . json . allowDecimalSymbol : ReactiveFormConfig . number . decimalSymbol ; } else { decimalSymbol = "." ; } acceptValue = ( acceptValue == undefined ) ? NumericValueType . PositiveNumber : acceptValue ; let regex = /^[0-9]+$/ ; switch ( acceptValue ) { case NumericValueType . PositiveNumber : regex = ( ! allowDecimal ) ? /^[0-9]+$/ : ( decimalSymbol == "." || decimalSymbol == undefined ) ? /^[0-9\.]+$/ : /^[0-9\,]+$/ ; break ; case NumericValueType . NegativeNumber : regex = ( ! allowDecimal ) ? /^[-][0-9]+$/ : ( decimalSymbol == "." || decimalSymbol == undefined ) ? /^[-][0-9\.]+$/ : /^[-][0-9\,]+$/ ; break ; case NumericValueType . Both : regex = ( ! allowDecimal ) ? /^[-|+]?[0-9]+$/ : ( decimalSymbol == "." || decimalSymbol == undefined ) ? /^[-|+]?[0-9\.]+$/ : /^[-|+]?[0-9\,]+$/ ; break ; } return regex ; } static configureControl ( control , config , type ) { if ( ! control . validatorConfig ) { let jObject = { } ; jObject [ type ] = config ; Object . assign ( control , { validatorConfig : jObject } ) ; } else control . validatorConfig [ type ] = config ; } static lowerCaseWithTrim ( value ) { return typeof value === "string" ? value . toLowerCase ( ) . trim ( ) : String ( value ) . toLowerCase ( ) . trim ( ) ; } /** Check if a value is an object */ static isObject ( value ) { return Object . prototype . toString . call ( value ) === '[object Object]' ; } /** Check if a value is an object */ static isArray ( value ) { return Array . isArray ( value ) ; } static cloneValue ( value ) { return ApplicationUtil . isObject ( value ) ? ApplicationUtil . isArray ( value ) ? [ ... value ] : { ... value } : value ; } }