argos/dmp-admin/src/app/managers/project-editor/project-editor.component.ts

139 lines
3.2 KiB
TypeScript

import { Component, OnInit, ViewChild } from '@angular/core';
import { DmpsServiceService } from '../../services/dmps-service.service';
import { ProjectService } from '../../services/project-service';
import { DmpProfileService } from '../../services/dmpprofile-service';
import { ContextMenuComponent } from 'ngx-contextmenu';
import { ReactiveFormsModule } from '@angular/forms';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import '../../../assets/custom.js';
declare function simple_notifier(type: string, title: string, message:string): any;
@Component({
selector: 'app-project-editor',
templateUrl: './project-editor.component.html',
styleUrls: ['./project-editor.component.css']
})
export class ProjectEditorComponent implements OnInit {
constructor(private projectService : ProjectService, private fb: FormBuilder) {
}
//whole dmp data model
tableData : any[] = new Array();
//project editor data model
editingProject: any = {};
projectEditorForm : any;
//required by the table
public filterQuery = "";
public rowsOnPage = 10;
//public sortBy = "email";
public sortOrder = "asc";
//visibility rules for containers
tableVisible: boolean = true;
editorVisible: boolean = false;
// for tableIds
showIDs : boolean = false;
ngOnInit() {
this.getAllProjects(false);
this.createProjectEditorForm();
}
createProjectEditorForm(){
this.projectEditorForm = this.fb.group({
id: null,
label: ['', Validators.required ],
abbreviation: '',
reference: '',
uri: '',
definition: ''
});
}
switchToTable(){
this.tableVisible = true;
this.editorVisible = false;
}
switchToEditor(projectID){
this.tableVisible = false;
this.editorVisible = true;
if(projectID == null){
this.editingProject = {id: null, label: "", abbreviation: "", reference: "", uri: "", definition: "" };
}
else{
this.editingProject = this.tableData.filter((project) => project.id === projectID)[0];
}
}
getAllProjects(showNotification : boolean){
this.projectService.getAllProjects().subscribe( (data) => {
this.tableData = data;
if(showNotification)
simple_notifier("info",null,"Refreshed the table");
});
}
editProject(project){
this.switchToEditor(project);
}
newProject(){
this.switchToEditor(null);
}
save(mouseEvent){
this.projectService.create(this.projectEditorForm.value).subscribe(
response => {
simple_notifier("success",null,"Saved project");
this.getAllProjects(false);
this.switchToTable();
},
err => {
simple_notifier("danger",null,"Could not save project");
}
);
}
delete(project){
console.log(project);
this.projectService.delete(project).subscribe(
(response) => {
simple_notifier("success",null,"Deleted project");
this.getAllProjects(false);
this.switchToTable();
},
(err) => {
simple_notifier("danger",null,"Could not delete project");
}
)
}
refreshTable($event){
this.getAllProjects(true);
}
@ViewChild(ContextMenuComponent) public basicMenu: ContextMenuComponent;
}