2018-03-28 15:24:47 +02:00
|
|
|
import { Component, ViewChild, OnInit, AfterViewInit, ViewEncapsulation } from "@angular/core";
|
|
|
|
import { MatPaginator, MatSort, MatSnackBar } from "@angular/material";
|
|
|
|
import { Router, ActivatedRoute, Params } from "@angular/router";
|
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
|
|
|
import { DataSource } from "@angular/cdk/table";
|
|
|
|
import { Observable } from "rxjs/Observable";
|
|
|
|
import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service";
|
|
|
|
import { ProjectModel } from "../../models/projects/ProjectModel";
|
|
|
|
import { JsonSerializer } from "../../utilities/JsonSerializer";
|
|
|
|
import { FormGroup, AbstractControl, FormControl, FormArray } from "@angular/forms";
|
|
|
|
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component";
|
|
|
|
import { BaseErrorModel } from "../../models/error/BaseErrorModel";
|
|
|
|
import { TdDialogService } from "@covalent/core";
|
2018-05-14 08:44:35 +02:00
|
|
|
import { HostConfiguration } from "../../app.constants";
|
2018-03-28 15:24:47 +02:00
|
|
|
import { DataManagementPlanProfileService } from "../../services/data-management-plan-profile/datamanagement-profile.service";
|
|
|
|
import { DataManagementPlanProfileModel } from "../../models/data-management-plan-profile/DataManagementPlanProfileModel";
|
|
|
|
import { DMPProfileFieldDataType, DMPProfileType, DataManagementProfileField } from "../../models/data-management-plan-profile/DataManagementProfileField";
|
|
|
|
import { Utilities } from "../../utilities/utilities";
|
|
|
|
import { DataManagementPlanProfileListingModel } from "../../models/data-management-plan-profile/DataManagementPlanProfileListingModel";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
2018-05-28 11:50:42 +02:00
|
|
|
selector: 'app-dmp-profile-editor-component',
|
|
|
|
templateUrl: 'dmp-profile-editor.component.html',
|
|
|
|
styleUrls: ['./dmp-profile-editor.component.scss'],
|
|
|
|
providers: [DataManagementPlanProfileService],
|
|
|
|
encapsulation: ViewEncapsulation.None
|
2018-03-28 15:24:47 +02:00
|
|
|
})
|
|
|
|
export class DataManagementPlanProfileEditorComponent implements AfterViewInit {
|
|
|
|
|
2018-05-28 11:50:42 +02:00
|
|
|
isNew = true;
|
|
|
|
dataManagementPlanProfileModel: DataManagementPlanProfileModel;
|
|
|
|
formGroup: FormGroup = null;
|
|
|
|
host = HostConfiguration.Server;
|
|
|
|
baseErrorModel: BaseErrorModel;
|
|
|
|
constructor(
|
|
|
|
private dmpProfileService: DataManagementPlanProfileService,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
public snackBar: MatSnackBar,
|
|
|
|
public router: Router,
|
|
|
|
public language: TranslateService,
|
2018-10-02 16:33:58 +02:00
|
|
|
private dialogService: TdDialogService,
|
|
|
|
private utilities: Utilities
|
2018-05-28 11:50:42 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
this.route.params.subscribe((params: Params) => {
|
|
|
|
const itemId = params['id'];
|
|
|
|
|
|
|
|
if (itemId != null) {
|
|
|
|
this.isNew = false;
|
|
|
|
this.dmpProfileService.getSingle(itemId).map(data => data as DataManagementPlanProfileModel)
|
|
|
|
.subscribe(data => {
|
|
|
|
this.dataManagementPlanProfileModel = JsonSerializer.fromJSONObject(data, DataManagementPlanProfileModel);
|
|
|
|
this.baseErrorModel = this.dataManagementPlanProfileModel.errorModel;
|
|
|
|
this.formGroup = this.dataManagementPlanProfileModel.buildForm();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.dataManagementPlanProfileModel = new DataManagementPlanProfileModel();
|
|
|
|
this.baseErrorModel = this.dataManagementPlanProfileModel.errorModel;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.formGroup = this.dataManagementPlanProfileModel.buildForm();
|
2018-03-28 15:24:47 +02:00
|
|
|
});
|
2018-05-28 11:50:42 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
formSubmit(): void {
|
|
|
|
this.touchAllFormFields(this.formGroup);
|
|
|
|
if (!this.isFormValid()) { return; }
|
|
|
|
this.onSubmit();
|
|
|
|
}
|
|
|
|
|
|
|
|
public isFormValid() {
|
|
|
|
return this.formGroup.valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit(): void {
|
|
|
|
this.dmpProfileService.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,
|
|
|
|
})
|
|
|
|
this.router.navigate(['/dmp-profiles']);
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: any) {
|
|
|
|
this.setErrorModel(errorResponse.error);
|
|
|
|
this.validateAllFormFields(this.formGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setErrorModel(errorModel: BaseErrorModel) {
|
|
|
|
Object.keys(errorModel).forEach(item => {
|
|
|
|
(<any>this.dataManagementPlanProfileModel.errorModel)[item] = (<any>errorModel)[item];
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancel(): void {
|
|
|
|
this.router.navigate(['/dmp-profiles']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public touchAllFormFields(formControl: AbstractControl) {
|
|
|
|
if (formControl instanceof FormControl) {
|
|
|
|
formControl.markAsTouched();
|
|
|
|
} else if (formControl instanceof FormGroup) {
|
|
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
|
|
const control = formControl.get(item);
|
|
|
|
this.touchAllFormFields(control);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else if (formControl instanceof FormArray) {
|
|
|
|
formControl.controls.forEach(item => {
|
|
|
|
this.touchAllFormFields(item);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public validateAllFormFields(formControl: AbstractControl) {
|
|
|
|
if (formControl instanceof FormControl) {
|
|
|
|
formControl.updateValueAndValidity({ emitEvent: false })
|
|
|
|
} else if (formControl instanceof FormGroup) {
|
|
|
|
Object.keys(formControl.controls).forEach(item => {
|
|
|
|
const control = formControl.get(item);
|
|
|
|
this.validateAllFormFields(control);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else if (formControl instanceof FormArray) {
|
|
|
|
formControl.controls.forEach(item => {
|
|
|
|
this.validateAllFormFields(item);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addField() {
|
|
|
|
(<FormArray>this.formGroup.get("definition").get("fields")).push(new DataManagementProfileField().buildForm())
|
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldDataTypeValues(): Number[] {
|
|
|
|
let keys: string[] = Object.keys(DMPProfileFieldDataType);
|
|
|
|
keys = keys.slice(0, keys.length / 2);
|
|
|
|
const values: Number[] = keys.map(Number);
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldDataTypeWithLanguage(role: DMPProfileFieldDataType): string {
|
|
|
|
let result = '';
|
2018-10-02 16:33:58 +02:00
|
|
|
this.language.get(this.utilities.convertFromDMPProfileDataType(role)).subscribe((value: string) => {
|
2018-05-28 11:50:42 +02:00
|
|
|
result = value;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldTypeValues(): Number[] {
|
|
|
|
let keys: string[] = Object.keys(DMPProfileType);
|
|
|
|
keys = keys.slice(0, keys.length / 2);
|
|
|
|
const values: Number[] = keys.map(Number);
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDMPProfileFieldTypeWithLanguage(role: DMPProfileType): string {
|
|
|
|
let result = '';
|
2018-10-02 16:33:58 +02:00
|
|
|
this.language.get(this.utilities.convertFromDMPProfileType(role)).subscribe((value: string) => {
|
2018-05-28 11:50:42 +02:00
|
|
|
result = value;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|