File

src/converter/multi-slot-container-converter/msc-content-item-converter.ts

Description

This class is a converter to transform content items from CmsComponent to MSCContentItemComponentData.

Index

Properties
Methods

Constructor

constructor(fsLinkFactory: FsLinkFactory)
Parameters :
Name Type Optional
fsLinkFactory FsLinkFactory No

Methods

Public convert
convert(source: CmsComponent)
Parameters :
Name Type Optional
source CmsComponent No
Private setImage
setImage(image: CmsImageMedia, imageAltTag?: string)
Parameters :
Name Type Optional
image CmsImageMedia No
imageAltTag string Yes
Returns : void
Private setLink
setLink(linkData: FsSearchLinkTemplate | FsProductLinkTemplate | FsCategoryLinkTemplate | FsContentLinkTemplate | null)
Parameters :
Name Type Optional
linkData FsSearchLinkTemplate | FsProductLinkTemplate | FsCategoryLinkTemplate | FsContentLinkTemplate | null No
Returns : void

Properties

Private target
Type : MSCContentItemComponentData
import { Injectable, InjectionToken } from '@angular/core';
import { CmsComponent, Converter } from '@spartacus/core';
import { nullSafe } from 'fs-spartacus-common';
import {
  CmsImageMedia,
  MSCContentItemComponentData,
} from '../../components/multi-slot-container/msc-items/msc-content-item/msc-content-item.model';
import { FsLinkFactory } from '../link-factory/fs-link-factory';
import { FsCategoryLinkTemplate, FsContentLinkTemplate, FsProductLinkTemplate, FsSearchLinkTemplate } from '../link-factory/fs-link.model';

/**
 * This class is a converter to transform content items from {@link CmsComponent} to {@link MSCContentItemComponentData}.
 *
 * @export
 * @class MSCContentItemConverter
 */
@Injectable({ providedIn: 'root' })
export class MSCContentItemtConverter implements Converter<CmsComponent, CmsComponent> {
  private target: MSCContentItemComponentData;
  constructor(private fsLinkFactory: FsLinkFactory) {}

  public convert(source: CmsComponent): MSCContentItemComponentData {
    this.target = {};
    const { otherProperties } = source;
    const { formData } = nullSafe(otherProperties, {});

    if (source.otherProperties != null) {
      this.target.previewId = otherProperties.previewId;
      this.setImage(formData.st_picture?.value, formData.st_altTag?.value);
      this.target.headline = nullSafe(formData.st_headline?.value, '');
      this.target.subline = nullSafe(formData.st_subline?.value, '');
      this.target.text = nullSafe(formData.st_text?.value, '');
      this.setLink(formData.st_link?.value);
      this.target.buttonText = nullSafe(formData.st_linkText?.value, '');
    }
    return this.target;
  }

  private setImage(image: CmsImageMedia, imageAltTag?: string): void {
    this.target.image = {
      url: nullSafe(image?.resolutions?.resolutionsMetaData?.ORIGINAL?.url, null),
      altTag: nullSafe(imageAltTag, ''),
    };
  }

  private setLink(linkData: FsSearchLinkTemplate | FsProductLinkTemplate | FsCategoryLinkTemplate | FsContentLinkTemplate | null): void {
    if (linkData != null && linkData.template && linkData.template.uid) {
      this.target.link = this.fsLinkFactory.createUrl(linkData.template.uid, linkData);
    }
  }
}

export const MSCContentItemConverterToken = new InjectionToken<Converter<CmsComponent, CmsComponent>>('MSCContentItemConverter');

results matching ""

    No results matching ""