import { Component , OnDestroy , ViewChild } from '@angular/core' ; import { FormGroup } from '@angular/forms' ; import { ObjectService } from '../../services/object.service' ; import { TypeService } from '../../services/type.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { RenderResult } from '../../models/cmdb-render' ; import { ReplaySubject } from 'rxjs' ; import { CmdbType } from '../../models/cmdb-type' ; import { httpObserveOptions } from '../../../services/api-call.service' ; import { CmdbObject } from '../../models/cmdb-object' ; import { takeUntil } from 'rxjs/operators' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { UserService } from '../../../management/services/user.service' ; import { ProgressSpinnerService } from '../../../layout/progress/progress-spinner.service' ; import { WizardComponent } from 'angular-archwizard' ; import { APIUpdateMultiResponse } from '../../../services/models/api-response' ; @ Component ( { selector : 'cmdb-object-bulk-change' , templateUrl : './object-bulk-change.component.html' , styleUrls : [ './object-bulk-change.component.scss' ] } ) export class ObjectBulkChangeComponent implements OnDestroy { /**
     * The aw-wizard component defines the root component of a wizard.
     */ @ ViewChild ( 'wizard' , { static : false } ) public wizard : WizardComponent ; private private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; /**
     * Type of objects to bulk change.
     */ public type : CmdbType ; /**
     * Form for bulk change editor.
     */ public changeForm : FormGroup = new FormGroup ( { } ) ; public renderForm : FormGroup = new FormGroup ( { } ) ; /**
     * Active status changed.
     */ public activeState : boolean | undefined = undefined ; /**
     * List of to change selected items.
     */ public renderResults : Array < RenderResult > = [ ] ; /**
     * APIUpdateMultiResponse
     */ public apiResponse : APIUpdateMultiResponse ; private private httpOptions ; constructor ( private objectService , private typeService , private userServer , private activeRoute , private router , private toastService , private spinner ) { if ( this . router . getCurrentNavigation ( ) . extras . state ) { this . type = this . router . getCurrentNavigation ( ) . extras . state . type ; this . renderResults = this . router . getCurrentNavigation ( ) . extras . state . objects ; this . httpOptions = Object . assign ( { } , httpObserveOptions ) ; this . httpOptions . params = { objectIDs : this . renderResults . map ( m => m . object_information . object_id ) } ; } } /**
     * Was the form touched.
     */ public get hasChanges ( ) : boolean { return this . renderForm . dirty ; } /**
     * Save a references object to the database.
     */ public saveObject ( ) { this . spinner . show ( ) ; const changes = this . changeForm . getRawValue ( ) ; const newObjectInstance = new CmdbObject ( ) ; if ( this . activeState !== undefined ) { newObjectInstance . active = this . activeState ; } newObjectInstance . author_id = this . userServer . getCurrentUser ( ) . public_id ; newObjectInstance . type_id = this . type . public_id ; newObjectInstance . fields = [ ] ; Object . keys ( changes ) . forEach ( ( key ) => { newObjectInstance . fields . push ( { name : key , value : changes [ key ] } ) ; } ) ; this . objectService . putObject ( 0 , newObjectInstance , this . httpOptions ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( response ) => { this . apiResponse = response ; this . goToNextStepIndex ( 2 ) ; } , ( ) => { this . spinner . hide ( ) ; } , ( ) => { this . spinner . hide ( ) ; } ) ; } private goToNextStepIndex ( index ) { if ( this . wizard ) { this . wizard . goToStep ( index ) ; } } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }