@@ -464,13 +464,126 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
464
464
* @ngdoc method
465
465
* @name $animate#cancel
466
466
* @kind function
467
- * @description Cancels the provided animation.
468
- *
469
- * @param {Promise } animationPromise The animation promise that is returned when an animation is started.
467
+ * @description Cancels the provided animation and applies the end state of the animation.
468
+ * Note that this does not cancel the underlying operation, e.g. the setting of classes or
469
+ * adding the element to the DOM.
470
+ *
471
+ * @param {animationRunner } animationRunner An animation runner returned by an $animate function.
472
+ *
473
+ * @example
474
+ <example module="animationExample" deps="angular-animate.js" animations="true" name="animate-cancel">
475
+ <file name="app.js">
476
+ angular.module('animationExample', ['ngAnimate']).component('cancelExample', {
477
+ templateUrl: 'template.html',
478
+ controller: function($element, $animate) {
479
+ this.runner = null;
480
+
481
+ this.addClass = function() {
482
+ this.runner = $animate.addClass($element.find('div'), 'red');
483
+ var ctrl = this;
484
+ this.runner.finally(function() {
485
+ ctrl.runner = null;
486
+ });
487
+ };
488
+
489
+ this.removeClass = function() {
490
+ this.runner = $animate.removeClass($element.find('div'), 'red');
491
+ var ctrl = this;
492
+ this.runner.finally(function() {
493
+ ctrl.runner = null;
494
+ });
495
+ };
496
+
497
+ this.cancel = function() {
498
+ $animate.cancel(this.runner);
499
+ };
500
+ }
501
+ });
502
+ </file>
503
+ <file name="template.html">
504
+ <p>
505
+ <button id="add" ng-click="$ctrl.addClass()">Add</button>
506
+ <button ng-click="$ctrl.removeClass()">Remove</button>
507
+ <br>
508
+ <button id="cancel" ng-click="$ctrl.cancel()" ng-disabled="!$ctrl.runner">Cancel</button>
509
+ <br>
510
+ <div id="target">CSS-Animated Text</div>
511
+ </p>
512
+ </file>
513
+ <file name="index.html">
514
+ <cancel-example></cancel-example>
515
+ </file>
516
+ <file name="style.css">
517
+ .red-add, .red-remove {
518
+ transition: all 4s cubic-bezier(0.250, 0.460, 0.450, 0.940);
519
+ }
520
+
521
+ .red,
522
+ .red-add.red-add-active {
523
+ color: #FF0000;
524
+ font-size: 40px;
525
+ }
526
+
527
+ .red-remove.red-remove-active {
528
+ font-size: 10px;
529
+ color: black;
530
+ }
531
+
532
+ </file>
533
+ <file name="protractor.js" type="protractor">
534
+
535
+ it('should cancel the animation', function() {
536
+ //Enable animations (we disable them by default, see protractor-shared-conf.js)
537
+ element(by.css('body')).allowAnimations(true);
538
+ // CSS transitions use $timeout, for which Protractor by default to finish,
539
+ // before executing the next action.
540
+ // In this case, this would mean that the click on "Cancel" is only executed
541
+ // after the transition has finished
542
+ browser.waitForAngularEnabled(false);
543
+
544
+ var target = element(by.id('target'));
545
+
546
+ target.getSize().then(function(size) {
547
+ expect(size.height).toBeGreaterThan(17);
548
+ expect(size.height).toBeLessThan(21);
549
+ });
550
+
551
+ element(by.buttonText('Add')).click();
552
+
553
+ target.getSize().then(function(size) {
554
+ expect(size.height).toBeGreaterThan(18);
555
+ expect(size.height).toBeLessThan(25);
556
+ });
557
+
558
+ var classes = target.getAttribute('class');
559
+
560
+ expect(classes).toContain('ng-animate');
561
+ expect(classes).toContain('red-add-active');
562
+ expect(classes).toContain('red');
563
+
564
+ element(by.buttonText('Cancel')).click();
565
+
566
+ target.getSize().then(function(size) {
567
+ expect(size.height).toBeGreaterThan(45);
568
+ expect(size.height).toBeLessThan(49);
569
+ });
570
+
571
+ classes = target.getAttribute('class');
572
+
573
+ expect(classes).not.toContain('ng-animate');
574
+ expect(classes).not.toContain('red-add-active');
575
+ expect(classes).toContain('red');
576
+
577
+ // Re-enable waiting for AngularJS, and disable animations again
578
+ browser.waitForAngularEnabled(true);
579
+ element(by.css('body')).allowAnimations(false);
580
+ });
581
+ </file>
582
+ </example>
470
583
*/
471
584
cancel : function ( runner ) {
472
- if ( runner . end ) {
473
- runner . end ( ) ;
585
+ if ( runner . cancel ) {
586
+ runner . cancel ( ) ;
474
587
}
475
588
} ,
476
589
@@ -496,7 +609,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
496
609
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
497
610
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
498
611
*
499
- * @return {Promise } the animation callback promise
612
+ * @return {Runner } the animation runner
500
613
*/
501
614
enter : function ( element , parent , after , options ) {
502
615
parent = parent && jqLite ( parent ) ;
@@ -528,7 +641,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
528
641
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
529
642
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
530
643
*
531
- * @return {Promise } the animation callback promise
644
+ * @return {Runner } the animation runner
532
645
*/
533
646
move : function ( element , parent , after , options ) {
534
647
parent = parent && jqLite ( parent ) ;
@@ -555,7 +668,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
555
668
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
556
669
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
557
670
*
558
- * @return {Promise } the animation callback promise
671
+ * @return {Runner } the animation runner
559
672
*/
560
673
leave : function ( element , options ) {
561
674
return $$animateQueue . push ( element , 'leave' , prepareAnimateOptions ( options ) , function ( ) {
@@ -585,7 +698,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
585
698
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
586
699
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
587
700
*
588
- * @return {Promise } the animation callback promise
701
+ * @return {Runner } animationRunner the animation runner
589
702
*/
590
703
addClass : function ( element , className , options ) {
591
704
options = prepareAnimateOptions ( options ) ;
@@ -615,7 +728,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
615
728
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
616
729
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
617
730
*
618
- * @return {Promise } the animation callback promise
731
+ * @return {Runner } the animation runner
619
732
*/
620
733
removeClass : function ( element , className , options ) {
621
734
options = prepareAnimateOptions ( options ) ;
@@ -646,7 +759,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
646
759
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
647
760
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
648
761
*
649
- * @return {Promise } the animation callback promise
762
+ * @return {Runner } the animation runner
650
763
*/
651
764
setClass : function ( element , add , remove , options ) {
652
765
options = prepareAnimateOptions ( options ) ;
@@ -693,7 +806,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
693
806
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
694
807
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
695
808
*
696
- * @return {Promise } the animation callback promise
809
+ * @return {Runner } the animation runner
697
810
*/
698
811
animate : function ( element , from , to , className , options ) {
699
812
options = prepareAnimateOptions ( options ) ;
0 commit comments