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 { LanguageResolverService } from '../../../../services/language-resolver/language-resolver.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'; @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; isCreateNew = false; isCreateNewProject = false; isCreateNewFunder = false; grant: GrantTabModel; grantAutoCompleteConfiguration: SingleAutoCompleteConfiguration; projectAutoCompleteConfiguration: SingleAutoCompleteConfiguration; funderAutoCompleteConfiguration: SingleAutoCompleteConfiguration; constructor( private grantService: GrantService, private projectService: ProjectService, private funderService: FunderService, public languageResolverService: LanguageResolverService ) { } ngOnInit() { const grantRequestItem: RequestItem = new RequestItem(); grantRequestItem.criteria = new GrantCriteria(); this.grantAutoCompleteConfiguration = { filterFn: this.searchGrant.bind(this), initialItems: (extraData) => this.searchGrant(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'], subtitleFn: (item) => item ? item['source'] : null }; this.projectAutoCompleteConfiguration = { filterFn: this.searchProject.bind(this), initialItems: (extraData) => this.searchProject(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'] } this.funderAutoCompleteConfiguration = { filterFn: this.searchFunder.bind(this), initialItems: (extraData) => this.searchFunder(''), displayFn: (item) => item['label'], titleFn: (item) => item['label'] } 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.setValidators(); this.setProjectValidators(); this.setFunderValidators(); } searchGrant(query: any) { const grantRequestItem: RequestItem = 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 = new RequestItem(); projectRequestItem.criteria = new ProjectCriteria(); projectRequestItem.criteria.like = query; return this.projectService.getWithExternal(projectRequestItem); } searchFunder(query: string) { const funderRequestItem: RequestItem = new RequestItem(); funderRequestItem.criteria = new FunderCriteria(); funderRequestItem.criteria.like = query; return this.funderService.getWithExternal(funderRequestItem) } create() { this.isCreateNew = !this.isCreateNew; this.setValidators(); } createProject() { this.isCreateNewProject = !this.isCreateNewProject; this.setProjectValidators(); } createFunder() { this.isCreateNewFunder = !this.isCreateNewFunder; this.setFunderValidators(); } setValidators() { if (this.isCreateNew) { this.grantformGroup.get('existGrant').disable(); this.grantformGroup.get('label').enable(); this.grantformGroup.get('description').enable(); } else if (this.isFinalized) { 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.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.funderFormGroup.get('existFunder').disable(); this.funderFormGroup.get('label').disable(); } else { this.funderFormGroup.enable(); this.funderFormGroup.get('label').disable(); this.funderFormGroup.get('label').reset(); } } }