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

Fix for #12096 : Fixed leave start & close callback #12278

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,28 @@ 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);
}
});
}

return matches;
}

function triggerCallback(event, element, phase, data) {
function triggerCallback(event, parent, element, phase, data) {
$$rAF(function() {
forEach(findCallbacks(element, event), function(callback) {
forEach(findCallbacks(parent, element, event), function(callback) {
callback(element, phase, data);
});
});
Expand Down Expand Up @@ -455,7 +458,7 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
return runner;

function notifyProgress(runner, event, phase, data) {
triggerCallback(event, element, phase, data);
triggerCallback(event, parent, element, phase, data);
runner.progress(event, phase, data);
}

Expand Down
82 changes: 82 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1646,5 +1646,87 @@ describe("animations", function() {
expect(count).toBe(1);
}));

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

var callbackTriggered = false;
$animate.on('leave', 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();
$$rAF.flush();

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) {

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

var capturedState;
var capturedElement;
$animate.on('leave', 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) {

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

var capturedState;
var capturedElement;
$animate.on('leave', 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);
}));

});
});