@@ -203,15 +203,15 @@ angular.module('ngAnimate', ['ng'])
203
203
var NG_ANIMATE_STATE = '$$ngAnimateState' ;
204
204
var rootAnimateState = { running :true } ;
205
205
206
- $provide . decorator ( '$animate' , [ '$delegate' , '$injector' , '$window' , '$ sniffer', '$rootElement' ,
207
- function ( $delegate , $injector , $window , $ sniffer, $rootElement ) {
206
+ $provide . decorator ( '$animate' , [ '$delegate' , '$injector' , '$sniffer' , '$rootElement' ,
207
+ function ( $delegate , $injector , $sniffer , $rootElement ) {
208
208
209
209
var noop = angular . noop ;
210
210
var forEach = angular . forEach ;
211
211
212
212
$rootElement . data ( NG_ANIMATE_STATE , rootAnimateState ) ;
213
213
214
- function lookup ( name ) {
214
+ function lookup ( name ) {
215
215
if ( name ) {
216
216
var classes = name . substr ( 1 ) . split ( '.' ) ,
217
217
classMap = { } ;
@@ -241,7 +241,7 @@ angular.module('ngAnimate', ['ng'])
241
241
/**
242
242
* @ngdoc object
243
243
* @name ngAnimate.$animate
244
- * @requires $window , $sniffer, $rootElement
244
+ * @requires $timeout , $sniffer, $rootElement
245
245
* @function
246
246
*
247
247
* @description
@@ -444,80 +444,72 @@ angular.module('ngAnimate', ['ng'])
444
444
and the onComplete callback will be fired once the animation is fully complete.
445
445
*/
446
446
function performAnimation ( event , className , element , parent , after , onComplete ) {
447
- if ( nothingToAnimate ( className , element ) ) {
448
- ( onComplete || noop ) ( ) ;
449
- } else {
450
- var classes = ( ( element . attr ( 'class' ) || '' ) + ' ' + className ) ,
451
- animationLookup = ( ' ' + classes ) . replace ( / \s + / g, '.' ) ,
452
- animations = [ ] ;
453
- forEach ( lookup ( animationLookup ) , function ( animation , index ) {
454
- animations . push ( {
455
- start : animation [ event ] ,
456
- done : false
457
- } ) ;
447
+ var classes = ( ( element . attr ( 'class' ) || '' ) + ' ' + className ) ,
448
+ animationLookup = ( ' ' + classes ) . replace ( / \s + / g, '.' ) ,
449
+ animations = [ ] ;
450
+ forEach ( lookup ( animationLookup ) , function ( animation , index ) {
451
+ animations . push ( {
452
+ start : animation [ event ]
458
453
} ) ;
454
+ } ) ;
459
455
460
- if ( ! parent ) {
461
- parent = after ? after . parent ( ) : element . parent ( ) ;
462
- }
463
- var disabledAnimation = { running : true } ;
456
+ if ( ! parent ) {
457
+ parent = after ? after . parent ( ) : element . parent ( ) ;
458
+ }
459
+ var disabledAnimation = { running : true } ;
464
460
465
- //skip the animation if animations are disabled, a parent is already being animated
466
- //or the element is not currently attached to the document body.
467
- if ( ( parent . inheritedData ( NG_ANIMATE_STATE ) || disabledAnimation ) . running ) {
468
- //avoid calling done() since there is no need to remove any
469
- //data or className values since this happens earlier than that
470
- ( onComplete || noop ) ( ) ;
471
- return ;
472
- }
461
+ //skip the animation if animations are disabled, a parent is already being animated
462
+ //or the element is not currently attached to the document body.
463
+ if ( ( parent . inheritedData ( NG_ANIMATE_STATE ) || disabledAnimation ) . running ) {
464
+ //avoid calling done() since there is no need to remove any
465
+ //data or className values since this happens earlier than that
466
+ ( onComplete || noop ) ( ) ;
467
+ return ;
468
+ }
473
469
474
- var animationData = element . data ( NG_ANIMATE_STATE ) || { } ;
470
+ var ngAnimateState = element . data ( NG_ANIMATE_STATE ) || { } ;
475
471
476
- //if an animation is currently running on the element then lets take the steps
477
- //to cancel that animation and fire any required callbacks
478
- if ( animationData . running ) {
479
- cancelAnimations ( animationData . animations ) ;
480
- animationData . done ( ) ;
481
- }
472
+ //if an animation is currently running on the element then lets take the steps
473
+ //to cancel that animation and fire any required callbacks
474
+ if ( ngAnimateState . running ) {
475
+ cancelAnimations ( ngAnimateState . animations ) ;
476
+ ngAnimateState . done ( ) ;
477
+ }
482
478
483
- element . data ( NG_ANIMATE_STATE , {
484
- running :true ,
485
- animations :animations ,
486
- done :done
487
- } ) ;
479
+ element . data ( NG_ANIMATE_STATE , {
480
+ running :true ,
481
+ animations :animations ,
482
+ done :done
483
+ } ) ;
488
484
489
- if ( event == 'addClass' ) {
490
- className = suffixClasses ( className , '-add' ) ;
491
- } else if ( event == 'removeClass' ) {
492
- className = suffixClasses ( className , '-remove' ) ;
493
- }
485
+ if ( event == 'addClass' ) {
486
+ className = suffixClasses ( className , '-add' ) ;
487
+ } else if ( event == 'removeClass' ) {
488
+ className = suffixClasses ( className , '-remove' ) ;
489
+ }
494
490
495
- element . addClass ( className ) ;
491
+ element . addClass ( className ) ;
496
492
497
- forEach ( animations , function ( animation , index ) {
498
- var fn = function ( ) {
499
- progress ( index ) ;
500
- } ;
493
+ forEach ( animations , function ( animation , index ) {
494
+ var fn = function ( ) {
495
+ progress ( index ) ;
496
+ } ;
501
497
502
- if ( animation . start ) {
503
- if ( event == 'addClass' || event == 'removeClass' ) {
504
- animation . cancel = animation . start ( element , className , fn ) ;
505
- } else {
506
- animation . cancel = animation . start ( element , fn ) ;
507
- }
498
+ if ( animation . start ) {
499
+ if ( event == 'addClass' || event == 'removeClass' ) {
500
+ animation . endFn = animation . start ( element , className , fn ) ;
508
501
} else {
509
- fn ( ) ;
502
+ animation . endFn = animation . start ( element , fn ) ;
510
503
}
511
- } ) ;
512
- }
513
-
514
- function nothingToAnimate ( className , element ) {
515
- return ! ( className && className . length > 0 && element . length > 0 ) ;
516
- }
504
+ } else {
505
+ fn ( ) ;
506
+ }
507
+ } ) ;
517
508
518
509
function cancelAnimations ( animations ) {
510
+ var isCancelledFlag = true ;
519
511
forEach ( animations , function ( animation ) {
520
- ( animation . cancel || noop ) ( element ) ;
512
+ ( animation . endFn || noop ) ( isCancelledFlag ) ;
521
513
} ) ;
522
514
}
523
515
@@ -534,6 +526,7 @@ angular.module('ngAnimate', ['ng'])
534
526
535
527
function progress ( index ) {
536
528
animations [ index ] . done = true ;
529
+ ( animations [ index ] . endFn || noop ) ( ) ;
537
530
for ( var i = 0 ; i < animations . length ; i ++ ) {
538
531
if ( ! animations [ i ] . done ) return ;
539
532
}
@@ -552,7 +545,7 @@ angular.module('ngAnimate', ['ng'])
552
545
} ] ) ;
553
546
} ] )
554
547
555
- . animation ( '' , [ '$window' , '$sniffer' , function ( $window , $sniffer ) {
548
+ . animation ( '' , [ '$window' , '$sniffer' , '$timeout' , function ( $window , $sniffer , $timeout ) {
556
549
return {
557
550
enter : function ( element , done ) {
558
551
return animate ( element , 'ng-enter' , done ) ;
@@ -576,13 +569,13 @@ angular.module('ngAnimate', ['ng'])
576
569
done ( ) ;
577
570
} else {
578
571
var activeClassName = '' ;
579
- $window . setTimeout ( startAnimation , 1 ) ;
572
+ $timeout ( startAnimation , 1 , false ) ;
580
573
581
574
//this acts as the cancellation function in case
582
575
//a new animation is triggered while another animation
583
576
//is still going on (otherwise the active className
584
577
//would still hang around until the timer is complete).
585
- return onComplete ;
578
+ return onEnd ;
586
579
}
587
580
588
581
function parseMaxTime ( str ) {
@@ -643,12 +636,21 @@ angular.module('ngAnimate', ['ng'])
643
636
}
644
637
} ) ;
645
638
646
- $window . setTimeout ( onComplete , duration * 1000 ) ;
639
+ $timeout ( done , duration * 1000 , false ) ;
647
640
}
648
641
649
- function onComplete ( ) {
642
+ //this will automatically be called by $animate so
643
+ //there is no need to attach this internally to the
644
+ //timeout done method
645
+ function onEnd ( cancelled ) {
650
646
element . removeClass ( activeClassName ) ;
651
- done ( ) ;
647
+
648
+ //only when the animation is cancelled is the done()
649
+ //function not called for this animation therefore
650
+ //this must be also called
651
+ if ( cancelled ) {
652
+ done ( ) ;
653
+ }
652
654
} ;
653
655
} ;
654
656
} ] ) ;
0 commit comments