Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
import {
Component,
OnInit,
OnDestroy,
Input,
Output,
Inject,
EventEmitter
} from '@angular/core';
import { FormGroup, FormBuilder, FormArray, Validators } from '@angular/forms';
import { DOCUMENT } from '@angular/platform-browser';
import { WindowRefService } from '../../window-ref.service';
import { PdfReferenceService } from './pdf-reference.service';
import { MenuModel } from '../../shared/menu/menu.component.model';
import {
FilesModel,
FileUploadRequestModel,
ArchiveRequestModel
} from '../ewv-viewer/ewv-viewer.model';
import { MessageModel } from './pdf-reference.model';
import { EwvViewerService } from '../ewv-viewer/ewv-viewer.service';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'app-pdf-reference',
templateUrl: './pdf-reference.component.html',
styleUrls: [
'./pdf-reference.component.scss',
'../ewv-viewer/ewv-viewer.component.scss'
]
})
export class PdfReferenceComponent implements OnInit, OnDestroy {
uploadFiles: FilesModel[] = [];
uploadRequest: FileUploadRequestModel = new FileUploadRequestModel();
uploadModalClosed = true;
confirmModalClosed = true;
uploadForm: FormGroup;
titlesFormArray: FormArray;
allowedExtensions = ['pdf'];
fileList: File[] = [];
invalidFileList: File[] = [];
uploadingFlag = false;
archiveFlag = false;
modifiedPdfReferences: FilesModel[] = [];
pdfReferenceSubscription = new Subscription();
uploadModalErrorMsg = '';
uploadModalErrorStatus = false;
ewvMenu: MenuModel;
@Input()
containerClassWidth = 1284;
@Input()
show = true;
@Input()
typeOfClaim = '';
@Input()
isMenu = true;
@Input()
tabIdx = '0';
@Output()
uploadSuccess: EventEmitter<MessageModel> = new EventEmitter();
@Output()
uploadError: EventEmitter<MessageModel> = new EventEmitter();
@Output()
archiveSuccess: EventEmitter<MessageModel> = new EventEmitter();
@Output()
archiveError: EventEmitter<MessageModel> = new EventEmitter();
constructor(
private ewvViewerService: EwvViewerService,
private fb: FormBuilder,
@Inject(DOCUMENT) private document,
private _window: WindowRefService,
private pdfReferenceService: PdfReferenceService
) {}
ngOnInit() {
this.ewvMenu = this.ewvViewerService.getEwvMenu();
this.formInit();
this.populateDynamicPdfReferences();
}
populateDynamicPdfReferences() {
this.pdfReferenceSubscription.add(
this.pdfReferenceSubscription.add(this.pdfReferenceService
.getAllPdfReferences()
.subscribe(pdfReferenceList => {
if (pdfReferenceList && pdfReferenceList.length > 0) {
this.modifiedPdfReferences = pdfReferenceList.slice();
}})));
}
formInit() {
this.uploadForm = this.fb.group({
titlesFormArray: this.fb.array([])
});
this.titlesFormArray = this.uploadForm.get('titlesFormArray') as FormArray;
}
openUploadModal() {
this.uploadModalClosed = false;
this.tabIdx = '-1';
this.ewvViewerService.setTabIdx(this.tabIdx);
this.document.body.style.overflow = 'hidden';
this.resetMsgs();
}
resetMsgs() {
this.uploadSuccess.emit({
msg: ``,
status: false
});
this.uploadError.emit({
msg: ``,
status: false
});
this.archiveError.emit({
msg: ``,
status: false
});
this.archiveSuccess.emit({
msg: ``,
status: false
});
}
resetUploadForm(bool: boolean) {
if (bool) {
// bool will be true if reset form is clicked, false otherwise
this.resetMsgs();
}
this.uploadModalClosed = true;
this.document.body.style.overflow = 'visible';
this.invalidFileList = [];
while (this.titlesFormArray.length !== 0) {
this.titlesFormArray.removeAt(0);
this.uploadFiles.pop();
this.fileList.pop();
}
this.populateDynamicPdfReferences();
}
// Called when valid files are dragged in to the drop zone
onValidFilesAdded(fileList: File[]) {
while (this.fileList.length < 11 && fileList.length !== 0) {
if (
!this.fileList.some(
file => file.name === fileList[fileList.length - 1].name
)
) {
// const tempFileModel = new FilesModel();
this.fileList.push(fileList.pop());
this.uploadFiles.push(new FilesModel());
this.addTitleRow();
this.uploadModalErrorMsg = '';
this.uploadModalErrorStatus = false;
} else {
this.uploadModalErrorStatus = true;
this.uploadModalErrorMsg = 'File(s) Already Selected';
break;
}
}
if (this.uploadFiles.length >= 11) {
this.uploadModalErrorMsg = 'Maximum 10 PDFs May be Uploaded';
this.uploadModalErrorStatus = true;
}
}
addTitleRow() {
if (this.titlesFormArray.length < 11) {
this.titlesFormArray.push(
this.fb.group({
title: ['', Validators.required]
})
);
}
}
removeTitleRow(index: number) {
// Removing Rows from the modal formarray
this.titlesFormArray.removeAt(index);
// Removing the files from uploaded files list
this.fileList.splice(index, 1);
this.uploadFiles.splice(index, 1);
}
invalidFilesAdded(invalidFileList: File[]) {
this.invalidFileList = invalidFileList;
this.uploadModalErrorStatus = true;
this.uploadModalErrorMsg = 'Only PDFs May be Uploaded';
}
closeConfirmationModal() {
this.confirmModalClosed = true;
this.tabIdx = '0';
this.ewvViewerService.setTabIdx(this.tabIdx);
}
openConfirmationModal() {
this.confirmModalClosed = false;
this.tabIdx = '-1';
this.ewvViewerService.setTabIdx(this.tabIdx);
}
submitUpload() {
this.resetMsgs();
if (
this.modifiedPdfReferences.some(
reference => reference.archive === true
) &&
this.confirmModalClosed
) {
this.openConfirmationModal();
} else {
this.closeConfirmationModal();
this.uploadModalClosed = true;
if (this.archiveFlag) {
const archiveArray: ArchiveRequestModel = new ArchiveRequestModel();
archiveArray.archivedPdfReferences = this.modifiedPdfReferences.filter(
reference => reference.archive === true
);
this.archiveSubmitted(archiveArray);
}
if (this.fileList.length > 0) {
this.uploadingFlag = true;
const formData: FormData = new FormData();
for (let i = 0; i < this.fileList.length; i++) {
formData.append('file', this.fileList[i], this.uploadFiles[i].alt);
}
this.ewvViewerService.uploadFiles(formData).subscribe(
data => {
this.resetUploadForm(false);
this.uploadingFlag = false;
data = JSON.parse(data);
this.pdfReferenceService
.getAllPdfReferences()
.subscribe(
pdfReferenceList =>
(this.modifiedPdfReferences = pdfReferenceList)
);
const successList = this.populateMsg(
data.successfulFileUploadsList,
true
);
const errorList = this.populateMsg(
data.unSuccessfulFileUploadsList,
true
);
if (data.successfulFileUploadsList.length > 0) {
this.uploadSuccess.emit({
msg: `Successfully uploaded the following PDF(s): ${successList}`,
status: true
});
// this.successMsg =
// 'Successfully uploaded the following PDF(s): ' + successList;
// this.successStatus = true;
this.scrollToTop();
}
if (data.unSuccessfulFileUploadsList.length > 0) {
this.uploadError.emit({
msg: `Failed uploading the following PDF(s): ${errorList}`,
status: true
});
// this.errorMsg =
// 'Failed uploading the following PDF(s): ' + errorList;
// this.errorStatus = true;
this.scrollToTop();
}
},
error => {
this.uploadingFlag = false;
this.uploadError.emit({
msg: `Error Uploading Files`,
status: true
});
// this.errorMsg = 'Error Uploading Files';
this.scrollToTop();
// this.errorStatus = true;
this.resetUploadForm(false);
}
);
}
}
}
// Called when files are added through browse
onChange(evt) {
const validFiles: File[] = [];
const invalidFiles: File[] = [];
const fileList: File[] = <File[]>Array.from(evt.srcElement.files);
fileList.forEach(file => {
const ext = file.name.split('.')[file.name.split('.').length - 1];
if (this.allowedExtensions.lastIndexOf(ext) !== -1) {
validFiles.push(file);
} else {
invalidFiles.push(file);
}
});
if (validFiles.length > 0) {
this.onValidFilesAdded(validFiles);
}
if (invalidFiles.length > 0) {
this.invalidFilesAdded(invalidFiles);
}
}
archiveIconClicked(reference: FilesModel) {
if (reference.archive === true) {
reference.archive = false;
reference.editedFlag = false;
} else {
reference.archive = true;
reference.editedFlag = true;
}
}
archiveSubmitted(archiveArray: ArchiveRequestModel) {
this.ewvViewerService.archivePdfs(archiveArray).subscribe(
data => {
let archivedPdfList = '';
if (data.unSuccessfulFileArchivesList.length === 0) {
archivedPdfList = this.populateMsg(data.successfulFileArchivesList);
this.archiveSuccess.emit({
msg: `Successfully archived ${archivedPdfList} PDF(s)`,
status: true
});
} else {
archivedPdfList = this.populateMsg(data.unSuccessfulFileArchivesList);
this.archiveError.emit({
msg: `Failed to archive ${archivedPdfList} PDF(s)`,
status: true
});
}
this.scrollToTop();
this.pdfReferenceSubscription.add(this.pdfReferenceService
.getAllPdfReferences()
.subscribe(pdfReferenceList => {
this.modifiedPdfReferences = pdfReferenceList;
}));
this.archiveFlag = false;
this.resetUploadForm(false);
},
error => {
this.archiveFlag = false;
this.archiveError.emit({
msg: `Error archiving PDFs`,
status: true
});
this.scrollToTop();
this.resetUploadForm(false);
}
);
}
populateMsg(arr: any, upload?: boolean): string {
// Used to set various messages on uploading new pdf references
// Takes in an array of files that are either succesfully uploaded or failed to upload
// and sets the proper error message
let pdfList = '';
if (upload) {
for (let i = 0; i < arr.length; i++) {
if (i !== arr.length - 1) {
// Sperating the file name and extension
pdfList = pdfList + arr[i].originalName.split('.')[0] + ', ';
} else {
pdfList = pdfList + arr[i].originalName.split('.')[0];
}
}
return pdfList;
} else {
for (let i = 0; i < arr.length; i++) {
if (i !== arr.length - 1) {
pdfList = pdfList + arr[i].alt + ', ';
} else {
pdfList = pdfList + arr[i].alt;
}
}
return pdfList;
}
}
scrollToTop() {
this._window.nativeWindow.scrollTo({ top: 0, behavior: 'smooth' });
}
ngOnDestroy() {
this.pdfReferenceSubscription.unsubscribe();
}
}