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

Commit 3ba919c

Browse files
committed
feat($animate): introduce the $animate.animate() method
1 parent b5b7854 commit 3ba919c

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

src/ng/animate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ var $AnimateProvider = ['$provide', function($provide) {
174174
* page}.
175175
*/
176176
return {
177+
animate : function(element, from, to) {
178+
applyStyles(element, { from: from, to: to });
179+
return asyncPromise();
180+
},
177181

178182
/**
179183
*

src/ngAnimate/animate.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,10 @@ angular.module('ngAnimate', ['ng'])
631631
}
632632

633633
var isSetClassOperation = animationEvent == 'setClass';
634-
var isClassBased = isSetClassOperation ||
635-
animationEvent == 'addClass' ||
636-
animationEvent == 'removeClass';
634+
var isClassBased = isSetClassOperation
635+
|| animationEvent == 'addClass'
636+
|| animationEvent == 'removeClass'
637+
|| animationEvent == 'animate';
637638

638639
var currentClassName = element.attr('class');
639640
var classes = currentClassName + ' ' + className;
@@ -703,6 +704,9 @@ angular.module('ngAnimate', ['ng'])
703704
case 'setClass':
704705
cancellations.push(animation.fn(element, classNameAdd, classNameRemove, progress, options));
705706
break;
707+
case 'animate':
708+
cancellations.push(animation.fn(element, className, progress, options));
709+
break;
706710
case 'addClass':
707711
cancellations.push(animation.fn(element, classNameAdd || className, progress, options));
708712
break;
@@ -822,6 +826,17 @@ angular.module('ngAnimate', ['ng'])
822826
*
823827
*/
824828
return {
829+
animate : function(element, from, to, className, options) {
830+
className = className || 'ng-inline-animate';
831+
options = options || {};
832+
options.from = to ? from : null;
833+
options.to = to ? to : from;
834+
835+
return runAnimationPostDigest(function(done) {
836+
return performAnimation('animate', className, stripCommentsFromElement(element), null, null, noop, options, done);
837+
});
838+
},
839+
825840
/**
826841
* @ngdoc method
827842
* @name $animate#enter
@@ -1259,7 +1274,10 @@ angular.module('ngAnimate', ['ng'])
12591274
}
12601275
}
12611276

1262-
if (runner.isClassBased && !runner.isSetClassOperation && !skipAnimation) {
1277+
if (runner.isClassBased
1278+
&& !runner.isSetClassOperation
1279+
&& animationEvent != 'animate'
1280+
&& !skipAnimation) {
12631281
skipAnimation = (animationEvent == 'addClass') == element.hasClass(className); //opposite of XOR
12641282
}
12651283

@@ -1950,6 +1968,11 @@ angular.module('ngAnimate', ['ng'])
19501968
}
19511969

19521970
return {
1971+
animate : function(element, className, animationCompleted, options) {
1972+
options = options || {};
1973+
return animate('animate', element, className, animationCompleted, options);
1974+
},
1975+
19531976
enter : function(element, animationCompleted, options) {
19541977
options = options || {};
19551978
return animate('enter', element, 'ng-enter', animationCompleted, options);

src/ngMock/angular-mocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
803803
};
804804

805805
angular.forEach(
806-
['enter','leave','move','addClass','removeClass','setClass'], function(method) {
806+
['animate','enter','leave','move','addClass','removeClass','setClass'], function(method) {
807807
animate[method] = function() {
808808
animate.queue.push({
809809
event : method,

test/ng/animateSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ describe("$animate", function() {
5050
expect(element.text()).toBe('21');
5151
}));
5252

53+
it("should apply styles instantly to the element",
54+
inject(function($animate, $compile, $rootScope) {
55+
56+
$animate.animate(element, { color: 'rgb(0, 0, 0)' });
57+
expect(element.css('color')).toBe('rgb(0, 0, 0)');
58+
59+
$animate.animate(element, { color: 'rgb(255, 0, 0)' }, { color: 'rgb(0, 255, 0)' });
60+
expect(element.css('color')).toBe('rgb(0, 255, 0)');
61+
}));
5362

5463
it("should still perform DOM operations even if animations are disabled (post-digest)", inject(function($animate, $rootScope) {
5564
$animate.enabled(false);

test/ngAnimate/animateSpec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ describe("ngAnimate", function() {
329329
return function($animate, $compile, $rootScope, $rootElement) {
330330
element = $compile('<div></div>')($rootScope);
331331

332-
forEach(['.ng-hide-add', '.ng-hide-remove', '.ng-enter', '.ng-leave', '.ng-move'], function(selector) {
332+
forEach(['.ng-hide-add', '.ng-hide-remove', '.ng-enter', '.ng-leave', '.ng-move', '.my-inline-animation'], function(selector) {
333333
ss.addRule(selector, '-webkit-transition:1s linear all;' +
334334
'transition:1s linear all;');
335335
});
@@ -454,6 +454,20 @@ describe("ngAnimate", function() {
454454
expect(element.text()).toBe('21');
455455
}));
456456

457+
it("should perform the animate event",
458+
inject(function($animate, $compile, $rootScope, $timeout, $sniffer) {
459+
460+
$rootScope.$digest();
461+
$animate.animate(element, { color: 'rgb(255, 0, 0)' }, { color: 'rgb(0, 0, 255)' }, 'animated');
462+
$rootScope.$digest();
463+
464+
if($sniffer.transitions) {
465+
expect(element.css('color')).toBe('rgb(255, 0, 0)');
466+
$animate.triggerReflow();
467+
}
468+
expect(element.css('color')).toBe('rgb(0, 0, 255)');
469+
}));
470+
457471
it("should animate the show animation event",
458472
inject(function($animate, $rootScope, $sniffer) {
459473

@@ -653,6 +667,16 @@ describe("ngAnimate", function() {
653667
expect(child.attr('class')).toContain('ng-hide-remove-active');
654668
browserTrigger(child,'transitionend', { timeStamp: Date.now() + 1000, elapsedTime: 1 });
655669

670+
//animate
671+
$animate.animate(child, null, null, 'my-inline-animation');
672+
$rootScope.$digest();
673+
$animate.triggerReflow();
674+
675+
expect(child.attr('class')).toContain('my-inline-animation');
676+
expect(child.attr('class')).toContain('my-inline-animation-active');
677+
browserTrigger(child,'transitionend', { timeStamp: Date.now() + 1000, elapsedTime: 1 });
678+
$animate.triggerCallbackPromise();
679+
656680
//leave
657681
$animate.leave(child);
658682
$rootScope.$digest();

0 commit comments

Comments
 (0)