import { Component , OnDestroy , OnInit } from '@angular/core' ; import { TypeService } from '../../../../services/type.service' ; import { ConfigEditBaseComponent } from '../config.edit' ; import { RenderResult } from '../../../../models/cmdb-render' ; import { ObjectService } from '../../../../services/object.service' ; import { CmdbType } from '../../../../models/cmdb-type' ; import { CollectionParameters } from '../../../../../services/models/api-parameter' ; import { takeUntil , tap } from 'rxjs/operators' ; import { APIGetMultiResponse } from '../../../../../services/models/api-response' ; import { Sort , SortDirection } from '../../../../../layout/table/table.types' ; import { ReplaySubject } from 'rxjs' ; import { ToastService } from '../../../../../layout/toast/toast.service' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { nameConvention } from '../../../../../layout/directives/name.directive' ; import { ChangeDetectorRef } from '@angular/core' ; @ Component ( { selector : 'cmdb-ref-field-edit' , templateUrl : './ref-field-edit.component.html' , styleUrls : [ './ref-field-edit.component.scss' ] , } ) export class RefFieldEditComponent extends ConfigEditBaseComponent implements OnInit , OnDestroy { constructor ( private typeService , private objectService , private toast , private cd ) { super ( ) ; } protected protected subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Name form control.
     */ public nameControl : FormControl = new FormControl ( '' , Validators . required ) ; /**
     * Label form control.
     */ public labelControl : FormControl = new FormControl ( '' , Validators . required ) ; /**
     * Type form control.
     */ public typeControl : FormControl = new FormControl ( undefined , Validators . required ) ; /**
     * Summary form control.
     */ public summaryControl : FormControl = new FormControl ( undefined , Validators . required ) ; /**
     * Type list for reference selection
     */ public typesAPIResponse : APIGetMultiResponse < CmdbType > ; public typeList : Array < CmdbType > = [ ] ; public filteredTypeList : CmdbType [ ] = [ ] ; public totalTypes : number = 0 ; /**
     * Type loading indicator.
     */ public typeLoading : boolean = false ; /**
     * Begin with first page.
     */ public readonly initPage : number = 1 ; public page : number = this . initPage ; /**
     * Filter query from the table search input.
     */ public filter : string ; /**
     * Default sort filter.
     */ public sort : Sort = { name : 'public_id' , order : SortDirection . DESCENDING } as Sort ; /**
     * Nested summaries
     */ public summaries : any [ ] = [ ] ; /**
     * Object list for default reference value
     */ public objectList : RenderResult [ ] ; /**
     * Types params
     */ public typesParams : CollectionParameters = { filter : undefined , limit : 0 , sort : 'public_id' , order : 1 , page : 1 } ; /**
     * Reference form control.
     */ public referenceGroup : FormGroup = new FormGroup ( { type_id : this . typeControl , } ) ; public ngOnInit ( ) { this . form . addControl ( 'name' , this . nameControl ) ; this . form . addControl ( 'label' , this . labelControl ) ; this . form . addControl ( 'ref_types' , this . typeControl ) ; this . form . addControl ( 'summaries' , this . summaryControl ) ; this . disableControlOnEdit ( this . nameControl ) ; this . patchData ( this . data , this . form ) ; this . triggerAPICall ( ) ; } public triggerAPICall ( ) { this . typeLoading = true ; this . typeService . getTypes ( this . typesParams ) . pipe ( takeUntil ( this . subscriber ) ) . pipe ( tap ( ( ) => this . typeLoading = false ) ) . subscribe ( ( apiResponse ) => { this . typeList = [ ... apiResponse . results as Array < CmdbType > ] ; this . totalTypes = apiResponse . total ; this . typeLoading = false ; this . prepareSummaries ( ) ; this . cd . markForCheck ( ) ; } , ( err ) => this . toast . error ( err ) , ( ) => { if ( this . data . ref_types ) { this . objectService . getObjectsByType ( this . data . ref_types ) . subscribe ( ( res ) => { this . objectList = res ; this . cd . markForCheck ( ) ; } ) ; } } ) ; } private private prepareSummaries ( ) { if ( this . data . ref_types ) { if ( ! Array . isArray ( this . data . ref_types ) ) { this . data . ref_types = [ this . data . ref_types ] ; } this . filteredTypeList = this . typeList . filter ( type => this . data . ref_types . includes ( type . public_id ) ) ; this . data . summaries = this . data . summaries ? this . data . summaries : this . summaries ; this . summaries = this . data . summaries ; } } public onChange ( ) { const { ref_types } = this . data ; if ( Array . isArray ( this . data . ref_types ) && ref_types && ref_types . length === 0 ) { this . objectList = [ ] ; this . filteredTypeList = [ ] ; this . data . value = '' ; } else { this . objectService . getObjectsByType ( ref_types ) . subscribe ( ( res ) => { this . objectList = res ; this . prepareSummaries ( ) ; } ) ; } } public summaryFieldFilter ( id ) { const found = this . typeList . find ( s => s . public_id === id ) ; return id ? found ? found . fields : [ ] : [ ] ; } public changeSummaryOption ( type ) { const nestedSummary = this . summaries . find ( s => s . type_id === type . public_id ) ; nestedSummary . label = type . label ; nestedSummary . icon = type . render_meta . icon ; } public changeDefault ( value ) { this . data . default = parseInt ( value , 10 ) ; return this . data . default ; } public addSummary ( ) { if ( this . filteredTypeList ) { this . summaries . push ( { type_id : null , line : null , label : null , fields : [ ] , icon : null , prefix : false , } ) ; } } public delSummary ( value ) { if ( this . summaries . length > 0 ) { const index = this . summaries . indexOf ( value , 0 ) ; if ( index > - 1 ) { this . summaries . splice ( index , 1 ) ; } } } param name public onNameChange ( name ) { this . data . name = nameConvention ( name ) ; } /**
     * Destroy subscriptions after closed.
     */ public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }