Skip to content
Snippets Groups Projects
folder-action-list.component.ts 7.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Component, OnInit, Input, ViewChild, Output, EventEmitter } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    
    import { TranslateService } from '@ngx-translate/core';
    
    import { NotificationService } from '@service/notification/notification.service';
    
    import { MatDialog, MatDialogRef } from '@angular/material/dialog';
    import { MatMenuTrigger } from '@angular/material/menu';
    
    import { Router } from '@angular/router';
    import { ConfirmComponent } from '../../../plugins/modal/confirm.component';
    
    import { filter, exhaustMap, tap, map, catchError } from 'rxjs/operators';
    
    import { HeaderService } from '@service/header.service';
    
    import { FoldersService } from '../folders.service';
    
    import { of } from 'rxjs';
    
    import { PrivilegeService } from '@service/privileges.service';
    
    
    @Component({
        selector: 'app-folder-action-list',
    
        templateUrl: 'folder-action-list.component.html',
    
        styleUrls: ['folder-action-list.component.scss'],
    })
    export class FolderActionListComponent implements OnInit {
    
    
        loading: boolean = false;
    
        @ViewChild(MatMenuTrigger, { static: false }) contextMenu: MatMenuTrigger;
        @Output() triggerEvent = new EventEmitter<string>();
    
        contextMenuPosition = { x: '0px', y: '0px' };
        contextMenuTitle = '';
        currentAction: any = {};
        basketInfo: any = {};
        contextResId = 0;
        currentLock: any = null;
        arrRes: any[] = [];
    
    
        isSelectedFreeze: any;
        isSelectedBinding: any;
    
    
        actionsList: any[] = [];
    
        @Input() selectedRes: any;
        @Input() totalRes: number;
        @Input() contextMode: boolean;
        @Input() currentFolderInfo: any;
    
        @Input('currentResource') currentResource: any = {};
    
        @Output() refreshEvent = new EventEmitter<string>();
        @Output() refreshPanelFolders = new EventEmitter<string>();
    
            public translate: TranslateService,
    
            public http: HttpClient,
            private notify: NotificationService,
            public dialog: MatDialog,
    
            private foldersService: FoldersService,
            public privilegeService: PrivilegeService,
    
    
        dialogRef: MatDialogRef<any>;
    
        ngOnInit(): void { }
    
        open(x: number, y: number, row: any) {
    
            // Adjust the menu anchor position
            this.contextMenuPosition.x = x + 'px';
            this.contextMenuPosition.y = y + 'px';
    
    
            this.getFreezeBindingValue(this.contextResId);
    
            this.contextMenuTitle = row.chrono;
            this.contextResId = row.resId;
    
    
            // Opens the menu
            this.contextMenu.openMenu();
    
            // prevents default
            return false;
        }
    
        refreshFolders() {
    
            this.refreshPanelFolders.emit();
    
        }
    
        refreshDaoAfterAction() {
            this.refreshEvent.emit();
        }
    
        unclassify() {
    
            this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.delete'), msg: 'Voulez-vous enlever <b>' + this.selectedRes.length + '</b> document(s) du classement ?' } });
    
    
            this.dialogRef.afterClosed().pipe(
                filter((data: string) => data === 'ok'),
    
                exhaustMap(() => this.http.request('DELETE', '../rest/folders/' + this.currentFolderInfo.id + '/resources', { body: { resources: this.selectedRes } })),
    
                tap((data: any) => {
    
                    this.notify.success(this.translate.instant('lang.removedFromFolder'));
    
                    this.refreshFolders();
    
                    this.foldersService.getPinnedFolders();
    
                    this.refreshDaoAfterAction();
    
                }),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
    
            this.http.get('../rest/resources/' + this.selectedRes + '/baskets').pipe(
    
                    this.basketList.groups = data.groupsBaskets.filter((x: any, i: any, a: any) => x && a.map((info: any) => info.groupId).indexOf(x.groupId) === i);
                    this.basketList.list = data.groupsBaskets;
    
                }),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
    
            if (this.contextMenuTitle !== this.translate.instant('lang.undefined')) {
    
                this.router.navigate(['/basketList/users/' + this.headerService.user.id + '/groups/' + basket.groupId + '/baskets/' + basket.basketId], { queryParams: { chrono: '"' + this.contextMenuTitle + '"' } });
            } else {
                this.router.navigate(['/basketList/users/' + this.headerService.user.id + '/groups/' + basket.groupId + '/baskets/' + basket.basketId]);
            }
    
            this.dialogRef = this.dialog.open(ConfirmComponent, { panelClass: 'maarch-modal', autoFocus: false, disableClose: true, data: { title: this.translate.instant('lang.delete'), msg: this.translate.instant('lang.stopFollowingAlert') } });
    
    
            this.dialogRef.afterClosed().pipe(
                filter((data: string) => data === 'ok'),
    
                exhaustMap(() => this.http.request('DELETE', '../rest/resources/unfollow', { body: { resources: this.selectedRes } })),
    
                    this.notify.success(this.translate.instant('lang.removedFromFolder'));
    
                    this.headerService.nbResourcesFollowed -= data.unFollowed;
                    this.refreshDaoAfterAction();
    
                }),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
    
    
        toggleFreezing(value) {
            this.http.put('../rest/archival/freezeRetentionRule', { resources: this.selectedRes, freeze : value }).pipe(
                tap(() => {
                    if (value) {
                        this.notify.success(this.translate.instant('lang.retentionRuleFrozen'));
                    } else {
                        this.notify.success(this.translate.instant('lang.retentionRuleThawed'));
    
                    }
                }
                ),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
                })
            ).subscribe();
        }
    
        toogleBinding(value) {
            this.http.put('../rest/archival/binding', { resources: this.selectedRes, binding : value }).pipe(
                tap(() => {
                    if (value) {
                        this.notify.success(this.translate.instant('lang.bindingMail'));
                    } else if (value === false) {
                        this.notify.success(this.translate.instant('lang.noBindingMal'));
                    } else {
                        this.notify.success(this.translate.instant('lang.bindingUndefined'));
                    }
                }
                ),
                catchError((err: any) => {
                    this.notify.handleSoftErrors(err);
                    return of(false);
                })
            ).subscribe();
        }
    
    
        getFreezeBindingValue(id) {
            this.http.get(`../rest/resources/${id}?light=true`).pipe(
                tap((infos: any) => {
    
                    this.isSelectedFreeze = infos.retentionFrozen;
                    this.isSelectedBinding = infos.binding;
    
                }),
                catchError((err: any) => {
                    this.notify.handleErrors(err);
                    return of(false);
                })
            ).subscribe();
    
        }