File

projects/rebirth-ng/src/lib/carousel/carousel.component.ts

Implements

AfterContentInit OnDestroy

Metadata

changeDetection ChangeDetectionStrategy.OnPush
exportAs carousel
selector re-carousel
templateUrl ./carousel.component.html

Index

Properties
Methods
Inputs
Outputs
HostListeners

Constructor

constructor(renderer: Renderer2, rebirthNGConfig: RebirthNGConfig)
Parameters :
Name Type Optional
renderer Renderer2 no
rebirthNGConfig RebirthNGConfig no

Inputs

activeSlide

Default value: 0

animate

Type: boolean

interval

Type: number

Outputs

activeSlideChange $event type: EventEmitter

HostListeners

keydown.arrowLeft
Arguments : '$event'
keydown.arrowLeft($event?: Event)
keydown.arrowRight
Arguments : '$event'
keydown.arrowRight($event?: Event)
mouseenter
mouseenter()
mouseleave
mouseleave()

Methods

ngAfterContentInit
ngAfterContentInit()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
onActiveSlideChange
onActiveSlideChange(index: , carouselDirection: CarouselDirection)
Parameters :
Name Type Optional
index no
carouselDirection CarouselDirection no
Returns : void
selectSlide
selectSlide(index: )
Parameters :
Name Optional
index no
Returns : void
Private slideAnimation
slideAnimation(carouselDirection: CarouselDirection, index: )
Parameters :
Name Type Optional
carouselDirection CarouselDirection no
index no
Returns : any
Private stopInterval
stopInterval()
Returns : void

Properties

animationDuration
animationDuration: number
Type : number
intervalId
intervalId: any
Type : any
reflowDuration
reflowDuration: number
Type : number
slideItems
slideItems: QueryList<ElementRef>
Type : QueryList<ElementRef>
Decorators : ViewChildren
slides
slides: QueryList<SlideDirective>
Type : QueryList<SlideDirective>
Decorators : ContentChildren
import {
  Component, ChangeDetectionStrategy, QueryList, ContentChildren, Input, Output,
  EventEmitter, HostListener, AfterContentInit, OnDestroy, Renderer2, ViewChildren, ElementRef
} from '@angular/core';
import { SlideDirective } from './slide.directive';
import { animation, animationTimeout } from '../utils/animation-utils';
import { RebirthNGConfig } from '../rebirth-ng.config';

export enum CarouselDirection {
  NEXT, PREV
}

@Component({
  selector: 're-carousel',
  templateUrl: './carousel.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  exportAs: 'carousel'
})
export class CarouselComponent implements AfterContentInit, OnDestroy {

  @Input() activeSlide = 0;
  @Input() interval: number;
  @Input() animate: boolean;
  @Output() activeSlideChange = new EventEmitter<number>();
  @ContentChildren(SlideDirective) slides: QueryList<SlideDirective>;
  @ViewChildren('slideItem') slideItems: QueryList<ElementRef>;
  intervalId: any;
  reflowDuration: number;
  animationDuration: number;

  constructor(private  renderer: Renderer2, private rebirthNGConfig: RebirthNGConfig) {
    this.reflowDuration = rebirthNGConfig.carousel.reflowDuration;
    this.animationDuration = rebirthNGConfig.carousel.animationDuration;
    this.animate = rebirthNGConfig.carousel.animate;
    this.interval = rebirthNGConfig.carousel.interval;
  }

  ngAfterContentInit(): void {
    if (this.interval) {
      this.startInterval();
    }
  }

  selectSlide(index) {
    if (this.activeSlide !== index) {
      const direction = index > this.activeSlide ? CarouselDirection.NEXT : CarouselDirection.PREV;
      this.onActiveSlideChange(index, direction);
    }
  }

  @HostListener('keydown.arrowLeft', ['$event'])
  selectPrevSlide($event?: Event) {
    if (this.activeSlide === 0) {
      this.onActiveSlideChange(this.slides.length - 1, CarouselDirection.PREV);
      return;
    }

    this.onActiveSlideChange(this.activeSlide - 1, CarouselDirection.PREV);
  }

  @HostListener('keydown.arrowRight', ['$event'])
  selectNextSlide($event?: Event) {
    if (this.activeSlide === this.slides.length - 1) {
      this.onActiveSlideChange(0, CarouselDirection.NEXT);
      return;
    }
    this.onActiveSlideChange(this.activeSlide + 1, CarouselDirection.NEXT);
  }

  onActiveSlideChange(index, carouselDirection: CarouselDirection) {
    if (this.activeSlide === index) {
      return;
    }

    this.slideAnimation(carouselDirection, index)
      .then(() => {
        this.activeSlide = index;
        this.activeSlideChange.emit(this.activeSlide);
      });
  }

  @HostListener('mouseenter', [])
  pauseInterval() {
    this.stopInterval();
  }

  @HostListener('mouseleave', [])
  startInterval() {
    this.stopInterval();
    if (this.interval) {
      this.intervalId = setInterval(() => this.selectNextSlide(), this.interval);
    }
  }

  ngOnDestroy(): void {
    this.stopInterval();
  }

  private slideAnimation(carouselDirection: CarouselDirection, index) {
    this.stopInterval();
    const slideItems = this.slideItems.toArray();
    const direction = carouselDirection === CarouselDirection.NEXT ? 'left' : 'right';
    const orderDirection = carouselDirection === CarouselDirection.NEXT ? 'next' : 'prev';

    const nextElement = slideItems[index].nativeElement;
    const currentElement = slideItems[this.activeSlide].nativeElement;
    this.renderer.addClass(nextElement, orderDirection);

    return animationTimeout(this.reflowDuration)
      .then(() => {
        this.renderer.addClass(currentElement, direction);
        this.renderer.addClass(nextElement, direction);
      })
      .then(() => animation(this.renderer, nextElement, this.animationDuration))
      .then(() => {
        this.renderer.removeClass(nextElement, orderDirection);
        this.renderer.removeClass(nextElement, direction);
        this.renderer.removeClass(currentElement, direction);
        this.startInterval();
      });
  }

  private stopInterval() {
    if (this.intervalId) {
      clearInterval(this.intervalId);
    }
  }
}
<div class="carousel slide" data-ride="carousel" tabIndex="0">
  <ol class="carousel-indicators">
    <li *ngFor="let silde of slides; let $index = index" [attr.data-slide-to]="$index"
        (click)="selectSlide($index)" [ngClass]="{active: $index == activeSlide}"></li>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div #slideItem *ngFor="let silde of slides; let $index = index"
         class="item" [ngClass]="{active: $index == activeSlide}">
      <ng-template [ngTemplateOutlet]="silde.templateRef"> </ng-template>
    </div>
  </div>

  <a class="left carousel-control" role="button" data-slide="prev" (click)="selectPrevSlide()">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" role="button" data-slide="next" (click)="selectNextSlide()">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""