HRM-10/HRM.UI/ClientApp/src/app/picker/pdf-viewer-picker.component.ts

319 lines
11 KiB
TypeScript
Raw Normal View History

2026-11-03 13:06:00 +06:00
import {Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation, OnDestroy, ChangeDetectionStrategy} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {ApiService} from '../app.api.service';
import {EmployeeServices} from '../_services/employee/employee.service';
import {SelectableSettings} from '@progress/kendo-angular-grid';
import {SearchEmployee, SearchManager} from '../_models/Employee/searchEmployee';
import {EnumExtension, EnumGender, EnumSearchFrom, EnumSearchObjDataType, EnumSearchParameter, EnumSQLOperator} from '../_models/enums';
import {DynamicPicker, EnumDynamicpickerType} from './dynamic-picker/Dynamic-Picker';
import {BasicService} from '../_services/Basic/basic.service';
import {Subscription} from 'rxjs';
import {PickerDataTransferService} from '../data.transfer.service';
import {loadingPanelService} from '../hrm-loding panel/loding.panel.service';
import {RecruitmentRequisition} from '../_models/Requisition/recruitmentRequisition';
import {RecruitmentService} from '../_services/recruitment/recruitment.service';
import {CV} from '../_models/Recruitement/CV';
import {FileAttachment, FileTypeList} from '../_models/common/uploadFile';
import {FileInfo, RemoveEvent, SuccessEvent} from '@progress/kendo-angular-upload';
import {SafeHtml, SafeUrl, SafeStyle, DomSanitizer} from '@angular/platform-browser';
import {EmpChildren} from '../_models/HREmployee/EmpChildren';
import { LoadingPanelComponent } from '../hrm-loding panel/loading-panel.component';
@Component({
selector: 'app-pdf-viewer-picker',
templateUrl: './pdf-viewer-picker.component.html',
//styleUrls: ['../../assets/kendo-grid/grid.css'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.Eager,
standalone: false,
})
export class PDFViewerPickerComponent implements OnInit, OnDestroy {
src: any;
valueChangedSubscription: Subscription;
genders: any = EnumExtension.getNamesAndValues(EnumGender);
public active = true;
public showPopUp = false;
public checkboxOnly = false;
public mode = 'multiple';
fileName: string;
_typeList: FileTypeList[] = [];
myFiles: FileInfo[] = [];
public selectableSettings: SelectableSettings;
taggedSelection: number[] = [];
/*public searchItems: RecruitmentRequisition[];*/
public searchItems: CV[];
public selectedItems: RecruitmentRequisition[] = [];
public selectedItem: RecruitmentRequisition;
pdfFileList: FileAttachment[] = [];
previuosUploadList: FileAttachment[] = [];
public selectedCV: CV;
public searchCVs: CV[];
ItemCount: number = 0;
@Input() pickerFormGroup: FormGroup;
@Output() ItemSelected: EventEmitter<any> = new EventEmitter();
@Output() onCancel = new EventEmitter();
@Input()
public set setSelectedItem(inputedItem: RecruitmentRequisition) {
if (inputedItem !== undefined) {
this.selectedItem = inputedItem;
this.taggedSelection = [];
this.taggedSelection.push(this.selectedItem.id);
this.selectedItemText = this.selectedItem.position;
} else {
this.selectedItem = new RecruitmentRequisition();
this.selectedItemText = '';
}
}
private empCodeNameList: Array<string>;
private empCodeNameListSource: Array<string>;
public selectedItemText: string;
public gridMultiSelect = false;
attachmentFile: any;
@Input() attachment;
@Input() attachmentFileType;
@Input()
public set MultiSelect(ismultiselect: boolean) {
if (ismultiselect !== undefined) {
/* this.gridMultiSelect = true;*/
this.gridMultiSelect = ismultiselect;
this.setSelectableSettings();
}
}
@Input()
public set GetID(id: number) {
if (id !== undefined) {
this.loadInitialData(id);
this.src = '';
this.pdfFileList = [];
/* this.gridMultiSelect = true;*/
//this.gridMultiSelect = ismultiselect;
//this.setSelectableSettings();
}
}
@Input()
public set isActive(isActive: boolean) {
this.active = isActive;
}
@Input() setFileName?: string; // Optional input property
/** role-permission-entry ctor */
constructor(public api: ApiService,
public formBuilder: FormBuilder,
public router: Router,
public route: ActivatedRoute,
public empSrvc: EmployeeServices,
public basicService: BasicService,
public pickerService: PickerDataTransferService,
public loadingService: loadingPanelService,
public recruitmentService: RecruitmentService,
private sanitizer: DomSanitizer
) {
if (this.pickerFormGroup === undefined) {
this.pickerFormGroup = new FormGroup({});
}
}
ngOnInit() {
debugger
console.log('Attachment:', this.attachment);
console.log('File Type:', this.attachmentFileType);
console.log('File Name:', this.setFileName);
this.processAttachment();
}
downloadPdf(): void {
debugger
const link = document.createElement('a');
link.href = this.src; // The URL or base64 string of the PDF file
if (this.setFileName)
link.download = this.setFileName+'.pdf';
else
link.download = 'downloaded-file.pdf';
link.click();
}
processAttachment() {
debugger
console.log(this.attachmentFileType);
this.loadingService.ShowLoadingPanel = true;
if (this.attachmentFileType === 'image') {
this.attachmentFile = this.sanitizer.bypassSecurityTrustResourceUrl(`data:image/png;base64, ${this.attachment}`);
}
else if (this.attachmentFileType === 'pdf') {
const byteCharacters = atob(this.attachment);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {type: 'application/pdf'});
const objectUrl: string = URL.createObjectURL(blob);
this.src = objectUrl;
}
this.loadingService.ShowLoadingPanel = false;
}
loadInitialData(id: number) {
this.recruitmentService.getFilesByCVId(id).subscribe(
(resp) => {
this.pdfFileList = resp as any;
},
(x) => {
console.log(x.message);
},
() => {
this.loadData();
this.loadingService.ShowLoadingPanel = false;
},
);
}
loadData() {
if (this.pdfFileList !== undefined && this.pdfFileList.length > 0) {
this._typeList = [];
for (let i = 0; i < this.pdfFileList.length; i++) {
this.fileName = '';
this.fileName = this.pdfFileList[i].originalFileName;
let str = this.pdfFileList[0].fileTobase64;
var fileExtension = this.fileName.split('.').pop();
let _type = this.function(fileExtension.toLowerCase());
const byteCharacters = atob(str);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {type: _type});
const objectUrl: string = URL.createObjectURL(blob);
this.src = objectUrl;
}
}
}
function(ext) {
if (ext !== undefined) {
return this.extToMimes(ext);
}
return undefined;
}
extToMimes(ext) {
let type = undefined;
switch (ext) {
case 'jpg':
type = 'image/jpeg';
break;
case 'png':
type = 'image/jpeg';
break;
case 'jpeg':
type = 'image/jpeg';
break;
case 'txt':
type = 'text/plain';
break;
case 'xls':
type = 'application/vnd.ms-excel';
break;
case 'doc':
type = 'application/msword';
break;
case 'xlsx':
type = 'application/vnd.ms-excel';
break;
case 'pdf':
type = 'application/pdf';
break;
default:
}
return type;
}
setSelectableSettings(): void {
if (this.gridMultiSelect === false) {
this.selectableSettings = {
checkboxOnly: true,
mode: 'single', // this.mode
};
} else {
this.selectableSettings = {
checkboxOnly: true,
mode: 'multiple', // this.mode
};
}
}
search() {
this.loadingService.ShowLoadingPanel = true;
this.recruitmentService.getCVs().subscribe(
(resp) => {
this.searchItems = resp;
},
(x) => {
console.log(x);
this.loadingService.ShowLoadingPanel = false;
},
() => {
this.loadingService.ShowLoadingPanel = false;
},
);
}
public onClose(): void {
this.onCancel.emit();
}
public onSelect(e): void {
this.searchCVs = [];
this.selectedCV = new CV();
if (this.taggedSelection.length > 0) {
if (this.gridMultiSelect === true) {
this.taggedSelection.forEach(positionid => {
this.searchCVs.push(this.searchItems.find(x => x.id === positionid));
});
} else {
this.taggedSelection.forEach(positionid => {
this.selectedCV = this.searchItems.find(x => x.id === positionid);
});
}
}
if (this.gridMultiSelect === false) {
// this.selectedAutoCmptEmp = this.selectedEmployee.employeeNo + '-' + this.selectedEmployee.name;
this.ItemSelected.emit(this.selectedCV);
} else {
this.ItemSelected.emit(this.searchCVs);
}
this.showPopUp = false;
}
public closeForm(): void {
this.showPopUp = false;
}
public OpenForm(): void {
//this.selectedEmployees = [];
//this.selectedEmployee = new SearchEmployee();
//this.taggedSelection = [];
this.showPopUp = true;
}
ngOnDestroy() {
}
}