added new decorator for metadata representations

This commit is contained in:
lotte
2019-09-27 16:58:24 +02:00
parent 20274bd4af
commit 5353e889a3
5 changed files with 53 additions and 33 deletions

View File

@@ -3,6 +3,7 @@
*/ */
export enum Context { export enum Context {
Undefined = 'undefined',
SearchList = 'searchList', SearchList = 'searchList',
SearchGrid = 'searchGrid', SearchGrid = 'searchGrid',
Submission = 'submission', Submission = 'submission',

View File

@@ -1,11 +1,9 @@
import { hasNoValue, hasValue } from '../empty.util'; import { hasNoValue, hasValue } from '../empty.util';
import { MetadataRepresentationType } from '../../core/shared/metadata-representation/metadata-representation.model';
import { ViewMode } from '../../core/shared/view-mode.model'; import { ViewMode } from '../../core/shared/view-mode.model';
export const DEFAULT_ITEM_TYPE = 'Default'; export const DEFAULT_ITEM_TYPE = 'Default';
export const DEFAULT_VIEW_MODE = ViewMode.ListElement; 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(); 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) * Decorator used for rendering simple item pages by type and viewMode (and optionally a representationType)
* @param type * @param type
* @param viewMode * @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) { return function decorator(component: any) {
if (hasNoValue(map.get(viewMode))) { if (hasNoValue(map.get(viewMode))) {
map.set(viewMode, new Map()); map.set(viewMode, new Map());
} }
if (hasNoValue(map.get(viewMode).get(type))) { if (hasValue(map.get(viewMode).get(type))) {
map.get(viewMode).set(type, new Map()); throw new Error(`There can't be more than one component to render Metadata of type "${type}" in view mode "${viewMode}"`);
} }
if (hasNoValue(representationType)) { map.get(viewMode).set(type, component);
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);
}; };
} }
@@ -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) * Get the component used for rendering an item by type and viewMode (and optionally a representationType)
* @param type * @param type
* @param viewMode * @param viewMode
* @param representationType
*/ */
export function getComponentByItemType(type: string, viewMode: string, representationType?: MetadataRepresentationType) { export function getComponentByItemType(type: string, viewMode: string) {
if (hasNoValue(representationType)) {
representationType = NO_REPRESENTATION_TYPE;
}
if (hasNoValue(map.get(viewMode))) { if (hasNoValue(map.get(viewMode))) {
viewMode = DEFAULT_VIEW_MODE; viewMode = DEFAULT_VIEW_MODE;
} }
if (hasNoValue(map.get(viewMode).get(type))) { if (hasNoValue(map.get(viewMode).get(type))) {
type = DEFAULT_ITEM_TYPE; type = DEFAULT_ITEM_TYPE;
} }
let representationComponent = map.get(viewMode).get(type).get(representationType); return map.get(viewMode).get(type);
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;
} }

View File

@@ -20,7 +20,7 @@ export class ItemTypeSwitcherComponent implements OnInit {
/** /**
* The item or metadata to determine the component for * The item or metadata to determine the component for
*/ */
@Input() object: Item | SearchResult<Item> | MetadataRepresentation; @Input() object: Item | SearchResult<Item>;
/** /**
* The preferred view-mode to display * The preferred view-mode to display
@@ -50,11 +50,6 @@ export class ItemTypeSwitcherComponent implements OnInit {
* @returns {string} * @returns {string}
*/ */
private getComponent(): 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; let item: Item;
if (hasValue((this.object as any).indexableObject)) { if (hasValue((this.object as any).indexableObject)) {
const searchResult = this.object as ItemSearchResult; const searchResult = this.object as ItemSearchResult;

View File

@@ -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;
}