2024-01-15 17:47:34 +01:00
|
|
|
import { Component, Input, OnInit } from '@angular/core';
|
2024-04-01 15:05:28 +02:00
|
|
|
import { UntypedFormArray, UntypedFormGroup } from '@angular/forms';
|
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
2024-02-09 21:46:05 +01:00
|
|
|
import { ReferenceType } from '@app/core/model/reference-type/reference-type';
|
2024-01-15 17:47:34 +01:00
|
|
|
import { ReferenceService } from '@app/core/services/reference/reference.service';
|
|
|
|
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
|
|
|
|
import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration';
|
2024-02-21 16:52:50 +01:00
|
|
|
import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration';
|
2024-01-15 17:47:34 +01:00
|
|
|
import { BaseComponent } from '@common/base/base.component';
|
2024-04-01 15:05:28 +02:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { ReferenceDialogEditorComponent } from './editor/reference-dialog-editor.component';
|
2024-04-02 10:15:07 +02:00
|
|
|
import { Reference, ReferencePersist } from '@app/core/model/reference/reference';
|
|
|
|
import { ReferenceSearchLookup } from '@app/core/query/reference-search.lookup';
|
|
|
|
import { Guid } from '@common/types/guid';
|
|
|
|
import { nameof } from 'ts-simple-nameof';
|
2023-12-28 16:18:49 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-reference-field-component',
|
|
|
|
templateUrl: 'reference-field.component.html',
|
|
|
|
styleUrls: ['./reference-field.component.scss']
|
|
|
|
})
|
|
|
|
export class ReferenceFieldComponent extends BaseComponent implements OnInit {
|
|
|
|
|
|
|
|
@Input() referenceType: ReferenceType = null;
|
|
|
|
@Input() form: UntypedFormGroup = null;
|
2024-02-01 13:42:23 +01:00
|
|
|
@Input() label: string = null;
|
2024-02-08 18:48:14 +01:00
|
|
|
@Input() required: boolean = false;
|
|
|
|
@Input() multiple: boolean = true;
|
|
|
|
@Input() hint: string;
|
2024-02-09 21:46:05 +01:00
|
|
|
@Input() placeholder: string;
|
2023-12-28 16:18:49 +01:00
|
|
|
|
2024-01-15 17:47:34 +01:00
|
|
|
multipleAutoCompleteSearchConfiguration: MultipleAutoCompleteConfiguration;
|
2024-02-21 16:52:50 +01:00
|
|
|
singleAutoCompleteSearchConfiguration: SingleAutoCompleteConfiguration;
|
2023-12-28 16:18:49 +01:00
|
|
|
|
|
|
|
constructor(
|
2024-01-15 17:47:34 +01:00
|
|
|
private referenceService: ReferenceService,
|
|
|
|
public enumUtils: EnumUtils,
|
2024-04-01 15:05:28 +02:00
|
|
|
private dialog: MatDialog,
|
2023-12-28 16:18:49 +01:00
|
|
|
) { super(); }
|
|
|
|
|
|
|
|
ngOnInit() {
|
2024-02-21 16:52:50 +01:00
|
|
|
if (this.multiple) {
|
|
|
|
this.multipleAutoCompleteSearchConfiguration = this.referenceService.getMultipleAutoCompleteSearchConfiguration(this.referenceType.id);
|
|
|
|
} else {
|
|
|
|
this.singleAutoCompleteSearchConfiguration = this.referenceService.getSingleAutocompleteSearchConfiguration(this.referenceType.id);
|
|
|
|
}
|
2023-12-28 16:18:49 +01:00
|
|
|
}
|
|
|
|
|
2024-01-18 13:23:20 +01:00
|
|
|
addReference() {
|
2024-04-01 15:05:28 +02:00
|
|
|
|
2024-04-02 10:15:07 +02:00
|
|
|
this.referenceService.searchWithDefinition(this.buildAutocompleteSearchLookup(this.referenceType.id))
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(
|
|
|
|
references => {
|
|
|
|
const dialogRef = this.dialog.open(ReferenceDialogEditorComponent, {
|
|
|
|
width: '500px',
|
|
|
|
restoreFocus: false,
|
|
|
|
data: {
|
|
|
|
referenceTypeId: this.referenceType.id,
|
|
|
|
label: this.label ?? this.referenceType.name,
|
|
|
|
existingReferences: references.map(x => x.reference)
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dialogRef.afterClosed()
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(newResult => {
|
|
|
|
if (!newResult) { return; }
|
|
|
|
let results = this.form.value as ReferencePersist[];
|
|
|
|
if (results == undefined) results = [];
|
|
|
|
results.push(newResult);
|
|
|
|
this.form.setValue(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildAutocompleteSearchLookup(typeId: Guid): ReferenceSearchLookup {
|
|
|
|
const lookup: ReferenceSearchLookup = new ReferenceSearchLookup();
|
|
|
|
lookup.page = { size: 100, offset: 0 };
|
|
|
|
lookup.project = {
|
|
|
|
fields: [
|
|
|
|
nameof<Reference>(x => x.reference),
|
|
|
|
]
|
|
|
|
};
|
|
|
|
lookup.typeId = typeId;
|
|
|
|
lookup.order = { items: [nameof<Reference>(x => x.label)] };
|
|
|
|
return lookup;
|
2024-01-18 13:23:20 +01:00
|
|
|
}
|
2023-12-28 16:18:49 +01:00
|
|
|
}
|