import { Component, OnInit, ViewChild } from '@angular/core'; import { DmpProfileService } from '../../services/dmpprofile-service'; 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-dmp-profile-editor', templateUrl: './dmp-profile-editor.component.html', styleUrls: ['./dmp-profile-editor.component.css'] }) export class DmpProfileEditorComponent implements OnInit { constructor(private dmpProfileService : DmpProfileService, private fb: FormBuilder) { } //whole dmp data model tableData : any[] = new Array(); //editor's data model editingDmpProfile: any = {}; dmpProfileEditorForm : 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.getAllDmpProfiles(false); this.createDmpProfileEditorForm(); } createDmpProfileEditorForm(){ this.dmpProfileEditorForm = this.fb.group({ id: null, label: ['', Validators.required ], definition: '' }); } switchToTable(){ this.tableVisible = true; this.editorVisible = false; } switchToEditor(dmpProfileID){ this.tableVisible = false; this.editorVisible = true; if(dmpProfileID == null){ this.editingDmpProfile = {id: null, label: "", definition: "" }; } else{ this.editingDmpProfile = this.tableData.filter((dmpProfile) => dmpProfile.id === dmpProfileID)[0]; } } getAllDmpProfiles(showNotification : boolean){ this.dmpProfileService.getAllDmpProfiles().subscribe( (data) => { this.tableData = data; if(showNotification) simple_notifier("info",null,"Refreshed the table"); }); } editDmpProfile(dmpProfile){ this.switchToEditor(dmpProfile); } newDmpProfile(){ this.switchToEditor(null); } save(mouseEvent){ this.dmpProfileService.create(this.dmpProfileEditorForm.value).subscribe( response => { simple_notifier("success",null,"Saved DMP Profile"); this.getAllDmpProfiles(false); this.switchToTable(); }, err => { simple_notifier("danger",null,"Could not save DMP Profile"); } ); } delete(dmpProfile){ this.dmpProfileService.delete(dmpProfile).subscribe( (response) => { simple_notifier("success",null,"Deleted DMP Profile"); this.getAllDmpProfiles(false); this.switchToTable(); }, (err) => { simple_notifier("danger",null,"Could not delete DMP Profile"); } ) } refreshTable($event){ this.getAllDmpProfiles(true); } }