import { COLON , BLANK , STRING , SQUARE } from '../../const/app.const' ; import { DynamicNodeConfig } from '../../models/interface/dynamic-node-config' ; import { objectPropValue } from '../../functions/object-prop-value.function' ; import { FormControlConfig } from "../../services/form-control-config" ; const PROPS = ":props." ; const GLOBAL_MATCH = "g" ; const DOT = "." ; export abstract class BaseObjectAccessor { controlConfig : FormControlConfig ; subscribeProps : { [ key ] : any ; } = { names : [ ] , props : { } } ; constructor ( public dynamicNodeConfig ) { this . controlConfig = this . dynamicNodeConfig . controlConfig ; } getPropName ( text ) { if ( text [ 0 ] == COLON || ( text [ 0 ] == SQUARE ) ) { return text . replace ( new RegExp ( COLON , GLOBAL_MATCH ) , BLANK ) . replace ( new RegExp ( SQUARE , GLOBAL_MATCH ) , BLANK ) ; } return text ; } getValue ( text ) { if ( typeof text == STRING && ( ( text [ 0 ] == COLON ) || ( text [ 0 ] == SQUARE ) ) ) { text = text . replace ( new RegExp ( COLON , GLOBAL_MATCH ) , BLANK ) . replace ( new RegExp ( SQUARE , GLOBAL_MATCH ) , BLANK ) ; return objectPropValue ( text , this . controlConfig ) ; } return text ; } setPropSubscription ( propName , type , attributeName , valuePropName = '' , parentPropName = '' ) { if ( propName . startsWith ( PROPS ) ) this . defineProp ( propName ) ; let prop = this . getPropName ( propName ) ; if ( ! this . subscribeProps . props [ prop ] ) this . subscribeProps . props [ prop ] = { } ; if ( ! this . subscribeProps . props [ prop ] [ type ] ) this . subscribeProps . props [ prop ] [ type ] = { } ; if ( parentPropName ) { this . subscribeProps . props [ prop ] [ type ] [ parentPropName ] = { } ; this . subscribeProps . props [ prop ] [ type ] [ parentPropName ] [ attributeName ] = ( valuePropName ) ? valuePropName : propName ; } else this . subscribeProps . props [ prop ] [ type ] [ attributeName ] = ( valuePropName ) ? valuePropName : propName ; if ( this . subscribeProps . names . indexOf ( prop ) == - 1 ) this . subscribeProps . names . push ( prop ) ; } isSubscribeProp ( propName ) { return ( typeof propName == STRING && ( propName [ 0 ] == COLON ) ) ; } defineProp ( propName ) { let splitText = propName . split ( DOT ) ; if ( splitText . length > 1 ) { let descriptor = Object . getOwnPropertyDescriptor ( Object . getPrototypeOf ( this . controlConfig . props ) , splitText [ 1 ] ) ; if ( ! descriptor ) this . controlConfig . defineProp ( splitText [ 1 ] ) ; } } }