Skip to content
Snippets Groups Projects
document.component.ts 14.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alex ORLUC's avatar
    Alex ORLUC committed
    import { Component, OnInit, ViewChild, ElementRef, Input, Inject } from '@angular/core';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { Observable } from 'rxjs';
    import { SignaturesContentService } from '../service/signatures.service';
    import * as PDFJS from 'pdfjs-dist/build/pdf.js';
    import { DomSanitizer } from '@angular/platform-browser';
    import * as $ from 'jquery';
    import { SignaturePad } from 'angular2-signaturepad/signature-pad';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import {
        MatDialogRef,
        MatDialog,
        MAT_DIALOG_DATA,
        MatBottomSheet,
        MatBottomSheetRef,
        MatBottomSheetConfig,
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        MatSnackBar,
        MatSidenav
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    } from '@angular/material';
    import { SignaturesComponent } from '../signatures/signatures.component';
    import { ActivatedRoute } from '@angular/router';
    import { HttpClient } from '@angular/common/http';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { connectableObservableDescriptor } from 'rxjs/internal/observable/ConnectableObservable';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    @Component({
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        selector: 'app-document',
        templateUrl: 'document.component.html',
        styleUrls: ['document.component.styl']
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    export class DocumentComponent implements OnInit {
    
        pdf: any;
        pageNum = 1;
        signaturesContent: any = [];
        pageRendering = false;
        pageNumPending: null;
        scale = 1;
        totalPages: number;
        draggable: boolean;
        loadingDoc = true;
        signaturePadPosX = 0;
        signaturePadPosY = 0;
        currentDoc = 0;
        docList: any = [];
        annotationPadOptions = {
            throttle: 0,
            minWidth: 0.8,
            maxWidth: 1,
            backgroundColor: 'rgba(255, 255, 255, 0)',
            canvasWidth: 380,
            canvasHeight: 270
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        };
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        freezeSidenavClose = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        penColors = [{ id: 'black' }, { id: 'orange' }, { id: '#FF0000' }];
    
        @Input() mainDocument: any = {};
    
        @ViewChild('pdfpage') elPdfContainer: ElementRef;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('snav') snav: MatSidenav;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('canvas') canvas: ElementRef;
        @ViewChild('canvasWrapper') canvasWrapper: ElementRef;
        @ViewChild(SignaturePad) signaturePad: SignaturePad;
    
        public context: CanvasRenderingContext2D;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        constructor(private route: ActivatedRoute, public http: HttpClient,
            public snackBar: MatSnackBar, public signaturesService: SignaturesContentService,
            private sanitization: DomSanitizer, public dialog: MatDialog, private bottomSheet: MatBottomSheet) {
            this.draggable = false;
            PDFJS.GlobalWorkerOptions.workerSrc = './node_modules/pdfjs-dist/build/pdf.worker.js';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        // TO DO REMOVE DEFINE EXEMPLE
        ngOnInit(): void {
            this.route.params.subscribe(params => {
                if (typeof params['id'] !== 'undefined') {
                    this.http.get('../rest/documents/' + params['id'])
                        .subscribe((data: any) => {
                            this.mainDocument = data.document;
                            this.docList.push({ 'id': this.mainDocument.id, 'document': this.mainDocument.document, 'title': this.mainDocument.subject });
                            this.mainDocument.attachments.forEach((attach: any, index: any) => {
                                this.docList.push({ 'id': attach.id, 'document': '', 'title': '' });
                            });
                            this.pdfRender(this.docList[this.currentDoc]);
                            this.context = (<HTMLCanvasElement>this.canvas.nativeElement).getContext('2d');
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.loadNextDoc();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                        }, () => {
                            console.log('error !');
                        });
                } else {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.snav.open();
                    this.freezeSidenavClose = true;
                    console.log('OK !');
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                }
            });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        pdfRender(document: any) {
    
            const pdfData = atob(document.document);
            PDFJS.getDocument({ data: pdfData }).then((_pdfDoc: any) => {
                this.pdf = _pdfDoc;
                this.totalPages = this.pdf.pdfInfo.numPages;
                this.signaturesService.totalPage = this.totalPages;
                this.renderPage(this.pageNum, this.canvas.nativeElement, 1);
            });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        renderPage(pageNumber: any, canvas: any, scale: number) {
            this.pdf.getPage(pageNumber).then((page: any) => {
                const viewport = page.getViewport(scale);
                const ctx = this.context;
                canvas.height = viewport.height;
                canvas.width = viewport.width;
                this.annotationPadOptions.canvasWidth = viewport.width;
                this.annotationPadOptions.canvasHeight = viewport.height / 2;
                const renderContext = {
                    canvasContext: ctx,
                    viewport: viewport
                };
                this.canvas.nativeElement.style.width = 'auto';
                this.canvas.nativeElement.style.height = '100vh';
                const renderTask = page.render(renderContext);
                renderTask.then(() => {
                    this.signaturesService.signWidth = viewport.width / 5;
                    console.log('Page rendered - WIDTH : ' + viewport.width);
                    this.loadingDoc = false;
                });
            });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        prevPage() {
            this.pageNum--;
            if (this.pageNum === 0) {
                this.pageNum = 1;
            } else {
                this.renderPage(this.pageNum, this.canvas, 1);
            }
    
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
                console.log('Current page : ' + this.signaturesService.currentPage);
                console.log('Signatures content:');
                console.log(this.signaturesService.signaturesContent[this.signaturesService.currentPage]);
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        nextPage() {
            if (this.pageNum >= this.totalPages) {
                this.pageNum = this.totalPages;
            } else {
                this.pageNum++;
                this.renderPage(this.pageNum, this.canvas, 1);
            }
    
            // only for main document
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
                console.log('Current page : ' + this.signaturesService.currentPage);
                console.log('Signatures content:');
                console.log(this.signaturesService.signaturesContent[this.signaturesService.currentPage]);
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        nextDoc() {
            this.loadingDoc = true;
            this.signaturesService.isTaggable = false;
            this.pageNum = 1;
            this.currentDoc++;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.pdfRender(this.docList[this.currentDoc]);
            this.loadNextDoc();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        prevDoc() {
            this.loadingDoc = true;
            this.pageNum = 1;
            this.currentDoc--;
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = 1;
                this.signaturesService.isTaggable = true;
            }
            this.pdfRender(this.docList[this.currentDoc]);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        addAnnotation(e: any) {
            console.log(e);
            if (!this.signaturesService.lockNote && this.currentDoc === 0) {
                this.scale = 1;
                this.signaturesService.annotationMode = true;
                this.renderPage(this.pageNum, this.canvas.nativeElement, this.scale);
                this.signaturePadPosX = e.layerX - 40;
                this.signaturePadPosY = e.layerY - 40;
                this.signaturesService.lockNote = true;
            }
        }
    
        addSignature() {
            console.log(this.signaturesService.signaturesContent);
            // this.signaturesContent.push(this.signaturesContent.length);
        }
    
        test(event: any, i: number) {
            this.signaturesService.signaturesContent[this.signaturesService.currentPage][i].positionX = event.x;
            this.signaturesService.signaturesContent[this.signaturesService.currentPage][i].positionY = event.y;
            console.log(this.signaturesService.signaturesContent[this.signaturesService.currentPage][i]);
        }
    
        test2(event: any, i: number) {
            this.signaturesService.notesContent[this.signaturesService.currentPage][i].positionX = event.x;
            this.signaturesService.notesContent[this.signaturesService.currentPage][i].positionY = event.y;
            console.log(this.signaturesService.notesContent[this.signaturesService.currentPage][i]);
        }
    
        movePad(event: any) {
            this.signaturePadPosX = event.x;
            this.signaturePadPosY = event.y;
            $('.page-viewer, .canvas-wrapper, .mat-sidenav-content').css({ 'overflow': 'auto' });
        }
    
        resetPos(event: any, i: number) {
            console.log(event.source);
        }
    
        deleteSignature(index: number) {
            this.signaturesService.signaturesContent[this.signaturesService.currentPage].splice(index, 1);
        }
    
        deleteNote(index: number) {
            this.signaturesService.notesContent[this.signaturesService.currentPage].splice(index, 1);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        validateAnnotation() {
            if (!this.signaturesService.notesContent[this.signaturesService.currentPage]) {
                this.signaturesService.notesContent[this.signaturesService.currentPage] = [];
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.notesContent[this.signaturesService.currentPage].push(
                {
                    'fullPath': this.signaturePad.toDataURL('image/npg'),
                    'positionX': this.signaturePadPosX,
                    'positionY': this.signaturePadPosY,
                    'height': this.annotationPadOptions.canvasHeight,
                    'width': this.annotationPadOptions.canvasWidth,
                }
            );
            this.signaturePad.clear();
            this.scale = 1;
            this.signaturesService.annotationMode = false;
            this.signaturesService.lockNote = false;
            this.renderPage(this.pageNum, this.canvas.nativeElement, this.scale);
            this.snackBar.open('Annotation ajoutée', null,
                {
                    duration: 3000,
                    panelClass: 'center-snackbar',
                    verticalPosition: 'top'
                }
            );
            console.log(this.signaturesService.notesContent);
        }
    
        cancelAnnotation() {
            this.signaturePad.clear();
            this.scale = 1;
            this.signaturesService.annotationMode = false;
            this.signaturesService.lockNote = false;
            this.renderPage(this.pageNum, this.canvas.nativeElement, this.scale);
        }
    
        freezDoc() {
            $('.page-viewer, .canvas-wrapper, .mat-sidenav-content').css({ 'overflow': 'hidden' });
        }
    
        onColorChange(entry: any) {
            this.signaturePad.set('penColor', entry.id);
        }
    
        onDotChange(entry: any) {
            console.log(entry);
            this.signaturePad.set('minWidth', parseFloat(entry));
            this.signaturePad.set('maxWidth', parseFloat(entry) + 2);
        }
    
        openDialog(): void {
            const dialogRef = this.dialog.open(WarnModalComponent, {
                width: '350px',
                data: {}
            });
    
            dialogRef.afterClosed().subscribe(result => {
                console.log('The dialog was closed');
                if (result === 'sucess') {
                    const config: MatBottomSheetConfig = {
                        disableClose: true,
                        direction: 'ltr'
                    };
                    this.bottomSheet.open(RejectInfoBottomSheetComponent, config);
                    setTimeout(() => {
                        this.bottomSheet.dismiss();
                    }, 2000);
                } else if (result === 'annotation') {
                    this.signaturesService.annotationMode = true;
                    this.signaturesService.lockNote = true;
                }
            });
        }
    
        confirmDialog(): void {
            const dialogRef = this.dialog.open(ConfirmModalComponent, {
                width: '350px',
                data: { msg: 'Êtes-vous sûr  ?' }
            });
    
            dialogRef.afterClosed().subscribe(result => {
                console.log('The dialog was closed');
                if (result) {
                    const config: MatBottomSheetConfig = {
                        disableClose: true,
                        direction: 'ltr'
                    };
                    this.bottomSheet.open(SuccessInfoValidBottomSheetComponent, config);
                    setTimeout(() => {
                        this.bottomSheet.dismiss();
                    }, 2000);
                }
            });
        }
    
        openDrawer(): void {
            this.signaturesService.showSign = true;
            const config: MatBottomSheetConfig = {
                disableClose: false,
                direction: 'ltr'
            };
            this.bottomSheet.open(SignaturesComponent, config);
        }
    
        removeTags() {
            const dialogRef = this.dialog.open(ConfirmModalComponent, {
                width: '350px',
                data: { msg: 'Effacer toutes les annotations et signatures ?' }
            });
    
            dialogRef.afterClosed().subscribe(result => {
                console.log('The dialog was closed');
                if (result) {
                    this.signaturesService.signaturesContent = [];
                    this.signaturesService.notesContent = [];
                    this.snackBar.open('Annotations / signatures supprimées du document', null,
                        {
                            duration: 3000,
                            panelClass: 'center-snackbar',
                            verticalPosition: 'top'
                        }
                    );
                }
            });
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        loadNextDoc () {
            if (this.docList[this.currentDoc + 1] && this.docList[this.currentDoc + 1].id && this.docList[this.currentDoc + 1].document === '') {
                this.http.get('../rest/attachments/' + this.docList[this.currentDoc + 1].id)
                    .subscribe((dataPj: any) => {
                        this.docList[this.currentDoc + 1] = dataPj.attachment;
                        this.snackBar.open('Pièce jointe chargé', null,
                        {
                            duration: 3000,
                            panelClass: 'center-snackbar',
                            verticalPosition: 'top'
                        }
                    );
                    }, () => {
                        console.log('error !');
                    });
            }
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    }
    
    @Component({
        templateUrl: '../modal/warn-modal.component.html',
        styleUrls: ['../modal/warn-modal.component.styl']
    })
    export class WarnModalComponent {
        constructor(@Inject(MAT_DIALOG_DATA) public data: any, public dialogRef: MatDialogRef<WarnModalComponent>) { }
    }
    
    @Component({
        templateUrl: '../modal/confirm-modal.component.html',
        styleUrls: ['../modal/confirm-modal.component.styl']
    })
    export class ConfirmModalComponent {
        constructor(@Inject(MAT_DIALOG_DATA) public data: any, public dialogRef: MatDialogRef<ConfirmModalComponent>) { }
    }
    
    @Component({
        templateUrl: '../modal/success-info-valid.html',
        styleUrls: ['../modal/success-info-valid.styl']
    })
    export class SuccessInfoValidBottomSheetComponent {
        constructor(private bottomSheetRef: MatBottomSheetRef<SuccessInfoValidBottomSheetComponent>) { }
    }
    
    @Component({
        templateUrl: '../modal/reject-info.html',
        styleUrls: ['../modal/reject-info.styl']
    })
    export class RejectInfoBottomSheetComponent {
        constructor(private bottomSheetRef: MatBottomSheetRef<RejectInfoBottomSheetComponent>) { }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    }