argos/dmp-frontend/src/app/dmps/dmp.component.ts

305 lines
7.9 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ViewEncapsulation } from '@angular/core';
2017-11-10 16:47:03 +01:00
import { GoogleSignInSuccess } from 'angular-google-signin';
import { Router, ActivatedRoute } from '@angular/router';
import { ServerService } from '../../app/services/server.service';
2017-10-27 10:34:05 +02:00
import { Dmp } from '../entities/model/dmp';
import { Dataset } from '../entities/model/dataset';
import { Project } from '../entities/model/project';
2017-11-07 14:07:25 +01:00
import { ConfirmationComponent } from '../widgets/confirmation/confirmation.component';
2017-11-03 18:57:06 +01:00
2017-11-10 16:47:03 +01:00
import { DataTable } from 'angular2-datatable';
import { DropdownField } from '../../app/form/fields/dropdown/field-dropdown';
import { Param } from '../entities/model/param';
import { ModalComponent } from '../modal/modal.component';
import { HttpErrorResponse } from '@angular/common/http';
import { FormGroup, FormControl } from '@angular/forms'; //na dw an xreiazontai
import { NgForm } from '@angular/forms';
2017-10-27 18:38:31 +02:00
import { DatasetsComponent } from '../datasets/dataset.component';
2017-11-03 18:57:06 +01:00
import { StatusToString } from '../pipes/various/status-to-string';
import { MenuItem } from 'primeng/primeng';
declare var $: any;
2017-10-27 16:08:10 +02:00
2017-11-07 12:30:11 +01:00
import '../../assets/custom.js';
declare function simple_notifier(type: string, title: string, message: string): any;
2017-11-07 12:30:11 +01:00
@Component({
selector: 'dmps',
templateUrl: 'dmps.html',
styleUrls: ['./dmp.component.css'],
providers: [ServerService],
encapsulation: ViewEncapsulation.None
})
export class DmpComponent implements OnInit {
//whole dmp data model
tableData: any[] = new Array();
//required by the table
public filterQuery = "";
public rowsOnPage = 10;
2017-11-01 13:01:14 +01:00
public sortBy = "label";
public sortOrder = "asc";
// for tableIds
showIDs: boolean = false;
2017-11-14 15:06:00 +01:00
dmp: any;
2017-11-14 15:06:00 +01:00
@Input() projectsDropDown: DropdownField;
2017-11-01 16:42:01 +01:00
@Input() statusDropDown: DropdownField;
@ViewChild(DatasetsComponent) datasetsComponent: DatasetsComponent;
filteredResearchers: any[];
filteredOrganizations: any[];
constructor(
private serverService: ServerService,
private route: ActivatedRoute,
private router: Router) {
this.projectsDropDown = new DropdownField();
this.projectsDropDown.options = [];
this.statusDropDown = new DropdownField();
this.statusDropDown.options = [{ key: '', value: null }, { key: '0', value: "Active" }, { key: '1', value: "Inactive" }]
//this.projects = [];
2017-11-15 11:59:08 +01:00
}
clearDmp() {
this.dmp = {
id: null,
label: '',
previous: '',
version: '',
organizations: [],
researchers: []
2017-11-15 11:59:08 +01:00
}
}
ngOnInit() {
2017-11-07 12:30:11 +01:00
this.getDmps();
this.serverService.getAllProjects().subscribe(
response => {
//let params = new Param();
response.forEach((dmp) => {
let params = new Param();
params.key = dmp.id;
params.value = dmp.label;
this.projectsDropDown.options.push(params);
});
2017-11-01 12:30:39 +01:00
},
(err: HttpErrorResponse) => {
simple_notifier("danger", null, "Could not load User's Projects");
}
)
2017-11-14 15:06:00 +01:00
2017-11-16 18:07:27 +01:00
this.clearDmp();
2017-11-14 23:51:39 +01:00
2017-11-14 23:51:39 +01:00
}
getDmps(muted?: boolean) {
this.serverService.getDmpOfUser().subscribe(
2017-11-14 23:51:39 +01:00
response => {
this.tableData = response;
if (muted && muted != true)
simple_notifier("success", null, "Refreshed DMPs");
2017-11-27 13:03:43 +01:00
this.tableData.forEach(dmp => {
if (dmp.previous != null)
this.serverService.getDmp(dmp.previous).subscribe(previous => { dmp.previous = previous; });
2017-11-27 13:03:43 +01:00
});
2017-11-14 23:51:39 +01:00
},
(err: HttpErrorResponse) => {
simple_notifier("danger", null, "Could not refresh DMPs");
}
);
}
newDMP() {
this.dmp.project = { "id": this.dmp.project };
2017-11-14 23:51:39 +01:00
this.dmp["version"] = 1;
2017-11-14 23:51:39 +01:00
this.serverService.createDmpForCurrentUser(this.dmp)
.subscribe(
response => {
simple_notifier("success", null, "DMP created");
2017-11-14 23:51:39 +01:00
this.getDmps();
},
error => {
simple_notifier("danger", null, "Could not create the DMP");
2017-11-14 23:51:39 +01:00
}
);
2017-11-14 23:51:39 +01:00
$("#newDmpModal").modal("hide");
}
2017-11-01 12:30:39 +01:00
updateDMP() {
this.dmp.project = { "id": this.dmp.project };
this.dmp.creator = { "id": this.dmp.creator };
if (this.dmp.previous != null)
2017-11-27 13:03:43 +01:00
this.dmp.previous = this.dmp.previous.id;
2017-10-27 11:24:04 +02:00
2017-11-14 23:51:39 +01:00
this.serverService.updateDmp(this.dmp)
.subscribe(
response => {
simple_notifier("success", null, "Edited the DMP");
2017-11-14 23:51:39 +01:00
this.getDmps();
},
error => {
simple_notifier("danger", null, "Failed to edit the DMP");
2017-11-14 23:51:39 +01:00
}
);
2017-11-14 23:51:39 +01:00
$("#newDmpModal").modal("hide");
$("#newVersionDmpModal").modal("hide");
2017-11-14 23:51:39 +01:00
}
cloneDMP(dmp) {
dmp = { "id": dmp.id, "label": dmp.label };
2017-11-14 23:51:39 +01:00
this.serverService.cloneDmp(dmp).subscribe(
response => {
simple_notifier("success", null, "Successfully cloned the DMP");
2017-11-14 23:51:39 +01:00
this.getDmps();
},
error => {
simple_notifier("danger", null, "Failed to clone the DMP");
2017-11-14 23:51:39 +01:00
}
);
$("#newVersionDmpModal").modal("hide");
}
2017-10-27 18:38:31 +02:00
SaveDmp() {
2017-11-14 23:51:39 +01:00
if (this.dmp.id == null)
this.newDMP();
else
this.updateDMP();
}
2017-11-01 12:59:57 +01:00
editDmp(item) {
this.serverService.getDmp(item.id).subscribe(result=>{
this.dmp = result;
this.dmp.project = result.project.id
$("#newDmpModal").modal("show");
})
}
2017-11-14 15:06:00 +01:00
cloneDmp(item) {
this.dmp = Object.assign({}, item);
this.dmp.project = item.project.id;
$("#newVersionDmpModal").modal("show");
}
newDmpForm(item) {
2017-11-15 11:59:08 +01:00
2017-11-16 18:07:27 +01:00
this.clearDmp();
2017-11-03 18:57:06 +01:00
2017-11-14 23:51:39 +01:00
$("#newDmpModal").modal("show");
}
2017-11-07 14:07:25 +01:00
markDMPForDelete(dmp) {
2017-11-14 23:51:39 +01:00
this.dmp = dmp;
}
2017-11-07 14:07:25 +01:00
deleteDMP(confirmation) {
if (confirmation == true)
2017-11-14 23:51:39 +01:00
this.deleteRow(this.dmp);
}
2017-11-06 14:11:03 +01:00
deleteRow(dmp) {
2017-11-06 14:11:03 +01:00
this.dmp = { "id": this.dmp.id }; //only id is needed to delete
2017-11-15 11:59:08 +01:00
this.serverService.deleteDmp(this.dmp).subscribe(
response => {
simple_notifier("success", null, "Successfully deleted the DMP");
2017-11-14 23:51:39 +01:00
this.getDmps();
},
(err: HttpErrorResponse) => {
if (err.status >= 200 && err.status < 300)
simple_notifier("success", null, "Successfully deleted the DMP");
2017-11-17 02:15:09 +01:00
else
simple_notifier("danger", null, "Failed to delete the DMP");
2017-11-17 02:15:09 +01:00
this.getDmps();
}
2017-11-14 23:51:39 +01:00
);
2017-11-06 14:11:03 +01:00
}
clickFilters(element) {
if (element.textContent == "More filters")
2017-11-14 23:51:39 +01:00
element.textContent = "Less Filters";
else
element.textContent = "More Filters";
}
2017-11-14 23:51:39 +01:00
SelectDMPStatus(dmp, event, oldValue) {
2017-11-14 23:51:39 +01:00
console.log(dmp);
let cannotChangeStatus: boolean;
if (dmp.status == 2) {
2017-11-14 23:51:39 +01:00
this.serverService.getDatasetForDmp({ "id": dmp.id }).subscribe(
response => {
response.forEach(dataset => {
if (dataset.status !== 2 || dataset.status !== 3) {
cannotChangeStatus = true;
2017-11-14 23:51:39 +01:00
}
return;
});
if (cannotChangeStatus == true) {
2017-11-14 23:51:39 +01:00
dmp.status = 0;
$("#messageForChangingStatus").modal("show");
}
2017-11-14 23:51:39 +01:00
},
error => {
console.log("could not retrieve dataset for dpm: " + dmp.id);
}
2017-11-14 23:51:39 +01:00
);
}
}
showDatasetsOfDmp(item) {
this.router.navigate(['/dataset'], { queryParams: { "dmpid": item.id, "label": item.label } });
2017-11-15 16:11:35 +01:00
}
viewDetailedDMP(dmp) {
2017-11-24 09:39:24 +01:00
let random = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
this.router.navigate(['/dmps/dmp'], { queryParams: { "dmpid": dmp.id, "label": dmp.label, random: random } });
2017-11-16 15:16:09 +01:00
}
viewDetailedProject(dmp) {
2017-11-24 09:39:24 +01:00
let random = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
this.router.navigate(['/dmps/project'], { queryParams: { "projectid": dmp.project.id, "label": dmp.project.label, random: random } });
2017-11-22 13:25:01 +01:00
}
2017-11-15 16:11:35 +01:00
searchResearchers(event) {
let query = event.query;
this.serverService.searchDMPResearchers(query).subscribe(researchers => {
this.filteredResearchers = researchers;
});
}
searchOrganizations(event) {
let query = event.query;
this.serverService.searchDMPOrganizations(query).subscribe(organizations => {
this.filteredOrganizations = organizations;
});
}
2017-11-01 12:30:39 +01:00
}