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

Commit 431b9c7

Browse files
committed
refactor(carousel): use ngAnimate for animation
Fixes #1350 Fixes #1513 Fixes #1185 Fixes #2062 Fixes #2235 Closes #2067
1 parent d03543e commit 431b9c7

File tree

3 files changed

+70
-40
lines changed

3 files changed

+70
-40
lines changed

Diff for: src/carousel/carousel.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.ng-animate.item:not(.left):not(.right) {
2+
-webkit-transition: 0s ease-in-out left;
3+
transition: 0s ease-in-out left
4+
}

Diff for: src/carousel/carousel.js

+65-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*/
99
angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
10-
.controller('CarouselController', ['$scope', '$timeout', '$interval', '$transition', function ($scope, $timeout, $interval, $transition) {
10+
.controller('CarouselController', ['$scope', '$interval', '$animate', function ($scope, $interval, $animate) {
1111
var self = this,
1212
slides = self.slides = $scope.slides = [],
1313
currentIndex = -1,
@@ -23,51 +23,30 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition'])
2323
direction = nextIndex > currentIndex ? 'next' : 'prev';
2424
}
2525
if (nextSlide && nextSlide !== self.currentSlide) {
26-
if ($scope.$currentTransition) {
27-
$scope.$currentTransition.cancel();
28-
//Timeout so ng-class in template has time to fix classes for finished slide
29-
$timeout(goNext);
30-
} else {
31-
goNext();
32-
}
26+
goNext();
3327
}
3428
function goNext() {
3529
// Scope has been destroyed, stop here.
3630
if (destroyed) { return; }
37-
//If we have a slide to transition from and we have a transition type and we're allowed, go
38-
if (self.currentSlide && angular.isString(direction) && !$scope.noTransition && nextSlide.$element) {
39-
//We shouldn't do class manip in here, but it's the same weird thing bootstrap does. need to fix sometime
40-
nextSlide.$element.addClass(direction);
41-
var reflow = nextSlide.$element[0].offsetWidth; //force reflow
4231

43-
//Set all other slides to stop doing their stuff for the new transition
44-
angular.forEach(slides, function(slide) {
45-
angular.extend(slide, {direction: '', entering: false, leaving: false, active: false});
46-
});
47-
angular.extend(nextSlide, {direction: direction, active: true, entering: true});
48-
angular.extend(self.currentSlide||{}, {direction: direction, leaving: true});
32+
angular.extend(nextSlide, {direction: direction, active: true});
33+
angular.extend(self.currentSlide || {}, {direction: direction, active: false});
4934

50-
$scope.$currentTransition = $transition(nextSlide.$element, {});
51-
//We have to create new pointers inside a closure since next & current will change
52-
(function(next,current) {
53-
$scope.$currentTransition.then(
54-
function(){ transitionDone(next, current); },
55-
function(){ transitionDone(next, current); }
56-
);
57-
}(nextSlide, self.currentSlide));
58-
} else {
59-
transitionDone(nextSlide, self.currentSlide);
35+
if ($animate.enabled() && !$scope.noTransition && nextSlide.$element) {
36+
$scope.$currentTransition = true;
37+
// TODO: Switch to use .one when upgrading beyond 1.2.21
38+
// (See https://github.com/angular/angular.js/pull/5984)
39+
nextSlide.$element.on('$animate:close', function closeFn() {
40+
$scope.$currentTransition = null;
41+
nextSlide.$element.off('$animate:close', closeFn);
42+
});
6043
}
44+
6145
self.currentSlide = nextSlide;
6246
currentIndex = nextIndex;
6347
//every time you change slides, reset the timer
6448
restartTimer();
6549
}
66-
function transitionDone(next, current) {
67-
angular.extend(next, {direction: '', active: true, leaving: false, entering: false});
68-
angular.extend(current||{}, {direction: '', active: false, leaving: false, entering: false});
69-
$scope.$currentTransition = null;
70-
}
7150
};
7251
$scope.$on('$destroy', function () {
7352
destroyed = true;
@@ -290,4 +269,55 @@ function CarouselDemoCtrl($scope) {
290269
});
291270
}
292271
};
293-
});
272+
})
273+
274+
.animation('.item', [
275+
'$animate',
276+
function ($animate) {
277+
return {
278+
beforeAddClass: function (element, className, done) {
279+
// Due to transclusion, noTransition property is on parent's scope
280+
if (className == 'active' && element.parent() &&
281+
!element.parent().scope().noTransition) {
282+
var stopped = false;
283+
var direction = element.isolateScope().direction;
284+
var directionClass = direction == 'next' ? 'left' : 'right';
285+
element.addClass(direction);
286+
$animate.addClass(element, directionClass).then(function () {
287+
if (!stopped) {
288+
element.removeClass(directionClass + ' ' + direction);
289+
}
290+
done();
291+
});
292+
293+
return function () {
294+
stopped = true;
295+
};
296+
}
297+
done();
298+
},
299+
beforeRemoveClass: function (element, className, done) {
300+
// Due to transclusion, noTransition property is on parent's scope
301+
if (className == 'active' && element.parent() &&
302+
!element.parent().scope().noTransition) {
303+
var stopped = false;
304+
var direction = element.isolateScope().direction;
305+
var directionClass = direction == 'next' ? 'left' : 'right';
306+
$animate.addClass(element, directionClass).then(function () {
307+
if (!stopped) {
308+
element.removeClass(directionClass);
309+
}
310+
done();
311+
});
312+
return function () {
313+
stopped = true;
314+
};
315+
}
316+
done();
317+
}
318+
};
319+
320+
}])
321+
322+
323+
;

Diff for: template/carousel/slide.html

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
<div ng-class="{
2-
'active': leaving || (active && !entering),
3-
'prev': (next || active) && direction=='prev',
4-
'next': (next || active) && direction=='next',
5-
'right': direction=='prev',
6-
'left': direction=='next'
2+
'active': active
73
}" class="item text-center" ng-transclude></div>

0 commit comments

Comments
 (0)