1
1
import {
2
2
AfterContentInit ,
3
3
Component ,
4
+ ElementRef ,
4
5
EventEmitter ,
5
6
HostBinding ,
6
- HostListener ,
7
7
Inject ,
8
8
Input ,
9
9
OnDestroy ,
@@ -12,15 +12,19 @@ import {
12
12
} from '@angular/core' ;
13
13
import { Subscription } from 'rxjs' ;
14
14
15
+ import { IntersectionService } from '../intersection.service' ;
16
+ import { IListenersConfig , ListenersService } from '../../services/listeners.service' ;
17
+
15
18
import { CarouselState } from '../carousel-state' ;
16
19
import { CarouselService } from '../carousel.service' ;
17
20
import { CarouselConfig } from '../carousel.config' ;
21
+ import { Triggers } from '../../coreui.types' ;
18
22
19
23
@Component ( {
20
24
selector : 'c-carousel' ,
21
- template : ` <ng-content></ng-content>` ,
25
+ template : ' <ng-content></ng-content>' ,
22
26
styleUrls : [ './carousel.component.scss' ] ,
23
- providers : [ CarouselService , CarouselState , CarouselConfig ]
27
+ providers : [ CarouselService , CarouselState , CarouselConfig , IntersectionService , ListenersService ]
24
28
} )
25
29
export class CarouselComponent implements OnInit , OnDestroy , AfterContentInit {
26
30
/**
@@ -46,8 +50,14 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
46
50
/**
47
51
* The amount of time to delay between automatically cycling an item. If false, carousel will not automatically cycle.
48
52
* @type number
53
+ * @default 0
49
54
*/
50
55
@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' ;
51
61
/**
52
62
* Set type of the transition.
53
63
* @type {'slide' | 'crossfade' }
@@ -72,24 +82,16 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
72
82
73
83
private carouselIndexSubscription ?: Subscription ;
74
84
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 ;
87
86
private activeItemInterval = 0 ;
88
87
89
88
constructor (
90
89
@Inject ( CarouselConfig ) private config : CarouselConfig ,
90
+ private hostElement : ElementRef ,
91
91
private carouselService : CarouselService ,
92
- private carouselState : CarouselState
92
+ private carouselState : CarouselState ,
93
+ private intersectionService : IntersectionService ,
94
+ private listenersService : ListenersService
93
95
) {
94
96
Object . assign ( this , config ) ;
95
97
}
@@ -99,11 +101,34 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
99
101
}
100
102
101
103
ngOnDestroy ( ) : void {
104
+ this . clearListeners ( ) ;
102
105
this . carouselStateSubscribe ( false ) ;
106
+ this . intersectionServiceSubscribe ( false ) ;
103
107
}
104
108
105
109
ngAfterContentInit ( ) : void {
110
+ this . intersectionService . createIntersectionObserver ( this . hostElement ) ;
111
+ this . intersectionServiceSubscribe ( ) ;
106
112
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 ( ) ;
107
132
}
108
133
109
134
set visible ( value ) {
@@ -142,4 +167,15 @@ export class CarouselComponent implements OnInit, OnDestroy, AfterContentInit {
142
167
this . carouselIndexSubscription ?. unsubscribe ( ) ;
143
168
}
144
169
}
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
+ }
145
181
}
0 commit comments