Skip to content

Commit 11348f9

Browse files
ajoslinpkozlowski-opensource
authored andcommitted
feat(transition): add animationEnd option for transition service
1 parent 5dfa324 commit 11348f9

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/transition/test/transition.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,22 @@ describe('$transition', function() {
7676
});
7777
});
7878

79+
describe('animationEndEventName', function() {
80+
it('should be a string ending with animationend', function() {
81+
expect($transition.animationEndEventName).toMatch(/animationend$/i);
82+
});
83+
});
84+
7985
it('binds a transitionEnd handler to the element', function() {
8086
$transition(element, '');
8187
expect(element.bind).toHaveBeenCalledWith($transition.transitionEndEventName, jasmine.any(Function));
8288
});
83-
89+
90+
it('binds an animationEnd handler to the element if option is given', function() {
91+
$transition(element, '', {animation: true});
92+
expect(element.bind).toHaveBeenCalledWith($transition.animationEndEventName, jasmine.any(Function));
93+
});
94+
8495
it('resolves the promise when the transitionEnd is triggered', function() {
8596
var resolutionHandler = jasmine.createSpy('resolutionHandler');
8697

src/transition/transition.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ angular.module('ui.bootstrap.transition', [])
1111
*/
1212
.factory('$transition', ['$q', '$timeout', '$rootScope', function($q, $timeout, $rootScope) {
1313

14-
var $transition = function(element, trigger) {
15-
14+
var $transition = function(element, trigger, options) {
15+
options = options || {};
1616
var deferred = $q.defer();
17+
var endEventName = $transition[options.animation ? "animationEndEventName" : "transitionEndEventName"];
18+
1719
var transitionEndHandler = function(event) {
1820
$rootScope.$apply(function() {
19-
element.unbind($transition.transitionEndEventName, transitionEndHandler);
21+
element.unbind(endEventName, transitionEndHandler);
2022
deferred.resolve(element);
2123
});
2224
};
2325

24-
// Only bind if the browser supports transitions
25-
if ( $transition.transitionEndEventName ) {
26-
element.bind($transition.transitionEndEventName, transitionEndHandler);
26+
if (endEventName) {
27+
element.bind(endEventName, transitionEndHandler);
2728
}
2829

2930
// Wrap in a timeout to allow the browser time to update the DOM before the transition is to occur
@@ -35,19 +36,18 @@ angular.module('ui.bootstrap.transition', [])
3536
} else if ( angular.isObject(trigger) ) {
3637
element.css(trigger);
3738
}
38-
39-
// If the browser doesn't support transitions then we immediately resolve the event
40-
if ( !$transition.transitionEndEventName ) {
39+
//If browser does not support transitions, instantly resolve
40+
if ( !endEventName ) {
4141
deferred.resolve(element);
4242
}
4343
});
4444

45-
// Add out custom cancel function to the promise that is returned
45+
// Add our custom cancel function to the promise that is returned
4646
// We can call this if we are about to run a new transition, which we know will prevent this transition from ending,
4747
// i.e. it will therefore never raise a transitionEnd event for that transition
4848
deferred.promise.cancel = function() {
49-
if ( $transition.transitionEndEventName ) {
50-
element.unbind($transition.transitionEndEventName, transitionEndHandler);
49+
if ( endEventName ) {
50+
element.unbind(endEventName, transitionEndHandler);
5151
}
5252
deferred.reject('Transition cancelled');
5353
};
@@ -64,10 +64,21 @@ angular.module('ui.bootstrap.transition', [])
6464
'msTransition': 'MSTransitionEnd',
6565
'transition': 'transitionend'
6666
};
67-
for (var name in transitionEndEventNames){
68-
if (transElement.style[name] !== undefined) {
69-
$transition.transitionEndEventName = transitionEndEventNames[name];
67+
var animationEndEventNames = {
68+
'WebkitTransition': 'webkitAnimationEnd',
69+
'MozTransition': 'animationend',
70+
'OTransition': 'oAnimationEnd',
71+
'msTransition': 'MSAnimationEnd',
72+
'transition': 'animationend'
73+
};
74+
function findEndEventName(endEventNames) {
75+
for (var name in endEventNames){
76+
if (transElement.style[name] !== undefined) {
77+
return endEventNames[name];
78+
}
7079
}
7180
}
81+
$transition.transitionEndEventName = findEndEventName(transitionEndEventNames);
82+
$transition.animationEndEventName = findEndEventName(animationEndEventNames);
7283
return $transition;
73-
}]);
84+
}]);

0 commit comments

Comments
 (0)