import { BaseComponent } from '@common/base/base.component'; import { OnInit, Component, Input, Output, EventEmitter } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { TranslateService } from '@ngx-translate/core'; import { ConfigurationService } from '@app/core/services/configuration/configuration.service'; import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration'; import { map, takeUntil } from 'rxjs/operators'; import { ExternalSourceItemModel } from '@app/core/model/external-sources/external-source-item'; import { Observable } from 'rxjs'; import { ExternalSourcesService } from '@app/core/services/external-sources/external-sources.service'; import { isNullOrUndefined } from 'util'; import { MatDialog } from '@angular/material'; import { AddOrganizationComponent } from '../add-organization/add-organization.component'; import { AddResearcherComponent } from '../add-researcher/add-researcher.component'; @Component({ selector: 'main-info', templateUrl: './main-info.component.html', styleUrls: ['./main-info.component.scss'] }) export class MainInfoComponent extends BaseComponent implements OnInit { @Input() formGroup: FormGroup = null; @Input() isNewVersion: boolean; @Input() isUserOwner: boolean; @Input() isClone: boolean; @Output() onFormChanged: EventEmitter = new EventEmitter(); organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: this.filterOrganisations.bind(this), initialItems: (excludedItems: any[]) => this.filterOrganisations('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))), displayFn: (item) => item['name'], titleFn: (item) => item['name'], subtitleFn: (item) => item['tag'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['tag'] : (item['key'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['key'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')) }; researchersAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = { filterFn: this.filterResearchers.bind(this), initialItems: (excludedItems: any[]) => this.filterResearchers('').pipe(map(result => result.filter(resultItem => (excludedItems || []).map(x => x.id).indexOf(resultItem.id) === -1))), displayFn: (item) => item['name'], titleFn: (item) => item['name'], subtitleFn: (item) => item['tag'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['tag'] : (item['key'] ? this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.SOURCE:') + item['key'] : this.language.instant('TYPES.EXTERNAL-DATASET-TYPE.NO-SOURCE')) }; constructor( private language: TranslateService, private configurationService: ConfigurationService, private externalSourcesService: ExternalSourcesService, private dialog: MatDialog ) { super(); } ngOnInit() { // if (this.formGroup.get('definition')) { this.selectedDmpProfileDefinition = this.formGroup.get('definition').value; } // this.registerFormEventsForDmpProfile(); if (this.isNewVersion) { this.formGroup.get('label').disable(); } if (!this.isUserOwner && !this.isClone) { this.formGroup.disable(); } if (isNullOrUndefined(this.formGroup.get('extraProperties').get('publicDate').value)) { this.formGroup.get('extraProperties').get('publicDate').patchValue(new Date()); } this.formGroup.valueChanges.pipe(takeUntil(this._destroyed)) .subscribe(x => { this.onFormChanged.emit(); }); } // Researchers filterResearchers(value: string): Observable { return this.externalSourcesService.searchDMPResearchers({ criteria: { name: value, like: null } }); } addResearcher(event: MouseEvent) { event.stopPropagation(); const dialogRef = this.dialog.open(AddResearcherComponent, { data: this.formGroup.get('researchers') }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { const fullName = result.firstName + " " + result.lastName; const newItem = { label: null, name: fullName, id: null, status: 0, key: "Internal", }; const researchersArray = this.formGroup.get('researchers').value || []; researchersArray.push(newItem); this.formGroup.get('researchers').setValue(researchersArray); } }); } // Organizations showOrganizationCreator(): boolean { return this.configurationService.allowOrganizationCreator; } filterOrganisations(value: string): Observable { return this.externalSourcesService.searchDMPOrganizations(value); } canAddOrganizations(): boolean { if (!isNullOrUndefined(this.formGroup.get('organizations'))) { return this.formGroup.get('organiztions').disabled; } else { return false; } } addOrganization(event: MouseEvent) { event.stopPropagation(); const dialogRef = this.dialog.open(AddOrganizationComponent, { data: this.formGroup.get('organisations') }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { const fullName = result.name; const newItem = { label: null, name: fullName, id: null, status: 0, key: "Internal", }; const organizationsArray = this.formGroup.get('organisations').value || []; organizationsArray.push(newItem); this.formGroup.get('organisations').setValue(organizationsArray); } }); } }