Skip to content

Commit

Permalink
fix(carousel): restart timer when interval changed
Browse files Browse the repository at this point in the history
Fixes #1690

Closes #1702
  • Loading branch information
dmytroyarmak authored and pkozlowski-opensource committed Jul 26, 2017
1 parent ebe7f10 commit ae4e3e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/carousel/carousel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,35 @@ describe('ngb-carousel', () => {
discardPeriodicTasks();
}));

it('should change slide with different rate when interval value changed', fakeAsync(() => {
const html = `
<ngb-carousel [interval]="interval">
<ng-template ngbSlide>foo</ng-template>
<ng-template ngbSlide>bar</ng-template>
<ng-template ngbSlide>zoo</ng-template>
</ngb-carousel>
`;

const fixture = createTestComponent(html);
fixture.componentInstance.interval = 5000;
fixture.detectChanges();

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

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

fixture.componentInstance.interval = 1000;
fixture.detectChanges();

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

discardPeriodicTasks();
}));

it('should pause / resume slide change with time passage on mouse enter / leave', fakeAsync(() => {
const html = `
<ngb-carousel>
Expand Down Expand Up @@ -491,6 +520,7 @@ describe('ngb-carousel', () => {

@Component({selector: 'test-cmp', template: ''})
class TestComponent {
interval;
activeSlideId;
keyboard = true;
carouselSlideCallBack = (event: NgbSlideEvent) => {};
Expand Down
11 changes: 9 additions & 2 deletions src/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
ContentChildren,
QueryList,
Input,
OnDestroy,
AfterContentChecked,
OnInit,
OnChanges,
OnDestroy,
Output,
EventEmitter
} from '@angular/core';
Expand Down Expand Up @@ -64,7 +65,7 @@ export class NgbSlide {
`
})
export class NgbCarousel implements AfterContentChecked,
OnDestroy, OnInit {
OnDestroy, OnInit, OnChanges {
@ContentChildren(NgbSlide) slides: QueryList<NgbSlide>;
private _slideChangeInterval;

Expand Down Expand Up @@ -107,6 +108,12 @@ export class NgbCarousel implements AfterContentChecked,

ngOnInit() { this._startTimer(); }

ngOnChanges(changes) {
if ('interval' in changes && !changes['interval'].isFirstChange()) {
this._restartTimer();
}
}

ngOnDestroy() { clearInterval(this._slideChangeInterval); }

/**
Expand Down

0 comments on commit ae4e3e9

Please sign in to comment.