import { Component , EventEmitter , Input , OnChanges , Output , SimpleChanges } from '@angular/core' ; import { FileElement , SelectedFileArray } from '../../model/file-element' ; import { BehaviorSubject } from 'rxjs' ; import { FileService } from '../../service/file.service' ; import { FileSaverService } from 'ngx-filesaver' ; import { RenameDialogComponent } from '../../modal/rename-dialog/rename-dialog.component' ; import { NgbModal , NgbModalConfig } from '@ng-bootstrap/ng-bootstrap' ; import { MoveDialogComponent } from '../../modal/move-dialog/move-dialog.component' ; import { CollectionParameters } from '../../../../../services/models/api-parameter' ; @ Component ( { selector : 'cmdb-file-view-list' , templateUrl : './file-view-list.component.html' , styleUrls : [ './file-view-list.component.scss' ] } ) export class FileViewListComponent implements OnChanges { @ Output ( ) loadFileElementEvent = new EventEmitter < any > ( ) ; /**
     * When setting the overview control,
     * the preselection is reset
     */ private viewControl : boolean = true ; @ Input ( ) public set listView ( value ) { if ( this . selectedFiles && this . selectedFiles . files . length > 0 ) { this . removeSelects ( ) ; } this . viewControl = value ; } public get listView ( ) : boolean { return this . viewControl ; } private elementFiles : FileElement [ ] = [ ] ; @ Input ( ) set fileElements ( value ) { this . elementFiles = value ; } get fileElements ( ) : FileElement [ ] { return this . elementFiles ; } private filesSelected : SelectedFileArray ; @ Input ( ) set selectedFiles ( value ) { this . filesSelected = value ; } get selectedFiles ( ) : SelectedFileArray { return this . filesSelected ; } constructor ( public fileService , private fileSaverService , private modalService , private config ) { config . backdrop = 'static' ; config . keyboard = false ; } ngOnChanges ( changes ) { if ( ! changes . hasOwnProperty ( 'listView' ) ) { this . listView = true ; } } public onSelect ( value , event ? ) { const array = this . selectedFiles . files . filter ( x => x . public_id === value . public_id ) ; if ( array . length === 0 ) { this . selectedFiles . files . push ( value ) ; this . selectedFiles . totalSize = this . selectedFiles . totalSize + value . size ; if ( event ) { ( event . target as HTMLElement ) . classList . add ( 'active' ) ; } } else { this . selectedFiles . files = this . selectedFiles . files . filter ( x => x . public_id !== value . public_id ) ; this . selectedFiles . totalSize = this . selectedFiles . totalSize - value . size ; if ( event ) { ( event . target as HTMLElement ) . classList . remove ( 'active' ) ; } } } public removeSelects ( ) { const inputElements = $ ( 'input[class="selected-file"]:checked' ) ; for ( const input of inputElements ) { input . checked = false ; } this . selectedFiles . files . length = 0 ; this . selectedFiles . totalSize = 0 ; } param value private updateSelectedFileList ( value ) { this . selectedFiles . files = this . selectedFiles . files . filter ( x => x . public_id !== value . public_id ) ; const inputElements = $ ( 'input[class="selected-file"]:checked' ) ; inputElements . length > 0 ? inputElements . checked = true : inputElements . checked = false ; } public downloadFile ( value ) { const { filename , metadata } = value ; this . fileService . downloadFile ( filename , metadata ) . subscribe ( ( data ) => { this . fileSaverService . save ( data . body , filename ) ; } ) ; } public renameFile ( value ) { const folderModal = this . modalService . open ( RenameDialogComponent ) ; folderModal . componentInstance . selectedFileFolder = new BehaviorSubject < any > ( value ) ; folderModal . result . then ( ( result ) => { if ( result ) { const fileElement = value ; fileElement . filename = result . filename ; this . postFileChanges ( fileElement ) ; } } ) ; } public moveFile ( value ) { const folderModal = this . modalService . open ( MoveDialogComponent ) ; this . updateSelectedFileList ( value ) ; folderModal . result . then ( ( result ) => { if ( result ) { const fileElement = value ; fileElement . filename = value . filename ; fileElement . metadata . parent = result . parent ; this . postFileChanges ( fileElement ) ; } } ) ; } public deleteFile ( value ) { this . updateSelectedFileList ( value ) ; this . fileService . deleteFile ( value . public_id , { } ) . subscribe ( ( ) => { this . loadFolderFiles ( ) ; } ) ; } private postFileChanges ( curr ) { this . fileService . putFile ( curr ) . subscribe ( ( ) => { this . loadFolderFiles ( ) ; } ) ; } public loadFolderFiles ( value ? ) { this . loadFileElementEvent . emit ( value ) ; } }