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

93.54% Statements 29/31
75% Branches 12/16
100% Functions 9/9
92.85% Lines 26/28

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 75106x 106x   106x                 106x   11x     11x 11x       20x   20x 12x 23x 11x     20x         23x     11x 8x   9x       12x     12x             17x 8x       9x         9x 4x       5x 5x      
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
 
import { getCurrentLocale, getICMStaticURL } from 'ish-core/store/core/configuration';
 
import { ContentConfigurationParameterData } from './content-configuration-parameter.interface';
 
export interface ContentConfigurationParameters {
  [key: string]: string | object | number;
}
 
@Injectable({ providedIn: 'root' })
export class ContentConfigurationParameterMapper {
  private staticURL: string;
  private lang = '-';
 
  constructor(store: Store) {
    store.pipe(select(getICMStaticURL)).subscribe(url => (this.staticURL = url));
    store.pipe(select(getCurrentLocale)).subscribe(lang => (this.lang = lang || '-'));
  }
 
  fromData(data: { [name: string]: ContentConfigurationParameterData }): ContentConfigurationParameters {
    let configurationParameters = {};
 
    if (data) {
      configurationParameters = Object.entries(data)
        .map(([key, value]) => ({ [key]: this.postProcessData(value) }))
        .reduce((acc, val) => ({ ...acc, ...val }));
    }
 
    return configurationParameters;
  }
 
  // post process the configuration parameter data to apply special handling for specific types
  private postProcessData(data: ContentConfigurationParameterData): string | object | number {
    switch (data.type) {
      case 'bc_pmc:types.pagelet2-ImageFileRef':
      case 'bc_pmc:types.pagelet2-FileRef':
        if (Array.isArray(data.value)) {
          return data.value.map(x => this.processFileReferences(x));
        } else {
          return this.processFileReferences(data.value.toString());
        }
      default:
        // parse values of configuration parameters that end in 'JSON' to JSON objects
        Iif (data.definitionQualifiedName.endsWith('JSON')) {
          return JSON.parse(data.value as string);
        }
        return data.value;
    }
  }
 
  // process file reference values according to their type
  private processFileReferences(value: string): string {
    // absolute URL references - keep them as they are (http:// and https://)
    if (value.startsWith('http')) {
      return value;
    }
 
    // relative URL references, e.g. to asset files are prefixed with 'file://'
    Iif (value.startsWith('file://')) {
      return value.split('file://')[1];
    }
 
    // everything else that does not include ':/' is not an ICM file reference and is left as it is
    if (!value.includes(':/')) {
      return value;
    }
 
    // convert ICM file references to full server URLs
    const split = value.split(':');
    return encodeURI(`${this.staticURL}/${split[0]}/${this.lang}${split[1]}`);
  }
}