@@ -464,13 +464,123 @@ 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 >= 18 && size.height <= 20).toBeTruthy();
548
+ });
549
+
550
+ element(by.buttonText('Add')).click();
551
+
552
+ target.getSize().then(function(size) {
553
+ expect(size.height >= 19 && size.height <= 24).toBeTruthy();
554
+ });
555
+
556
+ var classes = target.getAttribute('class');
557
+
558
+ expect(classes).toContain('ng-animate');
559
+ expect(classes).toContain('red-add-active');
560
+ expect(classes).toContain('red');
561
+
562
+ element(by.buttonText('Cancel')).click();
563
+
564
+ target.getSize().then(function(size) {
565
+ expect(size.height >= 46 && size.height <= 48).toBeTruthy();
566
+ });
567
+
568
+ classes = target.getAttribute('class');
569
+
570
+ expect(classes).not.toContain('ng-animate');
571
+ expect(classes).not.toContain('red-add-active');
572
+ expect(classes).toContain('red');
573
+
574
+ // Re-enable waiting for AngularJS, and disable animations again
575
+ browser.waitForAngularEnabled(true);
576
+ element(by.css('body')).allowAnimations(false);
577
+ });
578
+ </file>
579
+ </example>
470
580
*/
471
581
cancel : function ( runner ) {
472
- if ( runner . end ) {
473
- runner . end ( ) ;
582
+ if ( runner . cancel ) {
583
+ runner . cancel ( ) ;
474
584
}
475
585
} ,
476
586
@@ -496,7 +606,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
496
606
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
497
607
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
498
608
*
499
- * @return {Promise } the animation callback promise
609
+ * @return {Runner } the animation runner
500
610
*/
501
611
enter : function ( element , parent , after , options ) {
502
612
parent = parent && jqLite ( parent ) ;
@@ -528,7 +638,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
528
638
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
529
639
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
530
640
*
531
- * @return {Promise } the animation callback promise
641
+ * @return {Runner } the animation runner
532
642
*/
533
643
move : function ( element , parent , after , options ) {
534
644
parent = parent && jqLite ( parent ) ;
@@ -555,7 +665,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
555
665
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
556
666
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
557
667
*
558
- * @return {Promise } the animation callback promise
668
+ * @return {Runner } the animation runner
559
669
*/
560
670
leave : function ( element , options ) {
561
671
return $$animateQueue . push ( element , 'leave' , prepareAnimateOptions ( options ) , function ( ) {
@@ -585,7 +695,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
585
695
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
586
696
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
587
697
*
588
- * @return {Promise } the animation callback promise
698
+ * @return {Runner } animationRunner the animation runner
589
699
*/
590
700
addClass : function ( element , className , options ) {
591
701
options = prepareAnimateOptions ( options ) ;
@@ -615,7 +725,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
615
725
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
616
726
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
617
727
*
618
- * @return {Promise } the animation callback promise
728
+ * @return {Runner } the animation runner
619
729
*/
620
730
removeClass : function ( element , className , options ) {
621
731
options = prepareAnimateOptions ( options ) ;
@@ -646,7 +756,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
646
756
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
647
757
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
648
758
*
649
- * @return {Promise } the animation callback promise
759
+ * @return {Runner } the animation runner
650
760
*/
651
761
setClass : function ( element , add , remove , options ) {
652
762
options = prepareAnimateOptions ( options ) ;
@@ -693,7 +803,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
693
803
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
694
804
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
695
805
*
696
- * @return {Promise } the animation callback promise
806
+ * @return {Runner } the animation runner
697
807
*/
698
808
animate : function ( element , from , to , className , options ) {
699
809
options = prepareAnimateOptions ( options ) ;
0 commit comments