From ba92882d7fe593fa77b3859ae2fa28e26e5f790d Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 22 Nov 2018 13:37:52 +0100 Subject: [PATCH] 57557: Refactored EntitySearchResultComponent --- .../entity-search-result-component.ts | 81 +++---------------- .../search-result-list-element.component.ts | 8 +- 2 files changed, 18 insertions(+), 71 deletions(-) diff --git a/src/app/shared/object-list/item-list-element/entity-types/entity-search-result-component.ts b/src/app/shared/object-list/item-list-element/entity-types/entity-search-result-component.ts index 7c05584cf0..ed53c45395 100644 --- a/src/app/shared/object-list/item-list-element/entity-types/entity-search-result-component.ts +++ b/src/app/shared/object-list/item-list-element/entity-types/entity-search-result-component.ts @@ -1,13 +1,11 @@ import { Component, Inject } from '@angular/core'; -import { Observable } from 'rxjs/Observable'; import { Item } from '../../../../core/shared/item.model'; -import { Metadatum } from '../../../../core/shared/metadatum.model'; -import { hasNoValue, hasValue, isEmpty } from '../../../empty.util'; +import { hasValue } from '../../../empty.util'; import { ITEM } from '../../../entities/switcher/entity-type-switcher.component'; import { ItemSearchResult } from '../../../object-collection/shared/item-search-result.model'; import { TruncatableService } from '../../../truncatable/truncatable.service'; +import { SearchResultListElementComponent } from '../../search-result-list-element/search-result-list-element.component'; -// TODO lot of overlap with SearchResultListElementComponent => refactor! /** * A generic component for displaying entity list elements */ @@ -15,77 +13,24 @@ import { TruncatableService } from '../../../truncatable/truncatable.service'; selector: 'ds-entity-search-result', template: '' }) -export class EntitySearchResultComponent { +export class EntitySearchResultComponent extends SearchResultListElementComponent { item: Item; - searchResult: ItemSearchResult; constructor( - private truncatableService: TruncatableService, - @Inject(ITEM) public object: Item | ItemSearchResult, + protected truncatableService: TruncatableService, + @Inject(ITEM) public obj: Item | ItemSearchResult, ) { - - if (hasValue((this.object as any).dspaceObject)) { - this.searchResult = this.object as ItemSearchResult; - this.item = this.searchResult.dspaceObject; + super(undefined, truncatableService); + if (hasValue((obj as any).dspaceObject)) { + this.object = obj as ItemSearchResult; + this.dso = this.object.dspaceObject; } else { - this.searchResult = { - dspaceObject: this.object as Item, + this.object = { + dspaceObject: obj as Item, hitHighlights: [] }; - this.item = this.object as Item; + this.dso = obj as Item; } - } - - /** - * Get the values of metadata by keys - * @param {string[]} keys List of metadata keys to get values for - * @returns {string[]} List of metadata values - */ - getValues(keys: string[]): string[] { - const results: string[] = new Array(); - this.searchResult.hitHighlights.forEach( - (md: Metadatum) => { - if (keys.indexOf(md.key) > -1) { - results.push(md.value); - } - } - ); - if (isEmpty(results)) { - this.item.filterMetadata(keys).forEach( - (md: Metadatum) => { - results.push(md.value); - } - ); - } - return results; - } - - /** - * Get the first value of a metadatum by key - * @param {string} key Metadatum key - * @returns {string} Metadatum value - */ - getFirstValue(key: string): string { - let result: string; - this.searchResult.hitHighlights.some( - (md: Metadatum) => { - if (key === md.key) { - result = md.value; - return true; - } - } - ); - if (hasNoValue(result)) { - result = this.item.findMetadata(key); - } - return result; - } - - /** - * Whether or not the item's values are collapsed - * @returns {Observable} - */ - isCollapsed(): Observable { - return this.truncatableService.isCollapsed(this.item.id); + this.item = this.dso; } } diff --git a/src/app/shared/object-list/search-result-list-element/search-result-list-element.component.ts b/src/app/shared/object-list/search-result-list-element/search-result-list-element.component.ts index fd821997ad..e84d558552 100644 --- a/src/app/shared/object-list/search-result-list-element/search-result-list-element.component.ts +++ b/src/app/shared/object-list/search-result-list-element/search-result-list-element.component.ts @@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Observable'; import { SearchResult } from '../../../+search-page/search-result.model'; import { DSpaceObject } from '../../../core/shared/dspace-object.model'; import { Metadatum } from '../../../core/shared/metadatum.model'; -import { hasNoValue, isEmpty } from '../../empty.util'; +import { hasNoValue, hasValue, isEmpty } from '../../empty.util'; import { ListableObject } from '../../object-collection/shared/listable-object.model'; import { AbstractListableElementComponent } from '../../object-collection/shared/object-collection-element/abstract-listable-element.component'; import { TruncatableService } from '../../truncatable/truncatable.service'; @@ -17,9 +17,11 @@ import { TruncatableService } from '../../truncatable/truncatable.service'; export class SearchResultListElementComponent, K extends DSpaceObject> extends AbstractListableElementComponent { dso: K; - public constructor(@Inject('objectElementProvider') public listable: ListableObject, private truncatableService: TruncatableService) { + public constructor(@Inject('objectElementProvider') public listable: ListableObject, protected truncatableService: TruncatableService) { super(listable); - this.dso = this.object.dspaceObject; + if (hasValue(this.object)) { + this.dso = this.object.dspaceObject; + } } getValues(keys: string[]): string[] {