File

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

Description

This class is a converter to transform category items from CmsComponent to MSCCategoryItemComponentData.

Index

Properties
Methods

Constructor

constructor(semanticPathService: SemanticPathService)
Parameters :
Name Type Optional
semanticPathService SemanticPathService No

Methods

Public convert
convert(source: CmsComponent)
Parameters :
Name Type Optional
source CmsComponent No
Private extractCategoryCode
extractCategoryCode(categoryInputComponent: FsIndexFormData)
Parameters :
Name Type Optional
categoryInputComponent FsIndexFormData No
Returns : string | undefined
Private extractCategoryIdentifier
extractCategoryIdentifier(categoryInputComponent: FsIndexFormData)
Parameters :
Name Type Optional
categoryInputComponent FsIndexFormData No
Returns : string | undefined
Private getLinkUrl
getLinkUrl(categoryCode: string | undefined)
Parameters :
Name Type Optional
categoryCode string | undefined No
Returns : [] | undefined
Private setImage
setImage(image: CmsImageMedia, imageAltTag?: string)
Parameters :
Name Type Optional
image CmsImageMedia No
imageAltTag string Yes
Returns : void
Private setLink
setLink(categoryIdentifier: string | undefined)
Parameters :
Name Type Optional
categoryIdentifier string | undefined No
Returns : any | string | undefined
Private stripCatalogFromCategoryIdentifier
stripCatalogFromCategoryIdentifier(categoryIdentifier: string)
Parameters :
Name Type Optional
categoryIdentifier string No
Returns : any

Properties

fsLinkFactory
Type : any
Private target
Type : MSCCategoryItemComponentData
import { Injectable, InjectionToken } from '@angular/core';
import { CmsComponent, Converter, SemanticPathService } from '@spartacus/core';
import { nullSafe } from 'fs-spartacus-common';
import {
  CmsImageMedia,
  MSCCategoryItemComponentData,
} from '../../components/multi-slot-container/msc-items/msc-category-item/msc-category-item.model';
import { FsIndexFormData } from '../teaser/fs-teaser.model';

/**
 * This class is a converter to transform category items from {@link CmsComponent} to {@link MSCCategoryItemComponentData}.
 *
 * @export
 * @class MSCCategoryItemConverter
 */
@Injectable({ providedIn: 'root' })
export class MSCCategoryItemConverter implements Converter<CmsComponent, CmsComponent> {
  private target: MSCCategoryItemComponentData;
  fsLinkFactory: any;
  constructor(private semanticPathService: SemanticPathService) {}

  public convert(source: CmsComponent): MSCCategoryItemComponentData {
    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.target.link = this.setLink(this.extractCategoryCode(formData.st_category));
      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 extractCategoryCode(categoryInputComponent: FsIndexFormData): string | undefined {
    const categoryIdentifier = this.extractCategoryIdentifier(categoryInputComponent);
    if (categoryIdentifier) {
      return this.stripCatalogFromCategoryIdentifier(categoryIdentifier);
    }
  }

  private extractCategoryIdentifier(categoryInputComponent: FsIndexFormData): string | undefined {
    return (
      categoryInputComponent &&
      Array.isArray(categoryInputComponent.value) &&
      categoryInputComponent.value.length &&
      categoryInputComponent.value[0].identifier
    );
  }

  private stripCatalogFromCategoryIdentifier(categoryIdentifier: string) {
    const identifierParts = categoryIdentifier.split('/').filter(Boolean);
    if (identifierParts.length === 2) {
      return identifierParts[1];
    }
  }

  private setLink(categoryIdentifier: string | undefined): any | string | undefined {
    if (categoryIdentifier !== undefined) {
      return this.getLinkUrl(categoryIdentifier);
    }
  }

  private getLinkUrl(categoryCode: string | undefined): any[] | undefined {
    if (categoryCode != null && categoryCode.length > 0) {
      return this.semanticPathService.transform({ cxRoute: 'category', params: { code: categoryCode } });
    }
  }
}
export const MSCCategoryItemConverterToken = new InjectionToken<Converter<CmsComponent, CmsComponent>>('MSCCategoryItemConverter');

results matching ""

    No results matching ""