Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($animate): ensure leave animation calls close callback
Browse files Browse the repository at this point in the history
Closes #12278
Closes #12096
Closes #13054
  • Loading branch information
sreeramu authored and petebacondarwin committed Oct 29, 2015
1 parent 7170f9d commit 6bd6dbf
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,18 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
return mergeAnimationOptions(element, options, {});
}

function findCallbacks(element, event) {
function findCallbacks(parent, element, event) {
var targetNode = getDomNode(element);
var targetParentNode = getDomNode(parent);

var matches = [];
var entries = callbackRegistry[event];
if (entries) {
forEach(entries, function(entry) {
if (entry.node.contains(targetNode)) {
matches.push(entry.callback);
} else if (event === 'leave' && entry.node.contains(targetParentNode)) {
matches.push(entry.callback);
}
});
}
Expand Down Expand Up @@ -473,7 +476,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {

function notifyProgress(runner, event, phase, data) {
runInNextPostDigestOrNow(function() {
var callbacks = findCallbacks(element, event);
var callbacks = findCallbacks(parent, element, event);
if (callbacks.length) {
// do not optimize this call here to RAF because
// we don't know how heavy the callback code here will
Expand Down
81 changes: 81 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,5 +1781,86 @@ describe("animations", function() {
expect(isElementRemoved).toBe(true);
}));

it('leave : should trigger a callback for an leave animation',
inject(function($animate, $rootScope, $$rAF, $rootElement, $document) {

var callbackTriggered = false;
$animate.on('leave', jqLite($document[0].body), function() {
callbackTriggered = true;
});

element = jqLite('<div></div>');
$rootElement.append(element);
$animate.leave(element, $rootElement);
$rootScope.$digest();

$$rAF.flush();

expect(callbackTriggered).toBe(true);
}));

it('leave : should not fire a callback if the element is outside of the given container',
inject(function($animate, $rootScope, $$rAF, $rootElement) {

var callbackTriggered = false;
var innerContainer = jqLite('<div></div>');
$rootElement.append(innerContainer);

$animate.on('leave', innerContainer,
function(element, phase, data) {
callbackTriggered = true;
});

element = jqLite('<div></div>');
$rootElement.append(element);
$animate.leave(element, $rootElement);
$rootScope.$digest();

expect(callbackTriggered).toBe(false);
}));

it('leave : should fire a `start` callback when the animation starts with the matching element',
inject(function($animate, $rootScope, $$rAF, $rootElement, $document) {

element = jqLite('<div></div>');

var capturedState;
var capturedElement;
$animate.on('leave', jqLite($document[0].body), function(element, phase) {
capturedState = phase;
capturedElement = element;
});

$rootElement.append(element);
$animate.leave(element, $rootElement);
$rootScope.$digest();
$$rAF.flush();

expect(capturedState).toBe('start');
expect(capturedElement).toBe(element);
}));

it('leave : should fire a `close` callback when the animation ends with the matching element',
inject(function($animate, $rootScope, $$rAF, $rootElement, $document) {

element = jqLite('<div></div>');

var capturedState;
var capturedElement;
$animate.on('leave', jqLite($document[0].body), function(element, phase) {
capturedState = phase;
capturedElement = element;
});

$rootElement.append(element);
var runner = $animate.leave(element, $rootElement);
$rootScope.$digest();
runner.end();
$$rAF.flush();

expect(capturedState).toBe('close');
expect(capturedElement).toBe(element);
}));

});
});

0 comments on commit 6bd6dbf

Please sign in to comment.