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

190 lines
6.8 KiB
TypeScript
Raw Normal View History

2017-12-18 11:01:22 +01:00
import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
2017-12-14 18:13:28 +01:00
import { MatPaginator, MatSort, MatSnackBar } 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";
import { FormGroup } from "@angular/forms";
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";
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'],
2017-12-21 10:39:17 +01:00
providers: [DataManagementPlanService, ExternalSourcesService, ProjectService],
2017-12-18 11:01:22 +01:00
encapsulation: ViewEncapsulation.None
2017-12-14 18:13:28 +01:00
})
2017-12-18 11:01:22 +01:00
export class DataManagementPlanEditorComponent implements AfterViewInit {
2017-12-21 10:16:46 +01:00
2017-12-18 11:01:22 +01:00
isNew = true;
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;
2017-12-14 18:13:28 +01:00
constructor(
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,
2017-12-21 10:16:46 +01:00
private _service: DataManagementPlanService
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();
2017-12-22 14:46:36 +01:00
this.projectAutoCompleteConfiguration = new AutoCompleteConfiguration(this.projectService.getWithExternal.bind(this.projectService), projectRequestItem);
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)
.subscribe(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();
});
} else {
this.dataManagementPlan = new DataManagementPlanModel();
setTimeout(() => {
this.formGroup = this.dataManagementPlan.buildForm();
});
}
});
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;
}
onSubmit(): void {
this.dataManagementPlanService.createDataManagementPlan(this.formGroup.value).subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
}
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,
extraClasses: ['snackbar-success']
})
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
filterOrganisations(value: string): void {
2017-12-21 10:39:17 +01:00
2017-12-18 12:24:12 +01:00
this.filteredOrganisations = undefined;
if (value) {
this.filteringOrganisationsAsync = true;
2017-12-21 10:39:17 +01:00
2017-12-18 12:24:12 +01:00
this.externalSourcesService.searchDMPOrganizations(value).subscribe(items => {
this.filteredOrganisations = items;
this.filteringOrganisationsAsync = false;
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
2017-12-21 10:39:17 +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
2017-12-18 12:24:12 +01:00
this.externalSourcesService.searchDMPResearchers(value).subscribe(items => {
this.filteredResearchers = items;
this.filteringResearchersAsync = false;
// this.filteredOrganisations = items.filter((filteredObj: any) => {
// return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// });
});
}
}
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
// this.externalSourcesService.searchDMPProfiles(value).subscribe(items => {
// this.filteredProfiles = items;
// this.filteredProfilesAsync = false;
2017-12-20 17:50:16 +01:00
2017-12-21 10:16:46 +01:00
// // this.filteredOrganisations = items.filter((filteredObj: any) => {
// // return this.objectsModel ? this.objectsModel.indexOf(filteredObj) < 0 : true;
// // });
// });
2017-12-20 17:50:16 +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
});
}
}
2017-12-14 18:13:28 +01:00
}