Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 8a7451b

Browse files
author
Gabriel Schulhof
committed
animationComplete: Remove only the handler attached by us
Closes gh-7267 Fixes gh-7265
1 parent dc3fa7d commit 8a7451b

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

js/animationComplete.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ define( [
4949
$.fn.animationComplete = function( callback, type, fallbackTime ) {
5050
var timer, duration,
5151
that = this,
52+
eventBinding = function() {
53+
54+
// Clear the timer so we don't call callback twice
55+
clearTimeout( timer );
56+
callback.apply( this, arguments );
57+
},
5258
animationType = ( !type || type === "animation" ) ? "animation" : "transition";
5359

5460
// Make sure selected type is supported by browser
@@ -76,17 +82,12 @@ define( [
7682

7783
// Sets up the fallback if event never comes
7884
timer = setTimeout( function() {
79-
$( that ).off( props[ animationType ].event );
85+
$( that ).off( props[ animationType ].event, eventBinding );
8086
callback.apply( that );
8187
}, duration );
8288

8389
// Bind the event
84-
return $( this ).one( props[ animationType ].event, function() {
85-
86-
// Clear the timer so we dont call callback twice
87-
clearTimeout( timer );
88-
callback.call( this, arguments );
89-
});
90+
return $( this ).one( props[ animationType ].event, eventBinding );
9091
} else {
9192

9293
// CSS animation / transitions not supported

tests/integration/animation-complete/animationComplete.js

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,50 @@
22
* mobile flipswitch unit tests
33
*/
44
(function($){
5-
var oldTransitions, oldAnimations;
5+
var oldTransitions, oldAnimations,
6+
countEvents = function( element, eventName ) {
7+
var count = 0,
8+
events = $._data( element, "events" );
9+
10+
if ( events && events[ eventName ] ) {
11+
count = events[ eventName ].length;
12+
}
13+
14+
return count;
15+
},
16+
events = ( function() {
17+
var nameIndex, match, event,
18+
names = [
19+
"animation", "transition",
20+
"mozAnimation", "mozTransition",
21+
"oAnimation", "oTransition",
22+
"webkitAnimation", "webkitTransition"
23+
],
24+
element = document.createElement( "a" ),
25+
events = {
26+
animation: { name: "animationend" },
27+
transition: { name: "transitionend" }
28+
};
29+
30+
for( nameIndex in names ) {
31+
if ( element.style[ names[ nameIndex ] ] !== undefined ) {
32+
match = names[ nameIndex ].match( /(.*)(animation|transition)$/i );
33+
event = match[ 2 ].toLowerCase();
34+
35+
// Unprefixed is the best, so do not overwrite if we've already found an
36+
// unprefixed version
37+
if ( events[ event ].prefix !== "" ) {
38+
events[ event ] = {
39+
name: match[ 1 ] + match[ 2 ] + ( match[ 1 ] ? "End" : "end" ),
40+
prefix: match[ 1 ]
41+
};
42+
}
43+
}
44+
}
45+
46+
return events;
47+
})();
48+
649
module( "Callbacks: Event", {
750
teardown: function() {
851
$( "#transition-test" )
@@ -73,7 +116,7 @@
73116
$( "#animation-test" ).removeClass( "in" );
74117
}
75118
});
76-
asyncTest( "call back executes immeditly when animations not supported on device", function() {
119+
asyncTest( "callback executes immediately when animations unsupported on device", function() {
77120
expect( 2 );
78121
var transitionComplete = false,
79122
animationComplete = false;
@@ -140,7 +183,7 @@
140183
$( "#animation-test" ).removeClass( "in" );
141184
}
142185
});
143-
asyncTest( "Make sure no bidnings when no cssanimation support", function() {
186+
asyncTest( "Make sure no bindings when no cssanimation support", function() {
144187
expect( 2 );
145188
var transitionComplete = false,
146189
animationComplete = false;
@@ -167,7 +210,7 @@
167210
$( "#animation-test" ).removeClass( "in" );
168211
}
169212
});
170-
asyncTest( "Make sure no bidnings remain after event", function() {
213+
asyncTest( "Make sure no bindings remain after event", function() {
171214
expect( 2 );
172215
var transitionComplete = false,
173216
animationComplete = false;
@@ -190,13 +233,19 @@
190233
}, 800 );
191234
});
192235
module( "Event Removal: fallback", {
236+
setup: function() {
237+
$( "#transition-test" ).on( events.transition.name, $.noop );
238+
$( "#animation-test" ).on( events.animation.name, $.noop );
239+
},
193240
teardown: function() {
194241
$( "#transition-test" )
195242
.removeClass( "ui-panel-animate ui-panel-position-left ui-panel-display-overlay" );
196243
$( "#animation-test" ).removeClass( "in" );
244+
$( "#transition-test" ).off( events.transition.name, $.noop );
245+
$( "#animation-test" ).off( events.animation.name, $.noop );
197246
}
198247
});
199-
asyncTest( "Make sure no bidnings remain after fallback", function() {
248+
asyncTest( "Make sure no bindings remain after fallback", function() {
200249
expect( 2 );
201250
var transitionComplete = false,
202251
animationComplete = false;
@@ -210,11 +259,11 @@
210259
});
211260

212261
window.setTimeout( function(){
213-
ok( $._data( $("#animation-test")[0], "events" ) === undefined,
262+
deepEqual( countEvents( $("#animation-test")[0], events.animation.name ), 1,
214263
"no animation bindings remain" );
215-
ok( $._data( $("#transition-test")[0], "events" ) === undefined,
264+
deepEqual( countEvents( $("#transition-test")[0], events.transition.name ), 1,
216265
"no transition bindings remain" );
217266
start();
218267
}, 1200 );
219268
});
220-
})( jQuery );
269+
})( jQuery );

0 commit comments

Comments
 (0)