2019-01-21 12:14:20 +01:00
|
|
|
import { Component, Input, OnInit } from '@angular/core';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { BaseComponent } from '../../../../../core/common/base/base.component';
|
2019-01-21 12:14:20 +01:00
|
|
|
import { DatasetProfileComboBoxType } from '../../../../../core/common/enum/dataset-profile-combo-box-type';
|
|
|
|
import { DatasetProfileFieldViewStyle } from '../../../../../core/common/enum/dataset-profile-field-view-style';
|
|
|
|
import { DatasetExternalAutocompleteCriteria } from '../../../../../core/query/dataset/daatset-external-autocomplete-criteria';
|
|
|
|
import { RequestItem } from '../../../../../core/query/request-item';
|
|
|
|
import { DatasetExternalAutocompleteService } from '../../../../../core/services/dataset/dataset-external-autocomplete.service';
|
|
|
|
import { SingleAutoCompleteConfiguration } from '../../../../../library/auto-complete/single/single-auto-complete-configuration';
|
2019-01-18 18:03:45 +01:00
|
|
|
import { VisibilityRulesService } from '../../visibility-rules/visibility-rules.service';
|
2019-03-26 16:09:02 +01:00
|
|
|
import { MultipleAutoCompleteConfiguration } from '../../../../../library/auto-complete/multiple/multiple-auto-complete-configuration';
|
2019-07-16 15:41:12 +02:00
|
|
|
import { DatasetProfileInternalDmpEntitiesType } from '../../../../../core/common/enum/dataset-profile-internal-dmp-entities-type';
|
|
|
|
import { ResearcherCriteria } from '../../../../../core/query/researcher/researcher-criteria';
|
|
|
|
import { ExternalSourcesService } from '../../../../../core/services/external-sources/external-sources.service';
|
2019-07-29 11:26:08 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-01-18 18:03:45 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-form-field',
|
|
|
|
templateUrl: './form-field.component.html',
|
|
|
|
styleUrls: ['./form-field.component.scss']
|
|
|
|
})
|
|
|
|
export class FormFieldComponent extends BaseComponent implements OnInit {
|
2019-01-28 14:51:33 +01:00
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
// @Input() field: Field;
|
|
|
|
@Input() form: FormGroup;
|
2019-01-28 13:18:48 +01:00
|
|
|
@Input() datasetProfileId: String;
|
2019-02-04 14:41:54 +01:00
|
|
|
@Input() isChild: Boolean = false;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
// change: Subscription;
|
|
|
|
// trackByFn = (index, item) => item ? item['id'] : null;
|
2019-01-18 18:03:45 +01:00
|
|
|
|
2019-01-21 12:14:20 +01:00
|
|
|
public singleAutoCompleteConfiguration: SingleAutoCompleteConfiguration;
|
2019-03-26 16:09:02 +01:00
|
|
|
public multipleAutoCompleteConfiguration: MultipleAutoCompleteConfiguration;
|
2019-01-21 12:14:20 +01:00
|
|
|
datasetProfileFieldViewStyleEnum = DatasetProfileFieldViewStyle;
|
|
|
|
datasetProfileComboBoxTypeEnum = DatasetProfileComboBoxType;
|
2019-07-16 15:41:12 +02:00
|
|
|
datasetProfileInternalDmpEntitiesTypeEnum = DatasetProfileInternalDmpEntitiesType;
|
2019-01-21 12:14:20 +01:00
|
|
|
|
2019-01-18 18:03:45 +01:00
|
|
|
constructor(
|
2019-01-21 12:14:20 +01:00
|
|
|
public visibilityRulesService: VisibilityRulesService,
|
2019-07-16 15:41:12 +02:00
|
|
|
private datasetExternalAutocompleteService: DatasetExternalAutocompleteService,
|
2019-07-29 11:26:08 +02:00
|
|
|
private externalSourcesService: ExternalSourcesService,
|
|
|
|
public language: TranslateService,
|
2019-01-18 18:03:45 +01:00
|
|
|
) { super(); }
|
|
|
|
|
|
|
|
ngOnInit() {
|
2019-02-04 14:41:54 +01:00
|
|
|
if (this.form.get('value').value) {
|
|
|
|
this.visibilityRulesService.updateValueAndVisibility(this.form.get('id').value, this.form.get('value').value);
|
|
|
|
}
|
2019-01-21 12:14:20 +01:00
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
// Setup autocomplete configuration if needed
|
|
|
|
if (this.form.get('viewStyle').value.renderStyle === DatasetProfileFieldViewStyle.ComboBox && this.form.get('data').value.type === DatasetProfileComboBoxType.Autocomplete) {
|
2019-03-26 16:09:02 +01:00
|
|
|
if (!(this.form.controls['data'].value.multiAutoComplete)) {
|
|
|
|
this.singleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchFromAutocomplete.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchFromAutocomplete(''),
|
|
|
|
displayFn: (item) => (item != null && item.length > 1) ? JSON.parse(item).label : item['label'],
|
|
|
|
titleFn: (item) => item['label'],
|
2019-07-29 11:26:08 +02:00
|
|
|
valueAssign: (item) => JSON.stringify(item),
|
|
|
|
subtitleFn: (item) => this.language.instant('DATASET-WIZARD.EDITOR.FIELDS.EXTERNAL-AUTOCOMPLETE-SUBTITLE') + item['source']
|
2019-03-26 16:09:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.multipleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchFromAutocomplete.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchFromAutocomplete(''),
|
|
|
|
displayFn: (item) => item['label'],
|
|
|
|
titleFn: (item) => item['label'],
|
2019-07-29 11:26:08 +02:00
|
|
|
valueAssign: this._transformValue,
|
|
|
|
subtitleFn: (item) => this.language.instant('DATASET-WIZARD.EDITOR.FIELDS.EXTERNAL-AUTOCOMPLETE-SUBTITLE') + item['source']
|
2019-03-26 16:09:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
}
|
2019-01-28 14:47:31 +01:00
|
|
|
|
2019-07-16 15:41:12 +02:00
|
|
|
if (this.form.get('viewStyle').value.renderStyle === DatasetProfileFieldViewStyle.InternalDmpEntities) {
|
|
|
|
if (this.form.get('data').value.type === DatasetProfileInternalDmpEntitiesType.Researchers) {
|
|
|
|
if (!(this.form.controls['data'].value.multiAutoComplete)) {
|
|
|
|
this.singleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchResearchers.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchResearchers(''),
|
|
|
|
displayFn: (item) => (item != null && item.length > 1) ? JSON.parse(item).name : item['name'],
|
|
|
|
titleFn: (item) => item['name'],
|
|
|
|
valueAssign: (item) => JSON.stringify(item)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.multipleAutoCompleteConfiguration = {
|
|
|
|
filterFn: this.searchResearchers.bind(this),
|
|
|
|
initialItems: (extraData) => this.searchResearchers(''),
|
|
|
|
displayFn: (item) => item['name'],
|
|
|
|
titleFn: (item) => item['name'],
|
|
|
|
valueAssign: this._transformValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
// this.form = this.visibilityRulesService.getFormGroup(this.field.id);
|
|
|
|
this.form.get('value').valueChanges
|
|
|
|
.pipe(takeUntil(this._destroyed))
|
|
|
|
.subscribe(item => {
|
|
|
|
this.visibilityRulesService.updateValueAndVisibility(this.form.get('id').value, item);
|
|
|
|
});
|
2019-01-18 18:03:45 +01:00
|
|
|
}
|
2019-03-26 16:09:02 +01:00
|
|
|
|
|
|
|
_optionRemove(event) {
|
|
|
|
const array = JSON.parse(this.form.get('value').value);
|
|
|
|
if (array) {
|
|
|
|
const index = array.map(x => x.id).indexOf(event.id);
|
|
|
|
if (index >= 0) {
|
|
|
|
array.splice(index, 1);
|
|
|
|
}
|
|
|
|
this.form.get('value').patchValue(JSON.stringify(array));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_transformValue(item: any) {
|
|
|
|
if (!item) return [];
|
|
|
|
return item && typeof item === 'string' ? JSON.parse(item) : JSON.stringify(item);
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
|
2019-01-21 12:14:20 +01:00
|
|
|
searchFromAutocomplete(query: string) {
|
|
|
|
const autocompleteRequestItem: RequestItem<DatasetExternalAutocompleteCriteria> = new RequestItem();
|
|
|
|
autocompleteRequestItem.criteria = new DatasetExternalAutocompleteCriteria();
|
2019-01-28 14:51:33 +01:00
|
|
|
|
2019-01-28 14:47:31 +01:00
|
|
|
autocompleteRequestItem.criteria.fieldID = this.form.get('id').value;
|
2019-01-28 13:18:48 +01:00
|
|
|
autocompleteRequestItem.criteria.profileID = this.datasetProfileId;
|
2019-01-31 11:29:35 +01:00
|
|
|
autocompleteRequestItem.criteria.like = query;
|
2019-01-21 12:14:20 +01:00
|
|
|
return this.datasetExternalAutocompleteService.queryAutocomplete(autocompleteRequestItem);
|
|
|
|
}
|
2019-07-16 15:41:12 +02:00
|
|
|
|
|
|
|
searchResearchers(query: string) {
|
|
|
|
const reasearcherAutocompleteRequestItem: RequestItem<ResearcherCriteria> = new RequestItem();
|
|
|
|
reasearcherAutocompleteRequestItem.criteria = new ResearcherCriteria;
|
|
|
|
reasearcherAutocompleteRequestItem.criteria.name = query;
|
|
|
|
return this.externalSourcesService.searchDMPResearchers(reasearcherAutocompleteRequestItem);
|
|
|
|
}
|
2019-01-18 18:03:45 +01:00
|
|
|
}
|