argos/dmp-frontend/src/app/dmps/editor/dmp-editor.component.ts

308 lines
12 KiB
TypeScript
Raw Normal View History

2017-12-18 11:01:22 +01:00
import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
2018-02-14 11:38:19 +01:00
import { MatPaginator, MatSort, MatSnackBar, MatDialog, MatChipList, MatChip } from "@angular/material";
2017-12-18 11:01:22 +01:00
import { Router, ActivatedRoute, Params } from "@angular/router";
2017-12-14 18:13:28 +01:00
import { TranslateService } from "@ngx-translate/core";
import { DataSource } from "@angular/cdk/table";
import { Observable } from "rxjs/Observable";
2017-12-18 11:01:22 +01:00
import { JsonSerializer } from "../../utilities/JsonSerializer";
2018-03-28 15:24:47 +02:00
import { FormGroup, FormControl } from "@angular/forms";
2017-12-18 11:01:22 +01:00
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component";
import { BaseErrorModel } from "../../models/error/BaseErrorModel";
2017-12-14 18:13:28 +01:00
import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service";
2017-12-18 11:01:22 +01:00
import { DataManagementPlanModel } from "../../models/data-managemnt-plans/DataManagementPlanModel";
import { ExternalSourcesService } from "../../services/external-sources/external-sources.service";
2017-12-18 12:24:12 +01:00
import { ExternalSourcesItemModel } from "../../models/external-sources/ExternalSourcesItemModel";
2017-12-21 10:16:46 +01:00
import { RequestItem } from "../../models/criteria/RequestItem";
import { DatasetProfileCriteria } from "../../models/criteria/dataset/DatasetProfileCriteria";
import { DataManagementPlanCriteriaComponent } from "../../shared/components/criteria/data-management-plan/dmp-criteria.component";
import { DatasetProfileModel } from "../../models/datasets/DatasetProfileModel";
2017-12-21 10:39:17 +01:00
import { AutoCompleteConfiguration } from "../../shared/components/autocomplete/AutoCompleteConfiguration";
import { ProjectCriteria } from "../../models/criteria/project/ProjectCriteria";
import { ProjectService } from "../../services/project/project.service";
2018-05-14 08:44:35 +02:00
import { DmpUsersModel } from "../../models/dmpUsers/DmpUsersModel";
2018-05-28 11:50:42 +02:00
import { AddResearchersComponent } from "../../shared/components/add-researchers/add-researchers.component";
2018-02-05 11:34:25 +01:00
import { ViewContainerRef } from '@angular/core';
import { TdDialogService } from '@covalent/core';
2018-05-28 11:50:42 +02:00
import { AvailableProfilesComponent } from "../../shared/components/available-profiles/available-profiles.component";
2018-05-14 08:44:35 +02:00
import { AutoCompleteChipConfiguration } from "../../shared/components/autocompleteChips/AutoCompleteChipConfiguration";
import { BaseCriteria } from "../../models/criteria/BaseCriteria";
2018-03-28 15:24:47 +02:00
import { DataManagementPlanProfileService } from "../../services/data-management-plan-profile/datamanagement-profile.service";
import { DataManagementPlanProfileListingModel } from "../../models/data-management-plan-profile/DataManagementPlanProfileListingModel";
import { DataManagementPlanProfileCriteria } from "../../models/criteria/dmp-profile/DataManagementPlanProfileCriteria";
import { DataManagementPlanProfile } from "../../models/data-management-plan-profile/DataManagementPlanProfile";
import { LanguageResolverService } from "../../services/language-resolver/language-resolver.service";
2018-06-27 12:29:21 +02:00
import { IBreadCrumbComponent } from "../../shared/components/breadcrumb/definition/IBreadCrumbComponent";
import { BreadcrumbItem } from "../../shared/components/breadcrumb/definition/breadcrumb-item";
2017-12-14 18:13:28 +01:00
@Component({
selector: 'app-dmp-editor-component',
templateUrl: 'dmp-editor.component.html',
2017-12-18 11:01:22 +01:00
styleUrls: ['./dmp-editor.component.scss'],
encapsulation: ViewEncapsulation.None
2017-12-14 18:13:28 +01:00
})
2018-06-27 12:29:21 +02:00
export class DataManagementPlanEditorComponent implements AfterViewInit, IBreadCrumbComponent {
2017-12-18 11:01:22 +01:00
2017-12-21 10:16:46 +01:00
2018-06-27 12:29:21 +02:00
breadCrumbs: Observable<BreadcrumbItem[]>;
2017-12-18 11:01:22 +01:00
isNew = true;
2018-03-28 15:24:47 +02:00
textCtrl = new FormControl();
2017-12-18 11:01:22 +01:00
dataManagementPlan: DataManagementPlanModel;
formGroup: FormGroup = null;
filteringOrganisationsAsync: boolean = false;
filteringResearchersAsync: boolean = false;
2017-12-20 17:50:16 +01:00
filteredProfilesAsync: boolean = false;
2017-12-18 12:24:12 +01:00
filteredOrganisations: ExternalSourcesItemModel[];
filteredResearchers: ExternalSourcesItemModel[];
2017-12-21 10:16:46 +01:00
filteredProfiles: DatasetProfileModel[];
2017-12-14 18:13:28 +01:00
2017-12-21 10:39:17 +01:00
projectAutoCompleteConfiguration: AutoCompleteConfiguration;
2018-02-16 17:48:23 +01:00
organisationsAutoCompleteConfiguration: AutoCompleteChipConfiguration;
2018-01-23 12:56:43 +01:00
createNewVersion;
2018-01-23 14:46:38 +01:00
associatedUsers: Array<DmpUsersModel>
2018-03-28 15:24:47 +02:00
filteredOptions: Observable<DataManagementPlanProfileListingModel>
2017-12-21 10:39:17 +01:00
2017-12-14 18:13:28 +01:00
constructor(
2018-03-28 15:24:47 +02:00
private dmpProfileService: DataManagementPlanProfileService,
2017-12-18 11:01:22 +01:00
private dataManagementPlanService: DataManagementPlanService,
2017-12-21 10:39:17 +01:00
private projectService: ProjectService,
2017-12-18 11:01:22 +01:00
private externalSourcesService: ExternalSourcesService,
private route: ActivatedRoute,
public snackBar: MatSnackBar,
public router: Router,
public language: TranslateService,
2018-02-05 10:44:40 +01:00
private _service: DataManagementPlanService,
2018-02-05 11:34:25 +01:00
public dialog: MatDialog,
private _dialogService: TdDialogService,
2018-03-28 15:24:47 +02:00
private _viewContainerRef: ViewContainerRef,
private languageResolverService: LanguageResolverService
2017-12-14 18:13:28 +01:00
) {
2018-03-28 15:24:47 +02:00
this.filteredOptions = dmpProfileService.getAll({ criteria: new DataManagementPlanProfileCriteria() });
2017-12-14 18:13:28 +01:00
}
2017-12-18 11:01:22 +01:00
ngAfterViewInit() {
this.route.params.subscribe((params: Params) => {
const itemId = params['id'];
2017-12-14 18:13:28 +01:00
2017-12-21 10:39:17 +01:00
let projectRequestItem: RequestItem<ProjectCriteria> = new RequestItem();
projectRequestItem.criteria = new ProjectCriteria();
2018-02-16 17:48:23 +01:00
let organisationRequestItem: RequestItem<BaseCriteria> = new RequestItem();
organisationRequestItem.criteria = new BaseCriteria();
2017-12-22 14:46:36 +01:00
this.projectAutoCompleteConfiguration = new AutoCompleteConfiguration(this.projectService.getWithExternal.bind(this.projectService), projectRequestItem);
2018-02-16 17:48:23 +01:00
this.organisationsAutoCompleteConfiguration = new AutoCompleteChipConfiguration(this.externalSourcesService.searchDMPOrganizations.bind(this.externalSourcesService), organisationRequestItem);
2017-12-21 10:39:17 +01:00
2017-12-18 11:01:22 +01:00
if (itemId != null) {
this.isNew = false;
this.dataManagementPlanService.getSingle(itemId).map(data => data as DataManagementPlanModel)
2018-06-27 12:29:21 +02:00
.subscribe(async data => {
2018-01-03 17:36:31 +01:00
this.dataManagementPlan = JsonSerializer.fromJSONObject(data, DataManagementPlanModel);
2017-12-18 11:01:22 +01:00
this.formGroup = this.dataManagementPlan.buildForm();
2018-03-28 15:24:47 +02:00
if (this.formGroup.get("profile") && this.formGroup.get("profile").value) {
this.textCtrl.patchValue(this.formGroup.get("profile").value);
}
2018-06-27 12:29:21 +02:00
this.breadCrumbs = Observable.of([
{
parentComponentName: "DataManagementPlanListingComponent",
label: 'DMPs',
url: "dmps",
notFoundResolver: [await this.projectService.getSingle(this.dataManagementPlan.project.id).map(x => ({ label: x.label, url: '/projects/edit/' + x.id }) as BreadcrumbItem).toPromise()]
},
{
parentComponentName: null,
label: this.dataManagementPlan.label,
url: "/dmps/edit/" + this.dataManagementPlan.id
}]
)
2018-01-23 14:46:38 +01:00
this.associatedUsers = data.associatedUsers;
2017-12-18 11:01:22 +01:00
});
} else {
this.dataManagementPlan = new DataManagementPlanModel();
setTimeout(() => {
this.formGroup = this.dataManagementPlan.buildForm();
2018-03-28 15:24:47 +02:00
if (this.formGroup.get("profile") && this.formGroup.get("profile").value) {
this.textCtrl.patchValue(this.formGroup.get("profile").value);
}
2017-12-18 11:01:22 +01:00
});
}
2018-03-28 15:24:47 +02:00
2017-12-18 11:01:22 +01:00
});
2018-01-23 12:56:43 +01:00
this.route
2018-02-07 10:56:30 +01:00
.queryParams
.subscribe(params => {
this.createNewVersion = params["clone"];
});
2017-12-14 18:13:28 +01:00
}
2017-12-18 11:01:22 +01:00
formSubmit(): void {
//this.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) { return; }
this.onSubmit();
}
public isFormValid() {
return this.formGroup.valid;
}
2018-03-08 11:54:56 +01:00
onSubmit(): void {
2018-02-07 10:56:30 +01:00
this.dataManagementPlanService.createDataManagementPlan(this.formGroup.value).subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
)
2017-12-18 11:01:22 +01:00
}
onCallbackSuccess(): void {
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE', language: this.language },
duration: 3000,
})
2017-12-20 15:52:09 +01:00
this.router.navigate(['/dmps']);
2017-12-18 11:01:22 +01:00
}
onCallbackError(error: any) {
this.setErrorModel(error.error);
//this.validateAllFormFields(this.formGroup);
}
public setErrorModel(errorModel: BaseErrorModel) {
Object.keys(errorModel).forEach(item => {
(<any>this.dataManagementPlan.errorModel)[item] = (<any>errorModel)[item];
})
}
2017-12-14 18:13:28 +01:00
2017-12-18 11:01:22 +01:00
public cancel(): void {
2017-12-20 15:52:09 +01:00
this.router.navigate(['/dmps']);
2017-12-14 18:13:28 +01:00
}
2017-12-18 12:24:12 +01:00
2018-01-05 16:40:19 +01:00
public invite(): void {
2018-02-07 10:56:30 +01:00
this.router.navigate(['/invite/' + this.dataManagementPlan.id]);
2018-01-05 16:40:19 +01:00
}
2018-02-23 13:12:58 +01:00
filterOrganisations(value: string): void {
2017-12-21 10:39:17 +01:00
2018-02-23 13:12:58 +01:00
this.filteredOrganisations = undefined;
if (value) {
this.filteringOrganisationsAsync = true;
2017-12-21 10:39:17 +01:00
2018-02-23 13:12:58 +01:00
this.externalSourcesService.searchDMPOrganizations(value).subscribe(items => {
this.filteredOrganisations = items;
this.filteringOrganisationsAsync = false;
2017-12-18 12:24:12 +01:00
2018-02-23 13:12:58 +01:00
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
2017-12-18 12:24:12 +01:00
2018-02-23 13:12:58 +01:00
});
2017-12-21 10:39:17 +01:00
2018-02-23 13:12:58 +01:00
}
}
2017-12-18 12:24:12 +01:00
filterResearchers(value: string): void {
2017-12-21 10:39:17 +01:00
2017-12-18 12:24:12 +01:00
this.filteredResearchers = undefined;
if (value) {
this.filteringResearchersAsync = true;
2017-12-21 10:39:17 +01:00
2018-03-08 11:54:56 +01:00
this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } }).subscribe(items => {
2017-12-18 12:24:12 +01:00
this.filteredResearchers = items;
this.filteringResearchersAsync = false;
});
}
}
2017-12-20 17:50:16 +01:00
filterProfiles(value: string): void {
2017-12-21 10:39:17 +01:00
2017-12-20 17:50:16 +01:00
this.filteredProfiles = undefined;
if (value) {
this.filteredProfilesAsync = true;
2017-12-21 10:39:17 +01:00
2017-12-21 10:16:46 +01:00
const request = new RequestItem<DatasetProfileCriteria>();
let criteria = new DatasetProfileCriteria();
criteria.like = value;
request.criteria = criteria;
this._service.searchDMPProfiles(request).subscribe(items => {
this.filteredProfiles = items;
this.filteredProfilesAsync = false;
2017-12-20 17:50:16 +01:00
});
}
}
2018-02-05 10:44:40 +01:00
addResearcher(rowId: any, rowName: any) {
let dialogRef = this.dialog.open(AddResearchersComponent, {
height: '255px',
width: '700px',
data: {
dmpId: rowId,
dmpName: rowName
}
});
2018-03-08 11:54:56 +01:00
2018-02-08 09:50:39 +01:00
}
availableProfiles() {
let dialogRef = this.dialog.open(AvailableProfilesComponent, {
height: '355px',
width: '700px',
data: {
}
});
2018-03-08 11:54:56 +01:00
dialogRef.afterClosed().subscribe(result => {
2018-02-08 16:32:23 +01:00
this.formGroup.get("profiles").setValue(result);
2018-02-08 09:50:39 +01:00
});
2018-03-08 11:54:56 +01:00
2018-02-08 09:50:39 +01:00
return false;
2018-02-05 10:44:40 +01:00
}
2018-02-05 12:26:23 +01:00
openConfirm(dmpLabel, id): void {
2018-02-05 11:34:25 +01:00
this._dialogService.openConfirm({
2018-03-08 11:54:56 +01:00
message: 'Are you sure you want to delete the "' + dmpLabel + '"',
2018-02-05 11:34:25 +01:00
disableClose: true || false, // defaults to false
viewContainerRef: this._viewContainerRef, //OPTIONAL
title: 'Confirm', //OPTIONAL, hides if not provided
cancelButton: 'No', //OPTIONAL, defaults to 'CANCEL'
acceptButton: 'Yes' //OPTIONAL, defaults to 'ACCEPT'
2018-03-08 11:54:56 +01:00
// width: '500px', //OPTIONAL, defaults to 400px
2018-02-05 11:34:25 +01:00
}).afterClosed().subscribe((accept: boolean) => {
if (accept) {
2018-03-08 11:54:56 +01:00
this.dataManagementPlanService.delete(id).subscribe(() => {
2018-02-05 12:26:23 +01:00
this.router.navigate(['/dmps'])
});
2018-02-05 11:34:25 +01:00
} else {
// DO SOMETHING ELSE
}
});
}
2018-03-28 15:24:47 +02:00
selectOption(option: any) {
this.dataManagementPlan.definition = null;
this.formGroup.get("profile").patchValue(option, { emitEvent: false })
this.dmpProfileService.getSingle(option.id).subscribe(result => {
this.dataManagementPlan.definition = result.definition;
})
}
displayWith(item: any) {
if (!item) return null;
return item["label"];
}
2018-05-28 11:50:42 +02:00
redirectToProject() {
this.router.navigate(["projects/edit/" + this.dataManagementPlan.project.id])
}
redirectToDatasets() {
this.router.navigate(["datasets/dmp/" + this.dataManagementPlan.id])
}
newVersion(id: String, label: String) {
this.router.navigate(['/dmps/new_version/' + id, { dmpLabel: label }]);
}
clone(id: String) {
this.router.navigate(['/dmps/clone/' + id]);
}
}