import { Component, EventEmitter, OnInit } from "@angular/core"; import { CommonService } from "src/app/hmg-common/services/common/common.service"; import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service"; import { FileUploader } from "ng2-file-upload"; import { AddAttachService } from "../service/add-attach-service"; import { AddEitResponse } from "src/app/eit/models/add.eit.response"; import { ModalController } from '@ionic/angular'; /** * Generated class for the AddAttachPage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @Component({ selector: "absence-attachment", templateUrl: "absence-attachment.component.html", styleUrls: ["./absence-attachment.component.scss"] }) export class AbsenceAttachmentComponent implements OnInit { private eitResponse: any; //AddEitResponse=new AddEitResponse(); isUpload: boolean = false; inQueue: boolean = false; addAttachmentListReq: any; attachmentID: number = 0; addAttachRequest: any = []; fileData: any; fileType: any; index: any = 1; dirPage: any; TransactionID: any; indexLastObj: any; filterAllowedType: any = [ "application/pdf", "image/jpeg", "image/png", "text/plain", "image/jpg", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ]; constructor( public common: CommonService, public AddAttachService: AddAttachService, public modalCtrl: ModalController, public ts: TranslatorService ) { this.eitResponse = this.common.sharedService.getSharedData( AddEitResponse.SHARED_DATA, true ); // this.TransactionID = this.navParams.get("TransactionID"); // this.indexLastObj = this.navParams.get("indexLastObj"); // console.log("this.TransactionID : "+ this.TransactionID); this.isUpload = false; } public uploader: FileUploader = new FileUploader({ allowedMimeType: [ "application/pdf", "image/jpeg", "image/png", "text/plain", "image/jpg", "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ], maxFileSize: 10 * 1024 * 1024, formatDataFunctionIsAsync: true, formatDataFunction: async item => { return new Promise((resolve, reject) => { resolve({ name: item._file.name, length: item._file.size, contentType: item._file.type, date: new Date() }); }); } }); public hasBaseDropZoneOver: boolean = false; public hasAnotherDropZoneOver: boolean = false; ngOnInit() {} public fileOverBase(e: any): void { this.hasBaseDropZoneOver = e; } public fileOverAnother(e: any): void { this.hasAnotherDropZoneOver = e; } OkBtnModal() { let fileCount: number = this.uploader.queue.length; if (fileCount > 0) { let data: any = this.addAttachRequest; this.modalCtrl.dismiss(data); } else { let msg: string = ""; msg = this.ts.trPK("general", "noFileSelect"); this.common.presentAlert(msg); return; } } // end ok button closeBtnModal() { // this.viewCtrl.dismiss("cancel"); } onFileSelected(input) { if (!(this.filterAllowedType.indexOf(input.target.files[0].type) > -1)) { let msg: string = ""; msg = this.ts.trPK("general", "noFileSelect"); this.common.presentAlert(msg); return; } // todo: show alert that you tried uploading wrong files else { const file = input.target.files[0]; // console.log(file); this.getBase64(file).then(data => this.pushObject(data, file.name, file.type) ); } } getBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); }); } pushObject(fileData, name, type) { // console.log("before push: "+ this.index); this.indexLastObj++; try { let array = name.split("."); let attachType: string = array[array.length - 1]; this.addAttachRequest.push({ AttachmentID: this.indexLastObj, P_FILE_CONTENT_TYPE: attachType, //type.split('/')[1], P_FILE_DATA: fileData.split(",")[1], P_FILE_NAME: name, //.split('.')[0], P_TRANSACTION_ID: this.TransactionID }); } catch (e) {} } removeFile(objectitem) { let objIndex1 = this.uploader.queue.findIndex(item => item == objectitem); this.uploader.queue.splice(objIndex1, 1); let objIndex = this.addAttachRequest.findIndex( item => item.AttachmentID == objectitem.AttachmentID ); this.addAttachRequest.splice(objIndex, 1); } }