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

Commit 5476cb6

Browse files
matskomhevery
authored andcommitted
feat($animator): allow to globally disable and enable animations
1 parent d9d5347 commit 5476cb6

File tree

2 files changed

+141
-38
lines changed

2 files changed

+141
-38
lines changed

src/ng/animator.js

+61-36
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,24 @@
121121
*
122122
*/
123123

124-
/**
125-
* @ngdoc function
126-
* @name ng.$animator
127-
*
128-
* @description
129-
* The $animator service provides the DOM manipulation API which is decorated with animations.
130-
*
131-
* @param {Scope} scope the scope for the ng-animate.
132-
* @param {Attributes} attr the attributes object which contains the ngAnimate key / value pair. (The attributes are
133-
* passed into the linking function of the directive using the `$animator`.)
134-
* @return {object} the animator object which contains the enter, leave, move, show, hide and animate methods.
135-
*/
136124
var $AnimatorProvider = function() {
137-
this.$get = ['$animation', '$window', '$sniffer', function($animation, $window, $sniffer) {
138-
return function(scope, attrs) {
125+
var globalAnimationEnabled = true;
126+
127+
this.$get = ['$animation', '$window', '$sniffer', '$rootScope', function($animation, $window, $sniffer, $rootScope) {
128+
/**
129+
* @ngdoc function
130+
* @name ng.$animator
131+
* @function
132+
*
133+
* @description
134+
* The $animator.create service provides the DOM manipulation API which is decorated with animations.
135+
*
136+
* @param {Scope} scope the scope for the ng-animate.
137+
* @param {Attributes} attr the attributes object which contains the ngAnimate key / value pair. (The attributes are
138+
* passed into the linking function of the directive using the `$animator`.)
139+
* @return {object} the animator object which contains the enter, leave, move, show, hide and animate methods.
140+
*/
141+
var AnimatorService = function(scope, attrs) {
139142
var ngAnimateAttr = attrs.ngAnimate;
140143
var animator = {};
141144

@@ -230,7 +233,7 @@ var $AnimatorProvider = function() {
230233
var startClass = className + '-start';
231234

232235
return function(element, parent, after) {
233-
if (!$sniffer.supportsTransitions && !polyfillSetup && !polyfillStart) {
236+
if (!globalAnimationEnabled || !$sniffer.supportsTransitions && !polyfillSetup && !polyfillStart) {
234237
beforeFn(element, parent, after);
235238
afterFn(element, parent, after);
236239
return;
@@ -280,32 +283,54 @@ var $AnimatorProvider = function() {
280283
}
281284
}
282285
}
283-
}
284286

285-
function show(element) {
286-
element.css('display', '');
287-
}
287+
function show(element) {
288+
element.css('display', '');
289+
}
288290

289-
function hide(element) {
290-
element.css('display', 'none');
291-
}
291+
function hide(element) {
292+
element.css('display', 'none');
293+
}
292294

293-
function insert(element, parent, after) {
294-
if (after) {
295-
after.after(element);
296-
} else {
297-
parent.append(element);
295+
function insert(element, parent, after) {
296+
if (after) {
297+
after.after(element);
298+
} else {
299+
parent.append(element);
300+
}
301+
}
302+
303+
function remove(element) {
304+
element.remove();
298305
}
299-
}
300306

301-
function remove(element) {
302-
element.remove();
303-
}
307+
function move(element, parent, after) {
308+
// Do not remove element before insert. Removing will cause data associated with the
309+
// element to be dropped. Insert will implicitly do the remove.
310+
insert(element, parent, after);
311+
}
312+
};
313+
314+
/**
315+
* @ngdoc function
316+
* @name ng.animator#enabled
317+
* @methodOf ng.$animator
318+
* @function
319+
*
320+
* @param {Boolean=} If provided then set the animation on or off.
321+
* @return {Boolean} Current animation state.
322+
*
323+
* @description
324+
* Globally enables/disables animations.
325+
*
326+
*/
327+
AnimatorService.enabled = function(value) {
328+
if (arguments.length) {
329+
globalAnimationEnabled = !!value;
330+
}
331+
return globalAnimationEnabled;
332+
};
304333

305-
function move(element, parent, after) {
306-
// Do not remove element before insert. Removing will cause data associated with the
307-
// element to be dropped. Insert will implicitly do the remove.
308-
insert(element, parent, after);
309-
}
334+
return AnimatorService;
310335
}];
311336
};

test/ng/animatorSpec.js

+80-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ describe("$animator", function() {
88
dealoc(element);
99
});
1010

11+
describe("enable / disable", function() {
12+
13+
it("should disable and enable the animations", inject(function($animator) {
14+
expect($animator.enabled()).toBe(true);
15+
16+
expect($animator.enabled(0)).toBe(false);
17+
expect($animator.enabled()).toBe(false);
18+
19+
expect($animator.enabled(1)).toBe(true);
20+
expect($animator.enabled()).toBe(true);
21+
}));
22+
23+
});
24+
1125
describe("without animation", function() {
1226
var window, animator;
1327

@@ -46,20 +60,27 @@ describe("$animator", function() {
4660
expect(element.text()).toBe('21');
4761
}));
4862

49-
it("should animate the show animation event", inject(function($animator, $compile, $rootScope) {
63+
it("should animate the show animation event", inject(function() {
5064
element.css('display','none');
5165
expect(element.css('display')).toBe('none');
5266
animator.show(element);
5367
expect(element[0].style.display).toBe('');
5468
}));
5569

56-
it("should animate the hide animation event", inject(function($animator, $compile, $rootScope) {
70+
it("should animate the hide animation event", inject(function() {
5771
element.css('display','block');
5872
expect(element.css('display')).toBe('block');
5973
animator.hide(element);
6074
expect(element.css('display')).toBe('none');
6175
}));
6276

77+
it("should still perform DOM operations even if animations are disabled", inject(function($animator) {
78+
$animator.enabled(false);
79+
element.css('display','block');
80+
expect(element.css('display')).toBe('block');
81+
animator.hide(element);
82+
expect(element.css('display')).toBe('none');
83+
}));
6384
});
6485

6586
describe("with polyfill", function() {
@@ -206,6 +227,63 @@ describe("$animator", function() {
206227
window.setTimeout.expect(1).process();
207228
expect(element.text()).toBe('memento');
208229
}));
230+
231+
it("should not run if animations are disabled", inject(function($animator, $rootScope) {
232+
$animator.enabled(false);
233+
234+
animator = $animator($rootScope, {
235+
ngAnimate : '{show: \'setup-memo\'}'
236+
});
237+
element.text('123');
238+
expect(element.text()).toBe('123');
239+
animator.show(element);
240+
expect(element.text()).toBe('123');
241+
242+
$animator.enabled(true);
243+
244+
animator.show(element);
245+
window.setTimeout.expect(1).process();
246+
expect(element.text()).toBe('memento');
247+
}));
248+
});
249+
250+
describe("with css3", function() {
251+
var window, animator, prefix, vendorPrefix;
252+
253+
beforeEach(function() {
254+
module(function($animationProvider, $provide) {
255+
$provide.value('$window', window = angular.mock.createMockWindow());
256+
return function($sniffer) {
257+
vendorPrefix = '-' + $sniffer.vendorPrefix + '-';
258+
};
259+
})
260+
});
261+
262+
it("should skip animations if disabled and run when enabled",
263+
inject(function($animator, $rootScope, $compile, $sniffer) {
264+
$animator.enabled(false);
265+
element = $compile('<div style="transition: 1s linear all">1</div>')($rootScope);
266+
var animator = $animator($rootScope, {
267+
ngAnimate : '{show: \'inline-show\'}'
268+
});
269+
270+
element.css('display','none');
271+
expect(element.css('display')).toBe('none');
272+
animator.show(element);
273+
expect(element[0].style.display).toBe('');
274+
275+
$animator.enabled(true);
276+
277+
element.css('display','none');
278+
expect(element.css('display')).toBe('none');
279+
280+
animator.show(element);
281+
if ($sniffer.supportsTransitions) {
282+
window.setTimeout.expect(1).process();
283+
window.setTimeout.expect(1000).process();
284+
}
285+
expect(element[0].style.display).toBe('');
286+
}));
209287
});
210288

211289
it("should throw an error when an invalid ng-animate syntax is provided", inject(function($compile, $rootScope) {

0 commit comments

Comments
 (0)