import { Component, OnInit, ViewChild } from '@angular/core'; import { ResearcherService } from '../../services/researcher-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-researcher-editor', templateUrl: './researcher-editor.component.html', styleUrls: ['./researcher-editor.component.css'] }) export class ResearcherEditorComponent implements OnInit { constructor(private researcherService : ResearcherService, private fb: FormBuilder) { } //whole dmp data model tableData : any[] = new Array(); //researcher editor data model editingResearcher: any = {}; researcherEditorForm : 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.getAllResearchers(false); this.createResearcherEditorForm(); } createResearcherEditorForm(){ this.researcherEditorForm = this.fb.group({ id: null, label: ['', Validators.required ], uri: '', primaryEmail: '', definition: '', reference: '' }); } switchToTable(){ this.tableVisible = true; this.editorVisible = false; } switchToEditor(researcherID){ this.tableVisible = false; this.editorVisible = true; if(researcherID == null){ this.editingResearcher = {id: null, label: "", uri: "", primaryEmail: "", definition: "", reference: "" }; } else{ this.editingResearcher = this.tableData.filter((researcher) => researcher.id === researcherID)[0]; } } getAllResearchers(showNotification : boolean){ this.researcherService.getAllResearchers().subscribe( (data) => { this.tableData = data; if(showNotification) simple_notifier("info",null,"Refreshed the table"); }); } editResearcher(researcher){ this.switchToEditor(researcher); } newResearcher(){ this.switchToEditor(null); } save(mouseEvent){ this.researcherService.create(this.researcherEditorForm.value).subscribe( response => { simple_notifier("success",null,"Saved resercher"); this.getAllResearchers(false); this.switchToTable(); }, err => { simple_notifier("danger",null,"Could not save researcher"); } ); } delete(resercher){ this.researcherService.delete(resercher).subscribe( (response) => { simple_notifier("success",null,"Deleted researcher"); this.getAllResearchers(false); this.switchToTable(); }, (err) => { simple_notifier("danger",null,"Could not delete researcher"); } ) } refreshTable($event){ this.getAllResearchers(true); } @ViewChild(ContextMenuComponent) public basicMenu: ContextMenuComponent; }