import { Component , EventEmitter , HostListener , OnDestroy , OnInit , Output , ViewChild } from '@angular/core' ; import { TypeService } from '../../services/type.service' ; import { CmdbType } from '../../models/cmdb-type' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { BehaviorSubject , Observable , ReplaySubject } from 'rxjs' ; import { CmdbMode } from '../../modes.enum' ; import { RenderComponent } from '../../render/render.component' ; import { CmdbObject } from '../../models/cmdb-object' ; import { UserService } from '../../../management/services/user.service' ; import { ObjectService } from '../../services/object.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { CategoryService } from '../../services/category.service' ; import { SidebarService } from '../../../layout/services/sidebar.service' ; import { AccessControlPermission } from '../../../acl/acl.types' ; import { ToastService } from '../../../layout/toast/toast.service' ; import { takeUntil } from 'rxjs/operators' ; @ Component ( { selector : 'cmdb-object-add' , templateUrl : './object-add.component.html' , styleUrls : [ './object-add.component.scss' ] } ) export class ObjectAddComponent implements OnInit , OnDestroy { public typeList : CmdbType [ ] = [ ] ; public typeIDForm : FormGroup ; private typeIDSubject : BehaviorSubject < number > ; public typeID : Observable < number > ; public typeInstance : CmdbType ; public mode : CmdbMode = CmdbMode . Create ; public objectInstance : CmdbObject ; public renderForm : FormGroup ; public fieldsGroups : FormGroup ; @ Output ( ) parentSubmit = new EventEmitter < any > ( ) ; @ ViewChild ( RenderComponent , { static : false } ) render : RenderComponent ; /**
     * Global un-subscriber for http calls to the rest backend.
     */ private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; constructor ( private router , private typeService , private categoryService , private objectService , private userService , private route , private sidebarService , private toastService ) { this . objectInstance = new CmdbObject ( ) ; this . typeIDSubject = new BehaviorSubject < number > ( null ) ; this . route . params . subscribe ( ( params ) => { if ( params . publicID !== undefined ) { this . typeIDSubject . next ( + params . publicID ) ; } } ) ; this . typeID = this . typeIDSubject . asObservable ( ) ; this . typeID . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( selectedTypeID => { if ( selectedTypeID !== null ) { this . typeService . getType ( selectedTypeID ) . subscribe ( ( typeInstance ) => { this . typeInstance = typeInstance ; } ) ; } } ) ; this . fieldsGroups = new FormGroup ( { } ) ; this . renderForm = new FormGroup ( { active : new FormControl ( true ) } ) ; } public ngOnInit ( ) { this . typeService . getTypeList ( AccessControlPermission . CREATE ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( typeList ) => { this . typeList = typeList ; } , ( e ) => { this . toastService . error ( e ) ; } ) ; this . typeIDForm = new FormGroup ( { typeID : new FormControl ( null , Validators . required ) } ) ; } @ 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 ngOnDestroy ( ) { this . typeIDSubject . unsubscribe ( ) ; this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } public get formTypeID ( ) { return this . typeIDForm . get ( 'typeID' ) . value ; } public useTypeID ( ) { this . typeIDSubject . next ( this . formTypeID ) ; } public get currentTypeID ( ) { return this . typeIDSubject . value ; } public saveObject ( ) { this . renderForm . markAllAsTouched ( ) ; if ( this . renderForm . valid ) { this . objectInstance . type_id = this . currentTypeID ; this . objectInstance . version = '1.0.0' ; this . objectInstance . author_id = this . userService . getCurrentUser ( ) . public_id ; this . objectInstance . fields = [ ] ; this . render . renderForm . removeControl ( 'active' ) ; Object . keys ( this . render . renderForm . controls ) . forEach ( field => { let val = this . renderForm . value [ field ] ; if ( val === undefined || val == null ) { val = '' ; } this . objectInstance . fields . push ( { name : field , value : val } ) ; } ) ; let ack = null ; this . objectService . postObject ( this . objectInstance ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( newObjectID => { ack = newObjectID ; } , ( e ) => { console . error ( e ) ; } , ( ) => { this . router . navigate ( [ '/framework/object/view/' + ack ] ) ; this . sidebarService . updateTypeCounter ( this . typeInstance . public_id ) ; this . toastService . success ( `Object ${ ack } was created succesfully!` ) ; } ) ; } } }