mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-27 06:54:01 +00:00
feat($animator): allow to globally disable and enable animations
This commit is contained in:
parent
d9d53472ec
commit
5476cb6e9b
2 changed files with 143 additions and 40 deletions
|
|
@ -121,21 +121,24 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @ngdoc function
|
|
||||||
* @name ng.$animator
|
|
||||||
*
|
|
||||||
* @description
|
|
||||||
* The $animator service provides the DOM manipulation API which is decorated with animations.
|
|
||||||
*
|
|
||||||
* @param {Scope} scope the scope for the ng-animate.
|
|
||||||
* @param {Attributes} attr the attributes object which contains the ngAnimate key / value pair. (The attributes are
|
|
||||||
* passed into the linking function of the directive using the `$animator`.)
|
|
||||||
* @return {object} the animator object which contains the enter, leave, move, show, hide and animate methods.
|
|
||||||
*/
|
|
||||||
var $AnimatorProvider = function() {
|
var $AnimatorProvider = function() {
|
||||||
this.$get = ['$animation', '$window', '$sniffer', function($animation, $window, $sniffer) {
|
var globalAnimationEnabled = true;
|
||||||
return function(scope, attrs) {
|
|
||||||
|
this.$get = ['$animation', '$window', '$sniffer', '$rootScope', function($animation, $window, $sniffer, $rootScope) {
|
||||||
|
/**
|
||||||
|
* @ngdoc function
|
||||||
|
* @name ng.$animator
|
||||||
|
* @function
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* The $animator.create service provides the DOM manipulation API which is decorated with animations.
|
||||||
|
*
|
||||||
|
* @param {Scope} scope the scope for the ng-animate.
|
||||||
|
* @param {Attributes} attr the attributes object which contains the ngAnimate key / value pair. (The attributes are
|
||||||
|
* passed into the linking function of the directive using the `$animator`.)
|
||||||
|
* @return {object} the animator object which contains the enter, leave, move, show, hide and animate methods.
|
||||||
|
*/
|
||||||
|
var AnimatorService = function(scope, attrs) {
|
||||||
var ngAnimateAttr = attrs.ngAnimate;
|
var ngAnimateAttr = attrs.ngAnimate;
|
||||||
var animator = {};
|
var animator = {};
|
||||||
|
|
||||||
|
|
@ -230,7 +233,7 @@ var $AnimatorProvider = function() {
|
||||||
var startClass = className + '-start';
|
var startClass = className + '-start';
|
||||||
|
|
||||||
return function(element, parent, after) {
|
return function(element, parent, after) {
|
||||||
if (!$sniffer.supportsTransitions && !polyfillSetup && !polyfillStart) {
|
if (!globalAnimationEnabled || !$sniffer.supportsTransitions && !polyfillSetup && !polyfillStart) {
|
||||||
beforeFn(element, parent, after);
|
beforeFn(element, parent, after);
|
||||||
afterFn(element, parent, after);
|
afterFn(element, parent, after);
|
||||||
return;
|
return;
|
||||||
|
|
@ -280,32 +283,54 @@ var $AnimatorProvider = function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function show(element) {
|
function show(element) {
|
||||||
element.css('display', '');
|
element.css('display', '');
|
||||||
}
|
|
||||||
|
|
||||||
function hide(element) {
|
|
||||||
element.css('display', 'none');
|
|
||||||
}
|
|
||||||
|
|
||||||
function insert(element, parent, after) {
|
|
||||||
if (after) {
|
|
||||||
after.after(element);
|
|
||||||
} else {
|
|
||||||
parent.append(element);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function remove(element) {
|
function hide(element) {
|
||||||
element.remove();
|
element.css('display', 'none');
|
||||||
}
|
}
|
||||||
|
|
||||||
function move(element, parent, after) {
|
function insert(element, parent, after) {
|
||||||
// Do not remove element before insert. Removing will cause data associated with the
|
if (after) {
|
||||||
// element to be dropped. Insert will implicitly do the remove.
|
after.after(element);
|
||||||
insert(element, parent, after);
|
} else {
|
||||||
}
|
parent.append(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove(element) {
|
||||||
|
element.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
function move(element, parent, after) {
|
||||||
|
// Do not remove element before insert. Removing will cause data associated with the
|
||||||
|
// element to be dropped. Insert will implicitly do the remove.
|
||||||
|
insert(element, parent, after);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ngdoc function
|
||||||
|
* @name ng.animator#enabled
|
||||||
|
* @methodOf ng.$animator
|
||||||
|
* @function
|
||||||
|
*
|
||||||
|
* @param {Boolean=} If provided then set the animation on or off.
|
||||||
|
* @return {Boolean} Current animation state.
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Globally enables/disables animations.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
AnimatorService.enabled = function(value) {
|
||||||
|
if (arguments.length) {
|
||||||
|
globalAnimationEnabled = !!value;
|
||||||
|
}
|
||||||
|
return globalAnimationEnabled;
|
||||||
|
};
|
||||||
|
|
||||||
|
return AnimatorService;
|
||||||
}];
|
}];
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,20 @@ describe("$animator", function() {
|
||||||
dealoc(element);
|
dealoc(element);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("enable / disable", function() {
|
||||||
|
|
||||||
|
it("should disable and enable the animations", inject(function($animator) {
|
||||||
|
expect($animator.enabled()).toBe(true);
|
||||||
|
|
||||||
|
expect($animator.enabled(0)).toBe(false);
|
||||||
|
expect($animator.enabled()).toBe(false);
|
||||||
|
|
||||||
|
expect($animator.enabled(1)).toBe(true);
|
||||||
|
expect($animator.enabled()).toBe(true);
|
||||||
|
}));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe("without animation", function() {
|
describe("without animation", function() {
|
||||||
var window, animator;
|
var window, animator;
|
||||||
|
|
||||||
|
|
@ -46,20 +60,27 @@ describe("$animator", function() {
|
||||||
expect(element.text()).toBe('21');
|
expect(element.text()).toBe('21');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it("should animate the show animation event", inject(function($animator, $compile, $rootScope) {
|
it("should animate the show animation event", inject(function() {
|
||||||
element.css('display','none');
|
element.css('display','none');
|
||||||
expect(element.css('display')).toBe('none');
|
expect(element.css('display')).toBe('none');
|
||||||
animator.show(element);
|
animator.show(element);
|
||||||
expect(element[0].style.display).toBe('');
|
expect(element[0].style.display).toBe('');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it("should animate the hide animation event", inject(function($animator, $compile, $rootScope) {
|
it("should animate the hide animation event", inject(function() {
|
||||||
element.css('display','block');
|
element.css('display','block');
|
||||||
expect(element.css('display')).toBe('block');
|
expect(element.css('display')).toBe('block');
|
||||||
animator.hide(element);
|
animator.hide(element);
|
||||||
expect(element.css('display')).toBe('none');
|
expect(element.css('display')).toBe('none');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it("should still perform DOM operations even if animations are disabled", inject(function($animator) {
|
||||||
|
$animator.enabled(false);
|
||||||
|
element.css('display','block');
|
||||||
|
expect(element.css('display')).toBe('block');
|
||||||
|
animator.hide(element);
|
||||||
|
expect(element.css('display')).toBe('none');
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("with polyfill", function() {
|
describe("with polyfill", function() {
|
||||||
|
|
@ -206,6 +227,63 @@ describe("$animator", function() {
|
||||||
window.setTimeout.expect(1).process();
|
window.setTimeout.expect(1).process();
|
||||||
expect(element.text()).toBe('memento');
|
expect(element.text()).toBe('memento');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it("should not run if animations are disabled", inject(function($animator, $rootScope) {
|
||||||
|
$animator.enabled(false);
|
||||||
|
|
||||||
|
animator = $animator($rootScope, {
|
||||||
|
ngAnimate : '{show: \'setup-memo\'}'
|
||||||
|
});
|
||||||
|
element.text('123');
|
||||||
|
expect(element.text()).toBe('123');
|
||||||
|
animator.show(element);
|
||||||
|
expect(element.text()).toBe('123');
|
||||||
|
|
||||||
|
$animator.enabled(true);
|
||||||
|
|
||||||
|
animator.show(element);
|
||||||
|
window.setTimeout.expect(1).process();
|
||||||
|
expect(element.text()).toBe('memento');
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("with css3", function() {
|
||||||
|
var window, animator, prefix, vendorPrefix;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
module(function($animationProvider, $provide) {
|
||||||
|
$provide.value('$window', window = angular.mock.createMockWindow());
|
||||||
|
return function($sniffer) {
|
||||||
|
vendorPrefix = '-' + $sniffer.vendorPrefix + '-';
|
||||||
|
};
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should skip animations if disabled and run when enabled",
|
||||||
|
inject(function($animator, $rootScope, $compile, $sniffer) {
|
||||||
|
$animator.enabled(false);
|
||||||
|
element = $compile('<div style="transition: 1s linear all">1</div>')($rootScope);
|
||||||
|
var animator = $animator($rootScope, {
|
||||||
|
ngAnimate : '{show: \'inline-show\'}'
|
||||||
|
});
|
||||||
|
|
||||||
|
element.css('display','none');
|
||||||
|
expect(element.css('display')).toBe('none');
|
||||||
|
animator.show(element);
|
||||||
|
expect(element[0].style.display).toBe('');
|
||||||
|
|
||||||
|
$animator.enabled(true);
|
||||||
|
|
||||||
|
element.css('display','none');
|
||||||
|
expect(element.css('display')).toBe('none');
|
||||||
|
|
||||||
|
animator.show(element);
|
||||||
|
if ($sniffer.supportsTransitions) {
|
||||||
|
window.setTimeout.expect(1).process();
|
||||||
|
window.setTimeout.expect(1000).process();
|
||||||
|
}
|
||||||
|
expect(element[0].style.display).toBe('');
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw an error when an invalid ng-animate syntax is provided", inject(function($compile, $rootScope) {
|
it("should throw an error when an invalid ng-animate syntax is provided", inject(function($compile, $rootScope) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue