coreui-angular-ex-dev
Version:
CoreUI Components Library for Angular
94 lines (78 loc) • 2.16 kB
text/typescript
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
HostBinding,
HostListener,
Input,
ViewChild
} from '@angular/core';
import { NgIf } from '@angular/common';
import { CarouselState } from '../carousel-state';
export class CarouselControlComponent implements AfterViewInit {
constructor(
private changeDetectorRef: ChangeDetectorRef,
private carouselState: CarouselState
) {}
private _caption?: string;
/**
* Carousel control caption. [docs]
* @type string
*/
set caption(value) {
this._caption = value;
}
get caption(): string {
return !!this._caption ? this._caption : this.direction === 'prev' ? 'Previous' : 'Next';
}
/**
* Carousel control direction. [docs]
* @type {'next' | 'prev'}
*/
direction: 'prev' | 'next' = 'next';
get hostRole(): string {
return 'button';
}
get hostClasses(): string {
return `carousel-control-${this.direction}`;
}
get carouselControlIconClass(): string {
return `carousel-control-${this.direction}-icon`;
}
content?: ElementRef;
hasContent = true;
onKeyUp($event: KeyboardEvent): void {
if ($event.key === 'Enter') {
this.play();
}
if ($event.key === 'ArrowLeft') {
this.play('prev');
}
if ($event.key === 'ArrowRight') {
this.play('next');
}
}
public onClick($event: MouseEvent): void {
this.play();
}
ngAfterViewInit(): void {
this.hasContent = this.content?.nativeElement.childNodes.length ?? false;
this.changeDetectorRef.detectChanges();
}
private play(direction = this.direction): void {
const nextIndex = this.carouselState.direction(direction);
this.carouselState.state = { activeItemIndex: nextIndex };
}
}