File

src/components/carousel/carousel.component.ts

Description

This component shows teasers in a carousel.

Implements

OnDestroy

Metadata

Index

Properties
Methods

Constructor

constructor(componentData: CmsComponentData<CarouselComponentData>, changeDetectorRef: ChangeDetectorRef, ngZone: NgZone)
Parameters :
Name Type Optional
componentData CmsComponentData<CarouselComponentData> No
changeDetectorRef ChangeDetectorRef No
ngZone NgZone No

Methods

ngOnDestroy
ngOnDestroy()
Returns : void

Properties

carouselData
Type : CarouselDataInterface
Default value : null
Public componentData
Type : CmsComponentData<CarouselComponentData>
sub$
Type : Subscription
import { CmsComponentData } from '@spartacus/storefront';
import { Component, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core';
import { CarouselComponentData, CarouselDataInterface } from './carousel.model';
import { of, Subscription } from 'rxjs';
import { TeaserComponentData } from '../../converter/teaser/fs-teaser.model';
import { nullSafe } from 'fs-spartacus-common';


/**
 * This component shows teasers in a carousel.
 *
 * @export
 * @class CarouselComponent
 */
@Component({
    selector: 'fs-carousel',
    templateUrl: './carousel.component.html',
    styleUrls: ['./carousel.component.scss'],
    standalone: false
})
export class CarouselComponent implements OnDestroy {
  carouselData: CarouselDataInterface = null;
  sub$: Subscription;

  constructor(
    public componentData: CmsComponentData<CarouselComponentData>,
    private changeDetectorRef: ChangeDetectorRef,
    private ngZone: NgZone
  ) {
    this.sub$ = this.componentData.data$.subscribe((data) => {
      this.ngZone.runTask(() => {
        const { carouselElements: teaserElements, ...carouselComponentData } = nullSafe(data, {});
        this.carouselData = Object.assign(
          {},
          {
            ...carouselComponentData,
            carouselElements: of(
              teaserElements?.map((carouselElement, index) => {
                const teaserComponentData: CmsComponentData<TeaserComponentData> = {
                  data$: of({
                    ...carouselElement,
                    previewId: `${carouselComponentData.previewId}/st_carouselItems/${index}`
                  }),
                  uid: carouselElement?.typeCode,
                };
                return of(teaserComponentData);
              })
            ),
          }
        );
        this.changeDetectorRef.markForCheck();
      });
    });
  }

  ngOnDestroy(): void {
    if (this.sub$) {
      this.sub$.unsubscribe();
    }
  }
}
<ng-container *ngIf="carouselData">
  <div class="fs-carousel-container" [attr.data-preview-id]="carouselData.previewId">
    <cx-carousel
      [items]="carouselData.carouselElements | async"
      [template]="teaserElementTemplate"
      itemWidth="100%"
      [autocycleOn]="carouselData.autoplay"
      [autocycleInterval]="carouselData.autoplaySpeed"
      [hideNavigation]="true"
    >
    </cx-carousel>
  </div>
</ng-container>

<ng-template #teaserElementTemplate let-item="item">
  <fs-teaser [data$]="item"></fs-teaser>
</ng-template>

./carousel.component.scss

.fs-carousel-container {
  display: block;
  position: relative;
  overflow: hidden;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""