File

src/converter/teaser/fs-teaser-category-link-overlay.ts

Description

This class is a factory to create category links from a CategoryLinkTemplate.

Implements

FsTeaserOverlayFactoryInterface

Index

Methods

Constructor

constructor(area: CategoryLinkTemplate, positionAndDimension, semanticPathService: SemanticPathService)
Parameters :
Name Type Optional
area CategoryLinkTemplate No
positionAndDimension No
semanticPathService SemanticPathService No

Methods

Public create
create()
Returns : OverlayLink
Private createCategoryLinkOverlay
createCategoryLinkOverlay(categoryCode: string, ltTooltipText: CmsInputTextFormData)
Parameters :
Name Type Optional
categoryCode string No
ltTooltipText CmsInputTextFormData No
Returns : OverlayLink
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 stripCatalogFromCategoryIdentifier
stripCatalogFromCategoryIdentifier(categoryIdentifier: string)
Parameters :
Name Type Optional
categoryIdentifier string No
Returns : any
import {
  CategoryLinkTemplate,
  CmsInputTextFormData,
  FsIndexFormData,
  FsTeaserOverlayFactoryInterface,
  OverlayDimension,
  OverlayLink,
  OverlayPosition,
} from './fs-teaser.model';
import { nullSafe } from 'fs-spartacus-common';
import { SemanticPathService } from '@spartacus/core';

/**
 * This class is a factory to create category links from a {@link CategoryLinkTemplate}.
 *
 * @export
 * @class FsTeaserCategoryLinkOverlay
 */
export class FsTeaserCategoryLinkOverlay implements FsTeaserOverlayFactoryInterface<OverlayLink> {
  constructor(
    private area: CategoryLinkTemplate,
    private positionAndDimension: OverlayPosition & OverlayDimension,
    private semanticPathService: SemanticPathService
  ) {}

  public create(): OverlayLink {
    if (this.area.link != null && this.area.link.formData != null) {
      const { lt_category, lt_tooltip_text } = this.area.link.formData;
      const categoryCode = this.extractCategoryCode(lt_category);
      if (categoryCode) {
        return this.createCategoryLinkOverlay(categoryCode, lt_tooltip_text);
      }
    }
  }

  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 createCategoryLinkOverlay(categoryCode: string, ltTooltipText: CmsInputTextFormData): OverlayLink {
    return {
      href: this.getLinkUrl(categoryCode),
      tooltip: nullSafe(ltTooltipText?.value, ''),
      ...this.positionAndDimension,
    };
  }

  private getLinkUrl(categoryCode: string | undefined): any[] | undefined {
    if (categoryCode != null && categoryCode.length > 0) {
      return this.semanticPathService.transform({ cxRoute: 'category', params: { code: categoryCode } });
    }
  }
}

results matching ""

    No results matching ""