diff --git a/projects/admin/src/app/classes/typeahead/mef-typeahead.ts b/projects/admin/src/app/classes/typeahead/mef-typeahead.ts index 37fe41bcd..fe16df0cf 100644 --- a/projects/admin/src/app/classes/typeahead/mef-typeahead.ts +++ b/projects/admin/src/app/classes/typeahead/mef-typeahead.ts @@ -243,7 +243,7 @@ export class MefTypeahead implements ITypeahead { private _buildRemoteSuggestions(hits: any): SuggestionMetadata[] { const suggestions = []; hits.map((hit: any) => { - for (const source of this._sources()) { + for (const source of this.sources()) { if (hit.metadata[source]) { suggestions.push(this._getNameRef(hit.metadata, source)); } @@ -265,7 +265,7 @@ export class MefTypeahead implements ITypeahead { label: metadata.authorized_access_point, externalLink: this._get_source_uri(metadata?.identifiedBy, sourceName), value: this._get_source_uri(metadata?.identifiedBy, 'mef'), - group: this.translateService.instant('link to authority {{ sourceName }}', {sourceName}), + group: sourceName, column: 0 }; } @@ -287,15 +287,14 @@ export class MefTypeahead implements ITypeahead { * Get sources * @return array of sources */ - private _sources(): string[] { + public sources(): string[] { const language = this.translateService.currentLang; const order: any = this.appSettingsService.agentLabelOrder; const key = language in order ? language : 'fallback'; const agentSources = (key === 'fallback') ? order[order[key]] : order[key]; - const sources = agentSources.filter((source: string) => source !== 'rero'); - return sources; + return agentSources.filter((source: string) => source !== 'rero'); } /** @@ -310,7 +309,7 @@ export class MefTypeahead implements ITypeahead { description: hit?.source_catalog, value: this.apiService.getRefEndpoint('local_entities', hit.pid), externalLink: this._buildLocalEntityDetailViewURI(hit.pid), - group: this.translateService.instant('link to local authority'), + group: 'local', column: 1, }; }); diff --git a/projects/admin/src/app/record/formly/type/entity-typeahead/entity-typeahead.component.ts b/projects/admin/src/app/record/formly/type/entity-typeahead/entity-typeahead.component.ts index 5b719ff59..7f0896647 100644 --- a/projects/admin/src/app/record/formly/type/entity-typeahead/entity-typeahead.component.ts +++ b/projects/admin/src/app/record/formly/type/entity-typeahead/entity-typeahead.component.ts @@ -90,7 +90,7 @@ export class EntityTypeaheadComponent extends FieldType implements OnInit { suggestions.push({ label: undefined, value: undefined, - group: this._translateService.instant('link to local authority'), + group: 'local', column: 1, }); } @@ -98,7 +98,8 @@ export class EntityTypeaheadComponent extends FieldType implements OnInit { }), map((suggestions: any) => { let tmpSuggestions = this._splitSuggestionsByColumn(suggestions); - tmpSuggestions = this._orderSuggestions(tmpSuggestions); + tmpSuggestions = this._orderBySources(this._orderSuggestions(tmpSuggestions)); + tmpSuggestions = this._labelGroup(tmpSuggestions); this.suggestionSections = this._createSuggestionGroupHeader(tmpSuggestions); return suggestions; }) @@ -182,6 +183,37 @@ export class EntityTypeaheadComponent extends FieldType implements OnInit { ); } + /** Order suggestions by sources */ + private _orderBySources(suggestionSections: SuggestionMetadata[][]): SuggestionMetadata[][] { + const sources = this._remoteTypeahead.sources(); + suggestionSections.map((section, index) => { + const order = []; + sources.map((source: string) => { + section.map((suggestion: any) => { + if (suggestion.group === source) { + order.push(suggestion); + } + }); + }); + if (order.length > 0 && (order.length === section.length)) { + suggestionSections[index] = order; + } + }); + return suggestionSections; + } + + /** Transform label */ + private _labelGroup(suggestionSections: SuggestionMetadata[][]): SuggestionMetadata[][] { + suggestionSections.map(section => { + section.map(suggestion => { + suggestion.group = suggestion.group === 'local' + ? this._translateService.instant('link to local authority') + : this._translateService.instant('link to authority {{ sourceName }}', {sourceName: suggestion.group}) + }); + }); + return suggestionSections; + } + /** Create group header for each suggestion sections. */ private _createSuggestionGroupHeader(suggestionSections: SuggestionMetadata[][]): TypeaheadMatch[][] { return suggestionSections.map(section => {