All files / src/app/core/models/content-pagelet content-pagelet.mapper.ts

96.87% Statements 31/32
69.23% Branches 9/13
100% Functions 11/11
96.55% Lines 28/29

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 8014x   14x               14x 7x     11x 1x     10x 10x   10x 10x 10x 4x 5x   5x 5x     10x                           5x       5x 5x 5x   4x 5x   5x             5x     4x 4x   5x             23x      
import { Injectable } from '@angular/core';
 
import { ContentConfigurationParameterMapper } from 'ish-core/models/content-configuration-parameter/content-configuration-parameter.mapper';
import { ContentSlotData } from 'ish-core/models/content-slot/content-slot.interface';
import { ContentSlot } from 'ish-core/models/content-slot/content-slot.model';
 
import { ContentPageletData } from './content-pagelet.interface';
import { ContentPagelet } from './content-pagelet.model';
 
@Injectable({ providedIn: 'root' })
export class ContentPageletMapper {
  constructor(private contentConfigurationParameterMapper: ContentConfigurationParameterMapper) {}
 
  fromData(data: ContentPageletData, context?: string): ContentPagelet[] {
    if (!data) {
      throw new Error('falsy input');
    }
 
    const { definitionQualifiedName, id, domain, displayName } = data;
    const configurationParameters = this.contentConfigurationParameterMapper.fromData(data.configurationParameters);
 
    let slots: ContentSlot[] = [];
    let pagelets: ContentPagelet[] = [];
    if (data.slots) {
      const deep = Object.values(data.slots).map((slot, index) =>
        this.fromSlotData(slot, this.appendContext(`$${index}@${id}`, context))
      );
      slots = deep.map(val => val.slot);
      pagelets = deep.map(val => val.pagelets).reduce((acc, val) => [...acc, ...val]);
    }
 
    return [
      {
        configurationParameters,
        definitionQualifiedName,
        id: this.appendContext(id, context),
        slots,
        domain,
        displayName,
      },
      ...pagelets,
    ];
  }
 
  private fromSlotData(data: ContentSlotData, context: string): { slot: ContentSlot; pagelets: ContentPagelet[] } {
    Iif (!data) {
      throw new Error('falsy input');
    }
 
    const definitionQualifiedName = data.definitionQualifiedName;
    const configurationParameters = this.contentConfigurationParameterMapper.fromData(data.configurationParameters);
    const pageletIDs = !data.pagelets
      ? []
      : data.pagelets.map((pagelet, index) => this.appendContext(`${pagelet.id}#${index}`, context));
    const displayName = data.displayName;
 
    const slot: ContentSlot = {
      definitionQualifiedName,
      configurationParameters,
      pageletIDs,
      displayName,
    };
 
    const pagelets = !data.pagelets
      ? []
      : data.pagelets
          .map((pagelet, index) => this.fromData(pagelet, this.appendContext(`#${index}`, context)))
          .reduce((acc, val) => [...acc, ...val], []);
 
    return { slot, pagelets };
  }
 
  /**
   * Append the context string to the id if given (needed for normalizing of component templates).
   */
  private appendContext(id: string, context?: string): string {
    return context ? `${id}${context}` : id;
  }
}