1/** 2 * @license AngularJS v1.3.20 3 * (c) 2010-2014 Google, Inc. http://angularjs.org 4 * License: MIT 5 */ 6(function(window, angular, undefined) {'use strict'; 7 8/* jshint maxlen: false */ 9 10/** 11 * @ngdoc module 12 * @name ngAnimate 13 * @description 14 * 15 * The `ngAnimate` module provides support for JavaScript, CSS3 transition and CSS3 keyframe animation hooks within existing core and custom directives. 16 * 17 * <div doc-module-components="ngAnimate"></div> 18 * 19 * # Usage 20 * 21 * To see animations in action, all that is required is to define the appropriate CSS classes 22 * or to register a JavaScript animation via the `myModule.animation()` function. The directives that support animation automatically are: 23 * `ngRepeat`, `ngInclude`, `ngIf`, `ngSwitch`, `ngShow`, `ngHide`, `ngView` and `ngClass`. Custom directives can take advantage of animation 24 * by using the `$animate` service. 25 * 26 * Below is a more detailed breakdown of the supported animation events provided by pre-existing ng directives: 27 * 28 * | Directive | Supported Animations | 29 * |----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------| 30 * | {@link ng.directive:ngRepeat#animations ngRepeat} | enter, leave and move | 31 * | {@link ngRoute.directive:ngView#animations ngView} | enter and leave | 32 * | {@link ng.directive:ngInclude#animations ngInclude} | enter and leave | 33 * | {@link ng.directive:ngSwitch#animations ngSwitch} | enter and leave | 34 * | {@link ng.directive:ngIf#animations ngIf} | enter and leave | 35 * | {@link ng.directive:ngClass#animations ngClass} | add and remove (the CSS class(es) present) | 36 * | {@link ng.directive:ngShow#animations ngShow} & {@link ng.directive:ngHide#animations ngHide} | add and remove (the ng-hide class value) | 37 * | {@link ng.directive:form#animation-hooks form} & {@link ng.directive:ngModel#animation-hooks ngModel} | add and remove (dirty, pristine, valid, invalid & all other validations) | 38 * | {@link module:ngMessages#animations ngMessages} | add and remove (ng-active & ng-inactive) | 39 * | {@link module:ngMessages#animations ngMessage} | enter and leave | 40 * 41 * You can find out more information about animations upon visiting each directive page. 42 * 43 * Below is an example of how to apply animations to a directive that supports animation hooks: 44 * 45 * ```html 46 * <style type="text/css"> 47 * .slide.ng-enter, .slide.ng-leave { 48 * -webkit-transition:0.5s linear all; 49 * transition:0.5s linear all; 50 * } 51 * 52 * .slide.ng-enter { } /* starting animations for enter */ 53 * .slide.ng-enter.ng-enter-active { } /* terminal animations for enter */ 54 * .slide.ng-leave { } /* starting animations for leave */ 55 * .slide.ng-leave.ng-leave-active { } /* terminal animations for leave */ 56 * </style> 57 * 58 * <!-- 59 * the animate service will automatically add .ng-enter and .ng-leave to the element 60 * to trigger the CSS transition/animations 61 * --> 62 * <ANY class="slide" ng-include="..."></ANY> 63 * ``` 64 * 65 * Keep in mind that, by default, if an animation is running, any child elements cannot be animated 66 * until the parent element's animation has completed. This blocking feature can be overridden by 67 * placing the `ng-animate-children` attribute on a parent container tag. 68 * 69 * ```html 70 * <div class="slide-animation" ng-if="on" ng-animate-children> 71 * <div class="fade-animation" ng-if="on"> 72 * <div class="explode-animation" ng-if="on"> 73 * ...
74 * </div> 75 * </div> 76 * </div> 77 * ``` 78 * 79 * When the `on` expression value changes and an animation is triggered then each of the elements within 80 * will all animate without the block being applied to child elements. 81 * 82 * ## Are animations run when the application starts? 83 * No they are not. When an application is bootstrapped Angular will disable animations from running to avoid 84 * a frenzy of animations from being triggered as soon as the browser has rendered the screen. For this to work, 85 * Angular will wait for two digest cycles until enabling animations. From there on, any animation-triggering 86 * layout changes in the application will trigger animations as normal. 87 * 88 * In addition, upon bootstrap, if the routing system or any directives or load remote data (via $http) then Angular 89 * will automatically extend the wait time to enable animations once **all** of the outbound HTTP requests 90 * are complete. 91 * 92 * ## CSS-defined Animations 93 * The animate service will automatically apply two CSS classes to the animated element and these two CSS classes 94 * are designed to contain the start and end CSS styling. Both CSS transitions and keyframe animations are supported 95 * and can be used to play along with this naming structure. 96 * 97 * The following code below demonstrates how to perform animations using **CSS transitions** with Angular: 98 * 99 * ```html 100 * <style type="text/css"> 101 * /* 102 * The animate class is apart of the element and the ng-enter class 103 * is attached to the element once the enter animation event is triggered 104 * */ 105 * .reveal-animation.ng-enter { 106 * -webkit-transition: 1s linear all; /* Safari/Chrome */ 107 * transition: 1s linear all; /* All other modern browsers and IE10+ */ 108 * 109 * /* The animation preparation code */ 110 * opacity: 0; 111 * } 112 * 113 * /* 114 * Keep in mind that you want to combine both CSS 115 * classes together to avoid any CSS-specificity 116 * conflicts 117 * */ 118 * .reveal-animation.ng-enter.ng-enter-active { 119 * /* The animation code itself */ 120 * opacity: 1; 121 * } 122 * </style> 123 * 124 * <div class="view-container"> 125 * <div ng-view class="reveal-animation"></div> 126 * </div> 127 * ``` 128 * 129 * The following code below demonstrates how to perform animations using **CSS animations** with Angular: 130 * 131 * ```html 132 * <style type="text/css"> 133 * .reveal-animation.ng-enter { 134 * -webkit-animation: enter_sequence 1s linear; /* Safari/Chrome */ 135 * animation: enter_sequence 1s linear; /* IE10+ and Future Browsers */ 136 * } 137 * @-webkit-keyframes enter_sequence { 138 * from { opacity:0; } 139 * to { opacity:1; } 140 * } 141 * @keyframes enter_sequence { 142 * from { opacity:0; } 143 * to { opacity:1; } 144 * } 145 * </style> 146 * 147 * <div class="view-container"> 148 * <div ng-view class="reveal-animation"></div> 149 * </div> 150 * ``` 151 * 152 * Both CSS3 animations and transitions can be used together and the animate service will figure out the correct duration and delay timing. 153 * 154 * Upon DOM mutation, the event class is added first (something like `ng-enter`), then the browser prepares itself to add 155 * the active class (in this case `ng-enter-active`) which then triggers the animation. The animation module will automatically 156 * detect the CSS code to determine when the animation ends. Once the animation is over then both CSS classes will be 157 * removed from the DOM. If a browser does not support CSS transitions or CSS animations then the animation will start and end 158 * immediately resulting in a DOM element that is at its final state. This final state is when the DOM element 159 * has no CSS transition/animation classes applied to it. 160 * 161 * ### Structural transition animations 162 * 163 * Structural transitions (such as enter, leave and move) will always apply a `0s none` transition 164 * value to force the browser into rendering the styles defined in the setup (`.ng-enter`, `.ng-leave` 165 * or `.ng-move`) class. This means that any active transition animations operating on the element 166 * will be cut off to make way for the enter, leave or move animation. 167 * 168 * ### Class-based transition animations 169 * 170 * Class-based transitions refer to transition animations that are triggered when a CSS class is 171 * added to or removed from the element (via `$animate.addClass`, `$animate.removeClass`, 172 * `$animate.setClass`, or by directives such as `ngClass`, `ngModel` and `form`). 173 * They are different when compared to structural animations since they **do not cancel existing 174 * animations** nor do they **block successive transitions** from rendering on the same element. 175 * This distinction allows for **multiple class-based transitions** to be performed on the same element. 176 * 177 * In addition to ngAnimate supporting the default (natural) functionality of class-based transition 178 * animations, ngAnimate also decorates the element with starting and ending CSS classes to aid the 179 * developer in further styling the element throughout the transition animation. Earlier versions 180 * of ngAnimate may have caused natural CSS transitions to break and not render properly due to 181 * $animate temporarily blocking transitions using `0s none` in order to allow the setup CSS class
182 * (the `-add` or `-remove` class) to be applied without triggering an animation. However, as of 183 * **version 1.3**, this workaround has been removed with ngAnimate and all non-ngAnimate CSS 184 * class transitions are compatible with ngAnimate. 185 * 186 * There is, however, one special case when dealing with class-based transitions in ngAnimate. 187 * When rendering class-based transitions that make use of the setup and active CSS classes 188 * (e.g. `.fade-add` and `.fade-add-active` for when `.fade` is added) be sure to define 189 * the transition value **on the active CSS class** and not the setup class. 190 * 191 * ```css 192 * .fade-add { 193 * /* remember to place a 0s transition here 194 * to ensure that the styles are applied instantly 195 * even if the element already has a transition style */ 196 * transition:0s linear all; 197 * 198 * /* starting CSS styles */ 199 * opacity:1; 200 * } 201 * .fade-add.fade-add-active { 202 * /* this will be the length of the animation */ 203 * transition:1s linear all; 204 * opacity:0; 205 * } 206 * ``` 207 * 208 * The setup CSS class (in this case `.fade-add`) also has a transition style property, however, it 209 * has a duration of zero. This may not be required, however, incase the browser is unable to render 210 * the styling present in this CSS class instantly then it could be that the browser is attempting 211 * to perform an unnecessary transition. 212 * 213 * This workaround, however, does not apply to standard class-based transitions that are rendered 214 * when a CSS class containing a transition is applied to an element: 215 * 216 * ```css 217 * /* this works as expected */ 218 * .fade { 219 * transition:1s linear all; 220 * opacity:0; 221 * } 222 * ``` 223 * 224 * Please keep this in mind when coding the CSS markup that will be used within class-based transitions. 225 * Also, try not to mix the two class-based animation flavors together since the CSS code may become 226 * overly complex. 227 * 228 * 229 * ### Preventing Collisions With Third Party Libraries 230 * 231 * Some third-party frameworks place animation duration defaults across many element or className 232 * selectors in order to make their code small and reuseable. This can lead to issues with ngAnimate, which 233 * is expecting actual animations on these elements and has to wait for their completion. 234 * 235 * You can prevent this unwanted behavior by using a prefix on all your animation classes: 236 * 237 * ```css 238 * /* prefixed with animate- */ 239 * .animate-fade-add.animate-fade-add-active { 240 * transition:1s linear all; 241 * opacity:0; 242 * } 243 * ``` 244 * 245 * You then configure `$animate` to enforce this prefix: 246 * 247 * ```js 248 * $animateProvider.classNameFilter(/animate-/); 249 * ``` 250 * </div> 251 * 252 * ### CSS Staggering Animations 253 * A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a 254 * curtain-like effect. The ngAnimate module (versions >=1.2) supports staggering animations and the stagger effect can be 255 * performed by creating a **ng-EVENT-stagger** CSS class and attaching that class to the base CSS class used for 256 * the animation. The style property expected within the stagger class can either be a **transition-delay** or an 257 * **animation-delay** property (or both if your animation contains both transitions and keyframe animations). 258 * 259 * ```css 260 * .my-animation.ng-enter { 261 * /* standard transition code */ 262 * -webkit-transition: 1s linear all; 263 * transition: 1s linear all; 264 * opacity:0; 265 * } 266 * .my-animation.ng-enter-stagger { 267 * /* this will have a 100ms delay between each successive leave animation */ 268 * -webkit-transition-delay: 0.1s; 269 * transition-delay: 0.1s; 270 * 271 * /* in case the stagger doesn't work then these two values 272 * must be set to 0 to avoid an accidental CSS inheritance */ 273 * -webkit-transition-duration: 0s; 274 * transition-duration: 0s; 275 * } 276 * .my-animation.ng-enter.ng-enter-active { 277 * /* standard transition styles */ 278 * opacity:1; 279 * } 280 * ``` 281 * 282 * Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations 283 * on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this 284 * are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation 285 * will also be reset if more than 10ms has passed after the last animation has been fired. 286 * 287 * The following code will issue the **ng-leave-stagger** event on the element provided: 288 * 289 * ```js 290 * var kids = parent.children(); 291 * 292 * $animate.leave(kids[0]); //stagger index=0 293 * $animate.leave(kids[1]); //stagger index=1 294 * $animate.leave(kids[2]); //stagger index=2 295 * $animate.leave(kids[3]); //stagger index=3 296 * $animate.leave(kids[4]); //stagger index=4 297 * 298 * $timeout(function() { 299 * //stagger has reset itself 300 * $animate.leave(kids[5]); //stagger index=0 301 * $animate.leave(kids[6]); //stagger index=1 302 * }, 100, false); 303 * ``` 304 * 305 * Stagger animations are currently only supported within CSS-defined animations. 306 * 307 * ## JavaScript-defined Animations 308 * In the event that you do not want to use CSS3 transitions or CSS3 animations or if you wish to offer animations on browsers that do not 309 * yet support CSS transitions/animations, then you can make use of JavaScript animations defined inside of your AngularJS module. 310 * 311 * ```js 312 * //!annotate="YourApp" Your AngularJS Module|Replace this or ngModule with the module that you used to define your application. 313 * var ngModule = angular.module('YourApp', ['ngAnimate']); 314 * ngModule.animation('.my-crazy-animation', function() { 315 * return { 316 * enter: function(element, done) { 317 * //run the animation here and call done when the animation is complete 318 * return function(cancelled) { 319 * //this (optional) function will be called when the animation 320 * //completes or when the animation is cancelled (the cancelled 321 * //flag will be set to true if cancelled). 322 * }; 323 * }, 324 * leave: function(element, done) { }, 325 * move: function(element, done) { }, 326 * 327 * //animation that can be triggered before the class is added 328 * beforeAddClass: function(element, className, done) { }, 329 * 330 * //animation that can be triggered after the class is added 331 * addClass: function(element, className, done) { }, 332 * 333 * //animation that can be triggered before the class is removed 334 * beforeRemoveClass: function(element, className, done) { }, 335 * 336 * //animation that can be triggered after the class is removed 337 * removeClass: function(element, className, done) { } 338 * }; 339 * }); 340 * ``` 341 * 342 * JavaScript-defined animations are created with a CSS-like class selector and a collection of events which are set to run 343 * a javascript callback function. When an animation is triggered, $animate will look for a matching animation which fits 344 * the element's CSS class attribute value and then run the matching animation event function (if found). 345 * In other words, if the CSS classes present on the animated element match any of the JavaScript animations then the callback function will 346 * be executed. It should be also noted that only simple, single class selectors are allowed (compound class selectors are not supported). 347 *
348 * Within a JavaScript animation, an object containing various event callback animation functions is expected to be returned. 349 * As explained above, these callbacks are triggered based on the animation event. Therefore if an enter animation is run, 350 * and the JavaScript animation is found, then the enter callback will handle that animation (in addition to the CSS keyframe animation 351 * or transition code that is defined via a stylesheet). 352 * 353 * 354 * ### Applying Directive-specific Styles to an Animation 355 * In some cases a directive or service may want to provide `$animate` with extra details that the animation will 356 * include into its animation. Let's say for example we wanted to render an animation that animates an element 357 * towards the mouse coordinates as to where the user clicked last. By collecting the X/Y coordinates of the click 358 * (via the event parameter) we can set the `top` and `left` styles into an object and pass that into our function 359 * call to `$animate.addClass`. 360 * 361 * ```js 362 * canvas.on('click', function(e) { 363 * $animate.addClass(element, 'on', { 364 * to: { 365 * left : e.client.x + 'px', 366 * top : e.client.y + 'px' 367 * } 368 * }): 369 * }); 370 * ``` 371 * 372 * Now when the animation runs, and a transition or keyframe animation is picked up, then the animation itself will 373 * also include and transition the styling of the `left` and `top` properties into its running animation. If we want 374 * to provide some starting animation values then we can do so by placing the starting animations styles into an object 375 * called `from` in the same object as the `to` animations. 376 * 377 * ```js 378 * canvas.on('click', function(e) { 379 * $animate.addClass(element, 'on', { 380 * from: { 381 * position: 'absolute', 382 * left: '0px', 383 * top: '0px' 384 * }, 385 * to: { 386 * left : e.client.x + 'px', 387 * top : e.client.y + 'px' 388 * } 389 * }): 390 * }); 391 * ``` 392 * 393 * Once the animation is complete or cancelled then the union of both the before and after styles are applied to the 394 * element. If `ngAnimate` is not present then the styles will be applied immediately. 395 * 396 */ 397 398angular.module('ngAnimate', ['ng']) 399 400 /** 401 * @ngdoc provider 402 * @name $animateProvider 403 * @description 404 * 405 * The `$animateProvider` allows developers to register JavaScript animation event handlers directly inside of a module. 406 * When an animation is triggered, the $animate service will query the $animate service to find any animations that match 407 * the provided name value. 408 * 409 * Requires the {@link ngAnimate `ngAnimate`} module to be installed. 410 * 411 * Please visit the {@link ngAnimate `ngAnimate`} module overview page learn more about how to use animations in your application. 412 * 413 */ 414 .directive('ngAnimateChildren', function() { 415 var NG_ANIMATE_CHILDREN = '$$ngAnimateChildren'; 416 return function(scope, element, attrs) { 417 var val = attrs.ngAnimateChildren; 418 if (angular.isString(val) && val.length === 0) { //empty attribute 419 element.data(NG_ANIMATE_CHILDREN, true); 420 } else { 421 scope.$watch(val, function(value) { 422 element.data(NG_ANIMATE_CHILDREN, !!value); 423 }); 424 } 425 }; 426 }) 427 428 //this private service is only used within CSS-enabled animations 429 //IE8 + IE9 do not support rAF natively, but that is fine since they 430 //also don't support transitions and keyframes which means that the code 431 //below will never be used by the two browsers. 432 .factory('$$animateReflow', ['$$rAF', '$document', function($$rAF, $document) { 433 var bod = $document[0].body; 434 return function(fn) { 435 //the returned function acts as the cancellation function 436 return $$rAF(function() { 437 //the line below will force the browser to perform a repaint 438 //so that all the animated elements within the animation frame 439 //will be properly updated and drawn on screen. This is 440 //required to perform multi-class CSS based animations with 441 //Firefox. DO NOT REMOVE THIS LINE. DO NOT OPTIMIZE THIS LINE. 442 //THE MINIFIER WILL REMOVE IT OTHERWISE WHICH WILL RESULT IN AN 443 //UNPREDICTABLE BUG THAT IS VERY HARD TO TRACK DOWN AND WILL 444 //TAKE YEARS AWAY FROM YOUR LIFE! 445 fn(bod.offsetWidth); 446 }); 447 }; 448 }]) 449 450 .config(['$provide', '$animateProvider', function($provide, $animateProvider) { 451 var noop = angular.noop;
452 var forEach = angular.forEach; 453 var selectors = $animateProvider.$$selectors; 454 var isArray = angular.isArray; 455 var isString = angular.isString; 456 var isObject = angular.isObject; 457 458 var ELEMENT_NODE = 1; 459 var NG_ANIMATE_STATE = '$$ngAnimateState'; 460 var NG_ANIMATE_CHILDREN = '$$ngAnimateChildren'; 461 var NG_ANIMATE_CLASS_NAME = 'ng-animate'; 462 var rootAnimateState = {running: true}; 463 464 function extractElementNode(element) { 465 for (var i = 0; i < element.length; i++) { 466 var elm = element[i]; 467 if (elm.nodeType == ELEMENT_NODE) { 468 return elm; 469 } 470 } 471 } 472 473 function prepareElement(element) { 474 return element && angular.element(element); 475 } 476 477 function stripCommentsFromElement(element) { 478 return angular.element(extractElementNode(element)); 479 } 480 481 function isMatchingElement(elm1, elm2) { 482 return extractElementNode(elm1) == extractElementNode(elm2); 483 } 484 var $$jqLite; 485 $provide.decorator('$animate', 486 ['$delegate', '$$q', '$injector', '$sniffer', '$rootElement', '$$asyncCallback', '$rootScope', '$document', '$templateRequest', '$$jqLite', 487 function($delegate, $$q, $injector, $sniffer, $rootElement, $$asyncCallback, $rootScope, $document, $templateRequest, $$$jqLite) { 488 489 $$jqLite = $$$jqLite; 490 $rootElement.data(NG_ANIMATE_STATE, rootAnimateState); 491 492 // Wait until all directive and route-related templates are downloaded and 493 // compiled. The $templateRequest.totalPendingRequests variable keeps track of 494 // all of the remote templates being currently downloaded. If there are no 495 // templates currently downloading then the watcher will still fire anyway. 496 var deregisterWatch = $rootScope.$watch( 497 function() { return $templateRequest.totalPendingRequests; }, 498 function(val, oldVal) { 499 if (val !== 0) return; 500 deregisterWatch(); 501 502 // Now that all templates have been downloaded, $animate will wait until 503 // the post digest queue is empty before enabling animations. By having two 504 // calls to $postDigest calls we can ensure that the flag is enabled at the 505 // very end of the post digest queue. Since all of the animations in $animate 506 // use $postDigest, it's important that the code below executes at the end. 507 // This basically means that the page is fully downloaded and compiled before 508 // any animations are triggered. 509 $rootScope.$$postDigest(function() { 510 $rootScope.$$postDigest(function() { 511 rootAnimateState.running = false; 512 }); 513 }); 514 } 515 ); 516 517 var globalAnimationCounter = 0; 518 var classNameFilter = $animateProvider.classNameFilter(); 519 var isAnimatableClassName = !classNameFilter 520 ? function() { return true; } 521 : function(className) { 522 return classNameFilter.test(className); 523 }; 524 525 function classBasedAnimationsBlocked(element, setter) { 526 var data = element.data(NG_ANIMATE_STATE) || {}; 527 if (setter) { 528 data.running = true; 529 data.structural = true; 530 element.data(NG_ANIMATE_STATE, data); 531 } 532 return data.disabled || (data.running && data.structural); 533 } 534 535 function runAnimationPostDigest(fn) { 536 var cancelFn, defer = $$q.defer(); 537 defer.promise.$$cancelFn = function() { 538 cancelFn && cancelFn(); 539 }; 540 $rootScope.$$postDigest(function() { 541 cancelFn = fn(function() { 542 defer.resolve(); 543 }); 544 }); 545 return defer.promise; 546 } 547 548 function parseAnimateOptions(options) { 549 // some plugin code may still be passing in the callback 550 // function as the last param for the $animate methods so 551 // it's best to only allow string or array values for now 552 if (isObject(options)) { 553 if (options.tempClasses && isString(options.tempClasses)) { 554 options.tempClasses = options.tempClasses.split(/\s+/); 555 } 556 return options; 557 } 558 } 559 560 function resolveElementClasses(element, cache, runningAnimations) { 561 runningAnimations = runningAnimations || {}; 562 563 var lookup = {};
564 forEach(runningAnimations, function(data, selector) { 565 forEach(selector.split(' '), function(s) { 566 lookup[s]=data; 567 }); 568 }); 569 570 var hasClasses = Object.create(null); 571 forEach((element.attr('class') || '').split(/\s+/), function(className) { 572 hasClasses[className] = true; 573 }); 574 575 var toAdd = [], toRemove = []; 576 forEach((cache && cache.classes) || [], function(status, className) { 577 var hasClass = hasClasses[className]; 578 var matchingAnimation = lookup[className] || {}; 579 580 // When addClass and removeClass is called then $animate will check to 581 // see if addClass and removeClass cancel each other out. When there are 582 // more calls to removeClass than addClass then the count falls below 0 583 // and then the removeClass animation will be allowed. Otherwise if the 584 // count is above 0 then that means an addClass animation will commence. 585 // Once an animation is allowed then the code will also check to see if 586 // there exists any on-going animation that is already adding or remvoing 587 // the matching CSS class. 588 if (status === false) { 589 //does it have the class or will it have the class 590 if (hasClass || matchingAnimation.event == 'addClass') { 591 toRemove.push(className); 592 } 593 } else if (status === true) { 594 //is the class missing or will it be removed? 595 if (!hasClass || matchingAnimation.event == 'removeClass') { 596 toAdd.push(className); 597 } 598 } 599 }); 600 601 return (toAdd.length + toRemove.length) > 0 && [toAdd.join(' '), toRemove.join(' ')]; 602 } 603 604 function lookup(name) { 605 if (name) { 606 var matches = [], 607 flagMap = {}, 608 classes = name.substr(1).split('.'); 609 610 //the empty string value is the default animation 611 //operation which performs CSS transition and keyframe 612 //animations sniffing. This is always included for each 613 //element animation procedure if the browser supports 614 //transitions and/or keyframe animations. The default 615 //animation is added to the top of the list to prevent 616 //any previous animations from affecting the element styling 617 //prior to the element being animated. 618 if ($sniffer.transitions || $sniffer.animations) { 619 matches.push($injector.get(selectors[''])); 620 } 621 622 for (var i=0; i < classes.length; i++) { 623 var klass = classes[i], 624 selectorFactoryName = selectors[klass]; 625 if (selectorFactoryName && !flagMap[klass]) { 626 matches.push($injector.get(selectorFactoryName)); 627 flagMap[klass] = true; 628 } 629 } 630 return matches; 631 } 632 } 633 634 function animationRunner(element, animationEvent, className, options) { 635 //transcluded directives may sometimes fire an animation using only comment nodes 636 //best to catch this early on to prevent any animation operations from occurring 637 var node = element[0]; 638 if (!node) { 639 return; 640 } 641 642 if (options) { 643 options.to = options.to || {}; 644 options.from = options.from || {}; 645 } 646 647 var classNameAdd; 648 var classNameRemove; 649 if (isArray(className)) { 650 classNameAdd = className[0]; 651 classNameRemove = className[1]; 652 if (!classNameAdd) { 653 className = classNameRemove; 654 animationEvent = 'removeClass'; 655 } else if (!classNameRemove) { 656 className = classNameAdd; 657 animationEvent = 'addClass'; 658 } else { 659 className = classNameAdd + ' ' + classNameRemove; 660 } 661 } 662 663 var isSetClassOperation = animationEvent == 'setClass'; 664 var isClassBased = isSetClassOperation 665 || animationEvent == 'addClass' 666 || animationEvent == 'removeClass' 667 || animationEvent == 'animate'; 668 669 var currentClassName = element.attr('class'); 670 var classes = currentClassName + ' ' + className; 671 if (!isAnimatableClassName(classes)) { 672 return; 673 } 674 675 var beforeComplete = noop, 676 beforeCancel = [], 677 before = [], 678 afterComplete = noop, 679 afterCancel = [], 680 after = []; 681 682 var animationLookup = (' ' + classes).replace(/\s+/g,'.');
683 forEach(lookup(animationLookup), function(animationFactory) { 684 var created = registerAnimation(animationFactory, animationEvent); 685 if (!created && isSetClassOperation) { 686 registerAnimation(animationFactory, 'addClass'); 687 registerAnimation(animationFactory, 'removeClass'); 688 } 689 }); 690 691 function registerAnimation(animationFactory, event) { 692 var afterFn = animationFactory[event]; 693 var beforeFn = animationFactory['before' + event.charAt(0).toUpperCase() + event.substr(1)]; 694 if (afterFn || beforeFn) { 695 if (event == 'leave') { 696 beforeFn = afterFn; 697 //when set as null then animation knows to skip this phase 698 afterFn = null; 699 } 700 after.push({ 701 event: event, fn: afterFn 702 }); 703 before.push({ 704 event: event, fn: beforeFn 705 }); 706 return true; 707 } 708 } 709 710 function run(fns, cancellations, allCompleteFn) { 711 var animations = []; 712 forEach(fns, function(animation) { 713 animation.fn && animations.push(animation); 714 }); 715 716 var count = 0; 717 function afterAnimationComplete(index) { 718 if (cancellations) { 719 (cancellations[index] || noop)(); 720 if (++count < animations.length) return; 721 cancellations = null; 722 } 723 allCompleteFn(); 724 } 725 726 //The code below adds directly to the array in order to work with 727 //both sync and async animations. Sync animations are when the done() 728 //operation is called right away. DO NOT REFACTOR! 729 forEach(animations, function(animation, index) { 730 var progress = function() { 731 afterAnimationComplete(index); 732 }; 733 switch (animation.event) { 734 case 'setClass': 735 cancellations.push(animation.fn(element, classNameAdd, classNameRemove, progress, options)); 736 break; 737 case 'animate': 738 cancellations.push(animation.fn(element, className, options.from, options.to, progress)); 739 break; 740 case 'addClass': 741 cancellations.push(animation.fn(element, classNameAdd || className, progress, options)); 742 break; 743 case 'removeClass': 744 cancellations.push(animation.fn(element, classNameRemove || className, progress, options)); 745 break; 746 default: 747 cancellations.push(animation.fn(element, progress, options)); 748 break; 749 } 750 }); 751 752 if (cancellations && cancellations.length === 0) { 753 allCompleteFn(); 754 } 755 } 756 757 return { 758 node: node, 759 event: animationEvent, 760 className: className, 761 isClassBased: isClassBased, 762 isSetClassOperation: isSetClassOperation, 763 applyStyles: function() { 764 if (options) { 765 element.css(angular.extend(options.from || {}, options.to || {})); 766 } 767 }, 768 before: function(allCompleteFn) { 769 beforeComplete = allCompleteFn; 770 run(before, beforeCancel, function() { 771 beforeComplete = noop; 772 allCompleteFn(); 773 }); 774 }, 775 after: function(allCompleteFn) { 776 afterComplete = allCompleteFn; 777 run(after, afterCancel, function() { 778 afterComplete = noop; 779 allCompleteFn(); 780 }); 781 }, 782 cancel: function() { 783 if (beforeCancel) { 784 forEach(beforeCancel, function(cancelFn) { 785 (cancelFn || noop)(true); 786 }); 787 beforeComplete(true); 788 } 789 if (afterCancel) { 790 forEach(afterCancel, function(cancelFn) { 791 (cancelFn || noop)(true); 792 }); 793 afterComplete(true); 794 } 795 } 796 }; 797 } 798 799 /** 800 * @ngdoc service 801 * @name $animate 802 * @kind object 803 *
804 * @description 805 * The `$animate` service provides animation detection support while performing DOM operations (enter, leave and move) as well as during addClass and removeClass operations. 806 * When any of these operations are run, the $animate service 807 * will examine any JavaScript-defined animations (which are defined by using the $animateProvider provider object) 808 * as well as any CSS-defined animations against the CSS classes present on the element once the DOM operation is run. 809 * 810 * The `$animate` service is used behind the scenes with pre-existing directives and animation with these directives 811 * will work out of the box without any extra configuration. 812 * 813 * Requires the {@link ngAnimate `ngAnimate`} module to be installed. 814 * 815 * Please visit the {@link ngAnimate `ngAnimate`} module overview page learn more about how to use animations in your application. 816 * ## Callback Promises 817 * With AngularJS 1.3, each of the animation methods, on the `$animate` service, return a promise when called. The 818 * promise itself is then resolved once the animation has completed itself, has been cancelled or has been 819 * skipped due to animations being disabled. (Note that even if the animation is cancelled it will still 820 * call the resolve function of the animation.) 821 * 822 * ```js 823 * $animate.enter(element, container).then(function() { 824 * //...this is called once the animation is complete... 825 * }); 826 * ``` 827 * 828 * Also note that, due to the nature of the callback promise, if any Angular-specific code (like changing the scope, 829 * location of the page, etc...) is executed within the callback promise then be sure to wrap the code using 830 * `$scope.$apply(...)`; 831 * 832 * ```js 833 * $animate.leave(element).then(function() { 834 * $scope.$apply(function() { 835 * $location.path('/new-page'); 836 * }); 837 * }); 838 * ``` 839 * 840 * An animation can also be cancelled by calling the `$animate.cancel(promise)` method with the provided 841 * promise that was returned when the animation was started. 842 * 843 * ```js 844 * var promise = $animate.addClass(element, 'super-long-animation'); 845 * promise.then(function() { 846 * //this will still be called even if cancelled 847 * }); 848 * 849 * element.on('click', function() { 850 * //tooo lazy to wait for the animation to end 851 * $animate.cancel(promise); 852 * }); 853 * ``` 854 * 855 * (Keep in mind that the promise cancellation is unique to `$animate` since promises in 856 * general cannot be cancelled.) 857 * 858 */ 859 return { 860 /** 861 * @ngdoc method 862 * @name $animate#animate 863 * @kind function 864 * 865 * @description 866 * Performs an inline animation on the element which applies the provided `to` and `from` CSS styles to the element. 867 * If any detected CSS transition, keyframe or JavaScript matches the provided `className` value then the animation 868 * will take on the provided styles. For example, if a transition animation is set for the given className then the 869 * provided `from` and `to` styles will be applied alongside the given transition. If a JavaScript animation is 870 * detected then the provided styles will be given in as function paramters. 871 * 872 * ```js 873 * ngModule.animation('.my-inline-animation', function() { 874 * return { 875 * animate : function(element, className, from, to, done) { 876 * //styles 877 * } 878 * } 879 * }); 880 * ``` 881 * 882 * Below is a breakdown of each step that occurs during the `animate` animation: 883 * 884 * | Animation Step | What the element class attribute looks like | 885 * |-----------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------| 886 * | 1. `$animate.animate(...)` is called | `class="my-animation"` | 887 * | 2. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` | 888 * | 3. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` | 889 * | 4. the `className` class value is added to the element | `class="my-animation ng-animate className"` | 890 * | 5. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate className"` | 891 * | 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"` | 892 * | 7. `$animate` applies the provided collection of `from` CSS styles to the element | `class="my-animation ng-animate className"` | 893 * | 8. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate className"` | 894 * | 9. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate className"` | 895 * | 10. the `className-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate className className-active"` | 896 * | 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"` | 897 * | 12. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate className className-active"` | 898 * | 13. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` | 899 * | 14. The returned promise is resolved. | `class="my-animation"` | 900 * 901 * @param {DOMElement} element the element that will be the focus of the enter animation 902 * @param {object} from a collection of CSS styles that will be applied to the element at the start of the animation 903 * @param {object} to a collection of CSS styles that the element will animate towards 904 * @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`) 905 * @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation 906 * @return {Promise} the animation callback promise 907 */ 908 animate: function(element, from, to, className, options) { 909 className = className || 'ng-inline-animate'; 910 options = parseAnimateOptions(options) || {}; 911 options.from = to ? from : null; 912 options.to = to ? to : from; 913 914 return runAnimationPostDigest(function(done) { 915 return performAnimation('animate', className, stripCommentsFromElement(element), null, null, noop, options, done); 916 }); 917 }, 918 919 /** 920 * @ngdoc method 921 * @name $animate#enter 922 * @kind function 923 *
924 * @description 925 * Appends the element to the parentElement element that resides in the document and then runs the enter animation. Once 926 * the animation is started, the following CSS classes will be present on the element for the duration of the animation: 927 * 928 * Below is a breakdown of each step that occurs during enter animation: 929 * 930 * | Animation Step | What the element class attribute looks like | 931 * |-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------| 932 * | 1. `$animate.enter(...)` is called | `class="my-animation"` | 933 * | 2. element is inserted into the `parentElement` element or beside the `afterElement` element | `class="my-animation"` | 934 * | 3. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` | 935 * | 4. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` | 936 * | 5. the `.ng-enter` class is added to the element | `class="my-animation ng-animate ng-enter"` | 937 * | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate ng-enter"` | 938 * | 7. `$animate` blocks all CSS transitions on the element to ensure the `.ng-enter` class styling is applied right away | `class="my-animation ng-animate ng-enter"` | 939 * | 8. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate ng-enter"` | 940 * | 9. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate ng-enter"` | 941 * | 10. the `.ng-enter-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate ng-enter ng-enter-active"` | 942 * | 11. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate ng-enter ng-enter-active"` | 943 * | 12. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` | 944 * | 13. The returned promise is resolved. | `class="my-animation"` | 945 * 946 * @param {DOMElement} element the element that will be the focus of the enter animation 947 * @param {DOMElement} parentElement the parent element of the element that will be the focus of the enter animation 948 * @param {DOMElement} afterElement the sibling element (which is the previous element) of the element that will be the focus of the enter animation 949 * @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation 950 * @return {Promise} the animation callback promise 951 */ 952 enter: function(element, parentElement, afterElement, options) { 953 options = parseAnimateOptions(options); 954 element = angular.element(element); 955 parentElement = prepareElement(parentElement); 956 afterElement = prepareElement(afterElement); 957 958 classBasedAnimationsBlocked(element, true); 959 $delegate.enter(element, parentElement, afterElement); 960 return runAnimationPostDigest(function(done) { 961 return performAnimation('enter', 'ng-enter', stripCommentsFromElement(element), parentElement, afterElement, noop, options, done); 962 }); 963 }, 964 965 /** 966 * @ngdoc method 967 * @name $animate#leave 968 * @kind function 969 *
970 * @description 971 * Runs the leave animation operation and, upon completion, removes the element from the DOM. Once 972 * the animation is started, the following CSS classes will be added for the duration of the animation: 973 * 974 * Below is a breakdown of each step that occurs during leave animation: 975 * 976 * | Animation Step | What the element class attribute looks like | 977 * |-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------| 978 * | 1. `$animate.leave(...)` is called | `class="my-animation"` | 979 * | 2. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` | 980 * | 3. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` | 981 * | 4. the `.ng-leave` class is added to the element | `class="my-animation ng-animate ng-leave"` | 982 * | 5. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate ng-leave"` | 983 * | 6. `$animate` blocks all CSS transitions on the element to ensure the `.ng-leave` class styling is applied right away | `class="my-animation ng-animate ng-leave"` | 984 * | 7. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate ng-leave"` | 985 * | 8. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate ng-leave"` | 986 * | 9. the `.ng-leave-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate ng-leave ng-leave-active"` | 987 * | 10. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate ng-leave ng-leave-active"` | 988 * | 11. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` | 989 * | 12. The element is removed from the DOM | ... | 990 * | 13. The returned promise is resolved. | ... | 991 * 992 * @param {DOMElement} element the element that will be the focus of the leave animation 993 * @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation 994 * @return {Promise} the animation callback promise 995 */ 996 leave: function(element, options) { 997 options = parseAnimateOptions(options); 998 element = angular.element(element); 999 1000 cancelChildAnimations(element); 1001 classBasedAnimationsBlocked(element, true); 1002 return runAnimationPostDigest(function(done) { 1003 return performAnimation('leave', 'ng-leave', stripCommentsFromElement(element), null, null, function() { 1004 $delegate.leave(element); 1005 }, options, done); 1006 }); 1007 }, 1008 1009 /** 1010 * @ngdoc method 1011 * @name $animate#move 1012 * @kind function 1013 *
1014 * @description 1015 * Fires the move DOM operation. Just before the animation starts, the animate service will either append it into the parentElement container or 1016 * add the element directly after the afterElement element if present. Then the move animation will be run. Once 1017 * the animation is started, the following CSS classes will be added for the duration of the animation: 1018 * 1019 * Below is a breakdown of each step that occurs during move animation: 1020 * 1021 * | Animation Step | What the element class attribute looks like | 1022 * |----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------| 1023 * | 1. `$animate.move(...)` is called | `class="my-animation"` | 1024 * | 2. element is moved into the parentElement element or beside the afterElement element | `class="my-animation"` | 1025 * | 3. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` | 1026 * | 4. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` | 1027 * | 5. the `.ng-move` class is added to the element | `class="my-animation ng-animate ng-move"` | 1028 * | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate ng-move"` | 1029 * | 7. `$animate` blocks all CSS transitions on the element to ensure the `.ng-move` class styling is applied right away | `class="my-animation ng-animate ng-move"` | 1030 * | 8. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate ng-move"` | 1031 * | 9. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate ng-move"` | 1032 * | 10. the `.ng-move-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate ng-move ng-move-active"` | 1033 * | 11. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate ng-move ng-move-active"` | 1034 * | 12. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` | 1035 * | 13. The returned promise is resolved. | `class="my-animation"` | 1036 * 1037 * @param {DOMElement} element the element that will be the focus of the move animation 1038 * @param {DOMElement} parentElement the parentElement element of the element that will be the focus of the move animation 1039 * @param {DOMElement} afterElement the sibling element (which is the previous element) of the element that will be the focus of the move animation 1040 * @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation 1041 * @return {Promise} the animation callback promise 1042 */ 1043 move: function(element, parentElement, afterElement, options) { 1044 options = parseAnimateOptions(options); 1045 element = angular.element(element); 1046 parentElement = prepareElement(parentElement); 1047 afterElement = prepareElement(afterElement); 1048 1049 cancelChildAnimations(element); 1050 classBasedAnimationsBlocked(element, true); 1051 $delegate.move(element, parentElement, afterElement); 1052 return runAnimationPostDigest(function(done) { 1053 return performAnimation('move', 'ng-move', stripCommentsFromElement(element), parentElement, afterElement, noop, options, done); 1054 }); 1055 }, 1056 1057 /** 1058 * @ngdoc method 1059 * @name $animate#addClass 1060 *
1061 * @description 1062 * Triggers a custom animation event based off the className variable and then attaches the className value to the element as a CSS class. 1063 * Unlike the other animation methods, the animate service will suffix the className value with {@type -add} in order to provide 1064 * the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if no CSS transitions 1065 * or keyframes are defined on the -add-active or base CSS class). 1066 * 1067 * Below is a breakdown of each step that occurs during addClass animation: 1068 * 1069 * | Animation Step | What the element class attribute looks like | 1070 * |--------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------| 1071 * | 1. `$animate.addClass(element, 'super')` is called | `class="my-animation"` | 1072 * | 2. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` | 1073 * | 3. the `.super-add` class is added to the element | `class="my-animation ng-animate super-add"` | 1074 * | 4. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate super-add"` | 1075 * | 5. the `.super` and `.super-add-active` classes are added (this triggers the CSS transition/animation) | `class="my-animation ng-animate super super-add super-add-active"` | 1076 * | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate super super-add super-add-active"` | 1077 * | 7. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate super super-add super-add-active"` | 1078 * | 8. The animation ends and all generated CSS classes are removed from the element | `class="my-animation super"` | 1079 * | 9. The super class is kept on the element | `class="my-animation super"` | 1080 * | 10. The returned promise is resolved. | `class="my-animation super"` | 1081 * 1082 * @param {DOMElement} element the element that will be animated 1083 * @param {string} className the CSS class that will be added to the element and then animated 1084 * @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation 1085 * @return {Promise} the animation callback promise 1086 */ 1087 addClass: function(element, className, options) { 1088 return this.setClass(element, className, [], options); 1089 }, 1090 1091 /** 1092 * @ngdoc method 1093 * @name $animate#removeClass 1094 * 1095 * @description 1096 * Triggers a custom animation event based off the className variable and then removes the CSS class provided by the className value 1097 * from the element. Unlike the other animation methods, the animate service will suffix the className value with {@type -remove} in 1098 * order to provide the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if 1099 * no CSS transitions or keyframes are defined on the -remove or base CSS classes). 1100 * 1101 * Below is a breakdown of each step that occurs during removeClass animation: 1102 * 1103 * | Animation Step | What the element class attribute looks like | 1104 * |----------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------| 1105 * | 1. `$animate.removeClass(element, 'super')` is called | `class="my-animation super"` | 1106 * | 2. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation super ng-animate"` | 1107 * | 3. the `.super-remove` class is added to the element | `class="my-animation super ng-animate super-remove"` | 1108 * | 4. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation super ng-animate super-remove"` | 1109 * | 5. the `.super-remove-active` classes are added and `.super` is removed (this triggers the CSS transition/animation) | `class="my-animation ng-animate super-remove super-remove-active"` | 1110 * | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate super-remove super-remove-active"` | 1111 * | 7. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate super-remove super-remove-active"` | 1112 * | 8. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` | 1113 * | 9. The returned promise is resolved. | `class="my-animation"` | 1114 * 1115 * 1116 * @param {DOMElement} element the element that will be animated 1117 * @param {string} className the CSS class that will be animated and then removed from the element 1118 * @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation 1119 * @return {Promise} the animation callback promise 1120 */ 1121 removeClass: function(element, className, options) { 1122 return this.setClass(element, [], className, options); 1123 }, 1124 1125 /** 1126 * 1127 * @ngdoc method 1128 * @name $animate#setClass 1129 *
1130 * @description Adds and/or removes the given CSS classes to and from the element. 1131 * Once complete, the `done()` callback will be fired (if provided). 1132 * 1133 * | Animation Step | What the element class attribute looks like | 1134 * |----------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------| 1135 * | 1. `$animate.setClass(element, 'on', 'off')` is called | `class="my-animation off"` | 1136 * | 2. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate off"` | 1137 * | 3. the `.on-add` and `.off-remove` classes are added to the element | `class="my-animation ng-animate on-add off-remove off"` | 1138 * | 4. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate on-add off-remove off"` | 1139 * | 5. the `.on`, `.on-add-active` and `.off-remove-active` classes are added and `.off` is removed (this triggers the CSS transition/animation) | `class="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"` | 1140 * | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"` | 1141 * | 7. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"` | 1142 * | 8. The animation ends and all generated CSS classes are removed from the element | `class="my-animation on"` | 1143 * | 9. The returned promise is resolved. | `class="my-animation on"` | 1144 * 1145 * @param {DOMElement} element the element which will have its CSS classes changed 1146 * removed from it 1147 * @param {string} add the CSS classes which will be added to the element 1148 * @param {string} remove the CSS class which will be removed from the element 1149 * CSS classes have been set on the element 1150 * @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation 1151 * @return {Promise} the animation callback promise 1152 */ 1153 setClass: function(element, add, remove, options) { 1154 options = parseAnimateOptions(options); 1155 1156 var STORAGE_KEY = '$$animateClasses'; 1157 element = angular.element(element); 1158 element = stripCommentsFromElement(element); 1159 1160 if (classBasedAnimationsBlocked(element)) { 1161 return $delegate.$$setClassImmediately(element, add, remove, options); 1162 } 1163 1164 // we're using a combined array for both the add and remove 1165 // operations since the ORDER OF addClass and removeClass matters 1166 var classes, cache = element.data(STORAGE_KEY); 1167 var hasCache = !!cache; 1168 if (!cache) { 1169 cache = {}; 1170 cache.classes = {}; 1171 } 1172 classes = cache.classes; 1173 1174 add = isArray(add) ? add : add.split(' ');
1175 forEach(add, function(c) { 1176 if (c && c.length) { 1177 classes[c] = true; 1178 } 1179 }); 1180 1181 remove = isArray(remove) ? remove : remove.split(' '); 1182 forEach(remove, function(c) { 1183 if (c && c.length) { 1184 classes[c] = false; 1185 } 1186 }); 1187 1188 if (hasCache) { 1189 if (options && cache.options) { 1190 cache.options = angular.extend(cache.options || {}, options); 1191 } 1192 1193 //the digest cycle will combine all the animations into one function 1194 return cache.promise; 1195 } else { 1196 element.data(STORAGE_KEY, cache = { 1197 classes: classes, 1198 options: options 1199 }); 1200 } 1201 1202 return cache.promise = runAnimationPostDigest(function(done) { 1203 var cache, parentNode, parentElement, elementNode = extractElementNode(element); 1204 if (elementNode) { 1205 cache = element.data(STORAGE_KEY); 1206 element.removeData(STORAGE_KEY); 1207 1208 parentElement = element.parent(); 1209 parentNode = elementNode.parentNode; 1210 } 1211 1212 // TODO(matsko): move this code into the animationsDisabled() function once #8092 is fixed 1213 if (!parentNode || parentNode['$$NG_REMOVED'] || elementNode['$$NG_REMOVED']) { 1214 done(); 1215 return; 1216 } 1217 1218 var state = element.data(NG_ANIMATE_STATE) || {}; 1219 var classes = resolveElementClasses(element, cache, state.active); 1220 return !classes 1221 ? done() 1222 : performAnimation('setClass', classes, element, parentElement, null, function() { 1223 if (classes[0]) $delegate.$$addClassImmediately(element, classes[0]); 1224 if (classes[1]) $delegate.$$removeClassImmediately(element, classes[1]); 1225 }, cache.options, done); 1226 }); 1227 }, 1228 1229 /** 1230 * @ngdoc method 1231 * @name $animate#cancel 1232 * @kind function 1233 * 1234 * @param {Promise} animationPromise The animation promise that is returned when an animation is started. 1235 * 1236 * @description 1237 * Cancels the provided animation. 1238 */ 1239 cancel: function(promise) { 1240 promise.$$cancelFn(); 1241 }, 1242 1243 /** 1244 * @ngdoc method 1245 * @name $animate#enabled 1246 * @kind function 1247 * 1248 * @param {boolean=} value If provided then set the animation on or off. 1249 * @param {DOMElement=} element If provided then the element will be used to represent the enable/disable operation 1250 * @return {boolean} Current animation state. 1251 * 1252 * @description 1253 * Globally enables/disables animations. 1254 * 1255 */ 1256 enabled: function(value, element) { 1257 switch (arguments.length) { 1258 case 2: 1259 if (value) { 1260 cleanup(element); 1261 } else { 1262 var data = element.data(NG_ANIMATE_STATE) || {}; 1263 data.disabled = true; 1264 element.data(NG_ANIMATE_STATE, data); 1265 } 1266 break; 1267 1268 case 1: 1269 rootAnimateState.disabled = !value; 1270 break; 1271 1272 default: 1273 value = !rootAnimateState.disabled; 1274 break; 1275 } 1276 return !!value; 1277 } 1278 }; 1279 1280 /* 1281 all animations call this shared animation triggering function internally. 1282 The animationEvent variable refers to the JavaScript animation event that will be triggered 1283 and the className value is the name of the animation that will be applied within the 1284 CSS code. Element, `parentElement` and `afterElement` are provided DOM elements for the animation 1285 and the onComplete callback will be fired once the animation is fully complete. 1286 */ 1287 function performAnimation(animationEvent, className, element, parentElement, afterElement, domOperation, options, doneCallback) { 1288 var noopCancel = noop; 1289 var runner = animationRunner(element, animationEvent, className, options); 1290 if (!runner) { 1291 fireDOMOperation(); 1292 fireBeforeCallbackAsync(); 1293 fireAfterCallbackAsync(); 1294 closeAnimation(); 1295 return noopCancel; 1296 } 1297 1298 animationEvent = runner.event; 1299 className = runner.className; 1300 var elementEvents = angular.element._data(runner.node); 1301 elementEvents = elementEvents && elementEvents.events; 1302 1303 if (!parentElement) { 1304 parentElement = afterElement ? afterElement.parent() : element.parent(); 1305 } 1306 1307 //skip the animation if animations are disabled, a parent is already being animated, 1308 //the element is not currently attached to the document body or then completely close 1309 //the animation if any matching animations are not found at all. 1310 //NOTE: IE8 + IE9 should close properly (run closeAnimation()) in case an animation was found. 1311 if (animationsDisabled(element, parentElement)) { 1312 fireDOMOperation(); 1313 fireBeforeCallbackAsync(); 1314 fireAfterCallbackAsync(); 1315 closeAnimation(); 1316 return noopCancel; 1317 } 1318 1319 var ngAnimateState = element.data(NG_ANIMATE_STATE) || {}; 1320 var runningAnimations = ngAnimateState.active || {}; 1321 var totalActiveAnimations = ngAnimateState.totalActive || 0; 1322 var lastAnimation = ngAnimateState.last; 1323 var skipAnimation = false;
1324 1325 if (totalActiveAnimations > 0) { 1326 var animationsToCancel = []; 1327 if (!runner.isClassBased) { 1328 if (animationEvent == 'leave' && runningAnimations['ng-leave']) { 1329 skipAnimation = true; 1330 } else { 1331 //cancel all animations when a structural animation takes place 1332 for (var klass in runningAnimations) { 1333 animationsToCancel.push(runningAnimations[klass]); 1334 } 1335 ngAnimateState = {}; 1336 cleanup(element, true); 1337 } 1338 } else if (lastAnimation.event == 'setClass') { 1339 animationsToCancel.push(lastAnimation); 1340 cleanup(element, className); 1341 } else if (runningAnimations[className]) { 1342 var current = runningAnimations[className]; 1343 if (current.event == animationEvent) { 1344 skipAnimation = true; 1345 } else { 1346 animationsToCancel.push(current); 1347 cleanup(element, className); 1348 } 1349 } 1350 1351 if (animationsToCancel.length > 0) { 1352 forEach(animationsToCancel, function(operation) { 1353 operation.cancel(); 1354 }); 1355 } 1356 } 1357 1358 if (runner.isClassBased 1359 && !runner.isSetClassOperation 1360 && animationEvent != 'animate' 1361 && !skipAnimation) { 1362 skipAnimation = (animationEvent == 'addClass') == element.hasClass(className); //opposite of XOR 1363 } 1364 1365 if (skipAnimation) { 1366 fireDOMOperation(); 1367 fireBeforeCallbackAsync(); 1368 fireAfterCallbackAsync(); 1369 fireDoneCallbackAsync(); 1370 return noopCancel; 1371 } 1372 1373 runningAnimations = ngAnimateState.active || {}; 1374 totalActiveAnimations = ngAnimateState.totalActive || 0; 1375 1376 if (animationEvent == 'leave') { 1377 //there's no need to ever remove the listener since the element 1378 //will be removed (destroyed) after the leave animation ends or 1379 //is cancelled midway 1380 element.one('$destroy', function(e) { 1381 var element = angular.element(this); 1382 var state = element.data(NG_ANIMATE_STATE); 1383 if (state) { 1384 var activeLeaveAnimation = state.active['ng-leave']; 1385 if (activeLeaveAnimation) { 1386 activeLeaveAnimation.cancel(); 1387 cleanup(element, 'ng-leave'); 1388 } 1389 } 1390 }); 1391 } 1392 1393 //the ng-animate class does nothing, but it's here to allow for 1394 //parent animations to find and cancel child animations when needed 1395 $$jqLite.addClass(element, NG_ANIMATE_CLASS_NAME); 1396 if (options && options.tempClasses) { 1397 forEach(options.tempClasses, function(className) { 1398 $$jqLite.addClass(element, className); 1399 }); 1400 } 1401 1402 var localAnimationCount = globalAnimationCounter++; 1403 totalActiveAnimations++; 1404 runningAnimations[className] = runner; 1405 1406 element.data(NG_ANIMATE_STATE, { 1407 last: runner, 1408 active: runningAnimations, 1409 index: localAnimationCount, 1410 totalActive: totalActiveAnimations 1411 }); 1412 1413 //first we run the before animations and when all of those are complete 1414 //then we perform the DOM operation and run the next set of animations 1415 fireBeforeCallbackAsync(); 1416 runner.before(function(cancelled) { 1417 var data = element.data(NG_ANIMATE_STATE); 1418 cancelled = cancelled || 1419 !data || !data.active[className] || 1420 (runner.isClassBased && data.active[className].event != animationEvent); 1421 1422 fireDOMOperation(); 1423 if (cancelled === true) { 1424 closeAnimation(); 1425 } else { 1426 fireAfterCallbackAsync(); 1427 runner.after(closeAnimation); 1428 } 1429 }); 1430 1431 return runner.cancel; 1432 1433 function fireDOMCallback(animationPhase) { 1434 var eventName = '$animate:' + animationPhase; 1435 if (elementEvents && elementEvents[eventName] && elementEvents[eventName].length > 0) { 1436 $$asyncCallback(function() { 1437 element.triggerHandler(eventName, { 1438 event: animationEvent, 1439 className: className 1440 }); 1441 }); 1442 } 1443 } 1444 1445 function fireBeforeCallbackAsync() { 1446 fireDOMCallback('before'); 1447 } 1448 1449 function fireAfterCallbackAsync() { 1450 fireDOMCallback('after'); 1451 } 1452 1453 function fireDoneCallbackAsync() { 1454 fireDOMCallback('close'); 1455 doneCallback(); 1456 } 1457 1458 //it is less complicated to use a flag than managing and canceling 1459 //timeouts containing multiple callbacks. 1460 function fireDOMOperation() { 1461 if (!fireDOMOperation.hasBeenRun) { 1462 fireDOMOperation.hasBeenRun = true; 1463 domOperation(); 1464 } 1465 } 1466 1467 function closeAnimation() { 1468 if (!closeAnimation.hasBeenRun) { 1469 if (runner) { //the runner doesn't exist if it fails to instantiate 1470 runner.applyStyles(); 1471 } 1472 1473 closeAnimation.hasBeenRun = true;
1474 if (options && options.tempClasses) { 1475 forEach(options.tempClasses, function(className) { 1476 $$jqLite.removeClass(element, className); 1477 }); 1478 } 1479 1480 var data = element.data(NG_ANIMATE_STATE); 1481 if (data) { 1482 1483 /* only structural animations wait for reflow before removing an 1484 animation, but class-based animations don't. An example of this 1485 failing would be when a parent HTML tag has a ng-class attribute 1486 causing ALL directives below to skip animations during the digest */ 1487 if (runner && runner.isClassBased) { 1488 cleanup(element, className); 1489 } else { 1490 $$asyncCallback(function() { 1491 var data = element.data(NG_ANIMATE_STATE) || {}; 1492 if (localAnimationCount == data.index) { 1493 cleanup(element, className, animationEvent); 1494 } 1495 }); 1496 element.data(NG_ANIMATE_STATE, data); 1497 } 1498 } 1499 fireDoneCallbackAsync(); 1500 } 1501 } 1502 } 1503 1504 function cancelChildAnimations(element) { 1505 var node = extractElementNode(element); 1506 if (node) { 1507 var nodes = angular.isFunction(node.getElementsByClassName) ? 1508 node.getElementsByClassName(NG_ANIMATE_CLASS_NAME) : 1509 node.querySelectorAll('.' + NG_ANIMATE_CLASS_NAME); 1510 forEach(nodes, function(element) { 1511 element = angular.element(element); 1512 var data = element.data(NG_ANIMATE_STATE); 1513 if (data && data.active) { 1514 forEach(data.active, function(runner) { 1515 runner.cancel(); 1516 }); 1517 } 1518 }); 1519 } 1520 } 1521 1522 function cleanup(element, className) { 1523 if (isMatchingElement(element, $rootElement)) { 1524 if (!rootAnimateState.disabled) { 1525 rootAnimateState.running = false; 1526 rootAnimateState.structural = false; 1527 } 1528 } else if (className) { 1529 var data = element.data(NG_ANIMATE_STATE) || {}; 1530 1531 var removeAnimations = className === true; 1532 if (!removeAnimations && data.active && data.active[className]) { 1533 data.totalActive--; 1534 delete data.active[className]; 1535 } 1536 1537 if (removeAnimations || !data.totalActive) { 1538 $$jqLite.removeClass(element, NG_ANIMATE_CLASS_NAME); 1539 element.removeData(NG_ANIMATE_STATE); 1540 } 1541 } 1542 } 1543 1544 function animationsDisabled(element, parentElement) { 1545 if (rootAnimateState.disabled) { 1546 return true; 1547 } 1548 1549 if (isMatchingElement(element, $rootElement)) { 1550 return rootAnimateState.running; 1551 } 1552 1553 var allowChildAnimations, parentRunningAnimation, hasParent; 1554 do { 1555 //the element did not reach the root element which means that it 1556 //is not apart of the DOM. Therefore there is no reason to do 1557 //any animations on it 1558 if (parentElement.length === 0) break; 1559 1560 var isRoot = isMatchingElement(parentElement, $rootElement); 1561 var state = isRoot ? rootAnimateState : (parentElement.data(NG_ANIMATE_STATE) || {}); 1562 if (state.disabled) { 1563 return true; 1564 } 1565 1566 //no matter what, for an animation to work it must reach the root element 1567 //this implies that the element is attached to the DOM when the animation is run 1568 if (isRoot) { 1569 hasParent = true; 1570 } 1571 1572 //once a flag is found that is strictly false then everything before 1573 //it will be discarded and all child animations will be restricted 1574 if (allowChildAnimations !== false) { 1575 var animateChildrenFlag = parentElement.data(NG_ANIMATE_CHILDREN); 1576 if (angular.isDefined(animateChildrenFlag)) { 1577 allowChildAnimations = animateChildrenFlag; 1578 } 1579 } 1580 1581 parentRunningAnimation = parentRunningAnimation || 1582 state.running || 1583 (state.last && !state.last.isClassBased); 1584 } 1585 while (parentElement = parentElement.parent()); 1586 1587 return !hasParent || (!allowChildAnimations && parentRunningAnimation); 1588 } 1589 }]); 1590 1591 $animateProvider.register('', ['$window', '$sniffer', '$timeout', '$$animateReflow', 1592 function($window, $sniffer, $timeout, $$animateReflow) { 1593 // Detect proper transitionend/animationend event names. 1594 var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMATIONEND_EVENT; 1595 1596 // If unprefixed events are not supported but webkit-prefixed are, use the latter.
1597 // Otherwise, just use W3C names, browsers not supporting them at all will just ignore them. 1598 // Note: Chrome implements `window.onwebkitanimationend` and doesn't implement `window.onanimationend` 1599 // but at the same time dispatches the `animationend` event and not `webkitAnimationEnd`. 1600 // Register both events in case `window.onanimationend` is not supported because of that, 1601 // do the same for `transitionend` as Safari is likely to exhibit similar behavior. 1602 // Also, the only modern browser that uses vendor prefixes for transitions/keyframes is webkit 1603 // therefore there is no reason to test anymore for other vendor prefixes: http://caniuse.com/#search=transition 1604 if (window.ontransitionend === undefined && window.onwebkittransitionend !== undefined) { 1605 CSS_PREFIX = '-webkit-'; 1606 TRANSITION_PROP = 'WebkitTransition'; 1607 TRANSITIONEND_EVENT = 'webkitTransitionEnd transitionend'; 1608 } else { 1609 TRANSITION_PROP = 'transition'; 1610 TRANSITIONEND_EVENT = 'transitionend'; 1611 } 1612 1613 if (window.onanimationend === undefined && window.onwebkitanimationend !== undefined) { 1614 CSS_PREFIX = '-webkit-'; 1615 ANIMATION_PROP = 'WebkitAnimation'; 1616 ANIMATIONEND_EVENT = 'webkitAnimationEnd animationend'; 1617 } else { 1618 ANIMATION_PROP = 'animation'; 1619 ANIMATIONEND_EVENT = 'animationend'; 1620 } 1621 1622 var DURATION_KEY = 'Duration'; 1623 var PROPERTY_KEY = 'Property'; 1624 var DELAY_KEY = 'Delay'; 1625 var ANIMATION_ITERATION_COUNT_KEY = 'IterationCount'; 1626 var ANIMATION_PLAYSTATE_KEY = 'PlayState'; 1627 var NG_ANIMATE_PARENT_KEY = '$$ngAnimateKey'; 1628 var NG_ANIMATE_CSS_DATA_KEY = '$$ngAnimateCSS3Data'; 1629 var ELAPSED_TIME_MAX_DECIMAL_PLACES = 3; 1630 var CLOSING_TIME_BUFFER = 1.5; 1631 var ONE_SECOND = 1000; 1632 1633 var lookupCache = {}; 1634 var parentCounter = 0; 1635 var animationReflowQueue = []; 1636 var cancelAnimationReflow; 1637 function clearCacheAfterReflow() { 1638 if (!cancelAnimationReflow) { 1639 cancelAnimationReflow = $$animateReflow(function() { 1640 animationReflowQueue = []; 1641 cancelAnimationReflow = null; 1642 lookupCache = {}; 1643 }); 1644 } 1645 } 1646 1647 function afterReflow(element, callback) { 1648 if (cancelAnimationReflow) { 1649 cancelAnimationReflow(); 1650 } 1651 animationReflowQueue.push(callback); 1652 cancelAnimationReflow = $$animateReflow(function() { 1653 forEach(animationReflowQueue, function(fn) { 1654 fn(); 1655 }); 1656 1657 animationReflowQueue = []; 1658 cancelAnimationReflow = null; 1659 lookupCache = {}; 1660 }); 1661 } 1662 1663 var closingTimer = null; 1664 var closingTimestamp = 0; 1665 var animationElementQueue = []; 1666 function animationCloseHandler(element, totalTime) { 1667 var node = extractElementNode(element); 1668 element = angular.element(node); 1669 1670 //this item will be garbage collected by the closing 1671 //animation timeout 1672 animationElementQueue.push(element); 1673 1674 //but it may not need to cancel out the existing timeout 1675 //if the timestamp is less than the previous one 1676 var futureTimestamp = Date.now() + totalTime; 1677 if (futureTimestamp <= closingTimestamp) { 1678 return; 1679 } 1680 1681 $timeout.cancel(closingTimer); 1682 1683 closingTimestamp = futureTimestamp; 1684 closingTimer = $timeout(function() { 1685 closeAllAnimations(animationElementQueue); 1686 animationElementQueue = []; 1687 }, totalTime, false); 1688 } 1689 1690 function closeAllAnimations(elements) { 1691 forEach(elements, function(element) { 1692 var elementData = element.data(NG_ANIMATE_CSS_DATA_KEY); 1693 if (elementData) { 1694 forEach(elementData.closeAnimationFns, function(fn) { 1695 fn(); 1696 }); 1697 } 1698 }); 1699 } 1700 1701 function getElementAnimationDetails(element, cacheKey) { 1702 var data = cacheKey ? lookupCache[cacheKey] : null; 1703 if (!data) { 1704 var transitionDuration = 0; 1705 var transitionDelay = 0; 1706 var animationDuration = 0; 1707 var animationDelay = 0; 1708 1709 //we want all the styles defined before and after
1710 forEach(element, function(element) { 1711 if (element.nodeType == ELEMENT_NODE) { 1712 var elementStyles = $window.getComputedStyle(element) || {}; 1713 1714 var transitionDurationStyle = elementStyles[TRANSITION_PROP + DURATION_KEY]; 1715 transitionDuration = Math.max(parseMaxTime(transitionDurationStyle), transitionDuration); 1716 1717 var transitionDelayStyle = elementStyles[TRANSITION_PROP + DELAY_KEY]; 1718 transitionDelay = Math.max(parseMaxTime(transitionDelayStyle), transitionDelay); 1719 1720 var animationDelayStyle = elementStyles[ANIMATION_PROP + DELAY_KEY]; 1721 animationDelay = Math.max(parseMaxTime(elementStyles[ANIMATION_PROP + DELAY_KEY]), animationDelay); 1722 1723 var aDuration = parseMaxTime(elementStyles[ANIMATION_PROP + DURATION_KEY]); 1724 1725 if (aDuration > 0) { 1726 aDuration *= parseInt(elementStyles[ANIMATION_PROP + ANIMATION_ITERATION_COUNT_KEY], 10) || 1; 1727 } 1728 animationDuration = Math.max(aDuration, animationDuration); 1729 } 1730 }); 1731 data = { 1732 total: 0, 1733 transitionDelay: transitionDelay, 1734 transitionDuration: transitionDuration, 1735 animationDelay: animationDelay, 1736 animationDuration: animationDuration 1737 }; 1738 if (cacheKey) { 1739 lookupCache[cacheKey] = data; 1740 } 1741 } 1742 return data; 1743 } 1744 1745 function parseMaxTime(str) { 1746 var maxValue = 0; 1747 var values = isString(str) ? 1748 str.split(/\s*,\s*/) : 1749 []; 1750 forEach(values, function(value) { 1751 maxValue = Math.max(parseFloat(value) || 0, maxValue); 1752 }); 1753 return maxValue; 1754 } 1755 1756 function getCacheKey(element) { 1757 var parentElement = element.parent(); 1758 var parentID = parentElement.data(NG_ANIMATE_PARENT_KEY); 1759 if (!parentID) { 1760 parentElement.data(NG_ANIMATE_PARENT_KEY, ++parentCounter); 1761 parentID = parentCounter; 1762 } 1763 return parentID + '-' + extractElementNode(element).getAttribute('class'); 1764 } 1765 1766 function animateSetup(animationEvent, element, className, styles) { 1767 var structural = ['ng-enter','ng-leave','ng-move'].indexOf(className) >= 0; 1768 1769 var cacheKey = getCacheKey(element); 1770 var eventCacheKey = cacheKey + ' ' + className; 1771 var itemIndex = lookupCache[eventCacheKey] ? ++lookupCache[eventCacheKey].total : 0; 1772 1773 var stagger = {}; 1774 if (itemIndex > 0) { 1775 var staggerClassName = className + '-stagger'; 1776 var staggerCacheKey = cacheKey + ' ' + staggerClassName; 1777 var applyClasses = !lookupCache[staggerCacheKey]; 1778 1779 applyClasses && $$jqLite.addClass(element, staggerClassName); 1780 1781 stagger = getElementAnimationDetails(element, staggerCacheKey); 1782 1783 applyClasses && $$jqLite.removeClass(element, staggerClassName); 1784 } 1785 1786 $$jqLite.addClass(element, className); 1787 1788 var formerData = element.data(NG_ANIMATE_CSS_DATA_KEY) || {}; 1789 var timings = getElementAnimationDetails(element, eventCacheKey); 1790 var transitionDuration = timings.transitionDuration; 1791 var animationDuration = timings.animationDuration; 1792 1793 if (structural && transitionDuration === 0 && animationDuration === 0) { 1794 $$jqLite.removeClass(element, className); 1795 return false; 1796 } 1797 1798 var blockTransition = styles || (structural && transitionDuration > 0); 1799 var blockAnimation = animationDuration > 0 && 1800 stagger.animationDelay > 0 && 1801 stagger.animationDuration === 0; 1802 1803 var closeAnimationFns = formerData.closeAnimationFns || []; 1804 element.data(NG_ANIMATE_CSS_DATA_KEY, { 1805 stagger: stagger, 1806 cacheKey: eventCacheKey, 1807 running: formerData.running || 0, 1808 itemIndex: itemIndex, 1809 blockTransition: blockTransition, 1810 closeAnimationFns: closeAnimationFns 1811 }); 1812 1813 var node = extractElementNode(element); 1814 1815 if (blockTransition) { 1816 blockTransitions(node, true); 1817 if (styles) { 1818 element.css(styles); 1819 } 1820 } 1821 1822 if (blockAnimation) { 1823 blockAnimations(node, true); 1824 } 1825 1826 return true; 1827 } 1828 1829 function animateRun(animationEvent, element, className, activeAnimationComplete, styles) { 1830 var node = extractElementNode(element); 1831 var elementData = element.data(NG_ANIMATE_CSS_DATA_KEY); 1832 if (node.getAttribute('class').indexOf(className) == -1 || !elementData) { 1833 activeAnimationComplete(); 1834 return; 1835 } 1836 1837 var activeClassName = ''; 1838 var pendingClassName = '';
1839 forEach(className.split(' '), function(klass, i) { 1840 var prefix = (i > 0 ? ' ' : '') + klass; 1841 activeClassName += prefix + '-active'; 1842 pendingClassName += prefix + '-pending'; 1843 }); 1844 1845 var style = ''; 1846 var appliedStyles = []; 1847 var itemIndex = elementData.itemIndex; 1848 var stagger = elementData.stagger; 1849 var staggerTime = 0; 1850 if (itemIndex > 0) { 1851 var transitionStaggerDelay = 0; 1852 if (stagger.transitionDelay > 0 && stagger.transitionDuration === 0) { 1853 transitionStaggerDelay = stagger.transitionDelay * itemIndex; 1854 } 1855 1856 var animationStaggerDelay = 0; 1857 if (stagger.animationDelay > 0 && stagger.animationDuration === 0) { 1858 animationStaggerDelay = stagger.animationDelay * itemIndex; 1859 appliedStyles.push(CSS_PREFIX + 'animation-play-state'); 1860 } 1861 1862 staggerTime = Math.round(Math.max(transitionStaggerDelay, animationStaggerDelay) * 100) / 100; 1863 } 1864 1865 if (!staggerTime) { 1866 $$jqLite.addClass(element, activeClassName); 1867 if (elementData.blockTransition) { 1868 blockTransitions(node, false); 1869 } 1870 } 1871 1872 var eventCacheKey = elementData.cacheKey + ' ' + activeClassName; 1873 var timings = getElementAnimationDetails(element, eventCacheKey); 1874 var maxDuration = Math.max(timings.transitionDuration, timings.animationDuration); 1875 if (maxDuration === 0) { 1876 $$jqLite.removeClass(element, activeClassName); 1877 animateClose(element, className); 1878 activeAnimationComplete(); 1879 return; 1880 } 1881 1882 if (!staggerTime && styles && Object.keys(styles).length > 0) { 1883 if (!timings.transitionDuration) { 1884 element.css('transition', timings.animationDuration + 's linear all'); 1885 appliedStyles.push('transition'); 1886 } 1887 element.css(styles); 1888 } 1889 1890 var maxDelay = Math.max(timings.transitionDelay, timings.animationDelay); 1891 var maxDelayTime = maxDelay * ONE_SECOND; 1892 1893 if (appliedStyles.length > 0) { 1894 //the element being animated may sometimes contain comment nodes in 1895 //the jqLite object, so we're safe to use a single variable to house 1896 //the styles since there is always only one element being animated 1897 var oldStyle = node.getAttribute('style') || ''; 1898 if (oldStyle.charAt(oldStyle.length - 1) !== ';') { 1899 oldStyle += ';'; 1900 } 1901 node.setAttribute('style', oldStyle + ' ' + style); 1902 } 1903 1904 var startTime = Date.now(); 1905 var css3AnimationEvents = ANIMATIONEND_EVENT + ' ' + TRANSITIONEND_EVENT; 1906 var animationTime = (maxDelay + maxDuration) * CLOSING_TIME_BUFFER; 1907 var totalTime = (staggerTime + animationTime) * ONE_SECOND; 1908 1909 var staggerTimeout; 1910 if (staggerTime > 0) { 1911 $$jqLite.addClass(element, pendingClassName); 1912 staggerTimeout = $timeout(function() { 1913 staggerTimeout = null; 1914 1915 if (timings.transitionDuration > 0) { 1916 blockTransitions(node, false); 1917 } 1918 if (timings.animationDuration > 0) { 1919 blockAnimations(node, false); 1920 } 1921 1922 $$jqLite.addClass(element, activeClassName); 1923 $$jqLite.removeClass(element, pendingClassName); 1924 1925 if (styles) { 1926 if (timings.transitionDuration === 0) { 1927 element.css('transition', timings.animationDuration + 's linear all'); 1928 } 1929 element.css(styles); 1930 appliedStyles.push('transition'); 1931 } 1932 }, staggerTime * ONE_SECOND, false); 1933 } 1934 1935 element.on(css3AnimationEvents, onAnimationProgress); 1936 elementData.closeAnimationFns.push(function() { 1937 onEnd(); 1938 activeAnimationComplete(); 1939 }); 1940 1941 elementData.running++; 1942 animationCloseHandler(element, totalTime); 1943 return onEnd; 1944 1945 // This will automatically be called by $animate so 1946 // there is no need to attach this internally to the 1947 // timeout done method. 1948 function onEnd() { 1949 element.off(css3AnimationEvents, onAnimationProgress); 1950 $$jqLite.removeClass(element, activeClassName); 1951 $$jqLite.removeClass(element, pendingClassName); 1952 if (staggerTimeout) { 1953 $timeout.cancel(staggerTimeout); 1954 } 1955 animateClose(element, className); 1956 var node = extractElementNode(element); 1957 for (var i in appliedStyles) { 1958 node.style.removeProperty(appliedStyles[i]); 1959 } 1960 } 1961 1962 function onAnimationProgress(event) { 1963 event.stopPropagation(); 1964 var ev = event.originalEvent || event;
1965 var timeStamp = ev.$manualTimeStamp || ev.timeStamp || Date.now(); 1966 1967 /* Firefox (or possibly just Gecko) likes to not round values up 1968 * when a ms measurement is used for the animation */ 1969 var elapsedTime = parseFloat(ev.elapsedTime.toFixed(ELAPSED_TIME_MAX_DECIMAL_PLACES)); 1970 1971 /* $manualTimeStamp is a mocked timeStamp value which is set 1972 * within browserTrigger(). This is only here so that tests can 1973 * mock animations properly. Real events fallback to event.timeStamp, 1974 * or, if they don't, then a timeStamp is automatically created for them. 1975 * We're checking to see if the timeStamp surpasses the expected delay, 1976 * but we're using elapsedTime instead of the timeStamp on the 2nd 1977 * pre-condition since animations sometimes close off early */ 1978 if (Math.max(timeStamp - startTime, 0) >= maxDelayTime && elapsedTime >= maxDuration) { 1979 activeAnimationComplete(); 1980 } 1981 } 1982 } 1983 1984 function blockTransitions(node, bool) { 1985 node.style[TRANSITION_PROP + PROPERTY_KEY] = bool ? 'none' : ''; 1986 } 1987 1988 function blockAnimations(node, bool) { 1989 node.style[ANIMATION_PROP + ANIMATION_PLAYSTATE_KEY] = bool ? 'paused' : ''; 1990 } 1991 1992 function animateBefore(animationEvent, element, className, styles) { 1993 if (animateSetup(animationEvent, element, className, styles)) { 1994 return function(cancelled) { 1995 cancelled && animateClose(element, className); 1996 }; 1997 } 1998 } 1999 2000 function animateAfter(animationEvent, element, className, afterAnimationComplete, styles) { 2001 if (element.data(NG_ANIMATE_CSS_DATA_KEY)) { 2002 return animateRun(animationEvent, element, className, afterAnimationComplete, styles); 2003 } else { 2004 animateClose(element, className); 2005 afterAnimationComplete(); 2006 } 2007 } 2008 2009 function animate(animationEvent, element, className, animationComplete, options) { 2010 //If the animateSetup function doesn't bother returning a 2011 //cancellation function then it means that there is no animation 2012 //to perform at all 2013 var preReflowCancellation = animateBefore(animationEvent, element, className, options.from); 2014 if (!preReflowCancellation) { 2015 clearCacheAfterReflow(); 2016 animationComplete(); 2017 return; 2018 } 2019 2020 //There are two cancellation functions: one is before the first 2021 //reflow animation and the second is during the active state 2022 //animation. The first function will take care of removing the 2023 //data from the element which will not make the 2nd animation 2024 //happen in the first place 2025 var cancel = preReflowCancellation; 2026 afterReflow(element, function() { 2027 //once the reflow is complete then we point cancel to 2028 //the new cancellation function which will remove all of the 2029 //animation properties from the active animation 2030 cancel = animateAfter(animationEvent, element, className, animationComplete, options.to); 2031 }); 2032 2033 return function(cancelled) { 2034 (cancel || noop)(cancelled); 2035 }; 2036 } 2037 2038 function animateClose(element, className) { 2039 $$jqLite.removeClass(element, className); 2040 var data = element.data(NG_ANIMATE_CSS_DATA_KEY); 2041 if (data) { 2042 if (data.running) { 2043 data.running--; 2044 } 2045 if (!data.running || data.running === 0) { 2046 element.removeData(NG_ANIMATE_CSS_DATA_KEY); 2047 } 2048 } 2049 } 2050 2051 return { 2052 animate: function(element, className, from, to, animationCompleted, options) { 2053 options = options || {}; 2054 options.from = from; 2055 options.to = to; 2056 return animate('animate', element, className, animationCompleted, options); 2057 }, 2058 2059 enter: function(element, animationCompleted, options) { 2060 options = options || {}; 2061 return animate('enter', element, 'ng-enter', animationCompleted, options); 2062 }, 2063 2064 leave: function(element, animationCompleted, options) { 2065 options = options || {}; 2066 return animate('leave', element, 'ng-leave', animationCompleted, options); 2067 }, 2068 2069 move: function(element, animationCompleted, options) { 2070 options = options || {}; 2071 return animate('move', element, 'ng-move', animationCompleted, options); 2072 }, 2073 2074 beforeSetClass: function(element, add, remove, animationCompleted, options) { 2075 options = options || {}; 2076 var className = suffixClasses(remove, '-remove') + ' ' + 2077 suffixClasses(add, '-add'); 2078 var cancellationMethod = animateBefore('setClass', element, className, options.from); 2079 if (cancellationMethod) { 2080 afterReflow(element, animationCompleted); 2081 return cancellationMethod; 2082 } 2083 clearCacheAfterReflow(); 2084 animationCompleted(); 2085 }, 2086 2087 beforeAddClass: function(element, className, animationCompleted, options) { 2088 options = options || {}; 2089 var cancellationMethod = animateBefore('addClass', element, suffixClasses(className, '-add'), options.from); 2090 if (cancellationMethod) { 2091 afterReflow(element, animationCompleted); 2092 return cancellationMethod; 2093 } 2094 clearCacheAfterReflow(); 2095 animationCompleted(); 2096 }, 2097 2098 beforeRemoveClass: function(element, className, animationCompleted, options) { 2099 options = options || {}; 2100 var cancellationMethod = animateBefore('removeClass', element, suffixClasses(className, '-remove'), options.from); 2101 if (cancellationMethod) { 2102 afterReflow(element, animationCompleted); 2103 return cancellationMethod; 2104 } 2105 clearCacheAfterReflow(); 2106 animationCompleted(); 2107 }, 2108 2109 setClass: function(element, add, remove, animationCompleted, options) { 2110 options = options || {}; 2111 remove = suffixClasses(remove, '-remove'); 2112 add = suffixClasses(add, '-add'); 2113 var className = remove + ' ' + add; 2114 return animateAfter('setClass', element, className, animationCompleted, options.to); 2115 }, 2116 2117 addClass: function(element, className, animationCompleted, options) { 2118 options = options || {}; 2119 return animateAfter('addClass', element, suffixClasses(className, '-add'), animationCompleted, options.to); 2120 }, 2121 2122 removeClass: function(element, className, animationCompleted, options) { 2123 options = options || {}; 2124 return animateAfter('removeClass', element, suffixClasses(className, '-remove'), animationCompleted, options.to); 2125 } 2126 }; 2127 2128 function suffixClasses(classes, suffix) { 2129 var className = '';
2130 classes = isArray(classes) ? classes : classes.split(/\s+/); 2131 forEach(classes, function(klass, i) { 2132 if (klass && klass.length > 0) { 2133 className += (i > 0 ? ' ' : '') + klass + suffix; 2134 } 2135 }); 2136 return className; 2137 } 2138 }]); 2139 }]); 2140 2141 2142})(window, window.angular);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.