Skip to content

Commit 8ecaacb

Browse files
committed
refactor(carousel): add ListenersService and missing pause prop, add IntersectionService
1 parent e02f751 commit 8ecaacb

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

projects/coreui-angular/src/lib/carousel/carousel/carousel.component.ts

+52-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
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';
1313
import { Subscription } from 'rxjs';
1414

15+
import { IntersectionService } from '../intersection.service';
16+
import { IListenersConfig, ListenersService } from '../../services/listeners.service';
17+
1518
import { CarouselState } from '../carousel-state';
1619
import { CarouselService } from '../carousel.service';
1720
import { 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
})
2529
export 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
}

projects/coreui-angular/src/lib/services/listeners.service.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Component, DebugElement, Renderer2, Type } from '@angular/core';
1+
import { Renderer2 } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
33

4-
54
import { ListenersService } from './listeners.service';
65

76
describe('ListenersService', () => {

0 commit comments

Comments
 (0)