src/converter/link-factory/fs-category-link.ts
This class represents a link and allows the creation of category links.
Methods |
|
constructor(link: FsCategoryLinkTemplate, semanticPathService: SemanticPathService)
|
|||||||||
Parameters :
|
Public create |
create()
|
Returns :
LinkData
|
Private createCategoryLink | |||||||||
createCategoryLink(categoryCode: string, ltTooltipText: CmsInputTextFormData)
|
|||||||||
Parameters :
Returns :
LinkData
|
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 { SemanticPathService } from '@spartacus/core';
import { nullSafe } from 'fs-spartacus-common';
import { CmsInputTextFormData, FsIndexFormData, LinkData } from '../teaser/fs-teaser.model';
import { FsCategoryLinkTemplate, FsLinkFactoryInterface } from './fs-link.model';
/**
* This class represents a link and allows the creation of category links.
*
* @export
* @class FsCategoryLink
*/
export class FsCategoryLink implements FsLinkFactoryInterface<LinkData> {
constructor(private link: FsCategoryLinkTemplate, private semanticPathService: SemanticPathService) {}
public create(): LinkData {
if (this.link != null && this.link.formData != null) {
const { lt_category, lt_tooltip_text } = this.link.formData;
const categoryCode = this.extractCategoryCode(lt_category);
if (categoryCode) {
return this.createCategoryLink(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 createCategoryLink(categoryCode: string, ltTooltipText: CmsInputTextFormData): LinkData {
return {
href: this.getLinkUrl(categoryCode),
tooltip: nullSafe(ltTooltipText?.value, ''),
};
}
private getLinkUrl(categoryCode: string | undefined): any[] | undefined {
if (categoryCode != null && categoryCode.length > 0) {
return this.semanticPathService.transform({ cxRoute: 'category', params: { code: categoryCode } });
}
}
}