diff --git a/src/app/core/shared/context.model.ts b/src/app/core/shared/context.model.ts index 96452bd5d6..907210b2f1 100644 --- a/src/app/core/shared/context.model.ts +++ b/src/app/core/shared/context.model.ts @@ -3,6 +3,7 @@ */ export enum Context { + Undefined = 'undefined', SearchList = 'searchList', SearchGrid = 'searchGrid', Submission = 'submission', diff --git a/src/app/shared/items/item-type-decorator.ts b/src/app/shared/items/item-type-decorator.ts index a3f7c6b6f8..e6c7c18150 100644 --- a/src/app/shared/items/item-type-decorator.ts +++ b/src/app/shared/items/item-type-decorator.ts @@ -1,11 +1,9 @@ import { hasNoValue, hasValue } from '../empty.util'; -import { MetadataRepresentationType } from '../../core/shared/metadata-representation/metadata-representation.model'; import { ViewMode } from '../../core/shared/view-mode.model'; export const DEFAULT_ITEM_TYPE = 'Default'; export const DEFAULT_VIEW_MODE = ViewMode.ListElement; -export const NO_REPRESENTATION_TYPE = MetadataRepresentationType.None; -export const DEFAULT_REPRESENTATION_TYPE = MetadataRepresentationType.PlainText; + const map = new Map(); @@ -13,23 +11,16 @@ const map = new Map(); * Decorator used for rendering simple item pages by type and viewMode (and optionally a representationType) * @param type * @param viewMode - * @param representationType */ -export function rendersItemType(type: string, viewMode: string, representationType?: MetadataRepresentationType) { +export function rendersItemType(type: string, viewMode: string) { return function decorator(component: any) { if (hasNoValue(map.get(viewMode))) { map.set(viewMode, new Map()); } - if (hasNoValue(map.get(viewMode).get(type))) { - map.get(viewMode).set(type, new Map()); + if (hasValue(map.get(viewMode).get(type))) { + throw new Error(`There can't be more than one component to render Metadata of type "${type}" in view mode "${viewMode}"`); } - if (hasNoValue(representationType)) { - representationType = NO_REPRESENTATION_TYPE; - } - if (hasValue(map.get(viewMode).get(type).get(representationType))) { - throw new Error(`There can't be more than one component to render Metadata of type "${type}" in view mode "${viewMode}" with representation type "${representationType}"`); - } - map.get(viewMode).get(type).set(representationType, component); + map.get(viewMode).set(type, component); }; } @@ -37,24 +28,13 @@ export function rendersItemType(type: string, viewMode: string, representationTy * Get the component used for rendering an item by type and viewMode (and optionally a representationType) * @param type * @param viewMode - * @param representationType */ -export function getComponentByItemType(type: string, viewMode: string, representationType?: MetadataRepresentationType) { - if (hasNoValue(representationType)) { - representationType = NO_REPRESENTATION_TYPE; - } +export function getComponentByItemType(type: string, viewMode: string) { if (hasNoValue(map.get(viewMode))) { viewMode = DEFAULT_VIEW_MODE; } if (hasNoValue(map.get(viewMode).get(type))) { type = DEFAULT_ITEM_TYPE; } - let representationComponent = map.get(viewMode).get(type).get(representationType); - if (hasNoValue(representationComponent)) { - representationComponent = map.get(viewMode).get(type).get(DEFAULT_REPRESENTATION_TYPE); - } - if (hasNoValue(representationComponent)) { - representationComponent = map.get(viewMode).get(type).get(NO_REPRESENTATION_TYPE); - } - return representationComponent; + return map.get(viewMode).get(type); } diff --git a/src/app/shared/items/switcher/item-type-switcher.component.ts b/src/app/shared/items/switcher/item-type-switcher.component.ts index cd061bc1dd..0f31b10c7a 100644 --- a/src/app/shared/items/switcher/item-type-switcher.component.ts +++ b/src/app/shared/items/switcher/item-type-switcher.component.ts @@ -20,7 +20,7 @@ export class ItemTypeSwitcherComponent implements OnInit { /** * The item or metadata to determine the component for */ - @Input() object: Item | SearchResult | MetadataRepresentation; + @Input() object: Item | SearchResult; /** * The preferred view-mode to display @@ -50,11 +50,6 @@ export class ItemTypeSwitcherComponent implements OnInit { * @returns {string} */ private getComponent(): string { - if (hasValue((this.object as any).representationType)) { - const metadataRepresentation = this.object as MetadataRepresentation; - return getComponentByItemType(metadataRepresentation.itemType, this.viewMode, metadataRepresentation.representationType); - } - let item: Item; if (hasValue((this.object as any).indexableObject)) { const searchResult = this.object as ItemSearchResult; diff --git a/src/app/shared/metadata-representation/metadata-representation-loader.component.ts b/src/app/shared/metadata-representation/metadata-representation-loader.component.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/app/shared/metadata-representation/metadata-representation.decorator.ts b/src/app/shared/metadata-representation/metadata-representation.decorator.ts new file mode 100644 index 0000000000..0eee4f6d2f --- /dev/null +++ b/src/app/shared/metadata-representation/metadata-representation.decorator.ts @@ -0,0 +1,44 @@ +import { MetadataRepresentationType } from '../../core/shared/metadata-representation/metadata-representation.model'; +import { hasNoValue, hasValue } from '../empty.util'; +import { Context } from '../../core/shared/context.model'; +import { DEFAULT_ITEM_TYPE } from '../items/item-type-decorator'; + +const map = new Map(); + +export const DEFAULT_REPRESENTATION_TYPE = MetadataRepresentationType.PlainText; +export const DEFAULT_CONTEXT = Context.Undefined; + +export function metadataRepresentationComponent(entityType: string, mdRepresentationType: MetadataRepresentationType, context: Context = DEFAULT_CONTEXT) { + return function decorator(component: any) { + if (hasNoValue(map.get(entityType))) { + map.set(entityType, new Map()); + } + + if (hasNoValue(map.get(entityType).get(mdRepresentationType))) { + map.get(entityType).set(mdRepresentationType, new Map()); + } + + if (hasValue(map.get(entityType).get(mdRepresentationType).get(context))) { + throw new Error(`There can't be more than one component to render Entity of type "${entityType}" in MetadataRepresentation "${mdRepresentationType}" with context "${context}"`); + } + map.get(entityType).get(mdRepresentationType).set(context, component); + } +} + +export function getMetadataRepresentationComponent(entityType: string, mdRepresentationType: MetadataRepresentationType, context: Context = DEFAULT_CONTEXT) { + if (hasNoValue(entityType) || hasNoValue(map.get(entityType))) { + entityType = DEFAULT_ITEM_TYPE; + } + + if (hasNoValue(map.get(entityType).get(mdRepresentationType))) { + mdRepresentationType = DEFAULT_REPRESENTATION_TYPE; + } + + let representationComponent = map.get(entityType).get(mdRepresentationType).get(context); + + if (hasNoValue(representationComponent)) { + representationComponent = map.get(entityType).get(mdRepresentationType).get(DEFAULT_REPRESENTATION_TYPE); + } + + return representationComponent; +} \ No newline at end of file