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

Commit 72a97a0

Browse files
committed
refactor(carousel): use ngAnimate
1 parent 26f719d commit 72a97a0

File tree

2 files changed

+83
-38
lines changed

2 files changed

+83
-38
lines changed

src/carousel/carousel.js

Lines changed: 82 additions & 33 deletions
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', '$transition', function ($scope, $timeout, $transition) {
10+
.controller('CarouselController', ['$scope', '$timeout', '$animate', function ($scope, $timeout, $animate) {
1111
var self = this,
1212
slides = self.slides = $scope.slides = [],
1313
currentIndex = -1,
@@ -23,51 +23,32 @@ 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 {
26+
if (!$animate.enabled() || !$scope.$currentTransition) {
3127
goNext();
3228
}
3329
}
3430
function goNext() {
3531
// Scope has been destroyed, stop here.
3632
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
4233

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});
34+
angular.extend(nextSlide, {direction: direction, active: true});
35+
angular.extend(self.currentSlide || {}, {direction: direction, active: false});
4936

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);
37+
if ($animate.enabled() && !$scope.noTransition && nextSlide.$element) {
38+
$scope.$currentTransition = true;
39+
// TODO: Switch to use .one when upgrading beyond 1.2.21
40+
// (See https://github.com/angular/angular.js/pull/5984)
41+
nextSlide.$element.on('$animate:close', function closeFn() {
42+
$scope.$currentTransition = null;
43+
nextSlide.$element.off('$animate:close', closeFn);
44+
});
6045
}
46+
6147
self.currentSlide = nextSlide;
6248
currentIndex = nextIndex;
6349
//every time you change slides, reset the timer
6450
restartTimer();
6551
}
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-
}
7152
};
7253
$scope.$on('$destroy', function () {
7354
destroyed = true;
@@ -290,4 +271,72 @@ function CarouselDemoCtrl($scope) {
290271
});
291272
}
292273
};
293-
});
274+
})
275+
276+
.animation('.item', [
277+
'$animate',
278+
function ($animate) {
279+
return {
280+
beforeAddClass: function (element, className, done) {
281+
// Due to transclusion, noTransition property is on parent's scope
282+
if (className == 'active' && element.parent() &&
283+
!element.parent().scope().noTransition) {
284+
var stopped = false;
285+
var direction = element.isolateScope().direction;
286+
var directionClass = direction == 'next' ? 'left' : 'right';
287+
element.addClass(direction);
288+
$animate.addClass(element, directionClass, function () {
289+
if (!stopped) {
290+
element.removeClass(directionClass + ' ' + direction);
291+
}
292+
done();
293+
});
294+
295+
return function () {
296+
stopped = true;
297+
};
298+
}
299+
done();
300+
},
301+
beforeRemoveClass: function (element, className, done) {
302+
// Due to transclusion, noTransition property is on parent's scope
303+
if (className == 'active' && element.parent() &&
304+
!element.parent().scope().noTransition) {
305+
var stopped = false;
306+
var direction = element.isolateScope().direction;
307+
var directionClass = direction == 'next' ? 'left' : 'right';
308+
$animate.addClass(element, directionClass, function () {
309+
if (!stopped) {
310+
element.removeClass(directionClass);
311+
}
312+
done();
313+
});
314+
return function () {
315+
stopped = true;
316+
};
317+
}
318+
done();
319+
}
320+
};
321+
322+
}])
323+
324+
.run([
325+
'$document',
326+
function ($document) {
327+
if(!angular.$$csp()) {
328+
angular.element($document).find('head')
329+
.prepend(
330+
'<style type="text/css">@charset "UTF-8";' +
331+
'.ng-animate.item.active-add:not(.left):not(.right) {' +
332+
' transition: 0 ease-in-out left;' +
333+
'}' +
334+
'.ng-animate.item.active-remove:not(.left):not(.right) {' +
335+
' transition: 0 ease-in-out left;' +
336+
'}'
337+
);
338+
}
339+
}])
340+
341+
342+
;

template/carousel/slide.html

Lines changed: 1 addition & 5 deletions
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)