Skip to content
Snippets Groups Projects
Commit 5353e889 authored by lotte's avatar lotte
Browse files

added new decorator for metadata representations

parent 20274bd4
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
*/
export enum Context {
Undefined = 'undefined',
SearchList = 'searchList',
SearchGrid = 'searchGrid',
Submission = 'submission',
......
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 (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}"`);
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}"`);
}
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);
}
......@@ -20,7 +20,7 @@ export class ItemTypeSwitcherComponent implements OnInit {
/**
* 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
......@@ -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;
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment