Newer
Older

Hamza HRAMCHI
committed
import { Component, OnInit, ViewChild, TemplateRef, ViewContainerRef, Injectable } from '@angular/core';
import { SignaturesContentService } from '../service/signatures.service';
import { DomSanitizer } from '@angular/platform-browser';
import { MatBottomSheet, MatBottomSheetConfig } from '@angular/material/bottom-sheet';
import { MatDialog } from '@angular/material/dialog';
import { MatSidenav } from '@angular/material/sidenav';
import { SignaturesComponent } from '../signatures/signatures.component';
import { ActivatedRoute, Router } from '@angular/router';
import { NotificationService } from '../service/notification.service';
import { CookieService } from 'ngx-cookie-service';
import { DocumentNotePadComponent } from '../documentNotePad/document-note-pad.component';
import { RejectInfoBottomSheetComponent } from '../modal/reject-info.component';
import { TranslateService } from '@ngx-translate/core';
import { DocumentListComponent } from './document-list/document-list.component';
import { AuthService } from '../service/auth.service';
import { LocalStorageService } from '../service/local-storage.service';
import { ActionSheetController, AlertController, LoadingController, MenuController, ModalController, NavController } from '@ionic/angular';
import { NgxExtendedPdfViewerService } from 'ngx-extended-pdf-viewer';
import { catchError, exhaustMap, tap } from 'rxjs/operators';
import { SignatureMethodService } from '../service/signature-method/signature-method.service';
import { FunctionsService } from '../service/functions.service';
import { ActionsService } from '../service/actions.service';
import { SuccessInfoValidBottomSheetComponent } from '../modal/success-info-valid.component';
selector: 'app-document',
templateUrl: 'document.component.html',

Hamza HRAMCHI
committed

