Skip to content

Commit

Permalink
entities: fix sources order by language
Browse files Browse the repository at this point in the history
Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr authored and Pascal Repond committed Oct 24, 2023
1 parent 3c71809 commit 7d7f5e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
11 changes: 5 additions & 6 deletions projects/admin/src/app/classes/typeahead/mef-typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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
};
}
Expand All @@ -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');
}

/**
Expand All @@ -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,
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ 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,
});
}
return suggestions;
}),
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;
})
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 7d7f5e4

Please sign in to comment.