import { Component , Input , OnChanges } from '@angular/core' ; import { HtmlUtils } from 'src/app/Utils/HtmlUtils' ; import { Constants } from 'src/app/Common/Constants' ; import { Utils } from 'src/app/Utils/Utils' ; import { ITextAndBadge } from 'src/app/Utils/ValueResolver' ; import size from 'lodash/size' ; import forOwn from 'lodash/forOwn' ; import startCase from 'lodash/startCase' ; import camelCase from 'lodash/camelCase' ; import isNumber from 'lodash/isNumber' ; import isBoolean from 'lodash/isBoolean' ; import isUndefined from 'lodash/isUndefined' ; import isNull from 'lodash/isNull' ; import isEmpty from 'lodash/isEmpty' ; import isObject from 'lodash/isObject' ; import first from 'lodash/first' ; export class ResolvedObject { [ index ] : any ; } @ Component ( { selector : 'app-detail-view-part' , templateUrl : './detail-view-part.component.html' , styleUrls : [ './detail-view-part.component.scss' ] } ) export class DetailViewPartComponent implements OnChanges { @ Input ( ) noFixedLayout = false ; resolvedData : any ; @ Input ( ) data : any ; @ Input ( ) title : string ; @ Input ( ) parent : any ; constructor ( ) { } ngOnChanges ( ) { if ( this . parent ) { this . resolvedData = this . getResolvedDataObjectInternal ( this . data , this . parent ) ; } else { this . resolvedData = this . getResolvedDataObject ( this . data ) ; } } public getResolvedObjectSize ( object ) { return size ( object ) ; } public getResolvedPropertyType ( value ) { if ( this . isResolvedObject ( value ) ) { return 'Object' ; } else if ( this . isArray ( value ) ) { return 'Array' ; } else if ( this . isHtml ( value ) ) { return 'Html' ; } else if ( this . isISODate ( value ) ) { return 'Date' ; } else { return 'Value' ; } } public getResolvedDataObject ( data , preserveEmptyProperties = false ) { if ( ! data ) { return null ; } if ( data instanceof ResolvedObject ) { return data ; } if ( data . hasOwnProperty ( 'raw' ) ) { if ( data . raw === undefined || data . raw === null ) { return null ; } return this . getResolvedDataObjectInternal ( data . raw , data , preserveEmptyProperties ) ; } return this . getResolvedDataObjectInternal ( data , null , preserveEmptyProperties ) ; } private isResolvedObject ( value ) { return value instanceof ResolvedObject ; } private isArray ( value ) { return Array . isArray ( value ) ; } private isHtml ( value ) { return HtmlUtils . isHtml ( value ) ; } private isISODate ( value ) { return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ . test ( value ) ; } private getResolvedDataObjectInternal ( data , parent , preserveEmptyProperties = false ) { const resolvedObject = new ResolvedObject ( ) ; forOwn ( data , ( value , name ) => { let resolvedName = startCase ( name ) ; let resolvedValue = null ; if ( parent && parent . decorators ) { if ( parent . decorators . showList && ! parent . decorators . showList . includes ( name ) ) { return ; } else if ( parent . decorators . hideList && parent . decorators . hideList . includes ( name ) ) { return ; } if ( parent . decorators . decorators && parent . decorators . decorators [ name ] ) { if ( parent . decorators . decorators [ name ] . displayName ) { resolvedName = parent . decorators . decorators [ name ] . displayName ( name ) ; } if ( parent . decorators . decorators [ name ] . displayValueInHtml ) { resolvedValue = parent . decorators . decorators [ name ] . displayValueInHtml ( value ) ; } } } if ( ! resolvedValue ) { if ( parent ) { resolvedValue = parent [ name ] || parent [ camelCase ( name ) ] ; } if ( ! resolvedValue ) { resolvedValue = data [ name ] ; } } if ( isNumber ( resolvedValue ) || isBoolean ( resolvedValue ) ) { } else if ( isUndefined ( resolvedValue ) || isNull ( resolvedValue ) || isEmpty ( resolvedValue ) ) { if ( preserveEmptyProperties ) { resolvedValue = Constants . Empty ; } else { resolvedValue = null ; } } else if ( Array . isArray ( resolvedValue ) ) { if ( ! isObject ( first ( resolvedValue ) ) ) { resolvedValue = `[${ resolvedValue . map ( v => v . toString ( ) ) . join ( ', ' ) }]` ; } else { resolvedValue = resolvedValue . map ( v => this . getResolvedDataObject ( v , true ) ) ; } } else if ( isObject ( resolvedValue ) ) { if ( Utils . isBadge ( resolvedValue ) ) { resolvedValue = resolvedValue as ITextAndBadge ; if ( resolvedValue . text && resolvedValue . badgeClass ) { resolvedValue = HtmlUtils . getBadgeHtml ( resolvedValue ) ; } else { resolvedValue = resolvedValue . text ; } } else { resolvedValue = this . getResolvedDataObject ( resolvedValue ) ; } } if ( isEmpty ( resolvedName ) ) { resolvedName = Constants . Empty ; } if ( resolvedValue !== null ) { resolvedObject [ resolvedName ] = resolvedValue ; } } ) ; return size ( resolvedObject ) > 0 ? resolvedObject : null ; } asIsOrder ( a , b ) { return 1 ; } }