2017-12-18 11:01:22 +01:00
import { Component , ViewChild , OnInit , AfterViewInit , ViewEncapsulation } from "@angular/core" ;
2018-02-14 11:38:19 +01:00
import { MatPaginator , MatSort , MatSnackBar , MatDialog , MatChipList , MatChip } from "@angular/material" ;
2017-12-18 11:01:22 +01:00
import { Router , ActivatedRoute , Params } from "@angular/router" ;
2017-12-14 18:13:28 +01:00
import { TranslateService } from "@ngx-translate/core" ;
import { DataSource } from "@angular/cdk/table" ;
import { Observable } from "rxjs/Observable" ;
2017-12-18 11:01:22 +01:00
import { JsonSerializer } from "../../utilities/JsonSerializer" ;
2018-03-28 15:24:47 +02:00
import { FormGroup , FormControl } from "@angular/forms" ;
2017-12-18 11:01:22 +01:00
import { SnackBarNotificationComponent } from "../../shared/components/notificaiton/snack-bar-notification.component" ;
import { BaseErrorModel } from "../../models/error/BaseErrorModel" ;
2017-12-14 18:13:28 +01:00
import { DataManagementPlanService } from "../../services/data-management-plan/data-management-plan.service" ;
2017-12-18 11:01:22 +01:00
import { DataManagementPlanModel } from "../../models/data-managemnt-plans/DataManagementPlanModel" ;
import { ExternalSourcesService } from "../../services/external-sources/external-sources.service" ;
2017-12-18 12:24:12 +01:00
import { ExternalSourcesItemModel } from "../../models/external-sources/ExternalSourcesItemModel" ;
2017-12-21 10:16:46 +01:00
import { RequestItem } from "../../models/criteria/RequestItem" ;
import { DatasetProfileCriteria } from "../../models/criteria/dataset/DatasetProfileCriteria" ;
import { DataManagementPlanCriteriaComponent } from "../../shared/components/criteria/data-management-plan/dmp-criteria.component" ;
import { DatasetProfileModel } from "../../models/datasets/DatasetProfileModel" ;
2017-12-21 10:39:17 +01:00
import { ProjectCriteria } from "../../models/criteria/project/ProjectCriteria" ;
import { ProjectService } from "../../services/project/project.service" ;
2018-05-14 08:44:35 +02:00
import { DmpUsersModel } from "../../models/dmpUsers/DmpUsersModel" ;
2018-05-28 11:50:42 +02:00
import { AddResearchersComponent } from "../../shared/components/add-researchers/add-researchers.component" ;
2018-02-05 11:34:25 +01:00
import { ViewContainerRef } from '@angular/core' ;
import { TdDialogService } from '@covalent/core' ;
2018-05-28 11:50:42 +02:00
import { AvailableProfilesComponent } from "../../shared/components/available-profiles/available-profiles.component" ;
2018-05-14 08:44:35 +02:00
import { BaseCriteria } from "../../models/criteria/BaseCriteria" ;
2018-03-28 15:24:47 +02:00
import { DataManagementPlanProfileService } from "../../services/data-management-plan-profile/datamanagement-profile.service" ;
import { DataManagementPlanProfileListingModel } from "../../models/data-management-plan-profile/DataManagementPlanProfileListingModel" ;
import { DataManagementPlanProfileCriteria } from "../../models/criteria/dmp-profile/DataManagementPlanProfileCriteria" ;
import { DataManagementPlanProfile } from "../../models/data-management-plan-profile/DataManagementPlanProfile" ;
import { LanguageResolverService } from "../../services/language-resolver/language-resolver.service" ;
2018-06-27 12:29:21 +02:00
import { IBreadCrumbComponent } from "../../shared/components/breadcrumb/definition/IBreadCrumbComponent" ;
import { BreadcrumbItem } from "../../shared/components/breadcrumb/definition/breadcrumb-item" ;
2018-07-24 13:19:52 +02:00
import { SingleAutoCompleteConfiguration } from "../../shared/components/autocompletes/single/single-auto-complete-configuration" ;
import { MultipleAutoCompleteConfiguration } from "../../shared/components/autocompletes/multiple/multiple-auto-complete-configuration" ;
2017-12-14 18:13:28 +01:00
@Component ( {
2018-07-24 11:04:12 +02:00
selector : 'app-dmp-editor-component' ,
templateUrl : 'dmp-editor.component.html' ,
styleUrls : [ './dmp-editor.component.scss' ] ,
encapsulation : ViewEncapsulation.None
2017-12-14 18:13:28 +01:00
} )
2018-06-27 12:29:21 +02:00
export class DataManagementPlanEditorComponent implements AfterViewInit , IBreadCrumbComponent {
2017-12-18 11:01:22 +01:00
2017-12-21 10:16:46 +01:00
2018-07-24 11:04:12 +02:00
breadCrumbs : Observable < BreadcrumbItem [ ] > ;
isNew = true ;
textCtrl = new FormControl ( ) ;
dataManagementPlan : DataManagementPlanModel ;
formGroup : FormGroup = null ;
filteringOrganisationsAsync : boolean = false ;
filteringResearchersAsync : boolean = false ;
filteredProfilesAsync : boolean = false ;
filteredOrganisations : ExternalSourcesItemModel [ ] ;
filteredResearchers : ExternalSourcesItemModel [ ] ;
filteredProfiles : DatasetProfileModel [ ] ;
2018-07-24 13:19:52 +02:00
projectAutoCompleteConfiguration : SingleAutoCompleteConfiguration ;
profilesAutoCompleteConfiguration : MultipleAutoCompleteConfiguration ;
organisationsAutoCompleteConfiguration : MultipleAutoCompleteConfiguration ;
researchersAutoCompleteConfiguration : MultipleAutoCompleteConfiguration ;
2018-07-24 11:04:12 +02:00
createNewVersion ;
associatedUsers : Array < DmpUsersModel >
filteredOptions : Observable < DataManagementPlanProfileListingModel >
constructor (
private dmpProfileService : DataManagementPlanProfileService ,
private dataManagementPlanService : DataManagementPlanService ,
private projectService : ProjectService ,
private externalSourcesService : ExternalSourcesService ,
private route : ActivatedRoute ,
public snackBar : MatSnackBar ,
public router : Router ,
public language : TranslateService ,
private _service : DataManagementPlanService ,
public dialog : MatDialog ,
private _dialogService : TdDialogService ,
private _viewContainerRef : ViewContainerRef ,
private languageResolverService : LanguageResolverService
) {
this . filteredOptions = dmpProfileService . getAll ( { criteria : new DataManagementPlanProfileCriteria ( ) } ) ;
}
ngAfterViewInit() {
this . route . params . subscribe ( ( params : Params ) = > {
const itemId = params [ 'id' ] ;
let projectRequestItem : RequestItem < ProjectCriteria > = new RequestItem ( ) ;
projectRequestItem . criteria = new ProjectCriteria ( ) ;
let organisationRequestItem : RequestItem < BaseCriteria > = new RequestItem ( ) ;
organisationRequestItem . criteria = new BaseCriteria ( ) ;
2018-07-24 13:19:52 +02:00
//this.projectAutoCompleteConfiguration = new AutoCompleteConfiguration(this.projectService.getWithExternal.bind(this.projectService), projectRequestItem);
this . projectAutoCompleteConfiguration = {
filterFn : this.searchProject.bind ( this ) ,
items : this.searchProject ( '' ) ,
displayFn : ( item ) = > item [ "label" ] ,
titleFn : ( item ) = > item [ "label" ] ,
//mapFn: (item) => new JsonSerializer<ProjectReference>().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()),
loadDataOnStart : true
} ;
this . profilesAutoCompleteConfiguration = {
filterFn : this.filterProfiles.bind ( this ) ,
initialItems : ( excludedItems : any [ ] ) = > this . filterProfiles ( '' ) . map ( result = > result . filter ( x = > excludedItems . map ( x = > x . id ) . indexOf ( x . id ) == - 1 ) ) ,
displayFn : ( item ) = > item [ "label" ] ,
titleFn : ( item ) = > item [ "label" ] ,
//mapFn: (item) => new JsonSerializer<ProjectReference>().fromJSONArray(item, ProjectReference).map(item => item.toDropdownList()),
loadDataOnStart : true
} ;
this . organisationsAutoCompleteConfiguration = {
filterFn : this.filterOrganisations.bind ( this ) ,
initialItems : ( excludedItems : any [ ] ) = > this . filterOrganisations ( '' ) . map ( result = > result . filter ( x = > excludedItems . map ( x = > x . id ) . indexOf ( x . id ) == - 1 ) ) ,
displayFn : ( item ) = > item [ "name" ] ,
titleFn : ( item ) = > item [ "name" ] ,
loadDataOnStart : true
} ;
this . researchersAutoCompleteConfiguration = {
filterFn : this.filterResearchers.bind ( this ) ,
initialItems : ( excludedItems : any [ ] ) = > this . filterResearchers ( '' ) . map ( result = > result . filter ( x = > excludedItems . map ( x = > x . id ) . indexOf ( x . id ) == - 1 ) ) ,
displayFn : ( item ) = > item [ "name" ] ,
titleFn : ( item ) = > item [ "name" ] ,
loadDataOnStart : true
} ;
2018-07-24 11:04:12 +02:00
if ( itemId != null ) {
this . isNew = false ;
this . dataManagementPlanService . getSingle ( itemId ) . map ( data = > data as DataManagementPlanModel )
. subscribe ( async data = > {
this . dataManagementPlan = JsonSerializer . fromJSONObject ( data , DataManagementPlanModel ) ;
this . formGroup = this . dataManagementPlan . buildForm ( ) ;
if ( this . formGroup . get ( "profile" ) && this . formGroup . get ( "profile" ) . value ) {
this . textCtrl . patchValue ( this . formGroup . get ( "profile" ) . value ) ;
}
this . breadCrumbs = Observable . of ( [
{
parentComponentName : "DataManagementPlanListingComponent" ,
label : 'DMPs' ,
url : "dmps" ,
notFoundResolver : [ await this . projectService . getSingle ( this . dataManagementPlan . project . id ) . map ( x = > ( { label : x.label , url : '/projects/edit/' + x . id } ) as BreadcrumbItem ) . toPromise ( ) ]
}
]
)
this . associatedUsers = data . associatedUsers ;
} ) ;
} else {
this . dataManagementPlan = new DataManagementPlanModel ( ) ;
setTimeout ( async ( ) = > {
this . formGroup = this . dataManagementPlan . buildForm ( ) ;
if ( this . formGroup . get ( "profile" ) && this . formGroup . get ( "profile" ) . value ) {
this . textCtrl . patchValue ( this . formGroup . get ( "profile" ) . value ) ;
}
this . breadCrumbs = Observable . of ( [
{
parentComponentName : "DataManagementPlanListingComponent" ,
label : 'DMPs' ,
url : "dmps" ,
}
] )
} ) ;
}
} ) ;
this . route
. queryParams
. subscribe ( params = > {
this . createNewVersion = params [ "clone" ] ;
} ) ;
}
2018-07-24 13:19:52 +02:00
searchProject ( query : string ) {
let projectRequestItem : RequestItem < ProjectCriteria > = new RequestItem ( ) ;
projectRequestItem . criteria = new ProjectCriteria ( ) ;
projectRequestItem . criteria . like = query ;
return this . projectService . getWithExternal ( projectRequestItem ) ;
}
2018-07-24 11:04:12 +02:00
formSubmit ( ) : void {
//this.touchAllFormFields(this.formGroup);
if ( ! this . isFormValid ( ) ) { return ; }
this . onSubmit ( ) ;
}
public isFormValid() {
return this . formGroup . valid ;
}
onSubmit ( ) : void {
this . dataManagementPlanService . createDataManagementPlan ( this . formGroup . value ) . subscribe (
complete = > this . onCallbackSuccess ( ) ,
error = > this . onCallbackError ( error )
)
}
onCallbackSuccess ( ) : void {
this . snackBar . openFromComponent ( SnackBarNotificationComponent , {
data : { message : this.isNew ? 'GENERAL.SNACK-BAR.SUCCESSFUL-CREATION' : 'GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE' , language : this.language } ,
duration : 3000 ,
} )
this . router . navigate ( [ '/dmps' ] ) ;
}
onCallbackError ( error : any ) {
this . setErrorModel ( error . error ) ;
//this.validateAllFormFields(this.formGroup);
}
public setErrorModel ( errorModel : BaseErrorModel ) {
Object . keys ( errorModel ) . forEach ( item = > {
( < any > this . dataManagementPlan . errorModel ) [ item ] = ( < any > errorModel ) [ item ] ;
} )
}
public cancel ( ) : void {
this . router . navigate ( [ '/dmps' ] ) ;
}
public invite ( ) : void {
this . router . navigate ( [ '/invite/' + this . dataManagementPlan . id ] ) ;
}
2018-07-24 13:19:52 +02:00
filterOrganisations ( value : string ) : Observable < ExternalSourcesItemModel [ ] > {
2018-07-24 11:04:12 +02:00
this . filteredOrganisations = undefined ;
2018-07-24 13:19:52 +02:00
this . filteringOrganisationsAsync = true ;
2018-07-24 11:04:12 +02:00
2018-07-24 13:19:52 +02:00
return this . externalSourcesService . searchDMPOrganizations ( value )
2018-07-24 11:04:12 +02:00
}
2018-07-24 13:19:52 +02:00
filterResearchers ( value : string ) : Observable < ExternalSourcesItemModel [ ] > {
2018-07-24 11:04:12 +02:00
this . filteredResearchers = undefined ;
2018-07-24 13:19:52 +02:00
this . filteringResearchersAsync = true ;
2018-07-24 11:04:12 +02:00
2018-07-24 13:19:52 +02:00
return this . externalSourcesService . searchDMPResearchers ( { criteria : { name : value , like : null } } )
2018-07-24 11:04:12 +02:00
}
2018-07-24 13:19:52 +02:00
filterProfiles ( value : string ) : Observable < DatasetProfileModel [ ] > {
2018-07-24 11:04:12 +02:00
this . filteredProfiles = undefined ;
2018-07-24 13:19:52 +02:00
this . filteredProfilesAsync = true ;
const request = new RequestItem < DatasetProfileCriteria > ( ) ;
let criteria = new DatasetProfileCriteria ( ) ;
criteria . like = value ;
request . criteria = criteria ;
return this . _service . searchDMPProfiles ( request )
2018-07-24 11:04:12 +02:00
}
addResearcher ( rowId : any , rowName : any ) {
let dialogRef = this . dialog . open ( AddResearchersComponent , {
height : '255px' ,
width : '700px' ,
data : {
dmpId : rowId ,
dmpName : rowName
}
} ) ;
}
availableProfiles() {
let dialogRef = this . dialog . open ( AvailableProfilesComponent , {
height : '355px' ,
width : '700px' ,
data : {
profiles : this.formGroup.get ( "profiles" )
}
} ) ;
return false ;
}
openConfirm ( dmpLabel , id ) : void {
this . _dialogService . openConfirm ( {
message : 'Are you sure you want to delete the "' + dmpLabel + '"' ,
disableClose : true || false , // defaults to false
viewContainerRef : this._viewContainerRef , //OPTIONAL
title : 'Confirm' , //OPTIONAL, hides if not provided
cancelButton : 'No' , //OPTIONAL, defaults to 'CANCEL'
acceptButton : 'Yes' //OPTIONAL, defaults to 'ACCEPT'
// width: '500px', //OPTIONAL, defaults to 400px
} ) . afterClosed ( ) . subscribe ( ( accept : boolean ) = > {
if ( accept ) {
this . dataManagementPlanService . delete ( id ) . subscribe ( ( ) = > {
this . router . navigate ( [ '/dmps' ] )
} ) ;
} else {
// DO SOMETHING ELSE
}
} ) ;
}
selectOption ( option : any ) {
this . dataManagementPlan . definition = null ;
this . formGroup . get ( "profile" ) . patchValue ( option , { emitEvent : false } )
this . dmpProfileService . getSingle ( option . id ) . subscribe ( result = > {
this . dataManagementPlan . definition = result . definition ;
} )
}
displayWith ( item : any ) {
if ( ! item ) return null ;
return item [ "label" ] ;
}
redirectToProject() {
this . router . navigate ( [ "projects/edit/" + this . dataManagementPlan . project . id ] )
}
redirectToDatasets() {
this . router . navigate ( [ "datasets/dmp/" + this . dataManagementPlan . id ] )
}
newVersion ( id : String , label : String ) {
this . router . navigate ( [ '/dmps/new_version/' + id , { dmpLabel : label } ] ) ;
}
clone ( id : String ) {
this . router . navigate ( [ '/dmps/clone/' + id ] ) ;
}
2018-08-30 13:09:36 +02:00
viewVersions ( rowId : String , rowLabel : String ) {
this . router . navigate ( [ '/dmps/viewversions/' + rowId ] , { queryParams : { groupLabel : rowLabel } } ) ;
}
2018-05-28 11:50:42 +02:00
}