argos/dmp-frontend/src/app/projects/editor/project-editor.component.ts

186 lines
6.7 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";
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";
2018-05-28 11:50:42 +02:00
import { ProjectModel, ProjectType } from "../../models/projects/ProjectModel";
2017-12-14 18:13:28 +01:00
import { ProjectService } from "../../services/project/project.service";
import { JsonSerializer } from "../../utilities/JsonSerializer";
2018-01-03 17:36:31 +01:00
import { FormGroup, AbstractControl, FormControl, FormArray } 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-18 16:53:17 +01:00
import { TdDialogService } from "@covalent/core";
2018-05-14 08:44:35 +02:00
import { ProjectFileUploaderService } from "../../services/files/project-file-uploader.service";
import { HostConfiguration } from "../../app.constants";
2018-03-28 15:24:47 +02:00
import { LanguageResolverService } from "../../services/language-resolver/language-resolver.service";
2018-05-28 11:50:42 +02:00
import { THIS_EXPR } from "@angular/compiler/src/output/output_ast";
import { BaseHttpService } from "../../utilities/cite-http-service-module/base-http.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-project-editor-component',
templateUrl: 'project-editor.component.html',
2017-12-18 11:01:22 +01:00
styleUrls: ['./project-editor.component.scss'],
encapsulation: ViewEncapsulation.None
2017-12-14 18:13:28 +01:00
})
2018-06-27 12:29:21 +02:00
export class ProjectEditorComponent implements OnInit, IBreadCrumbComponent {
2017-12-14 18:13:28 +01:00
2018-06-27 12:29:21 +02:00
breadCrumbs: Observable<BreadcrumbItem[]> = Observable.of([]);;
2017-12-14 18:13:28 +01:00
isNew = true;
project: ProjectModel;
formGroup: FormGroup = null;
2018-03-21 14:15:06 +01:00
host = HostConfiguration.Server;
2018-05-28 11:50:42 +02:00
editMode = false;
2017-12-14 18:13:28 +01:00
constructor(
private projectService: ProjectService,
private route: ActivatedRoute,
2017-12-18 11:01:22 +01:00
public snackBar: MatSnackBar,
public router: Router,
2017-12-18 16:53:17 +01:00
public language: TranslateService,
2018-03-21 14:15:06 +01:00
private dialogService: TdDialogService,
2018-03-28 15:24:47 +02:00
private uploaderService: ProjectFileUploaderService,
private languageResolverService: LanguageResolverService
2017-12-14 18:13:28 +01:00
) {
}
2018-06-27 12:29:21 +02:00
ngOnInit() {
2017-12-14 18:13:28 +01:00
this.route.params.subscribe((params: Params) => {
const itemId = params['id'];
if (itemId != null) {
this.isNew = false;
this.projectService.getSingle(itemId).map(data => data as ProjectModel)
.subscribe(data => {
2018-01-03 17:36:31 +01:00
this.project = JsonSerializer.fromJSONObject(data, ProjectModel);
2018-05-28 11:50:42 +02:00
this.formGroup = this.project.buildForm(null, this.project.type == ProjectType.External || !this.editMode);
2018-06-27 12:29:21 +02:00
this.breadCrumbs = Observable.of([{ parentComponentName: "ProjectListingComponent", label: 'Projects', url: "/projects" }, { parentComponentName: null, label: this.project.label, url: "/projects/edit/" + this.project.id }])
2017-12-14 18:13:28 +01:00
});
} else {
2018-06-27 12:29:21 +02:00
this.breadCrumbs = Observable.of([{ parentComponentName: "ProjectListingComponent", label: 'Projects', url: "/projects" }, { parentComponentName: null, label: "Project New", url: "/projects/new" }])
2017-12-14 18:13:28 +01:00
this.project = new ProjectModel();
2017-12-18 11:01:22 +01:00
setTimeout(() => {
this.formGroup = this.project.buildForm();
});
2017-12-14 18:13:28 +01:00
}
});
}
2017-12-18 11:01:22 +01:00
formSubmit(): void {
2018-01-03 17:36:31 +01:00
this.touchAllFormFields(this.formGroup);
2017-12-18 16:53:17 +01:00
if (!this.isFormValid()) { return; }
this.onSubmit();
2017-12-18 11:01:22 +01:00
}
public isFormValid() {
2017-12-18 16:53:17 +01:00
return this.formGroup.valid;
}
2017-12-18 11:01:22 +01:00
onSubmit(): void {
2017-12-18 16:53:17 +01:00
this.projectService.createProject(this.formGroup.value).subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2017-12-18 11:01:22 +01:00
onCallbackSuccess(): void {
2017-12-18 16:53:17 +01:00
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(['/projects']);
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2018-01-03 17:36:31 +01:00
onCallbackError(errorResponse: any) {
this.setErrorModel(errorResponse.error);
this.validateAllFormFields(this.formGroup);
2017-12-18 16:53:17 +01:00
}
2017-12-18 11:01:22 +01:00
public setErrorModel(errorModel: BaseErrorModel) {
2017-12-18 16:53:17 +01:00
Object.keys(errorModel).forEach(item => {
(<any>this.project.errorModel)[item] = (<any>errorModel)[item];
})
2017-12-18 11:01:22 +01:00
}
2017-12-18 16:53:17 +01:00
2017-12-18 11:01:22 +01:00
public cancel(): void {
2017-12-18 16:53:17 +01:00
this.router.navigate(['/projects']);
}
public delete(): void {
this.language.get('GENERAL.DELETE-CONFIRMATION').subscribe((messages: any) => {
this.dialogService.openConfirm({
message: messages.MESSAGE,
title: messages.TITLE,
cancelButton: messages.NEGATIVE,
acceptButton: messages.POSITIVE
}).afterClosed().subscribe((accept: boolean) => {
if (accept) {
this.projectService.inactivate(this.project.id).subscribe(
complete => { this.router.navigate(['/projects']); },
error => this.onCallbackError(error)
);
}
});
});
}
2018-01-03 17:36:31 +01:00
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) {
2018-03-21 14:15:06 +01:00
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);
})
}
}
2018-05-28 11:50:42 +02:00
public enableForm() {
if (!this.isExternalProject()) {
this.editMode = true;
this.formGroup.enable()
}
}
public disableForm() {
this.editMode = false;
this.formGroup.disable();
}
public goToProjectDmps() {
2018-06-27 12:29:21 +02:00
this.router.navigate(["dmps/project/" + this.project.id], { queryParams: { projectLabel: this.project.label } })
2018-05-28 11:50:42 +02:00
}
public isExternalProject() {
return this.project.type === ProjectType.External;
}
}