11import {
22 AfterContentInit ,
33 Component ,
4+ ElementRef ,
45 EventEmitter ,
56 HostBinding ,
6- HostListener ,
77 Inject ,
88 Input ,
99 OnDestroy ,
@@ -12,15 +12,19 @@ import {
1212} from '@angular/core' ;
1313import { Subscription } from 'rxjs' ;
1414
15+ import { IntersectionService } from '../intersection.service' ;
16+ import { IListenersConfig , ListenersService } from '../../services/listeners.service' ;
17+
1518import { CarouselState } from '../carousel-state' ;
1619import { CarouselService } from '../carousel.service' ;
1720import { CarouselConfig } from '../carousel.config' ;
21+ import { Triggers } from '../../coreui.types' ;
1822
1923@Component ( {
2024 selector : 'c-carousel' ,
21- template : ` <ng-content></ng-content>` ,
25+ template : ' <ng-content></ng-content>' ,
2226 styleUrls : [ './carousel.component.scss' ] ,
23- providers : [ CarouselService , CarouselState , CarouselConfig ]
27+ providers : [ CarouselService , CarouselState , CarouselConfig , IntersectionService , ListenersService ]
2428} )
2529export class CarouselComponent implements OnInit , OnDestroy , AfterContentInit {
2630 /**
@@ -46,8 +50,14 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
4650 /**
4751 * The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.
4852 * @type number
53+ * @default 0
4954 */
5055 @Input ( ) interval = 0 ;
56+ /**
57+ * Sets which event handlers you’d like provided to your pause prop. You can specify one trigger or an array of them.
58+ * @type {'hover' | 'focus' | 'click' }
59+ */
60+ @Input ( ) pause : Triggers | Triggers [ ] | false = 'hover' ;
5161 /**
5262 * Set type of the transition.
5363 * @type {'slide' | 'crossfade' }
@@ -72,24 +82,16 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
7282
7383 private carouselIndexSubscription ?: Subscription ;
7484 private timerId ! : any ;
75-
76- @HostListener ( 'mouseenter' , [ '$event' ] )
77- @HostListener ( 'mousedown' , [ '$event' ] )
78- public onMouseenter ( $event : MouseEvent ) : void {
79- this . resetTimer ( ) ;
80- }
81-
82- @HostListener ( 'mouseleave' , [ '$event' ] )
83- @HostListener ( 'mouseup' , [ '$event' ] )
84- public onMouseleave ( $event : MouseEvent ) : void {
85- this . setTimer ( ) ;
86- }
85+ private intersectingSubscription ?: Subscription ;
8786 private activeItemInterval = 0 ;
8887
8988 constructor (
9089 @Inject ( CarouselConfig ) private config : CarouselConfig ,
90+ private hostElement : ElementRef ,
9191 private carouselService : CarouselService ,
92- private carouselState : CarouselState
92+ private carouselState : CarouselState ,
93+ private intersectionService : IntersectionService ,
94+ private listenersService : ListenersService
9395 ) {
9496 Object . assign ( this , config ) ;
9597 }
@@ -99,11 +101,34 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
99101 }
100102
101103 ngOnDestroy ( ) : void {
104+ this . clearListeners ( ) ;
102105 this . carouselStateSubscribe ( false ) ;
106+ this . intersectionServiceSubscribe ( false ) ;
103107 }
104108
105109 ngAfterContentInit ( ) : void {
110+ this . intersectionService . createIntersectionObserver ( this . hostElement ) ;
111+ this . intersectionServiceSubscribe ( ) ;
106112 this . carouselState . state = { activeItemIndex : this . activeIndex , animate : this . animate } ;
113+ this . setListeners ( ) ;
114+ }
115+
116+ private setListeners ( ) : void {
117+ const config : IListenersConfig = {
118+ hostElement : this . hostElement ,
119+ trigger : this . pause || [ ] ,
120+ callbackOff : ( ) => {
121+ this . setTimer ( ) ;
122+ } ,
123+ callbackOn : ( ) => {
124+ this . resetTimer ( ) ;
125+ }
126+ } ;
127+ this . listenersService . setListeners ( config ) ;
128+ }
129+
130+ private clearListeners ( ) : void {
131+ this . listenersService . clearListeners ( ) ;
107132 }
108133
109134 set visible ( value ) {
@@ -142,4 +167,15 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
142167 this . carouselIndexSubscription ?. unsubscribe ( ) ;
143168 }
144169 }
170+
171+ private intersectionServiceSubscribe ( subscribe : boolean = true ) : void {
172+ if ( subscribe ) {
173+ this . intersectingSubscription = this . intersectionService . intersecting$ . subscribe ( isIntersecting => {
174+ this . visible = isIntersecting ;
175+ isIntersecting ? this . setTimer ( ) : this . resetTimer ( ) ;
176+ } ) ;
177+ } else {
178+ this . intersectingSubscription ?. unsubscribe ( ) ;
179+ }
180+ }
145181}
0 commit comments