import { Component , HostListener , OnInit } from '@angular/core' ; import { ApiCallService } from '../../../services/api-call.service' ; import { ObjectService } from '../../services/object.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { CmdbMode } from '../../modes.enum' ; import { CmdbObject } from '../../models/cmdb-object' ; import { FormGroup } from '@angular/forms' ; import { UserService } from '../../../management/services/user.service' ; import { RenderResult } from '../../models/cmdb-render' ; import { CmdbType } from '../../models/cmdb-type' ; import { TypeService } from '../../services/type.service' ; import { SidebarService } from '../../../layout/services/sidebar.service' ; import { ToastService } from '../../../layout/toast/toast.service' ; @ Component ( { selector : 'cmdb-object-copy' , templateUrl : './object-copy.component.html' , styleUrls : [ './object-copy.component.scss' ] } ) export class ObjectCopyComponent implements OnInit { public mode : CmdbMode = CmdbMode . Edit ; private objectID : number ; public typeInstance : CmdbType ; public renderResult : RenderResult ; public renderForm : FormGroup ; constructor ( private api , private objectService , private typeService , private route , private router , private userService , private sidebarService , private toastService ) { this . route . params . subscribe ( ( params ) => { this . objectID = params . publicID ; } ) ; this . renderForm = new FormGroup ( { } ) ; } public ngOnInit ( ) { this . objectService . getObject ( this . objectID ) . subscribe ( ( rr ) => { this . renderResult = rr ; } , error => { console . error ( error ) ; } , ( ) => { this . typeService . getType ( this . renderResult . type_information . type_id ) . subscribe ( ( value ) => { this . typeInstance = value ; } ) ; } ) ; } @ HostListener ( 'window:scroll' ) onWindowScroll ( ) { const dialog = document . getElementById ( 'object-form-action' ) ; if ( document . body . scrollTop > 20 || document . documentElement . scrollTop > 20 ) { dialog . style . visibility = 'visible' ; } else { dialog . style . visibility = 'hidden' ; } } public copyObject ( ) { this . renderForm . markAllAsTouched ( ) ; if ( this . renderForm . valid ) { const newObjectInstance = new CmdbObject ( ) ; newObjectInstance . type_id = this . renderResult . type_information . type_id ; newObjectInstance . active = this . renderResult . type_information . active ; newObjectInstance . version = '1.0.0' ; newObjectInstance . author_id = this . userService . getCurrentUser ( ) . public_id ; newObjectInstance . fields = [ ] ; Object . keys ( this . renderForm . controls ) . forEach ( field => { newObjectInstance . fields . push ( { name : field , value : this . renderForm . get ( field ) . value } ) ; } ) ; let ack = null ; this . objectService . postObject ( newObjectInstance ) . subscribe ( newObjectID => { ack = newObjectID ; } , ( e ) => { console . error ( e ) ; } , ( ) => { console . log ( ack ) ; this . router . navigate ( [ '/framework/object/view/' + ack ] ) ; this . sidebarService . updateTypeCounter ( this . renderResult . type_information . type_id ) ; this . toastService . success ( `Object ${ this . objectID } was successfully copied into ${ ack }!` ) ; } ) ; } } }