From c8600b315e320b32c2e992774ed9bb2cdeb9ba15 Mon Sep 17 00:00:00 2001 From: Diamantis Tziotzios Date: Wed, 18 Dec 2019 17:37:00 +0200 Subject: [PATCH] autocomplete fix --- .../multiple-auto-complete.component.ts | 14 +++++++------- .../form-field/form-field.component.ts | 17 ++++++----------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/dmp-frontend/src/app/library/auto-complete/multiple/multiple-auto-complete.component.ts b/dmp-frontend/src/app/library/auto-complete/multiple/multiple-auto-complete.component.ts index 53a023340..245f1d2be 100644 --- a/dmp-frontend/src/app/library/auto-complete/multiple/multiple-auto-complete.component.ts +++ b/dmp-frontend/src/app/library/auto-complete/multiple/multiple-auto-complete.component.ts @@ -135,16 +135,16 @@ export class MultipleAutoCompleteComponent extends _CustomComponentMixinBase imp getSelectedItems(value: any) { if (value != null && Array.isArray(value) && this.configuration) { - const newSelections = value.filter(x => !this._selectedItems.has(JSON.stringify(x))); + const newSelections = value.filter(x => !this._selectedItems.has(this.stringify(x))); if (newSelections.length > 0 && this.configuration.getSelectedItems != null) { this.configuration.getSelectedItems(newSelections).pipe(takeUntil(this._destroyed)).subscribe(x => { x.forEach(element => { - this._selectedItems.set(JSON.stringify(this.configuration.valueAssign != null ? this.configuration.valueAssign(element) : element), element); + this._selectedItems.set(this.stringify(this.configuration.valueAssign != null ? this.configuration.valueAssign(element) : element), element); }); }); } else { newSelections.forEach(element => { - this._selectedItems.set(JSON.stringify(this.configuration.valueAssign != null ? this.configuration.valueAssign(element) : element), element); + this._selectedItems.set(this.stringify(this.configuration.valueAssign != null ? this.configuration.valueAssign(element) : element), element); }); } } @@ -166,7 +166,7 @@ export class MultipleAutoCompleteComponent extends _CustomComponentMixinBase imp } stringify(value: any): string { - return JSON.stringify(value); + return typeof (value) == 'string' ? value : JSON.stringify(value); } isNullOrEmpty(query: string): boolean { @@ -182,7 +182,7 @@ export class MultipleAutoCompleteComponent extends _CustomComponentMixinBase imp const newValue = this._valueToAssign(item); //Update selected items - this._selectedItems.set(JSON.stringify(newValue), item); + this._selectedItems.set(this.stringify(newValue), item); const newValueArray = (Array.isArray(this.value) ? this.value : []); newValueArray.push(newValue); @@ -319,11 +319,11 @@ export class MultipleAutoCompleteComponent extends _CustomComponentMixinBase imp _removeSelectedItem(item: any, event: MouseEvent): void { if (event != null) { event.stopPropagation(); } const valueToDelete = this._valueToAssign(item); - this.value = this.value.filter(x => JSON.stringify(x) !== JSON.stringify(valueToDelete)); //TODO, maybe we need to implement equality here differently. + this.value = this.value.filter(x => this.stringify(x) !== this.stringify(valueToDelete)); //TODO, maybe we need to implement equality here differently. this.optionRemoved.emit(item); //Update chips - this._selectedItems.delete(JSON.stringify(valueToDelete)); + this._selectedItems.delete(this.stringify(valueToDelete)); this.autocompleteInput.nativeElement.focus(); this.pushChanges(this.value); diff --git a/dmp-frontend/src/app/ui/misc/dataset-description-form/components/form-field/form-field.component.ts b/dmp-frontend/src/app/ui/misc/dataset-description-form/components/form-field/form-field.component.ts index 396ed2825..62019e028 100644 --- a/dmp-frontend/src/app/ui/misc/dataset-description-form/components/form-field/form-field.component.ts +++ b/dmp-frontend/src/app/ui/misc/dataset-description-form/components/form-field/form-field.component.ts @@ -72,9 +72,9 @@ export class FormFieldComponent extends BaseComponent implements OnInit { this.multipleAutoCompleteConfiguration = { filterFn: this.searchFromAutocomplete.bind(this), initialItems: () => this.searchFromAutocomplete(''), - displayFn: (item) => item['label'], - titleFn: (item) => item['label'], - valueAssign: this._transformValue, + displayFn: (item) => typeof (item) == 'string' ? JSON.parse(item)['label'] : item['label'], + titleFn: (item) => typeof (item) == 'string' ? JSON.parse(item)['label'] : item['label'], + valueAssign: (item) => typeof (item) == 'string' ? item : JSON.stringify(item), subtitleFn: (item) => item['source'] ? this.language.instant('DATASET-WIZARD.EDITOR.FIELDS.EXTERNAL-AUTOCOMPLETE-SUBTITLE') + item['source'] : this.language.instant('DATASET-WIZARD.EDITOR.FIELDS.EXTERNAL-AUTOCOMPLETE-NO-SOURCE') } } @@ -111,11 +111,6 @@ export class FormFieldComponent extends BaseComponent implements OnInit { // } // } - _transformValue(item: any) { - if (!item) return []; - return item && typeof item === 'string' ? JSON.parse(item) : JSON.stringify(item); - } - searchFromAutocomplete(query: string) { const autocompleteRequestItem: RequestItem = new RequestItem(); autocompleteRequestItem.criteria = new DatasetExternalAutocompleteCriteria(); @@ -164,9 +159,9 @@ export class FormFieldComponent extends BaseComponent implements OnInit { this.multipleAutoCompleteConfiguration = { filterFn: myfunc.bind(this), initialItems: (extraData) => myfunc(''), - displayFn: (item) => item[title], - titleFn: (item) => item[title], - valueAssign: this._transformValue, + displayFn: (item) => typeof (item) == 'string' ? JSON.parse(item)['label'] : item['label'], + titleFn: (item) => typeof (item) == 'string' ? JSON.parse(item)['label'] : item['label'], + valueAssign: (item) => typeof (item) == 'string' ? item : JSON.stringify(item), subtitleFn: (item) => item[subtitle] } }