import { Component , OnInit } from '@angular/core' ; import { CmdbType } from '../../framework/models/cmdb-type' ; import { FormControl , FormGroup , Validators } from '@angular/forms' ; import { DatePipe } from '@angular/common' ; import { TypeService } from '../../framework/services/type.service' ; import { FileSaverService } from 'ngx-filesaver' ; import { FileService } from '../export.service' ; import { SupportedExporterExtension } from './model/supported-exporter-extension' ; import { CollectionParameters } from '../../services/models/api-parameter' ; @ Component ( { selector : 'cmdb-export-objects' , templateUrl : './export-objects.component.html' , styleUrls : [ './export-objects.component.scss' ] } ) export class ExportObjectsComponent implements OnInit { public typeList : CmdbType [ ] ; public formatList : SupportedExporterExtension [ ] = [ ] ; public formExport : FormGroup ; public isSubmitted = false ; constructor ( private exportService , private datePipe , private typeService , private fileSaverService ) { this . formExport = new FormGroup ( { type : new FormControl ( null , Validators . required ) , format : new FormControl ( null , Validators . required ) } ) ; } ngOnInit ( ) { this . typeService . getTypeList ( ) . subscribe ( data => { this . typeList = data ; } ) ; this . exportService . callFileFormatRoute ( ) . subscribe ( data => { this . formatList = data ; } ) ; } get type ( ) { return this . formExport . get ( 'type' ) ; } get format ( ) { return this . formExport . get ( 'format' ) ; } private resetForm ( ) { this . formExport . reset ( ) ; this . formExport . markAsPristine ( ) ; this . formExport . markAsUntouched ( ) ; this . formExport . markAsDirty ( ) ; } public exportObjectByTypeID ( ) { this . isSubmitted = true ; if ( ! this . formExport . valid ) { return false ; } const typeID = this . formExport . get ( 'type' ) . value ; const fileExtension = this . formExport . get ( 'format' ) . value ; this . resetForm ( ) ; if ( fileExtension != null && typeID != null ) { const filter = { type_id : typeID } ; const optional = { classname : fileExtension , zip : false } ; const exportAPI = { filter , optional , order : 1 , sort : 'public_id' } ; this . exportService . callExportRoute ( exportAPI ) . subscribe ( res => this . downLoadFile ( res , fileExtension ) ) ; } } public downLoadFile ( data , exportType ) { const timestamp = this . datePipe . transform ( new Date ( ) , 'MM_dd_yyyy_hh_mm_ss' ) ; const extension = this . formatList . find ( x => x . extension === exportType ) ; this . fileSaverService . save ( data . body , timestamp + '.' + extension . label ) ; } }