argos/dmp-frontend/src/app/ui/dmp/editor/grant-tab/grant-tab.component.ts

173 lines
6.7 KiB
TypeScript

import { Component, OnInit, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { SingleAutoCompleteConfiguration } from '../../../../library/auto-complete/single/single-auto-complete-configuration';
import { RequestItem } from '../../../../core/query/request-item';
import { GrantCriteria } from '../../../../core/query/grant/grant-criteria';
import { GrantService } from '../../../../core/services/grant/grant.service';
import { GrantTabModel } from './grant-tab-model';
import { ProjectService } from '../../../../core/services/project/project.service';
import { FunderService } from '../../../../core/services/funder/funder.service';
import { FunderCriteria } from '../../../../core/query/funder/funder-criteria';
import { ProjectCriteria } from '../../../../core/query/project/project-criteria';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-grant-tab',
templateUrl: './grant-tab.component.html',
styleUrls: ['./grant-tab.component.scss']
})
export class GrantTabComponent implements OnInit {
@Input() grantformGroup: FormGroup;
@Input() projectFormGroup: FormGroup;
@Input() funderFormGroup: FormGroup;
@Input() isFinalized: boolean;
@Input() isNewVersion: boolean;
isCreateNew = false;
isCreateNewProject = false;
isCreateNewFunder = false;
grant: GrantTabModel;
grantAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
funderAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
constructor(
private grantService: GrantService,
private projectService: ProjectService,
private funderService: FunderService,
private language: TranslateService
) { }
ngOnInit() {
const grantRequestItem: RequestItem<GrantCriteria> = new RequestItem();
grantRequestItem.criteria = new GrantCriteria();
this.funderAutoCompleteConfiguration = {
filterFn: this.searchFunder.bind(this),
initialItems: (extraData) => this.searchFunder(''),
displayFn: (item) => item['label'],
titleFn: (item) => item['label'],
subtitleFn: (item) => item['source'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['source'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')
}
this.grantAutoCompleteConfiguration = {
filterFn: this.searchGrant.bind(this),
initialItems: (extraData) => this.searchGrant(''),
displayFn: (item) => item['label'],
titleFn: (item) => item['label'],
subtitleFn: (item) => item['source'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['source'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')
};
this.projectAutoCompleteConfiguration = {
filterFn: this.searchProject.bind(this),
initialItems: (extraData) => this.searchProject(''),
displayFn: (item) => item['label'],
titleFn: (item) => item['label'],
subtitleFn: (item) => item['source'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['source'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')
}
this.isCreateNew = (this.grantformGroup.get('label').value != null && this.grantformGroup.get('label').value.length > 0);
this.isCreateNewProject = (this.projectFormGroup.get('label').value != null && this.projectFormGroup.get('label').value.length > 0);
this.isCreateNewFunder = (this.funderFormGroup.get('label').value != null && this.funderFormGroup.get('label').value.length > 0);
this.setGrantValidators();
this.setProjectValidators();
this.setFunderValidators();
}
searchGrant(query: any) {
const grantRequestItem: RequestItem<GrantCriteria> = new RequestItem();
grantRequestItem.criteria = new GrantCriteria();
grantRequestItem.criteria.like = query;
if (this.funderFormGroup.get('existFunder').value) {
grantRequestItem.criteria.funderReference = this.funderFormGroup.controls['existFunder'].value.reference;
}
return this.grantService.getWithExternal(grantRequestItem);
}
searchProject(query: string) {
const projectRequestItem: RequestItem<ProjectCriteria> = new RequestItem();
projectRequestItem.criteria = new ProjectCriteria();
projectRequestItem.criteria.like = query;
return this.projectService.getWithExternal(projectRequestItem);
}
searchFunder(query: string) {
const funderRequestItem: RequestItem<FunderCriteria> = new RequestItem();
funderRequestItem.criteria = new FunderCriteria();
funderRequestItem.criteria.like = query;
return this.funderService.getWithExternal(funderRequestItem)
}
createGrant() {
if (this.isNewVersion) { return };
this.isCreateNew = !this.isCreateNew;
this.setGrantValidators();
}
createProject() {
if (this.isNewVersion) { return };
this.isCreateNewProject = !this.isCreateNewProject;
this.setProjectValidators();
}
createFunder() {
if (this.isNewVersion) { return };
this.isCreateNewFunder = !this.isCreateNewFunder;
this.setFunderValidators();
}
setGrantValidators() {
if (this.isCreateNew) {
this.grantformGroup.get('existGrant').disable();
this.grantformGroup.get('label').enable();
this.grantformGroup.get('description').enable();
} else if (this.isFinalized || this.isNewVersion) {
this.grantformGroup.get('existGrant').disable();
this.grantformGroup.get('label').disable();
this.grantformGroup.get('description').disable();
} else {
this.grantformGroup.get('existGrant').enable();
this.grantformGroup.get('label').disable();
this.grantformGroup.get('label').reset();
this.grantformGroup.get('description').disable();
this.grantformGroup.get('description').reset();
}
}
setProjectValidators() {
if (this.isCreateNewProject) {
this.projectFormGroup.get('existProject').disable();
this.projectFormGroup.get('label').enable();
this.projectFormGroup.get('description').enable();
} else if (this.isFinalized || this.isNewVersion) {
this.projectFormGroup.get('existProject').disable();
this.projectFormGroup.get('label').disable();
this.projectFormGroup.get('description').disable();
} else {
this.projectFormGroup.get('existProject').enable();
this.projectFormGroup.get('label').disable();
this.projectFormGroup.get('label').reset();
this.projectFormGroup.get('description').disable();
this.projectFormGroup.get('description').reset();
}
}
setFunderValidators() {
if (this.isCreateNewFunder) {
this.funderFormGroup.get('existFunder').disable();
this.funderFormGroup.get('label').enable();
} else if (this.isFinalized || this.isNewVersion) {
this.funderFormGroup.get('existFunder').disable();
this.funderFormGroup.get('label').disable();
} else {
this.funderFormGroup.enable();
this.funderFormGroup.get('label').disable();
this.funderFormGroup.get('label').reset();
}
}
}