import { Component , HostListener , Input , OnInit } from '@angular/core' ; import { FileMetadata } from '../../../components/file-explorer/model/metadata' ; import { FileElement } from '../../../components/file-explorer/model/file-element' ; import { CollectionParameters } from '../../../../services/models/api-parameter' ; import { FileService } from '../../../components/file-explorer/service/file.service' ; import { FileSaverService } from 'ngx-filesaver' ; import { NgbActiveModal , NgbModal , NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ; import { ToastService } from '../../../toast/toast.service' ; import { InfiniteScrollService } from '../../../services/infinite-scroll.service' ; import { APIGetMultiResponse } from '../../../../services/models/api-response' ; import { FilemanagerModalComponent } from '../filemanager-modal/filemanager-modal.component' ; @ Component ( { selector : 'cmdb-attachments-list-modal' , templateUrl : './attachments-list-modal.component.html' , styleUrls : [ './attachments-list-modal.component.scss' ] } ) export class AttachmentsListModalComponent implements OnInit { @ Input ( ) metadata : FileMetadata = new FileMetadata ( ) ; public inProcess : boolean = false ; public attachments : FileElement [ ] = [ ] ; public recordsTotal : number = 0 ; private modalRef : NgbModalRef ; /**
     * Detecting scroll direction
     */ private page : number = 0 ; private lastPage : number ; private readonly tag : string = 'attachmentListScroll' ; private readonly defaultApiParameter : CollectionParameters = { page : 1 , limit : 100 , order : 1 } ; constructor ( private fileService , private fileSaverService , private modalService , public activeModal , private toast , private scrollService ) { } param event @ HostListener ( 'scroll' , [ '$event' ] ) onScrollHost ( event ) { if ( this . scrollService . bottomReached ( event , this . tag ) && this . page <= this . lastPage ) { this . getFiles ( this . scrollService . getCollectionParameters ( this . tag ) , true ) ; } } public ngOnInit ( ) { this . fileService . getAllFilesList ( this . metadata ) . subscribe ( ( data ) => { this . attachments . push ( ... data . results ) ; this . recordsTotal = data . total ; this . updatePagination ( data ) ; } ) ; } param apiParameters Instance of  CollectionParameters param onScroll public getFiles ( apiParameters ? , onScroll = false ) { this . fileService . getAllFilesList ( this . metadata , apiParameters ? apiParameters : this . defaultApiParameter ) . subscribe ( ( data ) => { if ( onScroll ) { this . attachments . push ( ... data . results ) ; } else { this . attachments = data . results ; } this . recordsTotal = data . total ; this . inProcess = false ; this . updatePagination ( data ) ; } ) ; } /**
     * Update pagination properties for infinite scrolling.
     * Current page values are retrieved from the response  APIGetMultiResponse param data response  APIGetMultiResponse from backend
      private updatePagination ( data ) { this . page = data . pager . page + 1 ; this . lastPage = data . pager . total_pages ; this . scrollService . setCollectionParameters ( this . page , 100 , 'filename' , 1 , this . tag ) ; } /**
     * Allows to attach existing files from the file manager (DATAGERRY File Explorer)
     * or new files form local File Explorer
     */ public addAttachments ( ) { this . modalRef = this . modalService . open ( FilemanagerModalComponent , { size : 'xl' } ) ; this . modalRef . componentInstance . localMetadata = this . metadata ; this . modalRef . result . then ( ( ) => { this . getFiles ( this . defaultApiParameter , false ) ; } ) ; } param filename public downloadFile ( filename ) { this . fileService . downloadFile ( filename , this . metadata ) . subscribe ( ( data ) => { this . fileSaverService . save ( data . body , filename ) ; } ) ; } param fileElement public deleteFile ( fileElement ) { const { reference , reference_type } = this . metadata ; const newReference = typeof reference === 'number' ? [ reference ] : reference ; const tempReference = fileElement . metadata . reference ? fileElement . metadata . reference : [ ] ; fileElement . metadata . reference = typeof tempReference === 'number' ? [ tempReference ] . concat ( newReference ) : tempReference . concat ( newReference ) ; fileElement . metadata . reference_type = reference_type ; fileElement . metadata . reference = fileElement . metadata . reference . filter ( x => x !== reference ) ; this . fileService . putFile ( fileElement , true ) . subscribe ( ( resp ) => { this . getFiles ( this . defaultApiParameter ) ; this . toast . info ( `File was successfully solved: ${ resp . filename }` ) ; } ) ; } }