Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

feat(carousel): use $interval instead of $timeout #1630

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/carousel/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*
*/
angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
.controller('CarouselController', ['$scope', '$timeout', '$transition', function ($scope, $timeout, $transition) {
.controller('CarouselController', ['$scope', '$timeout', '$interval', '$transition', function ($scope, $timeout, $interval, $transition) {
var self = this,
slides = self.slides = [],
currentIndex = -1,
currentTimeout, isPlaying;
currentInterval,
isPlaying;
self.currentSlide = null;

var destroyed = false;
Expand Down Expand Up @@ -61,7 +62,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
self.currentSlide = nextSlide;
currentIndex = nextIndex;
//every time you change slides, reset the timer
restartTimer();
restartInterval();
}
function transitionDone(next, current) {
angular.extend(next, {direction: '', active: true, leaving: false, entering: false});
Expand Down Expand Up @@ -108,28 +109,27 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
return slides;
};

$scope.$watch('interval', restartTimer);
$scope.$on('$destroy', resetTimer);
$scope.$watch('interval', restartInterval);
$scope.$on('$destroy', stopInterval);

function restartTimer() {
resetTimer();
function restartInterval() {
stopInterval();
var interval = +$scope.interval;
if (!isNaN(interval) && interval>=0) {
currentTimeout = $timeout(timerFn, interval);
currentInterval = $interval(intervalFn, interval);
}
}

function resetTimer() {
if (currentTimeout) {
$timeout.cancel(currentTimeout);
currentTimeout = null;
function stopInterval() {
if (currentInterval) {
$interval.cancel(currentInterval);
currentInterval = null;
}
}

function timerFn() {
function intervalFn() {
if (isPlaying) {
$scope.next();
restartTimer();
} else {
$scope.pause();
}
Expand All @@ -138,13 +138,13 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
$scope.play = function() {
if (!isPlaying) {
isPlaying = true;
restartTimer();
restartInterval();
}
};
$scope.pause = function() {
if (!$scope.noPause) {
isPlaying = false;
resetTimer();
stopInterval();
}
};

Expand Down
40 changes: 20 additions & 20 deletions src/carousel/test/carousel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ describe('carousel', function() {
beforeEach(module('ui.bootstrap.carousel'));
beforeEach(module('template/carousel/carousel.html', 'template/carousel/slide.html'));

var $rootScope, $compile, $controller, $timeout;
beforeEach(inject(function(_$rootScope_, _$compile_, _$controller_, _$timeout_) {
var $rootScope, $compile, $controller, $interval;
beforeEach(inject(function(_$rootScope_, _$compile_, _$controller_, _$interval_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$controller = _$controller_;
$timeout = _$timeout_;
$interval = _$interval_;
}));

describe('basics', function() {
Expand Down Expand Up @@ -126,12 +126,12 @@ describe('carousel', function() {
//no timeout to flush, interval watch doesn't make a new one when interval is invalid
testSlideActive(0);
scope.$apply('interval = 1000');
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(1);
scope.$apply('interval = false');
testSlideActive(1);
scope.$apply('interval = 1000');
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(2);
});

Expand All @@ -156,34 +156,34 @@ describe('carousel', function() {

it('should be playing by default and cycle through slides', function() {
testSlideActive(0);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(1);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(2);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(0);
});

it('should pause and play on mouseover', function() {
testSlideActive(0);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(1);
elm.trigger('mouseenter');
expect($timeout.flush).toThrow();//pause should cancel current timeout
expect($interval.flush).toThrow();//pause should cancel current timeout
testSlideActive(1);
elm.trigger('mouseleave');
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(2);
});

it('should not pause on mouseover if noPause', function() {
scope.$apply('nopause = true');
testSlideActive(0);
elm.trigger('mouseenter');
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(1);
elm.trigger('mouseleave');
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(2);
});

Expand All @@ -193,7 +193,7 @@ describe('carousel', function() {
scope.$apply('slides.splice(0,1)');
expect(elm.find('div.item').length).toBe(2);
testSlideActive(1);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(0);
scope.$apply('slides.splice(1,1)');
expect(elm.find('div.item').length).toBe(1);
Expand Down Expand Up @@ -227,14 +227,14 @@ describe('carousel', function() {

it('issue 1414 - should not continue running timers after scope is destroyed', function() {
testSlideActive(0);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(1);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(2);
$timeout.flush();
$interval.flush(scope.interval);
testSlideActive(0);
scope.$destroy();
expect($timeout.flush).toThrow('No deferred tasks to be flushed');
expect($interval.flush).toThrow('No deferred tasks to be flushed');
});

});
Expand Down Expand Up @@ -300,12 +300,12 @@ describe('carousel', function() {
scope.interval = 2000;
scope.$digest();

$timeout.flush();
$interval.flush();
expect(scope.next.calls.length).toBe(1);

scope.$destroy();

$timeout.flush(scope.interval);
$interval.flush(scope.interval);
expect(scope.next.calls.length).toBe(1);
});
});
Expand Down