import { Location } from '@angular/common'; import { HttpClient } from '@angular/common/http'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { Router } from '@angular/router'; import { DmpAccessType } from '@app/core/common/enum/dmp-access-type'; import { DmpUserRole } from '@app/core/common/enum/dmp-user-role'; import { ReferenceType } from '@app/core/common/enum/reference-type'; import { DescriptionTemplatesInSection, DmpBlueprint, DmpBlueprintDefinition, DmpBlueprintDefinitionSection, FieldInSection } from '@app/core/model/dmp-blueprint/dmp-blueprint'; import { Dmp } from '@app/core/model/dmp/dmp'; import { DmpBlueprintService } from '@app/core/services/dmp/dmp-blueprint.service'; import { DmpService } from '@app/core/services/dmp/dmp.service'; import { LockService } from '@app/core/services/lock/lock.service'; import { MatomoService } from '@app/core/services/matomo/matomo-service'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { ReferenceService } from '@app/core/services/reference/reference.service'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { FileUtils } from '@app/core/services/utilities/file-utils.service'; import { BaseComponent } from '@common/base/base.component'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import { Guid } from '@common/types/guid'; import { TranslateService } from '@ngx-translate/core'; import { map, takeUntil } from 'rxjs/operators'; import { nameof } from 'ts-simple-nameof'; import { DmpStatus } from '../../../../core/common/enum/dmp-status'; import { AuthService } from '../../../../core/services/auth/auth.service'; @Component({ selector: 'app-dmp-listing-item-component', templateUrl: './dmp-listing-item.component.html', styleUrls: ['./dmp-listing-item.component.scss'], }) export class DmpListingItemComponent extends BaseComponent implements OnInit { @Input() dmp: Dmp; @Input() showDivider: boolean = true; @Input() isPublic: boolean; @Output() onClick: EventEmitter = new EventEmitter(); isDraft: boolean; isFinalized: boolean; isPublished: boolean; dmpStatusEnum = DmpStatus; referenceTypeEnum = ReferenceType; constructor( private router: Router, private dialog: MatDialog, private authentication: AuthService, public enumUtils: EnumUtils, private dmpService: DmpService, private dmpBlueprintService: DmpBlueprintService, private language: TranslateService, private uiNotificationService: UiNotificationService, private lockService: LockService, private location: Location, private httpClient: HttpClient, private matomoService: MatomoService, public referenceService: ReferenceService, private fileUtils: FileUtils) { super(); } ngOnInit() { this.matomoService.trackPageView('DMP Listing Item'); if (this.dmp.status == DmpStatus.Draft) { this.isDraft = true; this.isFinalized = false; this.isPublished = false; } else if (this.dmp.status == DmpStatus.Finalized) { this.isDraft = false; this.isFinalized = true; this.isPublished = false; if (this.dmp.status === DmpStatus.Finalized && this.dmp.accessType === DmpAccessType.Public) { this.isPublished = true } } } public isAuthenticated(): boolean { return this.authentication.currentAccountIsAuthenticated(); } openShareDialog(rowId: any, rowName: any) { //TODO: add this. // const dialogRef = this.dialog.open(DmpInvitationDialogComponent, { // // height: '250px', // // width: '700px', // autoFocus: false, // restoreFocus: false, // data: { // dmpId: rowId, // dmpName: rowName // } // }); } editClicked(dmpId: String) { this.router.navigate(['/plans/edit/' + dmpId]); } showDatasets(rowId: String, rowLabel: String) { this.router.navigate(['/datasets/dmp/' + rowId, { dmpLabel: rowLabel }]); } viewVersions(rowId: String, rowLabel: String, dmp: Dmp) { // if (dmp.public && !this.isUserOwner(dmp)) { // let url = this.router.createUrlTree(['/explore-plans/versions/', rowId, { groupLabel: rowLabel }]); // window.open(url.toString(), '_blank'); // // this.router.navigate(['/explore-plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } }); // } else { // let url = this.router.createUrlTree(['/plans/versions/', rowId, { groupLabel: rowLabel }]); // window.open(url.toString(), '_blank'); // // this.router.navigate(['/plans/versions/' + rowId], { queryParams: { groupLabel: rowLabel } }); // } } isUserDMPRelated() { const principalId: Guid = this.authentication.userId(); return this.dmp.dmpUsers.some(x => (x.user.id === principalId)); } cloneOrNewVersionClicked(dmp: Dmp, isNewVersion: boolean) { // this.dmpService.getSingle(dmp.id).pipe(map(data => data as DmpModel)) // .pipe(takeUntil(this._destroyed)) // .subscribe(data => { // this.dmpModel = new DmpEditorModel(); // this.dmpModel.grant = new GrantTabModel(); // this.dmpModel.project = new ProjectFormModel(); // this.dmpModel.funder = new FunderFormModel(); // this.dmpModel.extraProperties = new ExtraPropertiesFormModel(); // this.dmpModel.fromModel(data); // this.dmpModel.status = DmpStatus.Draft; // this.dmpFormGroup = this.dmpModel.buildForm(); // if (!isNullOrUndefined(this.dmpFormGroup.get('profile').value)) { // this.getBlueprintDefinition(Guid.parse(this.dmpFormGroup.get('profile').value), result => { // this.checkForGrant(result.definition); // this.checkForFunder(result.definition); // this.checkForProject(result.definition); // }); // } // if (!isNewVersion) { // this.dmpFormGroup.get('label').setValue(dmp.label + " New"); // } // this.openCloneDialog(isNewVersion); // }); } private getBlueprintDefinition(blueprintId: Guid, successFunction) { this.dmpBlueprintService.getSingle(blueprintId, [ [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.id)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.label)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.description)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.ordinal)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.hasTemplates)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.id)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.category)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.dataType)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.systemFieldType)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.label)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.placeholder)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.description)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.required)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.fields), nameof(x => x.ordinal)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.id)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.descriptionTemplateId)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.label)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.minMultiplicity)].join('.'), [nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.maxMultiplicity)].join('.'), ] ) .pipe(map(data => data as DmpBlueprint), takeUntil(this._destroyed)) .subscribe( data => successFunction(data), ); } openCloneDialog(isNewVersion: boolean) { // TODO: fix this // const dialogRef = this.dialog.open(CloneDialogComponent, { // maxWidth: '700px', // maxHeight: '80vh', // data: { // formGroup: this.dmpFormGroup, // datasets: this.dmpFormGroup.get('datasets').value, // isNewVersion: isNewVersion, // confirmButton: this.language.instant('DMP-EDITOR.CLONE-DIALOG.SAVE'), // cancelButton: this.language.instant('DMP-EDITOR.CLONE-DIALOG.CANCEL'), // } // }); // dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { // if (result) { // if (!isNewVersion) { // this.dmpService.clone(this.dmpFormGroup.getRawValue(), this.dmp.id) // .pipe(takeUntil(this._destroyed)) // .subscribe( // complete => this.onCloneOrNewVersionCallbackSuccess(complete), // error => this.onCloneOrNewVersionCallbackError(error) // ); // } else if (isNewVersion) { // this.dmpService.newVersion(this.dmpFormGroup.getRawValue(), this.dmp.id) // .pipe(takeUntil(this._destroyed)) // .subscribe( // complete => this.onCloneOrNewVersionCallbackSuccess(complete), // error => this.onCloneOrNewVersionCallbackError(error) // ); // } // } // }); } downloadXml(id: Guid) { // TODO: Add this // this.dmpService.downloadXML(id) // .pipe(takeUntil(this._destroyed)) // .subscribe(response => { // const blob = new Blob([response.body], { type: 'application/xml' }); // const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition')); // FileSaver.saveAs(blob, filename); // this.matomoService.trackDownload('dmps', "xml", id); // }); } downloadDocx(id: Guid) { // TODO: Add this // this.dmpService.downloadDocx(id) // .pipe(takeUntil(this._destroyed)) // .subscribe(response => { // const blob = new Blob([response.body], { type: 'application/msword' }); // const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition')); // FileSaver.saveAs(blob, filename); // this.matomoService.trackDownload('dmps', "docx", id); // }); } downloadPDF(id: Guid) { // TODO: Add this // this.dmpService.downloadPDF(id) // .pipe(takeUntil(this._destroyed)) // .subscribe(response => { // const blob = new Blob([response.body], { type: 'application/pdf' }); // const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition')); // FileSaver.saveAs(blob, filename); // this.matomoService.trackDownload('dmps', "pdf", id); // }); } downloadJson(id: Guid) { // TODO: Add this // this.dmpService.downloadJson(id) // .pipe(takeUntil(this._destroyed)) // .subscribe(response => { // const blob = new Blob([response.body], { type: 'application/json' }); // const filename = this.fileUtils.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition')); // FileSaver.saveAs(blob, filename); // this.matomoService.trackDownload('dmps', "json", id); // }, async error => { // this.onExportCallbackError(error); // }); } // async onExportCallbackError(error: any) { // const errorJsonText = await error.error.text(); // const errorObj = JSON.parse(errorJsonText); // this.uiNotificationService.snackBarNotification(errorObj.message, SnackBarNotificationLevel.Error); // } deleteClicked(id: string) { this.lockService.checkLockStatus(id).pipe(takeUntil(this._destroyed)) .subscribe(lockStatus => { if (!lockStatus) { this.openDeleteDialog(id); } else { this.openLockedByUserDialog(); } }); } openDeleteDialog(id: string) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', restoreFocus: false, data: { message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.DELETE'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL'), isDeleteConfirmation: true } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.dmpService.delete(id) .pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onDeleteCallbackSuccess(), error => this.onDeleteCallbackError(error) ); } }); } openLockedByUserDialog() { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '400px', restoreFocus: false, data: { message: this.language.instant('DMP-EDITOR.ACTIONS.LOCK') } }); } isDraftDmp(activity: Dmp) { return activity.status == DmpStatus.Draft; } reloadPage(): void { const path = this.location.path(); this.router.navigateByUrl('/reload', { skipLocationChange: true }).then(() => { this.router.navigate([path]); }); } onDeleteCallbackSuccess(): void { this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-DELETE'), SnackBarNotificationLevel.Success); this.reloadPage(); } onDeleteCallbackError(error) { this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-DELETE'), SnackBarNotificationLevel.Error); } onCallbackSuccess(): void { this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); this.router.navigate(['/plans']); } onCloneOrNewVersionCallbackSuccess(cloneId: String): void { this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); this.router.navigate(['/plans/edit/', cloneId]); } onCloneOrNewVersionCallbackError(error: any) { this.uiNotificationService.snackBarNotification(error.error.message ? error.error.message : this.language.instant('GENERAL.SNACK-BAR.UNSUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Error); } isUserOwner(dmp: Dmp): boolean { const principalId: Guid = this.authentication.userId(); if (principalId) return !!dmp.dmpUsers.find(x => (x.role === DmpUserRole.Owner) && (principalId === x.id)); } }