argos/dmp-admin/src/app/managers/organisation-editor/organisation-editor.compone...

136 lines
3.1 KiB
TypeScript

import { Component, OnInit, ViewChild } from '@angular/core';
import { OrganisationService } from '../../services/organisation-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-organisation-editor',
templateUrl: './organisation-editor.component.html',
styleUrls: ['./organisation-editor.component.css']
})
export class OrganisationEditorComponent implements OnInit {
constructor(private organisationService : OrganisationService, private fb: FormBuilder) {
}
//whole dmp data model
tableData : any[] = new Array();
//organisation editor data model
editingOrganisation: any = {};
organisationEditorForm : 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.getAllOrganisations(false);
this.createOrganisationEditorForm();
}
createOrganisationEditorForm(){
this.organisationEditorForm = this.fb.group({
id: null,
label: ['', Validators.required ],
abbreviation: '',
reference: '',
uri: '',
definition: ''
});
}
switchToTable(){
this.tableVisible = true;
this.editorVisible = false;
}
switchToEditor(organisationID){
this.tableVisible = false;
this.editorVisible = true;
if(organisationID == null){
this.editingOrganisation = {id: null, label: "", abbreviation: "", reference: "", uri: "", definition: "" };
}
else{
this.editingOrganisation = this.tableData.filter((organisation) => organisation.id === organisationID)[0];
}
}
getAllOrganisations(showNotification : boolean){
this.organisationService.getAllOrganisations().subscribe( (data) => {
this.tableData = data;
if(showNotification)
simple_notifier("info",null,"Refreshed the table");
});
}
editOrganisation(organisation){
this.switchToEditor(organisation);
}
newOrganisation(){
this.switchToEditor(null);
}
save(mouseEvent){
this.organisationService.create(this.organisationEditorForm.value).subscribe(
response => {
simple_notifier("success",null,"Saved organisation");
this.getAllOrganisations(false);
},
err => {
simple_notifier("danger",null,"Could not save organisation");
}
);
}
delete(organisation){
this.organisationService.delete(organisation).subscribe(
(response) => {
simple_notifier("success",null,"Deleted organisation");
},
(err) => {
simple_notifier("danger",null,"Could not delete organisation");
console.log(err);
}
)
}
refreshTable($event){
this.getAllOrganisations(true);
}
@ViewChild(ContextMenuComponent) public basicMenu: ContextMenuComponent;
}