@@ -464,13 +464,150 @@ 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('span'), '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('span'), '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
+ this.enter = function() {
502
+ var ctrl = this;
503
+
504
+ this.runner = $animate.enter(this.element, null, $element.find('span'));
505
+ this.runner.catch(function(e) {
506
+ console.log('catch', e);
507
+ ctrl.cancelCount++;
508
+ });
509
+ };
510
+
511
+ this.leave = function() {
512
+ var ctrl = this;
513
+
514
+ this.runner = $animate.leave(this.element);
515
+ this.runner.catch(function(e) {
516
+ ctrl.cancelCount++;
517
+ });
518
+ };
519
+ }
520
+ });
521
+ </file>
522
+ <file name="template.html">
523
+ <p>
524
+ <button id="add" ng-click="$ctrl.addClass()">Add</button>
525
+ <button ng-click="$ctrl.removeClass()">Remove</button>
526
+ <button ng-click="$ctrl.enter()">Enter</button>
527
+ <button ng-click="$ctrl.leave()">Leave</button>
528
+ <br>
529
+ <button id="cancel" ng-click="$ctrl.cancel()" ng-disabled="!$ctrl.runner">Cancel</button>
530
+ <br>
531
+ <span id="target">CSS-Animated Text</span>
532
+ </p>
533
+ </file>
534
+ <file name="index.html">
535
+ <cancel-example></cancel-example>
536
+ </file>
537
+ <file name="style.css">
538
+ .red-add, .red-remove {
539
+ transition: all 4s cubic-bezier(0.250, 0.460, 0.450, 0.940);
540
+ }
541
+
542
+ .red,
543
+ .red-add.red-add-active {
544
+ color: #FF0000;
545
+ font-size: 3em;
546
+ }
547
+
548
+ .red-remove.red-remove-active {
549
+ font-size: 1em;
550
+ color: black;
551
+ }
552
+
553
+ .animate-if {
554
+ background:white;
555
+ border:1px solid black;
556
+ padding:10px;
557
+ }
558
+
559
+ .animate-if.ng-enter, .animate-if.ng-leave {
560
+ transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 5s;
561
+ }
562
+
563
+ .animate-if.ng-enter,
564
+ .animate-if.ng-leave.ng-leave-active {
565
+ opacity:0;
566
+ background: red;
567
+ }
568
+
569
+ .animate-if.ng-leave,
570
+ .animate-if.ng-enter.ng-enter-active {
571
+ opacity:1;
572
+ background: white;
573
+ }
574
+
575
+ </file>
576
+ <file name="protractor.js" type="protractor">
577
+
578
+ it('should cancel the animation', function() {
579
+ //Enable animations (we disable them by default, see protractor-shared-conf.js)
580
+ element(by.css('body')).allowAnimations(true);
581
+ // CSS transitions use $timeout, for which Protractor by default to finish,
582
+ // before executing the next action.
583
+ // In this case, this would mean that the click on "Cancel" is only executed
584
+ // after the transition has finished
585
+ browser.waitForAngularEnabled(false);
586
+
587
+ var target = element(by.id('target'));
588
+
589
+ element(by.buttonText('Add')).click();
590
+
591
+ var classes = target.getAttribute('class');
592
+
593
+ expect(classes).toContain('ng-animate');
594
+ expect(classes).toContain('red-add-active');
595
+ expect(classes).toContain('red');
596
+
597
+ element(by.buttonText('Cancel')).click();
598
+
599
+ classes = target.getAttribute('class');
600
+
601
+ expect(classes).not.toContain('ng-animate');
602
+ expect(classes).not.toContain('red-add-active');
603
+ expect(classes).toContain('red');
604
+ });
605
+ </file>
606
+ </example>
470
607
*/
471
608
cancel : function ( runner ) {
472
- if ( runner . end ) {
473
- runner . end ( ) ;
609
+ if ( runner . cancel ) {
610
+ runner . cancel ( ) ;
474
611
}
475
612
} ,
476
613
@@ -496,7 +633,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
496
633
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
497
634
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
498
635
*
499
- * @return {Promise } the animation callback promise
636
+ * @return {Runner } the animation runner
500
637
*/
501
638
enter : function ( element , parent , after , options ) {
502
639
parent = parent && jqLite ( parent ) ;
@@ -528,7 +665,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
528
665
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
529
666
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
530
667
*
531
- * @return {Promise } the animation callback promise
668
+ * @return {Runner } the animation runner
532
669
*/
533
670
move : function ( element , parent , after , options ) {
534
671
parent = parent && jqLite ( parent ) ;
@@ -555,7 +692,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
555
692
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
556
693
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
557
694
*
558
- * @return {Promise } the animation callback promise
695
+ * @return {Runner } the animation runner
559
696
*/
560
697
leave : function ( element , options ) {
561
698
return $$animateQueue . push ( element , 'leave' , prepareAnimateOptions ( options ) , function ( ) {
@@ -585,7 +722,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
585
722
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
586
723
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
587
724
*
588
- * @return {Promise } the animation callback promise
725
+ * @return {Runner } animationRunner the animation runner
589
726
*/
590
727
addClass : function ( element , className , options ) {
591
728
options = prepareAnimateOptions ( options ) ;
@@ -615,7 +752,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
615
752
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
616
753
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
617
754
*
618
- * @return {Promise } the animation callback promise
755
+ * @return {Runner } the animation runner
619
756
*/
620
757
removeClass : function ( element , className , options ) {
621
758
options = prepareAnimateOptions ( options ) ;
@@ -646,7 +783,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
646
783
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
647
784
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
648
785
*
649
- * @return {Promise } the animation callback promise
786
+ * @return {Runner } the animation runner
650
787
*/
651
788
setClass : function ( element , add , remove , options ) {
652
789
options = prepareAnimateOptions ( options ) ;
@@ -693,7 +830,7 @@ var $AnimateProvider = ['$provide', /** @this */ function($provide) {
693
830
* - **removeClass** - `{string}` - space-separated CSS classes to remove from element
694
831
* - **to** - `{Object}` - CSS properties & values at end of animation. Must have matching `from`
695
832
*
696
- * @return {Promise } the animation callback promise
833
+ * @return {Runner } the animation runner
697
834
*/
698
835
animate : function ( element , from , to , className , options ) {
699
836
options = prepareAnimateOptions ( options ) ;
0 commit comments