Skip to content
Snippets Groups Projects
document.component.ts 18.5 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 { TranslateService } from '@ngx-translate/core';
    
    import { CdkDragEnd, DragRef, CdkDrag } from '@angular/cdk/drag-drop';
    
    import { DocumentListComponent } from './document-list/document-list.component';
    
    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 = [
            {
                id : 2,
                label : 'lang.reject',
                color : '#e74c3c',
                logo : 'fas fa-backspace',
                event : 'refuseDocument'
            },
            {
                id : 3,
                label : 'lang.signatures',
                color : '#135F7F',
                logo : '',
                event : 'openDrawer'
            },
            {
                id : 1,
                label : 'lang.validate',
                color : '#2ecc71',
                logo : 'fas fa-check-circle',
                event : 'validateDocument'
            },
        ];
    
        pdfDataArr: any;
        freezeSidenavClose: boolean = false;
        startX: number = 0;
        startY: number = 0;
        widthDoc: string = '100%';
        resetDragPos: boolean = false;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            id: 0,
    
        loadingUI: any = false;
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('snav') snav: MatSidenav;
    
        @ViewChild('snavRight') snavRight: MatSidenav;
    
        @ViewChild('dragElem') dragElem: any;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        @ViewChild('appDocumentNotePad') appDocumentNotePad: DocumentNotePadComponent;
    
        @ViewChild('appDocumentList') appDocumentList: DocumentListComponent;
    
    
        @HostListener('mousedown', ['$event']) protected onPMouseDown(event: any) {
            event.preventDefault();
        }
    
        @HostListener('window:resize', ['$event'])
        onResize(event: any) {
    
            this.resetDragPos = true;
            setTimeout(() => {
                this.resetDragPos = false;
            }, 200);
            setTimeout(() => {
                this.signaturesService.workingAreaHeight = $('#snapshotPdf').height();
                this.signaturesService.workingAreaWidth = $('#snapshotPdf').width();
            }, 400);
    
        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;
    
    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.loadingDoc = true;
    
    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.totalPages = this.mainDocument.pages;
    
                            this.signaturesService.mainDocumentId = this.mainDocument.id;
    
                            this.signaturesService.totalPage = this.mainDocument.pages;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            this.initDoc();
    
    
                            const realUserWorkflow = this.mainDocument.workflow.filter((line: { current: boolean; }) => line.current === true)[0];
    
                            if (realUserWorkflow.userId !== this.signaturesService.userLogged.id) {
                                this.http.get('../rest/users/' + realUserWorkflow.userId + '/signatures')
    
                                    .subscribe((dataSign: any) => {
    
                                        this.signaturesService.signaturesListSubstituted = dataSign.signatures;
    
                                        this.signaturesService.loadingSign = false;
                                    });
    
                            } else {
                                this.signaturesService.signaturesListSubstituted = [];
    
                            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' });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                            });
    
                            this.renderImage();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                        });
                } else {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.docList = [];
                    this.signaturesService.signaturesContent = [];
                    this.signaturesService.notesContent = [];
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.snav.open();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.signaturesService.mainDocumentId = null;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                    this.freezeSidenavClose = true;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
        ngDoCheck() {
            if (this.signaturesService.workingAreaHeight !== $('#snapshotPdf').height() || this.signaturesService.workingAreaWidth !== $('#snapshotPdf').width()) {
                this.signaturesService.workingAreaHeight = $('#snapshotPdf').height();
                this.signaturesService.workingAreaWidth = $('#snapshotPdf').width();
            }
        }
    
    
        renderImage() {
            if (this.docList[this.currentDoc].imgContent[this.pageNum] === undefined) {
                this.loadingDoc = true;
                this.loadingUI = true;
                if (this.currentDoc === 0) {
                    this.http.get('../rest/documents/' + this.docList[this.currentDoc].id + '/thumbnails/' + this.pageNum)
                    .subscribe((data: any) => {
                        this.docList[this.currentDoc].imgContent[this.pageNum] = data.fileContent;
                        this.loadingDoc = false;
                        setTimeout(() => {
                            this.loadingUI = false;
                        }, 400);
                    });
                } else {
                    this.http.get('../rest/attachments/' + this.docList[this.currentDoc].id + '/thumbnails/' + this.pageNum)
                    .subscribe((data: any) => {
                        this.docList[this.currentDoc].imgContent[this.pageNum] = data.fileContent;
                        this.loadingDoc = false;
                        setTimeout(() => {
                            this.loadingUI = false;
                        }, 400);
                    });
                }
            }
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        initDoc() {
            this.docList = [];
            this.signaturesService.signaturesContent = [];
            this.signaturesService.notesContent = [];
    
    Damien's avatar
    Damien committed
    
    
            const notesContent = localStorage.getItem(this.mainDocument.id.toString());
    
    Damien's avatar
    Damien committed
            if (notesContent) {
    
                const 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;
        }
    
    
        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;
    
    
            // this.resetDragPosition();
    
            setTimeout(() => {
                this.resetDragPos = false;
            }, 200);
    
            setTimeout(() => {
                this.signaturesService.workingAreaHeight = $('#snapshotPdf').height();
                this.signaturesService.workingAreaWidth = $('#snapshotPdf').width();
            }, 400);
            this.signaturesService.scale = 1;
    
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
        prevPage() {
            this.pageNum--;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            if (this.pageNum === 0) {
                this.pageNum = 1;
            } else {
            }
    
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
            }
    
            this.renderImage();
    
    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++;
            }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            // only for main document
            if (this.currentDoc === 0) {
                this.signaturesService.currentPage = this.pageNum;
            }
    
            this.renderImage();
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        }
    
        addAnnotation(e: any) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    
            if (!this.signaturesService.annotationMode && this.currentDoc === 0 && this.signaturesService.userLogged.substitute === null) {
    
                const posX = e.srcEvent.layerX - this.signaturesService.x;
                const posY = e.srcEvent.layerY - this.signaturesService.y;
    
    
    
                if (this.signaturesService.mobileMode) {
                    this.signaturesService.x = -posX;
                } else {
                    this.signaturesService.x = -posX + 350;
                }
    
    
                this.signaturesService.y = -posY;
                this.zoomForNotes();
                $('.example-box').css({ 'transform': 'translate3d(' + -(posX) + 'px, ' + -(posY) + 'px, 0px)' });
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                this.signaturesService.annotationMode = true;
    
                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;
    
                    this.appDocumentNotePad.initPad();
    
    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;
            }
    
    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');
    
        loadDoc(index: any) {
            this.signaturesService.renderingDoc = true;
            if (index > 0) {
                this.signaturesService.isTaggable = false;
    
            this.pageNum = 1;
            this.currentDoc = index;
    
            this.totalPages = this.docList[index].pages;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        launchEvent(action: any) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            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;
        }
    
    
        openVisaWorkflow() {
    
            this.snavRight.open();
    
            this.signaturesService.sideNavRigtDatas = {
                mode : 'visaWorkflow',
    
                width : '450px',
    
            this.snavRight.open();
    
            this.signaturesService.sideNavRigtDatas = {
                mode : 'documentList',
    
                width : '450px',
    
    
        openMainDocumentDetail() {
            this.snavRight.open();
            this.signaturesService.sideNavRigtDatas = {
                mode : 'mainDocumentDetail',
                width : '450px',
                locked : false,
            };
        }
    
        backToDetails() {
            this.signaturesService.sideNavRigtDatas = {
                mode : 'mainDocumentDetail',
                width : '450px',
                locked : false,
            };
        }
    
    
        deleteSubstution() {
            const r = confirm(this.translate.instant('lang.deleteSubstitution') + ' ?');
    
            if (r) {
    
                const userUpdated = this.signaturesService.userLogged;
                userUpdated.substitute = null;
    
                this.http.put('../rest/users/' + this.signaturesService.userLogged.id, userUpdated)
                .subscribe(() => {
                    this.signaturesService.userLogged = userUpdated;
    
                    this.notificationService.success('lang.substitutionDeleted');
                });
            }
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    }