Skip to content

Commit

Permalink
feat(carousel): add input to control pause on mouseover
Browse files Browse the repository at this point in the history
Closes #1163
Closes #2436
  • Loading branch information
DheerajVislavath authored and pkozlowski-opensource committed Jun 22, 2018
1 parent 4b5d460 commit 8d54cac
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class NgbdCarouselConfig implements OnInit {
config.interval = 10000;
config.wrap = false;
config.keyboard = false;
config.mouse = false;
config.pauseOnHover = false;
}

ngOnInit() {
Expand Down
1 change: 1 addition & 0 deletions src/carousel/carousel-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ describe('ngb-carousel-config', () => {
expect(config.interval).toBe(5000);
expect(config.keyboard).toBe(true);
expect(config.wrap).toBe(true);
expect(config.pauseOnHover).toBe(true);
});
});
2 changes: 1 addition & 1 deletion src/carousel/carousel-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export class NgbCarouselConfig {
interval = 5000;
wrap = true;
keyboard = true;
mouse = true;
pauseOnHover = true;
}
37 changes: 10 additions & 27 deletions src/carousel/carousel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('ngb-carousel', () => {
expect(carousel.interval).toBe(defaultConfig.interval);
expect(carousel.wrap).toBe(defaultConfig.wrap);
expect(carousel.keyboard).toBe(defaultConfig.keyboard);
expect(carousel.mouse).toBe(defaultConfig.mouse);
expect(carousel.pauseOnHover).toBe(defaultConfig.pauseOnHover);
});

it('should render slides and navigation indicators', fakeAsync(() => {
Expand Down Expand Up @@ -322,10 +322,10 @@ describe('ngb-carousel', () => {
discardPeriodicTasks();
}));

it('should listen to mouse events based on mouse attribute', fakeAsync(() => {
it('should listen to mouse events based on pauseOnHover attribute', fakeAsync(() => {

const html = `
<ngb-carousel [mouse]='mouse'>
<ngb-carousel [pauseOnHover]="pauseOnHover">
<ng-template ngbSlide>foo</ng-template>
<ng-template ngbSlide>bar</ng-template>
</ngb-carousel>
Expand Down Expand Up @@ -353,7 +353,7 @@ describe('ngb-carousel', () => {
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [false, true]);

fixture.componentInstance.mouse = false;
fixture.componentInstance.pauseOnHover = false;
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [false, true]);

Expand All @@ -368,7 +368,7 @@ describe('ngb-carousel', () => {

it('should pause / resume slide change with time passage on mouse enter / leave', fakeAsync(() => {
const html = `
<ngb-carousel [mouse]='mouse'>
<ngb-carousel>
<ng-template ngbSlide>foo</ng-template>
<ng-template ngbSlide>bar</ng-template>
</ngb-carousel>
Expand Down Expand Up @@ -396,23 +396,6 @@ describe('ngb-carousel', () => {
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [false, true]);

fixture.componentInstance.mouse = false;
fixture.detectChanges();
carouselDebugEl.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [false, true]);

tick(6000);
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [true, false]);

carouselDebugEl.triggerEventHandler('mouseleave', {});
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [true, false]);

tick(6000);
fixture.detectChanges();
expectActiveSlides(fixture.nativeElement, [false, true]);
discardPeriodicTasks();
}));

Expand Down Expand Up @@ -543,7 +526,7 @@ describe('ngb-carousel', () => {
config.interval = 1000;
config.wrap = false;
config.keyboard = false;
config.mouse = false;
config.pauseOnHover = false;
}));

it('should initialize inputs with provided config', () => {
Expand All @@ -554,7 +537,7 @@ describe('ngb-carousel', () => {
expect(carousel.interval).toBe(config.interval);
expect(carousel.wrap).toBe(config.wrap);
expect(carousel.keyboard).toBe(config.keyboard);
expect(carousel.mouse).toBe(config.mouse);
expect(carousel.pauseOnHover).toBe(config.pauseOnHover);
});
});

Expand All @@ -563,7 +546,7 @@ describe('ngb-carousel', () => {
config.interval = 1000;
config.wrap = false;
config.keyboard = false;
config.mouse = false;
config.pauseOnHover = false;


beforeEach(() => {
Expand All @@ -579,7 +562,7 @@ describe('ngb-carousel', () => {
expect(carousel.interval).toBe(config.interval);
expect(carousel.wrap).toBe(config.wrap);
expect(carousel.keyboard).toBe(config.keyboard);
expect(carousel.mouse).toBe(config.mouse);
expect(carousel.pauseOnHover).toBe(config.pauseOnHover);
});
});

Expand All @@ -590,6 +573,6 @@ class TestComponent {
interval;
activeSlideId;
keyboard = true;
mouse = true;
pauseOnHover = true;
carouselSlideCallBack = (event: NgbSlideEvent) => {};
}
20 changes: 11 additions & 9 deletions src/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class NgbSlide {
'class': 'carousel slide',
'[style.display]': '"block"',
'tabIndex': '0',
'(mouseenter)': 'mouseEventPause()',
'(mouseleave)': 'mouseEventCycle()',
'(mouseenter)': 'onMouseEnter()',
'(mouseleave)': 'onMouseLeave()',
'(keydown.arrowLeft)': 'keyPrev()',
'(keydown.arrowRight)': 'keyNext()'
},
Expand Down Expand Up @@ -85,9 +85,11 @@ export class NgbCarousel implements AfterContentChecked,
@Input() keyboard: boolean;

/**
* A flag to enable/disable mouse events
* A flag to enable slide cycling pause/resume on mouseover.
* @since 2.2.0
*/
@Input() mouse: boolean;
@Input() pauseOnHover: boolean;

/**
* The active slide id.
*/
Expand All @@ -103,7 +105,7 @@ export class NgbCarousel implements AfterContentChecked,
this.interval = config.interval;
this.wrap = config.wrap;
this.keyboard = config.keyboard;
this.mouse = config.mouse;
this.pauseOnHover = config.pauseOnHover;
}

ngAfterContentChecked() {
Expand Down Expand Up @@ -188,14 +190,14 @@ export class NgbCarousel implements AfterContentChecked,
}
}

mouseEventPause() {
if (this.mouse) {
onMouseEnter() {
if (this.pauseOnHover) {
this.pause();
}
}

mouseEventCycle() {
if (this.mouse) {
onMouseLeave() {
if (this.pauseOnHover) {
this.cycle();
}
}
Expand Down

0 comments on commit 8d54cac

Please sign in to comment.