src/converter/teaser/fs-teaser-category-link-overlay.ts
This class is a factory to create category links from a CategoryLinkTemplate.
FsTeaserOverlayFactoryInterface
Methods |
|
constructor(area: CategoryLinkTemplate, positionAndDimension, semanticPathService: SemanticPathService)
|
||||||||||||
Parameters :
|
Public create |
create()
|
Returns :
OverlayLink
|
Private createCategoryLinkOverlay | |||||||||
createCategoryLinkOverlay(categoryCode: string, ltTooltipText: CmsInputTextFormData)
|
|||||||||
Parameters :
Returns :
OverlayLink
|
Private extractCategoryCode | ||||||
extractCategoryCode(categoryInputComponent: FsIndexFormData)
|
||||||
Parameters :
Returns :
string | undefined
|
Private extractCategoryIdentifier | ||||||
extractCategoryIdentifier(categoryInputComponent: FsIndexFormData)
|
||||||
Parameters :
Returns :
string | undefined
|
Private getLinkUrl | ||||||
getLinkUrl(categoryCode: string | undefined)
|
||||||
Parameters :
Returns :
[] | undefined
|
Private stripCatalogFromCategoryIdentifier | ||||||
stripCatalogFromCategoryIdentifier(categoryIdentifier: string)
|
||||||
Parameters :
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 } });
}
}
}