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

Commit c4bff29

Browse files
committed
fix(ngAnimate): fire callbacks when document is hidden
Since commit a3a7afd, animations are not run when the document is hidden (only their structural or class change effects are executed). However, some libraries rely on the $animate.on() callbacks to be called even when no actual animation runs. This commit restores the behavior for the ngAnimate.$animate functions. Note that callbacks still won't be called if animations are disabled, because this would be be a potential breaking change, as some applications might rely on this implementation. Fixes #14120
1 parent 55b0014 commit c4bff29

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ngAnimate/animateQueue.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,14 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
331331

332332
var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
333333

334+
var documentHidden = $document[0].hidden;
335+
334336
// this is a hard disable of all animations for the application or on
335337
// the element itself, therefore there is no need to continue further
336338
// past this point if not enabled
337339
// Animations are also disabled if the document is currently hidden (page is not visible
338340
// to the user), because browsers slow down or do not flush calls to requestAnimationFrame
339-
var skipAnimations = !animationsEnabled || $document[0].hidden || disabledElementsLookup.get(node);
341+
var skipAnimations = !animationsEnabled || documentHidden || disabledElementsLookup.get(node);
340342
var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
341343
var hasExistingAnimation = !!existingAnimation.state;
342344

@@ -347,7 +349,10 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
347349
}
348350

349351
if (skipAnimations) {
352+
// Callbacks should fire even if the document is hidden (regression fix for issue #14120)
353+
if (documentHidden) notifyProgress(runner, event, 'start');
350354
close();
355+
if (documentHidden) notifyProgress(runner, event, 'close');
351356
return runner;
352357
}
353358

test/ngAnimate/animateSpec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,6 +2269,36 @@ describe("animations", function() {
22692269
expect(classSpy).not.toHaveBeenCalled();
22702270
}));
22712271

2272+
2273+
describe('because the document is hidden', function() {
2274+
beforeEach(module(function($provide) {
2275+
var doc = jqLite({
2276+
body: document.body,
2277+
hidden: true
2278+
});
2279+
$provide.value('$document', doc);
2280+
}));
2281+
2282+
it('should trigger callbacks for an enter animation',
2283+
inject(function($animate, $rootScope, $rootElement, $document) {
2284+
2285+
var callbackTriggered = false;
2286+
var spy = jasmine.createSpy();
2287+
$animate.on('enter', jqLite($document[0].body), spy);
2288+
2289+
element = jqLite('<div></div>');
2290+
var runner = $animate.enter(element, $rootElement);
2291+
$rootScope.$digest();
2292+
2293+
$animate.flush(); // Flushes the animation frames for the callbacks
2294+
2295+
expect(spy.calls.count()).toBe(2);
2296+
expect(spy.calls.argsFor(0)[1]).toBe('start');
2297+
expect(spy.calls.argsFor(1)[1]).toBe('close');
2298+
}));
2299+
});
2300+
2301+
22722302
});
22732303

22742304
});

0 commit comments

Comments
 (0)