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

Commit a02f29c

Browse files
committed
feat($animate): introduce the $animate.animate() method
1 parent cae3239 commit a02f29c

File tree

5 files changed

+116
-6
lines changed

5 files changed

+116
-6
lines changed

Diff for: src/ng/animate.js

+4
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
*

Diff for: src/ngAnimate/animate.js

+77-4
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, options.from, options.to, progress));
709+
break;
706710
case 'addClass':
707711
cancellations.push(animation.fn(element, classNameAdd || className, progress, options));
708712
break;
@@ -822,6 +826,65 @@ angular.module('ngAnimate', ['ng'])
822826
*
823827
*/
824828
return {
829+
/**
830+
* @ngdoc method
831+
* @name $animate#animate
832+
* @kind function
833+
*
834+
* @description
835+
* Performs an inline animation on the element which applies the provided `to` and `from` CSS styles to the element.
836+
* If any detected CSS transition, keyframe or JavaScript matches the provided `className` value then the animation
837+
* will take on the provided styles. For example, if a transition animation is set for the given className then the
838+
* provided `from` and `to` styles will be applied alongside the given transition. If a JavaScript animation is
839+
* detected then the provided styles will be given in as function paramters.
840+
*
841+
* ```js
842+
* ngModule.animation('.my-inline-animation', function() {
843+
* return {
844+
* animate : function(element, className, from, to, done) {
845+
* //styles
846+
* }
847+
* }
848+
* });
849+
* ```
850+
*
851+
* Below is a breakdown of each step that occurs during the `animate` animation:
852+
*
853+
* | Animation Step | What the element class attribute looks like |
854+
* |-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
855+
* | 1. $animate.animate(...) is called | class="my-animation" |
856+
* | 2. $animate waits for the next digest to start the animation | class="my-animation ng-animate" |
857+
* | 3. $animate runs the JavaScript-defined animations detected on the element | class="my-animation ng-animate" |
858+
* | 4. the className class value is added to the element | class="my-animation ng-animate className" |
859+
* | 5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-animate className" |
860+
* | 6. $animate blocks all CSS transitions on the element to ensure the .className class styling is applied right away| class="my-animation ng-animate className" |
861+
* | 7. $animate applies the provided collection of `from` CSS styles to the element | class="my-animation ng-animate className" |
862+
* | 8. $animate waits for a single animation frame (this performs a reflow) | class="my-animation ng-animate className" |
863+
* | 9. $animate removes the CSS transition block placed on the element | class="my-animation ng-animate className" |
864+
* | 10. the className-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-animate className className-active" |
865+
* | 11. $animate applies the collection of `to` CSS styles to the element which are then handled by the transition | class="my-animation ng-animate className className-active" |
866+
* | 12. $animate waits for the animation to complete (via events and timeout) | class="my-animation ng-animate className className-active" |
867+
* | 13. The animation ends and all generated CSS classes are removed from the element | class="my-animation" |
868+
* | 14. The returned promise is resolved. | class="my-animation" |
869+
*
870+
* @param {DOMElement} element the element that will be the focus of the enter animation
871+
* @param {object} from a collection of CSS styles that will be applied to the element at the start of the animation
872+
* @param {object} to a collection of CSS styles that the element will animate towards
873+
* @param {string=} className an optional CSS class that will be added to the element for the duration of the animation (the default class is `ng-inline-animate`)
874+
* @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation
875+
* @return {Promise} the animation callback promise
876+
*/
877+
animate : function(element, from, to, className, options) {
878+
className = className || 'ng-inline-animate';
879+
options = parseAnimateOptions(options) || {};
880+
options.from = to ? from : null;
881+
options.to = to ? to : from;
882+
883+
return runAnimationPostDigest(function(done) {
884+
return performAnimation('animate', className, stripCommentsFromElement(element), null, null, noop, options, done);
885+
});
886+
},
887+
825888
/**
826889
* @ngdoc method
827890
* @name $animate#enter
@@ -1259,7 +1322,10 @@ angular.module('ngAnimate', ['ng'])
12591322
}
12601323
}
12611324

1262-
if (runner.isClassBased && !runner.isSetClassOperation && !skipAnimation) {
1325+
if (runner.isClassBased
1326+
&& !runner.isSetClassOperation
1327+
&& animationEvent != 'animate'
1328+
&& !skipAnimation) {
12631329
skipAnimation = (animationEvent == 'addClass') == element.hasClass(className); //opposite of XOR
12641330
}
12651331

@@ -1950,6 +2016,13 @@ angular.module('ngAnimate', ['ng'])
19502016
}
19512017

19522018
return {
2019+
animate : function(element, className, from, to, animationCompleted, options) {
2020+
options = options || {};
2021+
options.from = from;
2022+
options.to = to;
2023+
return animate('animate', element, className, animationCompleted, options);
2024+
},
2025+
19532026
enter : function(element, animationCompleted, options) {
19542027
options = options || {};
19552028
return animate('enter', element, 'ng-enter', animationCompleted, options);

Diff for: src/ngMock/angular-mocks.js

+1-1
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,

Diff for: test/ng/animateSpec.js

+9
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);

Diff for: test/ngAnimate/animateSpec.js

+25-1
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)