import { map, filter } from 'rxjs/operators'; import { Component } from "@angular/core"; import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog"; import { Observable } from "rxjs"; import { Inject } from "@angular/core"; import { TranslateService } from "@ngx-translate/core"; import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration'; import { Dmp, DmpDescriptionTemplate } from '@app/core/model/dmp/dmp'; import { DmpService } from '@app/core/services/dmp/dmp.service'; import { DescriptionService } from '@app/core/services/description/description.service'; import { DmpDescriptionTemplateLookup } from '@app/core/query/dmp-description-template.lookup'; import { IsActive } from '@app/core/common/enum/is-active.enum'; import { DmpLookup } from '@app/core/query/dmp.lookup'; import { Guid } from '@common/types/guid'; import { DmpStatus } from '@app/core/common/enum/dmp-status'; import { nameof } from 'ts-simple-nameof'; import { FilterService } from '@common/modules/text-filter/filter-service'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { DescriptionTemplatesInSection, DmpBlueprint, DmpBlueprintDefinition, DmpBlueprintDefinitionSection } from '@app/core/model/dmp-blueprint/dmp-blueprint'; @Component({ selector: 'description-copy-dialog-component', templateUrl: 'description-copy-dialog.component.html', styleUrls: ['./description-copy-dialog.component.scss'], }) export class DescriptionCopyDialogComponent { dmpModel: Dmp; sections: DmpBlueprintDefinitionSection[] = []; descriptionDescriptionTemplateLabel: String; dmpAutoCompleteConfiguration: SingleAutoCompleteConfiguration = { //TODO: add filter to only get DMPs that have connection with the same Description Template group. initialItems: (data?: any) => this.dmpService.query(this.buildDmpLookup(null,null,null, this.dmpDescriptionTemplateLookup)).pipe(map(x => x.items)), filterFn: (searchQuery: string, data?: any) => this.dmpService.query(this.buildDmpLookup(searchQuery, null, null, this.dmpDescriptionTemplateLookup)).pipe(map(x => x.items)), getSelectedItem: (selectedItem: any) => this.dmpService.query(this.buildDmpLookup(null, null, [selectedItem])).pipe(map(x => x.items[0])), displayFn: (item: Dmp) => item.label, titleFn: (item: Dmp) => item.label, valueAssign: (item: Dmp) => this.findSection(item), }; dmpDescriptionTemplateLookup: DmpDescriptionTemplateLookup = { descriptionTemplateGroupIds: [this.data.descriptionTemplate.groupId], isActive: [IsActive.Active] } as DmpDescriptionTemplateLookup; private buildDmpLookup(like?: string, excludedIds?: Guid[], ids?: Guid[], dmpDescriptionTemplateSubQuery?: DmpDescriptionTemplateLookup): DmpLookup { const lookup: DmpLookup = new DmpLookup(); lookup.page = { size: 100, offset: 0 }; if (excludedIds && excludedIds.length > 0) { lookup.excludedIds = excludedIds; } if (ids && ids.length > 0) { lookup.ids = ids; } lookup.isActive = [IsActive.Active]; lookup.statuses = [DmpStatus.Draft]; lookup.project = { fields: [ nameof(x => x.id), nameof(x => x.label), [nameof(x => x.blueprint), nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.id)].join('.'), [nameof(x => x.blueprint), nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.label)].join('.'), [nameof(x => x.blueprint), nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.ordinal)].join('.'), [nameof(x => x.blueprint), nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.hasTemplates)].join('.'), [nameof(x => x.blueprint), nameof(x => x.definition), nameof(x => x.sections), nameof(x => x.descriptionTemplates), nameof(x => x.descriptionTemplateGroupId)].join('.'), ] }; if (dmpDescriptionTemplateSubQuery != null) lookup.dmpDescriptionTemplateSubQuery = dmpDescriptionTemplateSubQuery; lookup.order = { items: [nameof(x => x.label)] }; if (like) { lookup.like = this.filterService.transformLike(like); } return lookup; } constructor( public dialogRef: MatDialogRef, public dmpService: DmpService, public descriptionService: DescriptionService, public language: TranslateService, private filterService: FilterService, @Inject(MAT_DIALOG_DATA) public data: any ) { } ngOnInit() { } findSection(dmp: Dmp){ this.sections = dmp.blueprint.definition.sections.filter(x => x.hasTemplates == true); if(this.sections.length == 1){ this.data.formGroup.get('sectionId').setValue(this.sections[0].id); }else { this.data.formGroup.get('sectionId').setValue(null); } return dmp.id } cancel() { this.dialogRef.close(this.data); } confirm() { this.dialogRef.close(this.data.formGroup); } getErrorMessage() { return this.language.instant('DESCRIPTION-COPY-DIALOG.ERROR-MESSAGE'); } close() { this.dialogRef.close(false); } }