angular-animate.js 102 KB

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