File

src/converter/carousel/fs-carousel-converter.ts

Description

This interface defines a converter to transform from CmsComponent to CarouselComponentData.

Extends

Converter

import { Injectable, InjectionToken } from '@angular/core';
import { CmsComponent, Converter } from '@spartacus/core';
import { CarouselComponentData } from '../../components/carousel/carousel.model';

/**
 * This interface defines a converter to transform from {@link CmsComponent} to {@link CarouselComponentData}.
 */
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FsCarouselConverterInterface extends Converter<CmsComponent, CarouselComponentData> {}

/**
 * This is an injection token for the {@link FsCarouselConverter}.
 */
export const FsCarouselConverterInjectionToken = new InjectionToken<Converter<CmsComponent, CmsComponent>>('FsCarouselConverter');

/**
 * This converter class for the {@link CarouselComponent} transform the model received from the CMS to a carousel configuration.
 *
 * @export
 * @class FsCarouselConverter
 */
@Injectable({ providedIn: 'root' })
export class FsCarouselConverter implements FsCarouselConverterInterface {

  public convert(source: CmsComponent): CarouselComponentData {
    const { otherProperties } = source;
    const { formData } = otherProperties;

    return {
      uid: source.uid,
      typeCode: source.typeCode,
      previewId: otherProperties.previewId,
      autoplay: formData.st_autoplayEnabled.value,
      autoplaySpeed: this.toValidAutoplaySpeed(formData.st_autoplaySpeed.value),
      carouselElements: formData.st_carouselItems.value || [],
    } as CarouselComponentData;
  }

  /**
   * This method transforms the given value to a valid number that is used as the autoplay speed.
   * For this, it first ensures the number to stay between 1 and 1800 and then converts it to milliseconds.
   *
   * @param value Value to validate in seconds.
   * @return The validated value in milliseconds.
   */
  toValidAutoplaySpeed(value: number): number {
    return Math.min(1800, Math.max(isNaN(value) ? 1 : value, 1)) * 1000;
  }
}

results matching ""

    No results matching ""