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

Commit 02be700

Browse files
matskoIgorMinar
authored andcommitted
feat($animate): introduce the $animate.animate() method
1 parent e5f4d7b commit 02be700

File tree

5 files changed

+116
-6
lines changed

5 files changed

+116
-6
lines changed

src/ng/animate.js

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ var $AnimateProvider = ['$provide', function($provide) {
170170
* page}.
171171
*/
172172
return {
173+
animate : function(element, from, to) {
174+
applyStyles(element, { from: from, to: to });
175+
return asyncPromise();
176+
},
173177

174178
/**
175179
*

src/ngAnimate/animate.js

+77-4
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,10 @@ angular.module('ngAnimate', ['ng'])
628628
}
629629

630630
var isSetClassOperation = animationEvent == 'setClass';
631-
var isClassBased = isSetClassOperation ||
632-
animationEvent == 'addClass' ||
633-
animationEvent == 'removeClass';
631+
var isClassBased = isSetClassOperation
632+
|| animationEvent == 'addClass'
633+
|| animationEvent == 'removeClass'
634+
|| animationEvent == 'animate';
634635

635636
var currentClassName = element.attr('class');
636637
var classes = currentClassName + ' ' + className;
@@ -700,6 +701,9 @@ angular.module('ngAnimate', ['ng'])
700701
case 'setClass':
701702
cancellations.push(animation.fn(element, classNameAdd, classNameRemove, progress, options));
702703
break;
704+
case 'animate':
705+
cancellations.push(animation.fn(element, className, options.from, options.to, progress));
706+
break;
703707
case 'addClass':
704708
cancellations.push(animation.fn(element, classNameAdd || className, progress, options));
705709
break;
@@ -819,6 +823,65 @@ angular.module('ngAnimate', ['ng'])
819823
*
820824
*/
821825
return {
826+
/**
827+
* @ngdoc method
828+
* @name $animate#animate
829+
* @kind function
830+
*
831+
* @description
832+
* Performs an inline animation on the element which applies the provided `to` and `from` CSS styles to the element.
833+
* If any detected CSS transition, keyframe or JavaScript matches the provided `className` value then the animation
834+
* will take on the provided styles. For example, if a transition animation is set for the given className then the
835+
* provided `from` and `to` styles will be applied alongside the given transition. If a JavaScript animation is
836+
* detected then the provided styles will be given in as function paramters.
837+
*
838+
* ```js
839+
* ngModule.animation('.my-inline-animation', function() {
840+
* return {
841+
* animate : function(element, className, from, to, done) {
842+
* //styles
843+
* }
844+
* }
845+
* });
846+
* ```
847+
*
848+
* Below is a breakdown of each step that occurs during the `animate` animation:
849+
*
850+
* | Animation Step | What the element class attribute looks like |
851+
* |-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
852+
* | 1. $animate.animate(...) is called | class="my-animation" |
853+
* | 2. $animate waits for the next digest to start the animation | class="my-animation ng-animate" |
854+
* | 3. $animate runs the JavaScript-defined animations detected on the element | class="my-animation ng-animate" |
855+
* | 4. the className class value is added to the element | class="my-animation ng-animate className" |
856+
* | 5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-animate className" |
857+
* | 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" |
858+
* | 7. $animate applies the provided collection of `from` CSS styles to the element | class="my-animation ng-animate className" |
859+
* | 8. $animate waits for a single animation frame (this performs a reflow) | class="my-animation ng-animate className" |
860+
* | 9. $animate removes the CSS transition block placed on the element | class="my-animation ng-animate className" |
861+
* | 10. the className-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-animate className className-active" |
862+
* | 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" |
863+
* | 12. $animate waits for the animation to complete (via events and timeout) | class="my-animation ng-animate className className-active" |
864+
* | 13. The animation ends and all generated CSS classes are removed from the element | class="my-animation" |
865+
* | 14. The returned promise is resolved. | class="my-animation" |
866+
*
867+
* @param {DOMElement} element the element that will be the focus of the enter animation
868+
* @param {object} from a collection of CSS styles that will be applied to the element at the start of the animation
869+
* @param {object} to a collection of CSS styles that the element will animate towards
870+
* @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`)
871+
* @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation
872+
* @return {Promise} the animation callback promise
873+
*/
874+
animate : function(element, from, to, className, options) {
875+
className = className || 'ng-inline-animate';
876+
options = parseAnimateOptions(options) || {};
877+
options.from = to ? from : null;
878+
options.to = to ? to : from;
879+
880+
return runAnimationPostDigest(function(done) {
881+
return performAnimation('animate', className, stripCommentsFromElement(element), null, null, noop, options, done);
882+
});
883+
},
884+
822885
/**
823886
* @ngdoc method
824887
* @name $animate#enter
@@ -1256,7 +1319,10 @@ angular.module('ngAnimate', ['ng'])
12561319
}
12571320
}
12581321

1259-
if (runner.isClassBased && !runner.isSetClassOperation && !skipAnimation) {
1322+
if (runner.isClassBased
1323+
&& !runner.isSetClassOperation
1324+
&& animationEvent != 'animate'
1325+
&& !skipAnimation) {
12601326
skipAnimation = (animationEvent == 'addClass') == element.hasClass(className); //opposite of XOR
12611327
}
12621328

@@ -1947,6 +2013,13 @@ angular.module('ngAnimate', ['ng'])
19472013
}
19482014

19492015
return {
2016+
animate : function(element, className, from, to, animationCompleted, options) {
2017+
options = options || {};
2018+
options.from = from;
2019+
options.to = to;
2020+
return animate('animate', element, className, animationCompleted, options);
2021+
},
2022+
19502023
enter : function(element, animationCompleted, options) {
19512024
options = options || {};
19522025
return animate('enter', element, 'ng-enter', animationCompleted, options);

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,

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

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)