import { Injectable, Component, OnInit, Inject } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; import { IcsToastService } from '@varmasagi/ics-toast'; import { IcsDialogService } from '@varmasagi/ics-dialog'; import { IcsDriveService } from '../ics-drive.service'; @Component({ selector: 'lib-app-image-popup', templateUrl: './image-popup.component.html', styleUrls: ['./image-popup.component.scss'], providers: [IcsDriveService] }) @Injectable() export class ImagePopupComponent implements OnInit { imgBaseUrl = 'https://s3-us-west-2.amazonaws.com/repository-leadics-test/leap/leap_icons/uploadRepo/'; imageID: string; public fileId: string; public fileLocation: any; public responseFiles: any; public imageDesc: any; public currImgLoc: string; public currImgName: string; public currImgId: number; public thumbShow = true; public filetypeimage: any; constructor( public dialogRef: MatDialogRef, private icsDialogService: IcsDialogService, public uploadService: IcsDriveService, private toastService: IcsToastService, @Inject(MAT_DIALOG_DATA) public data: any ) {} delete() { this.icsDialogService .confirm({ title: 'Delete', message: 'This file might be used in other Applications. Are you sure you want to delete it?' }) .subscribe(flag => { if (flag) { this.uploadService.delete(this.currImgId).subscribe( (res: any[]) => { this.toastService.success('Deleted!'); this.dialogRef.close(this.currImgId); return this.currImgId; }, err => { this.toastService.error('This file doesnot exist!'); } ); } }); } download(){ const link = document.createElement('a'); link.download = this.currImgName; link.href = this.currImgLoc; document.body.appendChild(link); link.click(); document.body.removeChild(link);} ngOnInit() { this.imageID = this.data.name; this.uploadService.getFilesLists(this.imageID).subscribe( (res: any[]) => { this.responseFiles = res; if (this.responseFiles.length === 1) { this.thumbShow = false; } else { this.thumbShow = true; } this.imageDesc = res[0]; this.currImgLoc = this.imageDesc.location; this.currImgName = this.imageDesc.name; this.getFileExt(this.currImgName); this.currImgId = this.imageDesc.id; }, err => { return err; } ); } showThisthumb(fileLoc, fileName, fileid) { this.currImgLoc = fileLoc; this.currImgName = fileName; this.currImgId = fileid; } getFileExt(args: string) { const key = args.lastIndexOf('.'); const value = args.substring(key, args.length); if ( value === '.png' || value === '.jpg' || value === '.jpeg' || value === '.svg' ) { this.filetypeimage = true; } else { this.filetypeimage = false; } } }