All files / src/app/core/models/content-view content-view.model.ts

100% Statements 22/22
100% Branches 33/33
100% Functions 13/13
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 11346x                                                                                     225x   46x   126x 96x     27x             111x                 53x     11x     46x 75x             1x   7x   7x           46x 7x               46x     31x                  
import { memoize } from 'lodash-es';
 
import { ContentConfigurationParameters } from 'ish-core/models/content-configuration-parameter/content-configuration-parameter.mapper';
import { ContentPageletEntryPoint } from 'ish-core/models/content-pagelet-entry-point/content-pagelet-entry-point.model';
import { ContentPagelet } from 'ish-core/models/content-pagelet/content-pagelet.model';
import { ContentSlot } from 'ish-core/models/content-slot/content-slot.model';
import { SeoAttributes } from 'ish-core/models/seo-attributes/seo-attributes.model';
 
export interface ContentConfigurationParameterView {
  hasParam(key: string): boolean;
  stringParam(key: string, defaultValue?: string): string;
  numberParam(key: string, defaultValue?: number): number;
  booleanParam(key: string, defaultValue?: boolean): boolean;
  configParam<T extends object>(key: string): T;
}
 
interface ContentEntryPointView {
  id: string;
  domain: string;
  displayName?: string;
  pageletIDs: string[];
}
 
interface ContentSlotView extends ContentEntryPointView, ContentConfigurationParameterView {}
 
export interface ContentPageletEntryPointView extends ContentEntryPointView, ContentConfigurationParameterView {
  id: string;
  domain: string;
  displayName: string;
  resourceSetId: string;
  pageletIDs: string[];
  seoAttributes: SeoAttributes;
}
 
export interface ContentPageletView extends ContentConfigurationParameterView {
  definitionQualifiedName: string;
  configurationParameters: ContentConfigurationParameters;
  id: string;
  domain: string;
  displayName: string;
  slot(qualifiedName: string): ContentSlotView;
}
 
const paramMemoize = <T>(key: string, defaultValue: T) => JSON.stringify({ key, defaultValue });
 
export const createContentConfigurationParameterView = (
  params: ContentConfigurationParameters
): ContentConfigurationParameterView => ({
  hasParam: memoize(key => Object.keys(params).includes(key)),
  booleanParam: memoize(
    (key, defaultValue = undefined) =>
      Object.keys(params).includes(key)
        ? typeof params[key] === 'string' && (params[key] as string).toLowerCase().trim() === 'true'
        : defaultValue,
    paramMemoize
  ),
  stringParam: memoize(
    (key, defaultValue = undefined) =>
      Object.keys(params).includes(key)
        ? typeof params[key] === 'string'
          ? (params[key] as string)
          : JSON.stringify(params[key])
        : defaultValue,
    paramMemoize
  ),
  numberParam: memoize(
    (key, defaultValue = NaN) =>
      Number(params[key]) === 0 ? Number(params[key]) : Number(params[key]) || defaultValue,
    paramMemoize
  ),
  configParam: <T extends object>(key: string) => params[key] as T,
});
 
export const createContentPageletView = (pagelet: ContentPagelet): ContentPageletView =>
  pagelet && {
    definitionQualifiedName: pagelet.definitionQualifiedName,
    configurationParameters: pagelet.configurationParameters,
    id: pagelet.id,
    domain: pagelet.domain,
    displayName: pagelet.displayName,
    slot: !pagelet.slots
      ? () => undefined
      : memoize(qualifiedName =>
          createContentSlotView(
            pagelet,
            pagelet.slots.find(slot => slot.definitionQualifiedName === qualifiedName)
          )
        ),
    ...createContentConfigurationParameterView(pagelet.configurationParameters || {}),
  };
 
const createContentSlotView = (pagelet: ContentPagelet, slot: ContentSlot): ContentSlotView =>
  slot && {
    id: slot.definitionQualifiedName,
    domain: pagelet.domain,
    pageletIDs: slot.pageletIDs || [],
    ...createContentConfigurationParameterView(slot.configurationParameters || {}),
    displayName: slot.displayName,
  };
 
export const createContentPageletEntryPointView = (
  pageletEntryPoint: ContentPageletEntryPoint
): ContentPageletEntryPointView =>
  pageletEntryPoint && {
    id: pageletEntryPoint.id,
    domain: pageletEntryPoint.domain,
    resourceSetId: pageletEntryPoint.resourceSetId,
    displayName: pageletEntryPoint.displayName,
    pageletIDs: pageletEntryPoint.pageletIDs || [],
    ...createContentConfigurationParameterView(pageletEntryPoint.configurationParameters || {}),
    seoAttributes: pageletEntryPoint.seoAttributes,
  };