Skip to content
Snippets Groups Projects
document.component.ts 15.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, OnInit, ViewChild, ElementRef, Input, HostListener } from '@angular/core';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { SignaturesContentService } from '../service/signatures.service';
    import { DomSanitizer } from '@angular/platform-browser';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { MatDialog, MatBottomSheet, MatBottomSheetConfig, MatSidenav } from '@angular/material';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { SignaturesComponent } from '../signatures/signatures.component';
    
    import { ActivatedRoute, Router } from '@angular/router';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { HttpClient } from '@angular/common/http';
    
    import { NotificationService } from '../service/notification.service';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { trigger, transition, style, animate } from '@angular/animations';
    
    import { CookieService } from 'ngx-cookie-service';
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    import { DocumentNotePadComponent } from '../documentNotePad/document-note-pad.component';
    import { WarnModalComponent } from '../modal/warn-modal.component';
    import { RejectInfoBottomSheetComponent } from '../modal/reject-info.component';
    import { ConfirmModalComponent } from '../modal/confirm-modal.component';
    import { SuccessInfoValidBottomSheetComponent } from '../modal/success-info-valid.component';
    
    import { SimplePdfViewerComponent } from 'simple-pdf-viewer';
    
    import { TranslateService } from '@ngx-translate/core';
    
    Damien's avatar
    Damien committed
    declare var PDFJS : any;
    
    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.scss'],
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        animations: [
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            trigger(
                'enterApp',
                [
                    transition(
                        ':leave', [
                            style({ transform: 'translateY(0)' }),
                            animate('500ms', style({ transform: 'translateY(-100%)' })),
                        ]
                    )]
            ),
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            trigger(
                'slideDown',
                [
                    transition(
                        ':enter', [
                            style({ transform: 'translateY(-100%)', opacity: 0 }),
                            animate('500ms', style({ transform: 'translateY(0)', 'opacity': 1 }))
                        ]
                    ),
                    transition(
                        ':leave', [
                            style({ transform: 'translateY(0)', 'opacity': 1 }),
                            animate('500ms', style({ transform: 'translateY(-100%)', 'opacity': 0 })),
                        ]
                    )]
            ),
            trigger(
                'slideUp',
                [
                    transition(
                        ':enter', [
                            style({ transform: 'translateY(100%)', opacity: 0 }),
                            animate('500ms', style({ transform: 'translateY(0)', 'opacity': 1 }))
                        ]
                    ),
                    transition(
                        ':leave', [
                            style({ transform: 'translateY(0)', 'opacity': 1 }),
                            animate('500ms', style({ transform: 'translateY(100%)', 'opacity': 0 })),
                        ]
                    )]
            )
        ],
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    })
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    export class DocumentComponent implements OnInit {
    
    Damien's avatar
    Damien committed
    
        enterApp            : boolean   = true;
        pageNum             : number    = 1;
        signaturesContent   : any       = [];
        totalPages          : number;
        draggable           : boolean;
        loadingDoc          : boolean   = true;
        currentDoc          : number    = 0;
        docList             : any       = [];
        actionsList         : any       = [];
        pdfDataArr          : any;
        freezeSidenavClose  : boolean   = false;
        disableState        : boolean   = true;
    
        startX              : number    = 0;
        startY              : number    = 0;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        outOfBounds = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        @Input() mainDocument: any = {};
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('snav') snav: MatSidenav;
    
        @ViewChild('snavRight') snavRight: MatSidenav;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('canvas') canvas: ElementRef;
        @ViewChild('canvasWrapper') canvasWrapper: ElementRef;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('appDocumentNotePad') appDocumentNotePad: DocumentNotePadComponent;
    
        @ViewChild(SimplePdfViewerComponent) private pdfViewer: SimplePdfViewerComponent;
    
        @HostListener('mousedown', ['$event']) protected onPMouseDown(event: any) {
            event.preventDefault();
        }
    
        constructor(private translate: TranslateService, private router: Router, private route: ActivatedRoute, public http: HttpClient,
    
            public signaturesService: SignaturesContentService,
            public notificationService: NotificationService,
    
            private cookieService: CookieService,
    
            private sanitizer: DomSanitizer, public dialog: MatDialog, private bottomSheet: MatBottomSheet) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.draggable = false;
    
            PDFJS.workerSrc = './../node_modules/simple-pdf-viewer/node_modules/pdfjs-dist/build/pdf.worker.min.js';
    
            if (!this.cookieService.check('maarchParapheurAuth')) {
                this.router.navigate(['/login']);
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        ngOnInit(): void {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            setTimeout(() => {
                this.enterApp = false;
            }, 500);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.route.params.subscribe(params => {
                if (typeof params['id'] !== 'undefined') {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.signaturesService.renderingDoc = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.http.get('../rest/documents/' + params['id'])
                        .subscribe((data: any) => {
                            this.mainDocument = data.document;
    
                            this.signaturesService.mainDocumentId = this.mainDocument.id;
    
    Damien's avatar
    Damien committed
                            this.initDoc();
    
                            if (this.actionsList.length === 0) {
                                this.http.get('../rest/actions')
    
                                    .subscribe((dataActionsList: any) => {
                                        this.actionsList = dataActionsList.actions;
                                        this.actionsList.forEach((element: any) => {
    
    Damien's avatar
    Damien committed
                                            element.allowed = (this.mainDocument.actionsAllowed.indexOf(element.id) > -1);
    
                            if (this.signaturesService.signaturesList.length === 0) {
    
    Florian Azizian's avatar
    Florian Azizian committed
                                this.http.get('../rest/users/' + this.signaturesService.userLogged.id + '/signatures')
    
                                    .subscribe((dataSign: any) => {
                                        this.signaturesService.signaturesList = dataSign.signatures;
                                        this.signaturesService.loadingSign = false;
                                    });
    
                            this.docList.push({ 'id': this.mainDocument.id, 'encodedDocument': this.mainDocument.encodedDocument, 'subject': this.mainDocument.subject });
    
                            this.mainDocument.attachments.forEach((attach: any) => {
                                this.docList.push({ 'id': attach, 'encodedDocument': '', 'title': '' });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.pdfRender(this.docList[this.currentDoc]);
    
                            setTimeout(() => {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                                this.loadingDoc = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.loadNextDoc();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                        }, (err: any) => {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.notificationService.handleErrors(err);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                        });
                } else {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.snav.open();
                    this.freezeSidenavClose = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        initDoc() {
            this.docList = [];
            this.signaturesService.signaturesContent = [];
            this.signaturesService.notesContent = [];
    
    Damien's avatar
    Damien committed
    
    
    Damien's avatar
    Damien committed
            let notesContent = localStorage.getItem(this.mainDocument.id.toString());
    
    Damien's avatar
    Damien committed
            if (notesContent) {
    
    Damien's avatar
    Damien committed
                let storageContent = JSON.parse(notesContent);
    
    Damien's avatar
    Damien committed
                this.signaturesService.notesContent = storageContent['note'];
                this.signaturesService.signaturesContent = storageContent['sign'];
            }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.currentAction = 0;
            this.signaturesService.currentPage = 1;
            this.pageNum = 1;
            this.signaturesContent.currentDoc = 1;
            this.currentDoc = 0;
        }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        pdfRender(document: any) {
    
            const binary_string = window.atob(document.encodedDocument);
            const len = binary_string.length;
            const bytes = new Uint8Array(len);
            for (let i = 0; i < len; i++) {
                bytes[i] = binary_string.charCodeAt(i);
            }
            this.pdfDataArr = bytes.buffer;
    
            this.pdfViewer.openDocument(this.pdfDataArr);
    
        pdfRendered() {
            this.pdfViewer.setZoom(this.signaturesService.scale);
            this.signaturesService.workingAreaHeight = $('.page').height();
            this.signaturesService.workingAreaWidth = $('.page').width();
    
            this.totalPages = this.pdfViewer.getNumberOfPages();
    
            this.signaturesService.totalPage = this.totalPages;
    
            this.disableState = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
            this.signaturesService.renderingDoc = false;
    
    
        pdfError(e: any) {
            console.log(e);
        }
    
        zoomPlus() {
            this.pdfViewer.setZoom(2);
            this.signaturesService.scale = this.pdfViewer.getZoom();
        }
    
        zoomMinus() {
            this.pdfViewer.setZoom(1);
            this.signaturesService.scale = this.pdfViewer.getZoom();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        prevPage() {
    
            this.disableState = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.pageNum--;
    
            this.pdfViewer.prevPage();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            if (this.pageNum === 0) {
                this.pageNum = 1;
            } else {
            }
    
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
            }
    
    
            // fix issue render pdf load is quick click
            setTimeout(() => {
                this.disableState = false;
            }, 500);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        nextPage() {
    
            this.disableState = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            if (this.pageNum >= this.totalPages) {
                this.pageNum = this.totalPages;
            } else {
                this.pageNum++;
            }
    
            this.pdfViewer.nextPage();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            // only for main document
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
            }
    
    
            // fix issue render pdf load is quick click
            setTimeout(() => {
                this.disableState = false;
            }, 500);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        nextDoc() {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.renderingDoc = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            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() {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.renderingDoc = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            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
        }
    
        addAnnotation(e: any) {
            console.log(e.srcEvent.layerY);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
    Damien's avatar
    Damien committed
            if (!this.signaturesService.annotationMode && this.currentDoc === 0) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                this.signaturesService.x = -e.srcEvent.layerX;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                if (e.srcEvent.layerY > 850) {
    
                    this.signaturesService.y = -(e.srcEvent.layerY + 200);
                } else {
                    this.signaturesService.y = -e.srcEvent.layerY;
                }
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                this.signaturesService.annotationMode = true;
    
                this.zoomPlus();
                this.appDocumentNotePad.initPad();
    
    Damien's avatar
    Damien committed
        refuseDocument(): void {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            const dialogRef = this.dialog.open(WarnModalComponent, {
                width: '350px',
                data: {}
            });
    
            dialogRef.afterClosed().subscribe(result => {
                if (result === 'sucess') {
                    const config: MatBottomSheetConfig = {
                        disableClose: true,
                        direction: 'ltr'
                    };
                    this.bottomSheet.open(RejectInfoBottomSheetComponent, config);
    
    Damien's avatar
    Damien committed
                    localStorage.removeItem(this.mainDocument.id.toString());
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                } else if (result === 'annotation') {
                    this.signaturesService.annotationMode = true;
                }
            });
        }
    
    
    Damien's avatar
    Damien committed
        validateDocument(mode: any): void {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            const dialogRef = this.dialog.open(ConfirmModalComponent, {
                width: '350px',
    
                data: { msg: 'lang.areYouSure' }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            });
    
            dialogRef.afterClosed().subscribe(result => {
                if (result) {
                    const config: MatBottomSheetConfig = {
                        disableClose: true,
                        direction: 'ltr'
                    };
                    this.bottomSheet.open(SuccessInfoValidBottomSheetComponent, config);
    
    Damien's avatar
    Damien committed
                    localStorage.removeItem(this.mainDocument.id.toString());
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                }
            });
        }
    
        openDrawer(): void {
    
            if (this.currentDoc > 0) {
                this.currentDoc = 0;
                this.pageNum = 1;
                this.pdfRender(this.docList[this.currentDoc]);
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.showSign = true;
    
            this.signaturesService.showPad = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            const config: MatBottomSheetConfig = {
                disableClose: false,
                direction: 'ltr'
            };
            this.bottomSheet.open(SignaturesComponent, config);
        }
    
        removeTags() {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.signaturesService.currentAction = 0;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            const dialogRef = this.dialog.open(ConfirmModalComponent, {
                width: '350px',
    
                data: { msg: 'lang.deleteNoteAndSignature' }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            });
    
            dialogRef.afterClosed().subscribe(result => {
                if (result) {
                    this.signaturesService.signaturesContent = [];
                    this.signaturesService.notesContent = [];
    
    Damien's avatar
    Damien committed
                    localStorage.removeItem(this.mainDocument.id.toString());
    
                    this.notificationService.success('lang.noteAndSignatureDeleted');
    
        loadNextDoc() {
    
            if (this.docList[this.currentDoc + 1] && this.docList[this.currentDoc + 1].id && this.docList[this.currentDoc + 1].encodedDocument === '') {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                this.http.get('../rest/attachments/' + this.docList[this.currentDoc + 1].id)
                    .subscribe((dataPj: any) => {
                        this.docList[this.currentDoc + 1] = dataPj.attachment;
    
                    }, (err: any) => {
                        this.notificationService.handleErrors(err);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        launchEvent(action: any) {
            this.signaturesService.currentAction = action.id;
            this[action.event]();
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        undoTag() {
            if (this.signaturesService.notesContent[this.pageNum]) {
                this.signaturesService.notesContent[this.pageNum].pop();
    
                localStorage.setItem(this.mainDocument.id.toString(), JSON.stringify({ "sign": this.signaturesService.signaturesContent, "note": this.signaturesService.notesContent }));
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            }
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        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.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;
        }
    
    
        onPanStart(event: any): void {
            this.startX = this.signaturesService.x;
            this.startY = this.signaturesService.y;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            this.outOfBounds = false;
    
        }
    
        onPan(event: any): void {
            event.preventDefault();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
            if (!this.signaturesService.annotationMode && !this.signaturesService.documentFreeze) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                /*this.signaturesService.x = this.startX + event.deltaX;*/
    
    
                this.signaturesService.y = this.startY + event.deltaY;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    }