Hamza HRAMCHI
committed
posX: number = 0;
posY: number = 0;
enterApp: boolean = true;
pageNum: number = 1;
signaturesContent: any = [];
totalPages: number;
draggable: boolean;
currentDoc: number = 0;
docList: any = [];
actionsList: any = [
{
color: 'danger',
logo: 'thumbs-down-outline',
},
{
event: 'openSignatures'
},
{
color: 'success',
logo: 'thumbs-up-outline',
},
];
pdfDataArr: any;
freezeSidenavClose: boolean = false;
startX: number = 0;
startY: number = 0;
widthDoc: string = '100%';
resetDragPos: boolean = false;
mainDocument: any = {
Guillaume Heurtier
committed
status,
hasWorkflowNotes: boolean = false;
currentTool = 'info';
load: HTMLIonLoadingElement = null;
dragging: boolean = false;
resizing: boolean = false;
pdfname: string = null;
loadingdocument: boolean = true;
loadingpdf: boolean = false;
loadingImage: boolean = true;
@ViewChild('mainContent') mainContent: any;
@ViewChild('img') img: any;
@ViewChild('snav', { static: true }) snav: MatSidenav;
@ViewChild('dragElem') dragElem: any;
@ViewChild('appDocumentNotePad') appDocumentNotePad: DocumentNotePadComponent;
@ViewChild('appDocumentList') appDocumentList: DocumentListComponent;
@ViewChild('rightContent', { static: true }) rightContent: TemplateRef<any>;
@ViewChild('pagesList') pagesList: any;
constructor(private translate: TranslateService,
private router: Router,
private route: ActivatedRoute,
public http: HttpClient,
public signaturesService: SignaturesContentService,
public notificationService: NotificationService,
private cookieService: CookieService,
public dialog: MatDialog,
private bottomSheet: MatBottomSheet,
public authService: AuthService,
private localStorage: LocalStorageService,
private menu: MenuController,
public actionSheetController: ActionSheetController,
public loadingController: LoadingController,
public viewContainerRef: ViewContainerRef,
public modalController: ModalController,
private pdfViewerService: NgxExtendedPdfViewerService,
public alertController: AlertController,
public signatureMethodService: SignatureMethodService,
public functionsService: FunctionsService,
public actionsService: ActionsService,
imageLoaded(ev: any) {
this.getImageDimensions(true);
// this.loadingDocument = false;
this.load.dismiss();
this.menu.enable(true, 'right-menu');
this.loadingImage = false;
document.getElementsByClassName('drag-scroll-content')[0].scrollTop = 0;
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
}
getImageDimensions(ajustSize: boolean = false): void {
const img = new Image();
img.onload = (data: any) => {
const percent = (data.target.naturalWidth * 100) / this.signaturesService.workingAreaWidth;
this.signaturesService.workingAreaWidth = data.target.naturalWidth;
this.signaturesService.workingAreaHeight = data.target.naturalHeight;
if (ajustSize) {
this.getAreaDimension();
}
/*if (percent !== Infinity) {
this.signatures.forEach(element => {
element.position.height = (percent * element.position.height) / 100;
element.position.width = (percent * element.position.width) / 100;
element.position.top = (percent * element.position.top) / 100;
element.position.left = (percent * element.position.left) / 100;
});
}*/
// this.originalSize = true;
};
img.src = this.docList[this.currentDoc].imgContent[this.pageNum];
}
getAreaDimension() {
const percent = (this.mainContent.el.offsetWidth * 100) / this.signaturesService.workingAreaWidth;
this.signaturesService.workingAreaWidth = (percent * this.signaturesService.workingAreaWidth) / 100;
this.signaturesService.workingAreaHeight = (percent * this.signaturesService.workingAreaHeight) / 100;
/*this.signatures.forEach(element => {
element.position.height = (percent * element.position.height) / 100;
element.position.width = (percent * element.position.width) / 100;
element.position.top = (percent * element.position.top) / 100;
element.position.left = (percent * element.position.left) / 100;
});*/
// this.originalSize = false;
}

Hamza HRAMCHI
committed
async openAction(event: any) {
this.posX = event.clientX;
let buttons = [];
if (!this.checkEmptyNote()) {
buttons.push({
text: this.translate.instant('lang.cancelPreviousNote'),
icon: 'arrow-undo-outline',
handler: () => {
this.undoTag();
}
});
}
if (!this.signaturesService.stampLock) {
buttons.push({
text: this.translate.instant('lang.affixSignature'),
icon: 'ribbon-outline',
handler: () => {
this.openSignatures();
}
});
}

Hamza HRAMCHI
committed
text: this.translate.instant('lang.annotateDocument'),
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
icon: 'receipt-outline',
handler: () => {
this.openNoteEditor();
}
});
/* if (this.originalSize) {
buttons.push({
text: 'Zoom taille écran',
icon: 'contract-outline',
handler: () => {
this.getAreaDimension();
console.log('Share clicked');
}
});
} else {
buttons.push({
text: 'Zoom taille écran',
icon: 'contract-outline',
handler: () => {
this.getImageDimensions();
console.log('Share clicked');
}
});
} */
if (!this.checkEmptiness()) {
buttons.push({
text: this.translate.instant('lang.deleteAll'),
icon: 'color-wand-outline',
handler: () => {
this.removeTags();
}
});
}
const actionSheet = await this.actionSheetController.create({
header: 'Actions',
cssClass: 'my-custom-class',
buttons: buttons,
});
await actionSheet.present();
}
async openSignatures() {
const modal = await this.modalController.create({
component: SignaturesComponent,
cssClass: 'my-custom-class',
componentProps: {
currentWorflow: this.mainDocument.workflow.filter((line: { current: boolean; }) => line.current === true)[0],
}
});
await modal.present();
const { data } = await modal.onWillDismiss();
this.dragging = false;
if (data !== undefined) {
if (data === 'success') {
} else if (data.redirectPage !== undefined) {
this.goTo(data.redirectPage);
}
}
// console.log('dissmiss');
}
async openNoteEditor(ev: any = null) {
let scrollPercentX = 0;
let scrollPercentY = 0;
if (ev !== null) {
const offsetTop = -($('#myBounds')[0].getBoundingClientRect().top - 70);
const realPosY = (ev.pageY - 75) + offsetTop;
// console.log(offsetTop);
scrollPercentX = ((ev.pageX - 350) / ($('#myBounds').width() - $(window).width())) * 100;
scrollPercentX = scrollPercentX;
scrollPercentY = (offsetTop / ($('#myBounds').height() - $(window).height())) * 100;
// scrollPercentY = (realPosY / ($('#myBounds').height() - $(window).height())) * 100;
// console.log('scrollPercentX', scrollPercentX);
// console.log('scrollPercentY', scrollPercentY);
}
const modal = await this.modalController.create({
component: DocumentNotePadComponent,
cssClass: 'fullscreen',
componentProps: {
precentScrollTop: this.posY,

Hamza HRAMCHI
committed
precentScrollLeft: this.posX,
content: this.docList[this.currentDoc].imgContent[this.pageNum]
}
});
await modal.present();
const { data } = await modal.onWillDismiss();
if (data === 'success') {
}
// console.log('dissmiss');
}
scrollToElem() {
const pageY = this.signaturesService.signaturesContent[this.pageNum][this.signaturesService.signaturesContent[this.pageNum].length - 1].positionY;
const offsetTop = -($('#myBounds')[0].getBoundingClientRect().top - 70);
const realPosY = (pageY - 75) + offsetTop;
const scrollY = (realPosY - $(window).height());
document.getElementsByClassName('drag-scroll-content')[0].scrollTo(1000, -scrollY);
ionViewWillEnter() {
this.signaturesService.initTemplate(this.rightContent, this.viewContainerRef, 'rightContent');
}
// console.log('oninit!');
this.menu.enable(false, 'right-menu');
this.menu.enable(true, 'left-menu');
this.route.params.subscribe(params => {
if (typeof params['id'] !== 'undefined') {
message: this.translate.instant('lang.loadingDocument'),
spinner: 'dots'
}).then((load: HTMLIonLoadingElement) => {
this.load = load;
this.load.present();
this.http.get('../rest/documents/' + params['id']).pipe(
tap((data: any) => {
this.mainDocument = data.document;
this.mainDocument.workflow = this.mainDocument.workflow.map((item: any) => {
if (item.note) {
this.hasWorkflowNotes = true;
}

Florian Azizian
committed
item.userSignatureModes.unshift('visa');
return {
...item,
'role': item.mode === 'visa' ? 'visa' : item.signatureMode,

Florian Azizian
committed
'modes': item.userSignatureModes
this.totalPages = this.mainDocument.pages;
this.signaturesService.mainDocumentId = this.mainDocument.id;
this.signaturesService.totalPage = this.mainDocument.pages;
this.menu.enable(true, 'right-menu');
this.initDoc();
const realUserWorkflow = this.mainDocument.workflow.filter((line: { current: boolean; }) => line.current === true);
this.mainDocument.isCertified = this.mainDocument.workflow.filter((line: any) => line.mode === 'sign' && line.signatureMode !== 'stamp' && line.processDate !== null).length > 0;
if (realUserWorkflow.length === 0 || this.mainDocument.readOnly) {
this.actionsList = [
{
id: 4,
label: 'lang.back',
color: 'medium',
logo: 'chevron-back-outline',
event: 'back'
},
];
this.detailMode = true;
this.signaturesService.stampLock = this.mainDocument.isCertified && ((realUserWorkflow[0].signatureMode === 'stamp' && realUserWorkflow[0].mode === 'sign') || (realUserWorkflow[0].mode === 'visa'));
if (realUserWorkflow[0].userId !== this.authService.user.id) {
this.http.get('../rest/users/' + realUserWorkflow[0].userId + '/signatures')
.subscribe((dataSign: any) => {
this.signaturesService.signaturesListSubstituted = dataSign.signatures;
});
} else {
this.signaturesService.signaturesListSubstituted = [];
}
if (realUserWorkflow[0].datePositions.length > 0 && this.functionsService.empty(this.signaturesService.datesContent)) {
realUserWorkflow[0].datePositions.forEach((date: any) => {
if (!this.signaturesService.datesContent[date.page]) {
this.signaturesService.datesContent[date.page] = [];
}
this.signaturesService.datesContent[date.page][0] = {
width: date.width,
height: date.height,
positionX: date.positionX,
positionY: date.positionY,
font: date.font,
size: date.size,
color: date.color,
format: date.format
};
});
}
}
this.docList.push({ 'id': this.mainDocument.id, 'title': this.mainDocument.title, 'pages': this.mainDocument.pages, 'imgContent': [], 'imgUrl': '../rest/documents/' + this.mainDocument.id + '/thumbnails' });
this.mainDocument.attachments.forEach((attach: any) => {
this.docList.push({ 'id': attach.id, 'title': attach.title, 'pages': attach.pages, 'imgContent': [], 'imgUrl': '../rest/attachments/' + attach.id + '/thumbnails' });
});
this.menu.enable(true, 'right-menu');
// this.renderPdf();
this.renderImage();
this.loadingdocument = false;
}),
catchError((err: any) => {
setTimeout(() => {
this.load.dismiss();
}, 200);

Hamza HRAMCHI
committed
this.notificationService.handleErrors(err);
this.router.navigate(['/home']);
return of(false);
})
).subscribe();
});
renderPdf() {
// console.log('renderPdf');
this.http.get('../rest/documents/' + this.docList[this.currentDoc].id + '/content')
.subscribe((data: any) => {
this.pdfname = 'data:application/pdf;base64,' + data.encodedDocument;
this.loadingpdf = true;
});
}
async onPagesLoaded(ev: any) {
this.totalPages = ev.pagesCount;
this.exportAsImage();
}
public async exportAsImage(): Promise<void> {
const scale = { width: 1000 };
// or: scale = {height: this.height};
// or: scale = {scale: this.scale};
const data = await this.pdfViewerService.getPageAsImage(this.pageNum, scale);
this.docList[this.currentDoc].imgContent[this.pageNum] = data;
this.loadingpdf = false;
this.load.dismiss();
}
renderImage() {
if (this.docList[this.currentDoc].imgContent[this.pageNum] === undefined) {
if (this.currentDoc === 0) {
this.http.get('../rest/documents/' + this.docList[this.currentDoc].id + '/thumbnails/' + this.pageNum).pipe(
tap((data: any) => {
this.docList[this.currentDoc].imgContent[this.pageNum] = 'data:image/png;base64,' + data.fileContent;
}),
catchError((err: any) => {
this.load.dismiss();

Hamza HRAMCHI
committed
this.notificationService.handleErrors(err);
this.router.navigate(['/home']);
return of(false);
})
).subscribe();
this.http.get('../rest/attachments/' + this.docList[this.currentDoc].id + '/thumbnails/' + this.pageNum).pipe(
tap((data: any) => {
this.docList[this.currentDoc].imgContent[this.pageNum] = 'data:image/png;base64,' + data.fileContent;
}),
catchError((err: any) => {
this.load.dismiss();

Hamza HRAMCHI
committed
this.notificationService.handleErrors(err);
this.router.navigate(['/home']);
return of(false);
})
).subscribe();
initDoc() {
this.docList = [];
this.signaturesService.signaturesContent = [];
this.signaturesService.notesContent = [];
this.signaturesService.datesContent = [];
this.signaturesService.currentToobal = 'mainDocumentDetail';
const notesContent = this.localStorage.get(this.mainDocument.id.toString());
const storageContent = JSON.parse(notesContent);
this.signaturesService.notesContent = storageContent['note'] !== undefined ? storageContent['note'] : [];
this.signaturesService.signaturesContent = storageContent['sign'] !== undefined ? storageContent['sign'] : [];

Alex ORLUC
committed
this.signaturesService.datesContent = storageContent['date'] !== undefined ? storageContent['date'] : [];
this.signaturesService.currentAction = 0;
this.signaturesService.currentPage = 1;
this.pageNum = 1;
this.signaturesContent.currentDoc = 1;
this.currentDoc = 0;
}
testDrag(event: any) {
const element = event.source.getRootElement();
const boundingClientRect = element.getBoundingClientRect();
const parentPosition = this.getPosition(element);
this.signaturesService.y = (boundingClientRect.y - parentPosition.top);
this.signaturesService.x = (boundingClientRect.x - parentPosition.left);
}
getPosition(el: any) {
let x = 0;
let y = 0;
while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
x += el.offsetLeft - el.scrollLeft;
y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: y, left: x };
zoomForNotes() {
this.widthDoc = '200%';
this.signaturesService.scale = 2;
$('.example-box').css({ 'transform': 'translate3d(' + this.signaturesService.x * this.signaturesService.scale + 'px, ' + this.signaturesService.y * this.signaturesService.scale + 'px, 0px)' });
this.signaturesService.workingAreaHeight *= this.signaturesService.scale;
this.signaturesService.workingAreaWidth *= this.signaturesService.scale;
zoomForView() {
// this.resetDragPosition();
this.resetDragPos = true;
this.widthDoc = '100%';
this.signaturesService.workingAreaHeight = this.signaturesService.workingAreaHeight / 2;
this.signaturesService.workingAreaWidth = this.signaturesService.workingAreaWidth / 2;
setTimeout(() => {
this.resetDragPos = false;
}, 200);
this.signaturesService.scale = 1;
if (this.pageNum === 0) {
this.pageNum = 1;
} else {
}
if (this.currentDoc === 0) {
this.signaturesService.currentPage = this.pageNum;
}
if (this.pageNum >= this.totalPages) {
this.pageNum = this.totalPages;
} else {
this.pageNum++;
}
// only for main document
if (this.currentDoc === 0) {
this.signaturesService.currentPage = this.pageNum;
}

Hamza HRAMCHI
committed
this.loadingController.create({
message: this.translate.instant('lang.loadingDocument'),
spinner: 'dots',
}).then((load: HTMLIonLoadingElement) => {
this.load = load;
this.load.present();

Hamza HRAMCHI
committed
this.loadingImage = true;
this.load.dismiss();

Hamza HRAMCHI
committed
});
this.pageNum = page;
// only for main document
if (this.currentDoc === 0) {
this.signaturesService.currentPage = this.pageNum;
}
// this.exportAsImage();

Hamza HRAMCHI
committed
}
pagesArray(n: number): number[] {
return Array(n);
/*if ((typeof this.signaturesService.workingAreaHeight !== 'number' || this.signaturesService.workingAreaHeight === 0) && (typeof this.signaturesService.workingAreaWidth !== 'number' || this.signaturesService.workingAreaWidth === 0)) {
this.img = document.querySelector('img.zoom');
const rect = this.img.getBoundingClientRect();
this.signaturesService.workingAreaHeight = rect.height;
this.signaturesService.workingAreaWidth = rect.width;
async refuseDocument(): Promise<void> {
let msg = this.translate.instant('lang.rejectDocumentWarning');
if (this.signaturesService.notesContent.length === 0) {
msg = this.translate.instant('lang.refuseDocumentWithoutNote');
}
const alert = await this.alertController.create({
cssClass: 'custom-alert-danger',

Hamza HRAMCHI
committed
header: this.translate.instant('lang.reject'),
inputs: [
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: this.translate.instant('lang.addReason')

Hamza HRAMCHI
committed
text: this.translate.instant('lang.reject'),
const idsToProcess = await this.actionsService.checkGroupMail(this.mainDocument);
const res = await this.signatureMethodService.launchDefaultMode(data.paragraph, idsToProcess);
if (!this.functionsService.empty(res)) {
const config: MatBottomSheetConfig = {
disableClose: true,
direction: 'ltr'
};
this.bottomSheet.open(RejectInfoBottomSheetComponent, config);
this.localStorage.remove(this.mainDocument.id.toString());
}
async validateDocument(mode: any): Promise<void> {
let msg = this.translate.instant('lang.validateDocumentWarning');
if (this.signaturesService.signaturesContent.length === 0 && this.signaturesService.notesContent.length === 0) {
msg = this.translate.instant('lang.validateDocumentWithoutSignOrNote');
}
if (this.signaturesService.stampLock) {
msg = this.translate.instant('lang.certifiedDocumentMsg2');
const alert = await this.alertController.create({
cssClass: 'custom-alert-success',

Hamza HRAMCHI
committed
header: this.translate.instant('lang.validate'),
inputs: [
{
name: 'paragraph',
id: 'paragraph',
type: 'textarea',
placeholder: this.translate.instant('lang.addReason')
},
],
buttons: [
{
text: this.translate.instant('lang.validate'),
handler: async (data: any) => {
const currentUserWorkflow = this.mainDocument.workflow.filter((line: { current: boolean; }) => line.current === true)[0];
const idsToProcess = await this.actionsService.checkGroupMail(this.mainDocument);
const res = await this.signatureMethodService.checkAuthenticationAndLaunchAction(currentUserWorkflow, data.paragraph, idsToProcess);
if (!this.functionsService.empty(res)) {
const config: MatBottomSheetConfig = {
disableClose: true,
direction: 'ltr'
};
this.bottomSheet.open(SuccessInfoValidBottomSheetComponent, config);
this.localStorage.remove(this.mainDocument.id.toString());
const alert = await this.alertController.create({
header: this.translate.instant('lang.deleteNoteAndSignature'),
buttons: [
{
text: this.translate.instant('lang.validate'),
handler: () => {
this.signaturesService.signaturesContent = [];
this.signaturesService.notesContent = [];
this.signaturesService.datesContent = [];
this.localStorage.remove(this.mainDocument.id.toString());
this.notificationService.success('lang.noteAndSignatureDeleted');
}
}
]
loadDoc(index: any) {
this.pageNum = 1;
this.currentDoc = index;
this.totalPages = this.docList[index].pages;
this.backToDetails();
this.signaturesService.currentAction = action.id;
this[action.event]();
}
undoTag() {
if (this.signaturesService.notesContent[this.pageNum]) {
this.signaturesService.notesContent[this.pageNum].pop();
this.localStorage.remove(this.mainDocument.id.toString());
this.localStorage.save(this.mainDocument.id.toString(), JSON.stringify({ 'sign': this.signaturesService.signaturesContent, 'note': this.signaturesService.notesContent }));
checkEmptyNote() {
if (!this.signaturesService.notesContent[this.pageNum]) {
return true;
} else if (this.signaturesService.notesContent[this.pageNum] === 'undefined') {
return true;
} else if (this.signaturesService.notesContent[this.pageNum].length === 0) {
return true;
} else {
return false;
}
}
checkEmptiness() {
let state = true;
for (let pageNum = 1; pageNum <= this.signaturesService.totalPage; pageNum++) {
if (this.signaturesService.datesContent[pageNum]) {
if (this.signaturesService.datesContent[pageNum].length > 0) {
state = false;
break;
}
}
if (this.signaturesService.notesContent[pageNum]) {
if (this.signaturesService.notesContent[pageNum].length > 0) {
state = false;
break;
}
}
if (this.signaturesService.signaturesContent[pageNum]) {
if (this.signaturesService.signaturesContent[pageNum].length > 0) {
state = false;
break;
}
}
}
return state;
}
this.signaturesService.currentToobal = 'visaWorkflow';
openDocumentList() {
this.signaturesService.currentToobal = 'documentList';
openAssociatedDocuments() {
this.menu.open('right-menu');
this.signaturesService.currentToobal = 'associatedDocuments';
openMainDocumentDetail() {
this.signaturesService.currentToobal = 'mainDocumentDetail';
backToDetails() {
this.signaturesService.currentToobal = 'mainDocumentDetail';
deleteSubstution() {
const r = confirm(this.translate.instant('lang.deleteSubstitution') + ' ?');
if (r) {
this.http.put('../rest/users/' + this.authService.user.id + '/substitute', { substitute: null })
.subscribe(() => {
this.authService.updateUserInfoWithTokenRefresh();
this.notificationService.success('lang.substitutionDeleted');
});
back() {
this.navCtrl.back();
}
ionViewWillLeave() {
this.signaturesService.detachTemplate('rightContent');
}
openSelect(event: any) {
if (this.totalPages > 1) {
this.pagesList.interface = 'popover';
this.pagesList.open(event);
}
}
fromHex(hexString: any) {
const res = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i = i + 2) {
const c = hexString.slice(i, i + 2);
res[i / 2] = parseInt(c, 16);
}
return res.buffer;
}