angular-animate.js 186 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166
  1. /**
  2. * @license AngularJS v1.6.5
  3. * (c) 2010-2017 Google, Inc. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function (window, angular) {
  7. 'use strict';
  8. var ELEMENT_NODE = 1;
  9. var COMMENT_NODE = 8;
  10. var ADD_CLASS_SUFFIX = '-add';
  11. var REMOVE_CLASS_SUFFIX = '-remove';
  12. var EVENT_CLASS_PREFIX = 'ng-';
  13. var ACTIVE_CLASS_SUFFIX = '-active';
  14. var PREPARE_CLASS_SUFFIX = '-prepare';
  15. var NG_ANIMATE_CLASSNAME = 'ng-animate';
  16. var NG_ANIMATE_CHILDREN_DATA = '$$ngAnimateChildren';
  17. // Detect proper transitionend/animationend event names.
  18. var CSS_PREFIX = '', TRANSITION_PROP, TRANSITIONEND_EVENT, ANIMATION_PROP, ANIMATIONEND_EVENT;
  19. // If unprefixed events are not supported but webkit-prefixed are, use the latter.
  20. // Otherwise, just use W3C names, browsers not supporting them at all will just ignore them.
  21. // Note: Chrome implements `window.onwebkitanimationend` and doesn't implement `window.onanimationend`
  22. // but at the same time dispatches the `animationend` event and not `webkitAnimationEnd`.
  23. // Register both events in case `window.onanimationend` is not supported because of that,
  24. // do the same for `transitionend` as Safari is likely to exhibit similar behavior.
  25. // Also, the only modern browser that uses vendor prefixes for transitions/keyframes is webkit
  26. // therefore there is no reason to test anymore for other vendor prefixes:
  27. // http://caniuse.com/#search=transition
  28. if ((window.ontransitionend === undefined) && (window.onwebkittransitionend !== undefined)) {
  29. CSS_PREFIX = '-webkit-';
  30. TRANSITION_PROP = 'WebkitTransition';
  31. TRANSITIONEND_EVENT = 'webkitTransitionEnd transitionend';
  32. } else {
  33. TRANSITION_PROP = 'transition';
  34. TRANSITIONEND_EVENT = 'transitionend';
  35. }
  36. if ((window.onanimationend === undefined) && (window.onwebkitanimationend !== undefined)) {
  37. CSS_PREFIX = '-webkit-';
  38. ANIMATION_PROP = 'WebkitAnimation';
  39. ANIMATIONEND_EVENT = 'webkitAnimationEnd animationend';
  40. } else {
  41. ANIMATION_PROP = 'animation';
  42. ANIMATIONEND_EVENT = 'animationend';
  43. }
  44. var DURATION_KEY = 'Duration';
  45. var PROPERTY_KEY = 'Property';
  46. var DELAY_KEY = 'Delay';
  47. var TIMING_KEY = 'TimingFunction';
  48. var ANIMATION_ITERATION_COUNT_KEY = 'IterationCount';
  49. var ANIMATION_PLAYSTATE_KEY = 'PlayState';
  50. var SAFE_FAST_FORWARD_DURATION_VALUE = 9999;
  51. var ANIMATION_DELAY_PROP = ANIMATION_PROP + DELAY_KEY;
  52. var ANIMATION_DURATION_PROP = ANIMATION_PROP + DURATION_KEY;
  53. var TRANSITION_DELAY_PROP = TRANSITION_PROP + DELAY_KEY;
  54. var TRANSITION_DURATION_PROP = TRANSITION_PROP + DURATION_KEY;
  55. var ngMinErr = angular.$$minErr('ng');
  56. function assertArg(arg, name, reason) {
  57. if (!arg) {
  58. throw ngMinErr('areq', 'Argument \'{0}\' is {1}', (name || '?'), (reason || 'required'));
  59. }
  60. return arg;
  61. }
  62. function mergeClasses(a, b) {
  63. if (!a && !b) return '';
  64. if (!a) return b;
  65. if (!b) return a;
  66. if (isArray(a)) a = a.join(' ');
  67. if (isArray(b)) b = b.join(' ');
  68. return a + ' ' + b;
  69. }
  70. function packageStyles(options) {
  71. var styles = {};
  72. if (options && (options.to || options.from)) {
  73. styles.to = options.to;
  74. styles.from = options.from;
  75. }
  76. return styles;
  77. }
  78. function pendClasses(classes, fix, isPrefix) {
  79. var className = '';
  80. classes = isArray(classes)
  81. ? classes
  82. : classes && isString(classes) && classes.length
  83. ? classes.split(/\s+/)
  84. : [];
  85. forEach(classes, function (klass, i) {
  86. if (klass && klass.length > 0) {
  87. className += (i > 0) ? ' ' : '';
  88. className += isPrefix ? fix + klass
  89. : klass + fix;
  90. }
  91. });
  92. return className;
  93. }
  94. function removeFromArray(arr, val) {
  95. var index = arr.indexOf(val);
  96. if (val >= 0) {
  97. arr.splice(index, 1);
  98. }
  99. }
  100. function stripCommentsFromElement(element) {
  101. if (element instanceof jqLite) {
  102. switch (element.length) {
  103. case 0:
  104. return element;
  105. case 1:
  106. // there is no point of stripping anything if the element
  107. // is the only element within the jqLite wrapper.
  108. // (it's important that we retain the element instance.)
  109. if (element[0].nodeType === ELEMENT_NODE) {
  110. return element;
  111. }
  112. break;
  113. default:
  114. return jqLite(extractElementNode(element));
  115. }
  116. }
  117. if (element.nodeType === ELEMENT_NODE) {
  118. return jqLite(element);
  119. }
  120. }
  121. function extractElementNode(element) {
  122. if (!element[0]) return element;
  123. for (var i = 0; i < element.length; i++) {
  124. var elm = element[i];
  125. if (elm.nodeType === ELEMENT_NODE) {
  126. return elm;
  127. }
  128. }
  129. }
  130. function $$addClass($$jqLite, element, className) {
  131. forEach(element, function (elm) {
  132. $$jqLite.addClass(elm, className);
  133. });
  134. }
  135. function $$removeClass($$jqLite, element, className) {
  136. forEach(element, function (elm) {
  137. $$jqLite.removeClass(elm, className);
  138. });
  139. }
  140. function applyAnimationClassesFactory($$jqLite) {
  141. return function (element, options) {
  142. if (options.addClass) {
  143. $$addClass($$jqLite, element, options.addClass);
  144. options.addClass = null;
  145. }
  146. if (options.removeClass) {
  147. $$removeClass($$jqLite, element, options.removeClass);
  148. options.removeClass = null;
  149. }
  150. };
  151. }
  152. function prepareAnimationOptions(options) {
  153. options = options || {};
  154. if (!options.$$prepared) {
  155. var domOperation = options.domOperation || noop;
  156. options.domOperation = function () {
  157. options.$$domOperationFired = true;
  158. domOperation();
  159. domOperation = noop;
  160. };
  161. options.$$prepared = true;
  162. }
  163. return options;
  164. }
  165. function applyAnimationStyles(element, options) {
  166. applyAnimationFromStyles(element, options);
  167. applyAnimationToStyles(element, options);
  168. }
  169. function applyAnimationFromStyles(element, options) {
  170. if (options.from) {
  171. element.css(options.from);
  172. options.from = null;
  173. }
  174. }
  175. function applyAnimationToStyles(element, options) {
  176. if (options.to) {
  177. element.css(options.to);
  178. options.to = null;
  179. }
  180. }
  181. function mergeAnimationDetails(element, oldAnimation, newAnimation) {
  182. var target = oldAnimation.options || {};
  183. var newOptions = newAnimation.options || {};
  184. var toAdd = (target.addClass || '') + ' ' + (newOptions.addClass || '');
  185. var toRemove = (target.removeClass || '') + ' ' + (newOptions.removeClass || '');
  186. var classes = resolveElementClasses(element.attr('class'), toAdd, toRemove);
  187. if (newOptions.preparationClasses) {
  188. target.preparationClasses = concatWithSpace(newOptions.preparationClasses, target.preparationClasses);
  189. delete newOptions.preparationClasses;
  190. }
  191. // noop is basically when there is no callback; otherwise something has been set
  192. var realDomOperation = target.domOperation !== noop ? target.domOperation : null;
  193. extend(target, newOptions);
  194. // TODO(matsko or sreeramu): proper fix is to maintain all animation callback in array and call at last,but now only leave has the callback so no issue with this.
  195. if (realDomOperation) {
  196. target.domOperation = realDomOperation;
  197. }
  198. if (classes.addClass) {
  199. target.addClass = classes.addClass;
  200. } else {
  201. target.addClass = null;
  202. }
  203. if (classes.removeClass) {
  204. target.removeClass = classes.removeClass;
  205. } else {
  206. target.removeClass = null;
  207. }
  208. oldAnimation.addClass = target.addClass;
  209. oldAnimation.removeClass = target.removeClass;
  210. return target;
  211. }
  212. function resolveElementClasses(existing, toAdd, toRemove) {
  213. var ADD_CLASS = 1;
  214. var REMOVE_CLASS = -1;
  215. var flags = {};
  216. existing = splitClassesToLookup(existing);
  217. toAdd = splitClassesToLookup(toAdd);
  218. forEach(toAdd, function (value, key) {
  219. flags[key] = ADD_CLASS;
  220. });
  221. toRemove = splitClassesToLookup(toRemove);
  222. forEach(toRemove, function (value, key) {
  223. flags[key] = flags[key] === ADD_CLASS ? null : REMOVE_CLASS;
  224. });
  225. var classes = {
  226. addClass: '',
  227. removeClass: ''
  228. };
  229. forEach(flags, function (val, klass) {
  230. var prop, allow;
  231. if (val === ADD_CLASS) {
  232. prop = 'addClass';
  233. allow = !existing[klass] || existing[klass + REMOVE_CLASS_SUFFIX];
  234. } else if (val === REMOVE_CLASS) {
  235. prop = 'removeClass';
  236. allow = existing[klass] || existing[klass + ADD_CLASS_SUFFIX];
  237. }
  238. if (allow) {
  239. if (classes[prop].length) {
  240. classes[prop] += ' ';
  241. }
  242. classes[prop] += klass;
  243. }
  244. });
  245. function splitClassesToLookup(classes) {
  246. if (isString(classes)) {
  247. classes = classes.split(' ');
  248. }
  249. var obj = {};
  250. forEach(classes, function (klass) {
  251. // sometimes the split leaves empty string values
  252. // incase extra spaces were applied to the options
  253. if (klass.length) {
  254. obj[klass] = true;
  255. }
  256. });
  257. return obj;
  258. }
  259. return classes;
  260. }
  261. function getDomNode(element) {
  262. return (element instanceof jqLite) ? element[0] : element;
  263. }
  264. function applyGeneratedPreparationClasses(element, event, options) {
  265. var classes = '';
  266. if (event) {
  267. classes = pendClasses(event, EVENT_CLASS_PREFIX, true);
  268. }
  269. if (options.addClass) {
  270. classes = concatWithSpace(classes, pendClasses(options.addClass, ADD_CLASS_SUFFIX));
  271. }
  272. if (options.removeClass) {
  273. classes = concatWithSpace(classes, pendClasses(options.removeClass, REMOVE_CLASS_SUFFIX));
  274. }
  275. if (classes.length) {
  276. options.preparationClasses = classes;
  277. element.addClass(classes);
  278. }
  279. }
  280. function clearGeneratedClasses(element, options) {
  281. if (options.preparationClasses) {
  282. element.removeClass(options.preparationClasses);
  283. options.preparationClasses = null;
  284. }
  285. if (options.activeClasses) {
  286. element.removeClass(options.activeClasses);
  287. options.activeClasses = null;
  288. }
  289. }
  290. function blockTransitions(node, duration) {
  291. // we use a negative delay value since it performs blocking
  292. // yet it doesn't kill any existing transitions running on the
  293. // same element which makes this safe for class-based animations
  294. var value = duration ? '-' + duration + 's' : '';
  295. applyInlineStyle(node, [TRANSITION_DELAY_PROP, value]);
  296. return [TRANSITION_DELAY_PROP, value];
  297. }
  298. function blockKeyframeAnimations(node, applyBlock) {
  299. var value = applyBlock ? 'paused' : '';
  300. var key = ANIMATION_PROP + ANIMATION_PLAYSTATE_KEY;
  301. applyInlineStyle(node, [key, value]);
  302. return [key, value];
  303. }
  304. function applyInlineStyle(node, styleTuple) {
  305. var prop = styleTuple[0];
  306. var value = styleTuple[1];
  307. node.style[prop] = value;
  308. }
  309. function concatWithSpace(a, b) {
  310. if (!a) return b;
  311. if (!b) return a;
  312. return a + ' ' + b;
  313. }
  314. var $$rAFSchedulerFactory = ['$$rAF', function ($$rAF) {
  315. var queue, cancelFn;
  316. function scheduler(tasks) {
  317. // we make a copy since RAFScheduler mutates the state
  318. // of the passed in array variable and this would be difficult
  319. // to track down on the outside code
  320. queue = queue.concat(tasks);
  321. nextTick();
  322. }
  323. queue = scheduler.queue = [];
  324. /* waitUntilQuiet does two things:
  325. * 1. It will run the FINAL `fn` value only when an uncanceled RAF has passed through
  326. * 2. It will delay the next wave of tasks from running until the quiet `fn` has run.
  327. *
  328. * The motivation here is that animation code can request more time from the scheduler
  329. * before the next wave runs. This allows for certain DOM properties such as classes to
  330. * be resolved in time for the next animation to run.
  331. */
  332. scheduler.waitUntilQuiet = function (fn) {
  333. if (cancelFn) cancelFn();
  334. cancelFn = $$rAF(function () {
  335. cancelFn = null;
  336. fn();
  337. nextTick();
  338. });
  339. };
  340. return scheduler;
  341. function nextTick() {
  342. if (!queue.length) return;
  343. var items = queue.shift();
  344. for (var i = 0; i < items.length; i++) {
  345. items[i]();
  346. }
  347. if (!cancelFn) {
  348. $$rAF(function () {
  349. if (!cancelFn) nextTick();
  350. });
  351. }
  352. }
  353. }];
  354. /**
  355. * @ngdoc directive
  356. * @name ngAnimateChildren
  357. * @restrict AE
  358. * @element ANY
  359. *
  360. * @description
  361. *
  362. * ngAnimateChildren allows you to specify that children of this element should animate even if any
  363. * of the children's parents are currently animating. By default, when an element has an active `enter`, `leave`, or `move`
  364. * (structural) animation, child elements that also have an active structural animation are not animated.
  365. *
  366. * Note that even if `ngAnimateChildren` is set, no child animations will run when the parent element is removed from the DOM (`leave` animation).
  367. *
  368. *
  369. * @param {string} ngAnimateChildren If the value is empty, `true` or `on`,
  370. * then child animations are allowed. If the value is `false`, child animations are not allowed.
  371. *
  372. * @example
  373. * <example module="ngAnimateChildren" name="ngAnimateChildren" deps="angular-animate.js" animations="true">
  374. <file name="index.html">
  375. <div ng-controller="MainController as main">
  376. <label>Show container? <input type="checkbox" ng-model="main.enterElement" /></label>
  377. <label>Animate children? <input type="checkbox" ng-model="main.animateChildren" /></label>
  378. <hr>
  379. <div ng-animate-children="{{main.animateChildren}}">
  380. <div ng-if="main.enterElement" class="container">
  381. List of items:
  382. <div ng-repeat="item in [0, 1, 2, 3]" class="item">Item {{item}}</div>
  383. </div>
  384. </div>
  385. </div>
  386. </file>
  387. <file name="animations.css">
  388. .container.ng-enter,
  389. .container.ng-leave {
  390. transition: all ease 1.5s;
  391. }
  392. .container.ng-enter,
  393. .container.ng-leave-active {
  394. opacity: 0;
  395. }
  396. .container.ng-leave,
  397. .container.ng-enter-active {
  398. opacity: 1;
  399. }
  400. .item {
  401. background: firebrick;
  402. color: #FFF;
  403. margin-bottom: 10px;
  404. }
  405. .item.ng-enter,
  406. .item.ng-leave {
  407. transition: transform 1.5s ease;
  408. }
  409. .item.ng-enter {
  410. transform: translateX(50px);
  411. }
  412. .item.ng-enter-active {
  413. transform: translateX(0);
  414. }
  415. </file>
  416. <file name="script.js">
  417. angular.module('ngAnimateChildren', ['ngAnimate'])
  418. .controller('MainController', function MainController() {
  419. this.animateChildren = false;
  420. this.enterElement = false;
  421. });
  422. </file>
  423. </example>
  424. */
  425. var $$AnimateChildrenDirective = ['$interpolate', function ($interpolate) {
  426. return {
  427. link: function (scope, element, attrs) {
  428. var val = attrs.ngAnimateChildren;
  429. if (isString(val) && val.length === 0) { //empty attribute
  430. element.data(NG_ANIMATE_CHILDREN_DATA, true);
  431. } else {
  432. // Interpolate and set the value, so that it is available to
  433. // animations that run right after compilation
  434. setData($interpolate(val)(scope));
  435. attrs.$observe('ngAnimateChildren', setData);
  436. }
  437. function setData(value) {
  438. value = value === 'on' || value === 'true';
  439. element.data(NG_ANIMATE_CHILDREN_DATA, value);
  440. }
  441. }
  442. };
  443. }];
  444. /* exported $AnimateCssProvider */
  445. var ANIMATE_TIMER_KEY = '$$animateCss';
  446. /**
  447. * @ngdoc service
  448. * @name $animateCss
  449. * @kind object
  450. *
  451. * @description
  452. * The `$animateCss` service is a useful utility to trigger customized CSS-based transitions/keyframes
  453. * from a JavaScript-based animation or directly from a directive. The purpose of `$animateCss` is NOT
  454. * to side-step how `$animate` and ngAnimate work, but the goal is to allow pre-existing animations or
  455. * directives to create more complex animations that can be purely driven using CSS code.
  456. *
  457. * Note that only browsers that support CSS transitions and/or keyframe animations are capable of
  458. * rendering animations triggered via `$animateCss` (bad news for IE9 and lower).
  459. *
  460. * ## Usage
  461. * Once again, `$animateCss` is designed to be used inside of a registered JavaScript animation that
  462. * is powered by ngAnimate. It is possible to use `$animateCss` directly inside of a directive, however,
  463. * any automatic control over cancelling animations and/or preventing animations from being run on
  464. * child elements will not be handled by Angular. For this to work as expected, please use `$animate` to
  465. * trigger the animation and then setup a JavaScript animation that injects `$animateCss` to trigger
  466. * the CSS animation.
  467. *
  468. * The example below shows how we can create a folding animation on an element using `ng-if`:
  469. *
  470. * ```html
  471. * <!-- notice the `fold-animation` CSS class -->
  472. * <div ng-if="onOff" class="fold-animation">
  473. * This element will go BOOM
  474. * </div>
  475. * <button ng-click="onOff=true">Fold In</button>
  476. * ```
  477. *
  478. * Now we create the **JavaScript animation** that will trigger the CSS transition:
  479. *
  480. * ```js
  481. * ngModule.animation('.fold-animation', ['$animateCss', function($animateCss) {
  482. * return {
  483. * enter: function(element, doneFn) {
  484. * var height = element[0].offsetHeight;
  485. * return $animateCss(element, {
  486. * from: { height:'0px' },
  487. * to: { height:height + 'px' },
  488. * duration: 1 // one second
  489. * });
  490. * }
  491. * }
  492. * }]);
  493. * ```
  494. *
  495. * ## More Advanced Uses
  496. *
  497. * `$animateCss` is the underlying code that ngAnimate uses to power **CSS-based animations** behind the scenes. Therefore CSS hooks
  498. * like `.ng-EVENT`, `.ng-EVENT-active`, `.ng-EVENT-stagger` are all features that can be triggered using `$animateCss` via JavaScript code.
  499. *
  500. * This also means that just about any combination of adding classes, removing classes, setting styles, dynamically setting a keyframe animation,
  501. * applying a hardcoded duration or delay value, changing the animation easing or applying a stagger animation are all options that work with
  502. * `$animateCss`. The service itself is smart enough to figure out the combination of options and examine the element styling properties in order
  503. * to provide a working animation that will run in CSS.
  504. *
  505. * The example below showcases a more advanced version of the `.fold-animation` from the example above:
  506. *
  507. * ```js
  508. * ngModule.animation('.fold-animation', ['$animateCss', function($animateCss) {
  509. * return {
  510. * enter: function(element, doneFn) {
  511. * var height = element[0].offsetHeight;
  512. * return $animateCss(element, {
  513. * addClass: 'red large-text pulse-twice',
  514. * easing: 'ease-out',
  515. * from: { height:'0px' },
  516. * to: { height:height + 'px' },
  517. * duration: 1 // one second
  518. * });
  519. * }
  520. * }
  521. * }]);
  522. * ```
  523. *
  524. * Since we're adding/removing CSS classes then the CSS transition will also pick those up:
  525. *
  526. * ```css
  527. * /&#42; since a hardcoded duration value of 1 was provided in the JavaScript animation code,
  528. * the CSS classes below will be transitioned despite them being defined as regular CSS classes &#42;/
  529. * .red { background:red; }
  530. * .large-text { font-size:20px; }
  531. *
  532. * /&#42; we can also use a keyframe animation and $animateCss will make it work alongside the transition &#42;/
  533. * .pulse-twice {
  534. * animation: 0.5s pulse linear 2;
  535. * -webkit-animation: 0.5s pulse linear 2;
  536. * }
  537. *
  538. * @keyframes pulse {
  539. * from { transform: scale(0.5); }
  540. * to { transform: scale(1.5); }
  541. * }
  542. *
  543. * @-webkit-keyframes pulse {
  544. * from { -webkit-transform: scale(0.5); }
  545. * to { -webkit-transform: scale(1.5); }
  546. * }
  547. * ```
  548. *
  549. * Given this complex combination of CSS classes, styles and options, `$animateCss` will figure everything out and make the animation happen.
  550. *
  551. * ## How the Options are handled
  552. *
  553. * `$animateCss` is very versatile and intelligent when it comes to figuring out what configurations to apply to the element to ensure the animation
  554. * works with the options provided. Say for example we were adding a class that contained a keyframe value and we wanted to also animate some inline
  555. * styles using the `from` and `to` properties.
  556. *
  557. * ```js
  558. * var animator = $animateCss(element, {
  559. * from: { background:'red' },
  560. * to: { background:'blue' }
  561. * });
  562. * animator.start();
  563. * ```
  564. *
  565. * ```css
  566. * .rotating-animation {
  567. * animation:0.5s rotate linear;
  568. * -webkit-animation:0.5s rotate linear;
  569. * }
  570. *
  571. * @keyframes rotate {
  572. * from { transform: rotate(0deg); }
  573. * to { transform: rotate(360deg); }
  574. * }
  575. *
  576. * @-webkit-keyframes rotate {
  577. * from { -webkit-transform: rotate(0deg); }
  578. * to { -webkit-transform: rotate(360deg); }
  579. * }
  580. * ```
  581. *
  582. * The missing pieces here are that we do not have a transition set (within the CSS code nor within the `$animateCss` options) and the duration of the animation is
  583. * going to be detected from what the keyframe styles on the CSS class are. In this event, `$animateCss` will automatically create an inline transition
  584. * style matching the duration detected from the keyframe style (which is present in the CSS class that is being added) and then prepare both the transition
  585. * and keyframe animations to run in parallel on the element. Then when the animation is underway the provided `from` and `to` CSS styles will be applied
  586. * and spread across the transition and keyframe animation.
  587. *
  588. * ## What is returned
  589. *
  590. * `$animateCss` works in two stages: a preparation phase and an animation phase. Therefore when `$animateCss` is first called it will NOT actually
  591. * start the animation. All that is going on here is that the element is being prepared for the animation (which means that the generated CSS classes are
  592. * added and removed on the element). Once `$animateCss` is called it will return an object with the following properties:
  593. *
  594. * ```js
  595. * var animator = $animateCss(element, { ... });
  596. * ```
  597. *
  598. * Now what do the contents of our `animator` variable look like:
  599. *
  600. * ```js
  601. * {
  602. * // starts the animation
  603. * start: Function,
  604. *
  605. * // ends (aborts) the animation
  606. * end: Function
  607. * }
  608. * ```
  609. *
  610. * To actually start the animation we need to run `animation.start()` which will then return a promise that we can hook into to detect when the animation ends.
  611. * If we choose not to run the animation then we MUST run `animation.end()` to perform a cleanup on the element (since some CSS classes and styles may have been
  612. * applied to the element during the preparation phase). Note that all other properties such as duration, delay, transitions and keyframes are just properties
  613. * and that changing them will not reconfigure the parameters of the animation.
  614. *
  615. * ### runner.done() vs runner.then()
  616. * It is documented that `animation.start()` will return a promise object and this is true, however, there is also an additional method available on the
  617. * runner called `.done(callbackFn)`. The done method works the same as `.finally(callbackFn)`, however, it does **not trigger a digest to occur**.
  618. * Therefore, for performance reasons, it's always best to use `runner.done(callback)` instead of `runner.then()`, `runner.catch()` or `runner.finally()`
  619. * unless you really need a digest to kick off afterwards.
  620. *
  621. * Keep in mind that, to make this easier, ngAnimate has tweaked the JS animations API to recognize when a runner instance is returned from $animateCss
  622. * (so there is no need to call `runner.done(doneFn)` inside of your JavaScript animation code).
  623. * Check the {@link ngAnimate.$animateCss#usage animation code above} to see how this works.
  624. *
  625. * @param {DOMElement} element the element that will be animated
  626. * @param {object} options the animation-related options that will be applied during the animation
  627. *
  628. * * `event` - The DOM event (e.g. enter, leave, move). When used, a generated CSS class of `ng-EVENT` and `ng-EVENT-active` will be applied
  629. * to the element during the animation. Multiple events can be provided when spaces are used as a separator. (Note that this will not perform any DOM operation.)
  630. * * `structural` - Indicates that the `ng-` prefix will be added to the event class. Setting to `false` or omitting will turn `ng-EVENT` and
  631. * `ng-EVENT-active` in `EVENT` and `EVENT-active`. Unused if `event` is omitted.
  632. * * `easing` - The CSS easing value that will be applied to the transition or keyframe animation (or both).
  633. * * `transitionStyle` - The raw CSS transition style that will be used (e.g. `1s linear all`).
  634. * * `keyframeStyle` - The raw CSS keyframe animation style that will be used (e.g. `1s my_animation linear`).
  635. * * `from` - The starting CSS styles (a key/value object) that will be applied at the start of the animation.
  636. * * `to` - The ending CSS styles (a key/value object) that will be applied across the animation via a CSS transition.
  637. * * `addClass` - A space separated list of CSS classes that will be added to the element and spread across the animation.
  638. * * `removeClass` - A space separated list of CSS classes that will be removed from the element and spread across the animation.
  639. * * `duration` - A number value representing the total duration of the transition and/or keyframe (note that a value of 1 is 1000ms). If a value of `0`
  640. * is provided then the animation will be skipped entirely.
  641. * * `delay` - A number value representing the total delay of the transition and/or keyframe (note that a value of 1 is 1000ms). If a value of `true` is
  642. * used then whatever delay value is detected from the CSS classes will be mirrored on the elements styles (e.g. by setting delay true then the style value
  643. * of the element will be `transition-delay: DETECTED_VALUE`). Using `true` is useful when you want the CSS classes and inline styles to all share the same
  644. * CSS delay value.
  645. * * `stagger` - A numeric time value representing the delay between successively animated elements
  646. * ({@link ngAnimate#css-staggering-animations Click here to learn how CSS-based staggering works in ngAnimate.})
  647. * * `staggerIndex` - The numeric index representing the stagger item (e.g. a value of 5 is equal to the sixth item in the stagger; therefore when a
  648. * `stagger` option value of `0.1` is used then there will be a stagger delay of `600ms`)
  649. * * `applyClassesEarly` - Whether or not the classes being added or removed will be used when detecting the animation. This is set by `$animate` when enter/leave/move animations are fired to ensure that the CSS classes are resolved in time. (Note that this will prevent any transitions from occurring on the classes being added and removed.)
  650. * * `cleanupStyles` - Whether or not the provided `from` and `to` styles will be removed once
  651. * the animation is closed. This is useful for when the styles are used purely for the sake of
  652. * the animation and do not have a lasting visual effect on the element (e.g. a collapse and open animation).
  653. * By default this value is set to `false`.
  654. *
  655. * @return {object} an object with start and end methods and details about the animation.
  656. *
  657. * * `start` - The method to start the animation. This will return a `Promise` when called.
  658. * * `end` - This method will cancel the animation and remove all applied CSS classes and styles.
  659. */
  660. var ONE_SECOND = 1000;
  661. var ELAPSED_TIME_MAX_DECIMAL_PLACES = 3;
  662. var CLOSING_TIME_BUFFER = 1.5;
  663. var DETECT_CSS_PROPERTIES = {
  664. transitionDuration: TRANSITION_DURATION_PROP,
  665. transitionDelay: TRANSITION_DELAY_PROP,
  666. transitionProperty: TRANSITION_PROP + PROPERTY_KEY,
  667. animationDuration: ANIMATION_DURATION_PROP,
  668. animationDelay: ANIMATION_DELAY_PROP,
  669. animationIterationCount: ANIMATION_PROP + ANIMATION_ITERATION_COUNT_KEY
  670. };
  671. var DETECT_STAGGER_CSS_PROPERTIES = {
  672. transitionDuration: TRANSITION_DURATION_PROP,
  673. transitionDelay: TRANSITION_DELAY_PROP,
  674. animationDuration: ANIMATION_DURATION_PROP,
  675. animationDelay: ANIMATION_DELAY_PROP
  676. };
  677. function getCssKeyframeDurationStyle(duration) {
  678. return [ANIMATION_DURATION_PROP, duration + 's'];
  679. }
  680. function getCssDelayStyle(delay, isKeyframeAnimation) {
  681. var prop = isKeyframeAnimation ? ANIMATION_DELAY_PROP : TRANSITION_DELAY_PROP;
  682. return [prop, delay + 's'];
  683. }
  684. function computeCssStyles($window, element, properties) {
  685. var styles = Object.create(null);
  686. var detectedStyles = $window.getComputedStyle(element) || {};
  687. forEach(properties, function (formalStyleName, actualStyleName) {
  688. var val = detectedStyles[formalStyleName];
  689. if (val) {
  690. var c = val.charAt(0);
  691. // only numerical-based values have a negative sign or digit as the first value
  692. if (c === '-' || c === '+' || c >= 0) {
  693. val = parseMaxTime(val);
  694. }
  695. // by setting this to null in the event that the delay is not set or is set directly as 0
  696. // then we can still allow for negative values to be used later on and not mistake this
  697. // value for being greater than any other negative value.
  698. if (val === 0) {
  699. val = null;
  700. }
  701. styles[actualStyleName] = val;
  702. }
  703. });
  704. return styles;
  705. }
  706. function parseMaxTime(str) {
  707. var maxValue = 0;
  708. var values = str.split(/\s*,\s*/);
  709. forEach(values, function (value) {
  710. // it's always safe to consider only second values and omit `ms` values since
  711. // getComputedStyle will always handle the conversion for us
  712. if (value.charAt(value.length - 1) === 's') {
  713. value = value.substring(0, value.length - 1);
  714. }
  715. value = parseFloat(value) || 0;
  716. maxValue = maxValue ? Math.max(value, maxValue) : value;
  717. });
  718. return maxValue;
  719. }
  720. function truthyTimingValue(val) {
  721. return val === 0 || val != null;
  722. }
  723. function getCssTransitionDurationStyle(duration, applyOnlyDuration) {
  724. var style = TRANSITION_PROP;
  725. var value = duration + 's';
  726. if (applyOnlyDuration) {
  727. style += DURATION_KEY;
  728. } else {
  729. value += ' linear all';
  730. }
  731. return [style, value];
  732. }
  733. function createLocalCacheLookup() {
  734. var cache = Object.create(null);
  735. return {
  736. flush: function () {
  737. cache = Object.create(null);
  738. },
  739. count: function (key) {
  740. var entry = cache[key];
  741. return entry ? entry.total : 0;
  742. },
  743. get: function (key) {
  744. var entry = cache[key];
  745. return entry && entry.value;
  746. },
  747. put: function (key, value) {
  748. if (!cache[key]) {
  749. cache[key] = {total: 1, value: value};
  750. } else {
  751. cache[key].total++;
  752. }
  753. }
  754. };
  755. }
  756. // we do not reassign an already present style value since
  757. // if we detect the style property value again we may be
  758. // detecting styles that were added via the `from` styles.
  759. // We make use of `isDefined` here since an empty string
  760. // or null value (which is what getPropertyValue will return
  761. // for a non-existing style) will still be marked as a valid
  762. // value for the style (a falsy value implies that the style
  763. // is to be removed at the end of the animation). If we had a simple
  764. // "OR" statement then it would not be enough to catch that.
  765. function registerRestorableStyles(backup, node, properties) {
  766. forEach(properties, function (prop) {
  767. backup[prop] = isDefined(backup[prop])
  768. ? backup[prop]
  769. : node.style.getPropertyValue(prop);
  770. });
  771. }
  772. var $AnimateCssProvider = ['$animateProvider', /** @this */ function ($animateProvider) {
  773. var gcsLookup = createLocalCacheLookup();
  774. var gcsStaggerLookup = createLocalCacheLookup();
  775. this.$get = ['$window', '$$jqLite', '$$AnimateRunner', '$timeout',
  776. '$$forceReflow', '$sniffer', '$$rAFScheduler', '$$animateQueue',
  777. function ($window, $$jqLite, $$AnimateRunner, $timeout,
  778. $$forceReflow, $sniffer, $$rAFScheduler, $$animateQueue) {
  779. var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
  780. var parentCounter = 0;
  781. function gcsHashFn(node, extraClasses) {
  782. var KEY = '$$ngAnimateParentKey';
  783. var parentNode = node.parentNode;
  784. var parentID = parentNode[KEY] || (parentNode[KEY] = ++parentCounter);
  785. return parentID + '-' + node.getAttribute('class') + '-' + extraClasses;
  786. }
  787. function computeCachedCssStyles(node, className, cacheKey, properties) {
  788. var timings = gcsLookup.get(cacheKey);
  789. if (!timings) {
  790. timings = computeCssStyles($window, node, properties);
  791. if (timings.animationIterationCount === 'infinite') {
  792. timings.animationIterationCount = 1;
  793. }
  794. }
  795. // we keep putting this in multiple times even though the value and the cacheKey are the same
  796. // because we're keeping an internal tally of how many duplicate animations are detected.
  797. gcsLookup.put(cacheKey, timings);
  798. return timings;
  799. }
  800. function computeCachedCssStaggerStyles(node, className, cacheKey, properties) {
  801. var stagger;
  802. // if we have one or more existing matches of matching elements
  803. // containing the same parent + CSS styles (which is how cacheKey works)
  804. // then staggering is possible
  805. if (gcsLookup.count(cacheKey) > 0) {
  806. stagger = gcsStaggerLookup.get(cacheKey);
  807. if (!stagger) {
  808. var staggerClassName = pendClasses(className, '-stagger');
  809. $$jqLite.addClass(node, staggerClassName);
  810. stagger = computeCssStyles($window, node, properties);
  811. // force the conversion of a null value to zero incase not set
  812. stagger.animationDuration = Math.max(stagger.animationDuration, 0);
  813. stagger.transitionDuration = Math.max(stagger.transitionDuration, 0);
  814. $$jqLite.removeClass(node, staggerClassName);
  815. gcsStaggerLookup.put(cacheKey, stagger);
  816. }
  817. }
  818. return stagger || {};
  819. }
  820. var rafWaitQueue = [];
  821. function waitUntilQuiet(callback) {
  822. rafWaitQueue.push(callback);
  823. $$rAFScheduler.waitUntilQuiet(function () {
  824. gcsLookup.flush();
  825. gcsStaggerLookup.flush();
  826. // DO NOT REMOVE THIS LINE OR REFACTOR OUT THE `pageWidth` variable.
  827. // PLEASE EXAMINE THE `$$forceReflow` service to understand why.
  828. var pageWidth = $$forceReflow();
  829. // we use a for loop to ensure that if the queue is changed
  830. // during this looping then it will consider new requests
  831. for (var i = 0; i < rafWaitQueue.length; i++) {
  832. rafWaitQueue[i](pageWidth);
  833. }
  834. rafWaitQueue.length = 0;
  835. });
  836. }
  837. function computeTimings(node, className, cacheKey) {
  838. var timings = computeCachedCssStyles(node, className, cacheKey, DETECT_CSS_PROPERTIES);
  839. var aD = timings.animationDelay;
  840. var tD = timings.transitionDelay;
  841. timings.maxDelay = aD && tD
  842. ? Math.max(aD, tD)
  843. : (aD || tD);
  844. timings.maxDuration = Math.max(
  845. timings.animationDuration * timings.animationIterationCount,
  846. timings.transitionDuration);
  847. return timings;
  848. }
  849. return function init(element, initialOptions) {
  850. // all of the animation functions should create
  851. // a copy of the options data, however, if a
  852. // parent service has already created a copy then
  853. // we should stick to using that
  854. var options = initialOptions || {};
  855. if (!options.$$prepared) {
  856. options = prepareAnimationOptions(copy(options));
  857. }
  858. var restoreStyles = {};
  859. var node = getDomNode(element);
  860. if (!node
  861. || !node.parentNode
  862. || !$$animateQueue.enabled()) {
  863. return closeAndReturnNoopAnimator();
  864. }
  865. var temporaryStyles = [];
  866. var classes = element.attr('class');
  867. var styles = packageStyles(options);
  868. var animationClosed;
  869. var animationPaused;
  870. var animationCompleted;
  871. var runner;
  872. var runnerHost;
  873. var maxDelay;
  874. var maxDelayTime;
  875. var maxDuration;
  876. var maxDurationTime;
  877. var startTime;
  878. var events = [];
  879. if (options.duration === 0 || (!$sniffer.animations && !$sniffer.transitions)) {
  880. return closeAndReturnNoopAnimator();
  881. }
  882. var method = options.event && isArray(options.event)
  883. ? options.event.join(' ')
  884. : options.event;
  885. var isStructural = method && options.structural;
  886. var structuralClassName = '';
  887. var addRemoveClassName = '';
  888. if (isStructural) {
  889. structuralClassName = pendClasses(method, EVENT_CLASS_PREFIX, true);
  890. } else if (method) {
  891. structuralClassName = method;
  892. }
  893. if (options.addClass) {
  894. addRemoveClassName += pendClasses(options.addClass, ADD_CLASS_SUFFIX);
  895. }
  896. if (options.removeClass) {
  897. if (addRemoveClassName.length) {
  898. addRemoveClassName += ' ';
  899. }
  900. addRemoveClassName += pendClasses(options.removeClass, REMOVE_CLASS_SUFFIX);
  901. }
  902. // there may be a situation where a structural animation is combined together
  903. // with CSS classes that need to resolve before the animation is computed.
  904. // However this means that there is no explicit CSS code to block the animation
  905. // from happening (by setting 0s none in the class name). If this is the case
  906. // we need to apply the classes before the first rAF so we know to continue if
  907. // there actually is a detected transition or keyframe animation
  908. if (options.applyClassesEarly && addRemoveClassName.length) {
  909. applyAnimationClasses(element, options);
  910. }
  911. var preparationClasses = [structuralClassName, addRemoveClassName].join(' ').trim();
  912. var fullClassName = classes + ' ' + preparationClasses;
  913. var activeClasses = pendClasses(preparationClasses, ACTIVE_CLASS_SUFFIX);
  914. var hasToStyles = styles.to && Object.keys(styles.to).length > 0;
  915. var containsKeyframeAnimation = (options.keyframeStyle || '').length > 0;
  916. // there is no way we can trigger an animation if no styles and
  917. // no classes are being applied which would then trigger a transition,
  918. // unless there a is raw keyframe value that is applied to the element.
  919. if (!containsKeyframeAnimation
  920. && !hasToStyles
  921. && !preparationClasses) {
  922. return closeAndReturnNoopAnimator();
  923. }
  924. var cacheKey, stagger;
  925. if (options.stagger > 0) {
  926. var staggerVal = parseFloat(options.stagger);
  927. stagger = {
  928. transitionDelay: staggerVal,
  929. animationDelay: staggerVal,
  930. transitionDuration: 0,
  931. animationDuration: 0
  932. };
  933. } else {
  934. cacheKey = gcsHashFn(node, fullClassName);
  935. stagger = computeCachedCssStaggerStyles(node, preparationClasses, cacheKey, DETECT_STAGGER_CSS_PROPERTIES);
  936. }
  937. if (!options.$$skipPreparationClasses) {
  938. $$jqLite.addClass(element, preparationClasses);
  939. }
  940. var applyOnlyDuration;
  941. if (options.transitionStyle) {
  942. var transitionStyle = [TRANSITION_PROP, options.transitionStyle];
  943. applyInlineStyle(node, transitionStyle);
  944. temporaryStyles.push(transitionStyle);
  945. }
  946. if (options.duration >= 0) {
  947. applyOnlyDuration = node.style[TRANSITION_PROP].length > 0;
  948. var durationStyle = getCssTransitionDurationStyle(options.duration, applyOnlyDuration);
  949. // we set the duration so that it will be picked up by getComputedStyle later
  950. applyInlineStyle(node, durationStyle);
  951. temporaryStyles.push(durationStyle);
  952. }
  953. if (options.keyframeStyle) {
  954. var keyframeStyle = [ANIMATION_PROP, options.keyframeStyle];
  955. applyInlineStyle(node, keyframeStyle);
  956. temporaryStyles.push(keyframeStyle);
  957. }
  958. var itemIndex = stagger
  959. ? options.staggerIndex >= 0
  960. ? options.staggerIndex
  961. : gcsLookup.count(cacheKey)
  962. : 0;
  963. var isFirst = itemIndex === 0;
  964. // this is a pre-emptive way of forcing the setup classes to be added and applied INSTANTLY
  965. // without causing any combination of transitions to kick in. By adding a negative delay value
  966. // it forces the setup class' transition to end immediately. We later then remove the negative
  967. // transition delay to allow for the transition to naturally do it's thing. The beauty here is
  968. // that if there is no transition defined then nothing will happen and this will also allow
  969. // other transitions to be stacked on top of each other without any chopping them out.
  970. if (isFirst && !options.skipBlocking) {
  971. blockTransitions(node, SAFE_FAST_FORWARD_DURATION_VALUE);
  972. }
  973. var timings = computeTimings(node, fullClassName, cacheKey);
  974. var relativeDelay = timings.maxDelay;
  975. maxDelay = Math.max(relativeDelay, 0);
  976. maxDuration = timings.maxDuration;
  977. var flags = {};
  978. flags.hasTransitions = timings.transitionDuration > 0;
  979. flags.hasAnimations = timings.animationDuration > 0;
  980. flags.hasTransitionAll = flags.hasTransitions && timings.transitionProperty === 'all';
  981. flags.applyTransitionDuration = hasToStyles && (
  982. (flags.hasTransitions && !flags.hasTransitionAll)
  983. || (flags.hasAnimations && !flags.hasTransitions));
  984. flags.applyAnimationDuration = options.duration && flags.hasAnimations;
  985. flags.applyTransitionDelay = truthyTimingValue(options.delay) && (flags.applyTransitionDuration || flags.hasTransitions);
  986. flags.applyAnimationDelay = truthyTimingValue(options.delay) && flags.hasAnimations;
  987. flags.recalculateTimingStyles = addRemoveClassName.length > 0;
  988. if (flags.applyTransitionDuration || flags.applyAnimationDuration) {
  989. maxDuration = options.duration ? parseFloat(options.duration) : maxDuration;
  990. if (flags.applyTransitionDuration) {
  991. flags.hasTransitions = true;
  992. timings.transitionDuration = maxDuration;
  993. applyOnlyDuration = node.style[TRANSITION_PROP + PROPERTY_KEY].length > 0;
  994. temporaryStyles.push(getCssTransitionDurationStyle(maxDuration, applyOnlyDuration));
  995. }
  996. if (flags.applyAnimationDuration) {
  997. flags.hasAnimations = true;
  998. timings.animationDuration = maxDuration;
  999. temporaryStyles.push(getCssKeyframeDurationStyle(maxDuration));
  1000. }
  1001. }
  1002. if (maxDuration === 0 && !flags.recalculateTimingStyles) {
  1003. return closeAndReturnNoopAnimator();
  1004. }
  1005. if (options.delay != null) {
  1006. var delayStyle;
  1007. if (typeof options.delay !== 'boolean') {
  1008. delayStyle = parseFloat(options.delay);
  1009. // number in options.delay means we have to recalculate the delay for the closing timeout
  1010. maxDelay = Math.max(delayStyle, 0);
  1011. }
  1012. if (flags.applyTransitionDelay) {
  1013. temporaryStyles.push(getCssDelayStyle(delayStyle));
  1014. }
  1015. if (flags.applyAnimationDelay) {
  1016. temporaryStyles.push(getCssDelayStyle(delayStyle, true));
  1017. }
  1018. }
  1019. // we need to recalculate the delay value since we used a pre-emptive negative
  1020. // delay value and the delay value is required for the final event checking. This
  1021. // property will ensure that this will happen after the RAF phase has passed.
  1022. if (options.duration == null && timings.transitionDuration > 0) {
  1023. flags.recalculateTimingStyles = flags.recalculateTimingStyles || isFirst;
  1024. }
  1025. maxDelayTime = maxDelay * ONE_SECOND;
  1026. maxDurationTime = maxDuration * ONE_SECOND;
  1027. if (!options.skipBlocking) {
  1028. flags.blockTransition = timings.transitionDuration > 0;
  1029. flags.blockKeyframeAnimation = timings.animationDuration > 0 &&
  1030. stagger.animationDelay > 0 &&
  1031. stagger.animationDuration === 0;
  1032. }
  1033. if (options.from) {
  1034. if (options.cleanupStyles) {
  1035. registerRestorableStyles(restoreStyles, node, Object.keys(options.from));
  1036. }
  1037. applyAnimationFromStyles(element, options);
  1038. }
  1039. if (flags.blockTransition || flags.blockKeyframeAnimation) {
  1040. applyBlocking(maxDuration);
  1041. } else if (!options.skipBlocking) {
  1042. blockTransitions(node, false);
  1043. }
  1044. // TODO(matsko): for 1.5 change this code to have an animator object for better debugging
  1045. return {
  1046. $$willAnimate: true,
  1047. end: endFn,
  1048. start: function () {
  1049. if (animationClosed) return;
  1050. runnerHost = {
  1051. end: endFn,
  1052. cancel: cancelFn,
  1053. resume: null, //this will be set during the start() phase
  1054. pause: null
  1055. };
  1056. runner = new $$AnimateRunner(runnerHost);
  1057. waitUntilQuiet(start);
  1058. // we don't have access to pause/resume the animation
  1059. // since it hasn't run yet. AnimateRunner will therefore
  1060. // set noop functions for resume and pause and they will
  1061. // later be overridden once the animation is triggered
  1062. return runner;
  1063. }
  1064. };
  1065. function endFn() {
  1066. close();
  1067. }
  1068. function cancelFn() {
  1069. close(true);
  1070. }
  1071. function close(rejected) {
  1072. // if the promise has been called already then we shouldn't close
  1073. // the animation again
  1074. if (animationClosed || (animationCompleted && animationPaused)) return;
  1075. animationClosed = true;
  1076. animationPaused = false;
  1077. if (!options.$$skipPreparationClasses) {
  1078. $$jqLite.removeClass(element, preparationClasses);
  1079. }
  1080. $$jqLite.removeClass(element, activeClasses);
  1081. blockKeyframeAnimations(node, false);
  1082. blockTransitions(node, false);
  1083. forEach(temporaryStyles, function (entry) {
  1084. // There is only one way to remove inline style properties entirely from elements.
  1085. // By using `removeProperty` this works, but we need to convert camel-cased CSS
  1086. // styles down to hyphenated values.
  1087. node.style[entry[0]] = '';
  1088. });
  1089. applyAnimationClasses(element, options);
  1090. applyAnimationStyles(element, options);
  1091. if (Object.keys(restoreStyles).length) {
  1092. forEach(restoreStyles, function (value, prop) {
  1093. if (value) {
  1094. node.style.setProperty(prop, value);
  1095. } else {
  1096. node.style.removeProperty(prop);
  1097. }
  1098. });
  1099. }
  1100. // the reason why we have this option is to allow a synchronous closing callback
  1101. // that is fired as SOON as the animation ends (when the CSS is removed) or if
  1102. // the animation never takes off at all. A good example is a leave animation since
  1103. // the element must be removed just after the animation is over or else the element
  1104. // will appear on screen for one animation frame causing an overbearing flicker.
  1105. if (options.onDone) {
  1106. options.onDone();
  1107. }
  1108. if (events && events.length) {
  1109. // Remove the transitionend / animationend listener(s)
  1110. element.off(events.join(' '), onAnimationProgress);
  1111. }
  1112. //Cancel the fallback closing timeout and remove the timer data
  1113. var animationTimerData = element.data(ANIMATE_TIMER_KEY);
  1114. if (animationTimerData) {
  1115. $timeout.cancel(animationTimerData[0].timer);
  1116. element.removeData(ANIMATE_TIMER_KEY);
  1117. }
  1118. // if the preparation function fails then the promise is not setup
  1119. if (runner) {
  1120. runner.complete(!rejected);
  1121. }
  1122. }
  1123. function applyBlocking(duration) {
  1124. if (flags.blockTransition) {
  1125. blockTransitions(node, duration);
  1126. }
  1127. if (flags.blockKeyframeAnimation) {
  1128. blockKeyframeAnimations(node, !!duration);
  1129. }
  1130. }
  1131. function closeAndReturnNoopAnimator() {
  1132. runner = new $$AnimateRunner({
  1133. end: endFn,
  1134. cancel: cancelFn
  1135. });
  1136. // should flush the cache animation
  1137. waitUntilQuiet(noop);
  1138. close();
  1139. return {
  1140. $$willAnimate: false,
  1141. start: function () {
  1142. return runner;
  1143. },
  1144. end: endFn
  1145. };
  1146. }
  1147. function onAnimationProgress(event) {
  1148. event.stopPropagation();
  1149. var ev = event.originalEvent || event;
  1150. // we now always use `Date.now()` due to the recent changes with
  1151. // event.timeStamp in Firefox, Webkit and Chrome (see #13494 for more info)
  1152. var timeStamp = ev.$manualTimeStamp || Date.now();
  1153. /* Firefox (or possibly just Gecko) likes to not round values up
  1154. * when a ms measurement is used for the animation */
  1155. var elapsedTime = parseFloat(ev.elapsedTime.toFixed(ELAPSED_TIME_MAX_DECIMAL_PLACES));
  1156. /* $manualTimeStamp is a mocked timeStamp value which is set
  1157. * within browserTrigger(). This is only here so that tests can
  1158. * mock animations properly. Real events fallback to event.timeStamp,
  1159. * or, if they don't, then a timeStamp is automatically created for them.
  1160. * We're checking to see if the timeStamp surpasses the expected delay,
  1161. * but we're using elapsedTime instead of the timeStamp on the 2nd
  1162. * pre-condition since animationPauseds sometimes close off early */
  1163. if (Math.max(timeStamp - startTime, 0) >= maxDelayTime && elapsedTime >= maxDuration) {
  1164. // we set this flag to ensure that if the transition is paused then, when resumed,
  1165. // the animation will automatically close itself since transitions cannot be paused.
  1166. animationCompleted = true;
  1167. close();
  1168. }
  1169. }
  1170. function start() {
  1171. if (animationClosed) return;
  1172. if (!node.parentNode) {
  1173. close();
  1174. return;
  1175. }
  1176. // even though we only pause keyframe animations here the pause flag
  1177. // will still happen when transitions are used. Only the transition will
  1178. // not be paused since that is not possible. If the animation ends when
  1179. // paused then it will not complete until unpaused or cancelled.
  1180. var playPause = function (playAnimation) {
  1181. if (!animationCompleted) {
  1182. animationPaused = !playAnimation;
  1183. if (timings.animationDuration) {
  1184. var value = blockKeyframeAnimations(node, animationPaused);
  1185. if (animationPaused) {
  1186. temporaryStyles.push(value);
  1187. } else {
  1188. removeFromArray(temporaryStyles, value);
  1189. }
  1190. }
  1191. } else if (animationPaused && playAnimation) {
  1192. animationPaused = false;
  1193. close();
  1194. }
  1195. };
  1196. // checking the stagger duration prevents an accidentally cascade of the CSS delay style
  1197. // being inherited from the parent. If the transition duration is zero then we can safely
  1198. // rely that the delay value is an intentional stagger delay style.
  1199. var maxStagger = itemIndex > 0
  1200. && ((timings.transitionDuration && stagger.transitionDuration === 0) ||
  1201. (timings.animationDuration && stagger.animationDuration === 0))
  1202. && Math.max(stagger.animationDelay, stagger.transitionDelay);
  1203. if (maxStagger) {
  1204. $timeout(triggerAnimationStart,
  1205. Math.floor(maxStagger * itemIndex * ONE_SECOND),
  1206. false);
  1207. } else {
  1208. triggerAnimationStart();
  1209. }
  1210. // this will decorate the existing promise runner with pause/resume methods
  1211. runnerHost.resume = function () {
  1212. playPause(true);
  1213. };
  1214. runnerHost.pause = function () {
  1215. playPause(false);
  1216. };
  1217. function triggerAnimationStart() {
  1218. // just incase a stagger animation kicks in when the animation
  1219. // itself was cancelled entirely
  1220. if (animationClosed) return;
  1221. applyBlocking(false);
  1222. forEach(temporaryStyles, function (entry) {
  1223. var key = entry[0];
  1224. var value = entry[1];
  1225. node.style[key] = value;
  1226. });
  1227. applyAnimationClasses(element, options);
  1228. $$jqLite.addClass(element, activeClasses);
  1229. if (flags.recalculateTimingStyles) {
  1230. fullClassName = node.getAttribute('class') + ' ' + preparationClasses;
  1231. cacheKey = gcsHashFn(node, fullClassName);
  1232. timings = computeTimings(node, fullClassName, cacheKey);
  1233. relativeDelay = timings.maxDelay;
  1234. maxDelay = Math.max(relativeDelay, 0);
  1235. maxDuration = timings.maxDuration;
  1236. if (maxDuration === 0) {
  1237. close();
  1238. return;
  1239. }
  1240. flags.hasTransitions = timings.transitionDuration > 0;
  1241. flags.hasAnimations = timings.animationDuration > 0;
  1242. }
  1243. if (flags.applyAnimationDelay) {
  1244. relativeDelay = typeof options.delay !== 'boolean' && truthyTimingValue(options.delay)
  1245. ? parseFloat(options.delay)
  1246. : relativeDelay;
  1247. maxDelay = Math.max(relativeDelay, 0);
  1248. timings.animationDelay = relativeDelay;
  1249. delayStyle = getCssDelayStyle(relativeDelay, true);
  1250. temporaryStyles.push(delayStyle);
  1251. node.style[delayStyle[0]] = delayStyle[1];
  1252. }
  1253. maxDelayTime = maxDelay * ONE_SECOND;
  1254. maxDurationTime = maxDuration * ONE_SECOND;
  1255. if (options.easing) {
  1256. var easeProp, easeVal = options.easing;
  1257. if (flags.hasTransitions) {
  1258. easeProp = TRANSITION_PROP + TIMING_KEY;
  1259. temporaryStyles.push([easeProp, easeVal]);
  1260. node.style[easeProp] = easeVal;
  1261. }
  1262. if (flags.hasAnimations) {
  1263. easeProp = ANIMATION_PROP + TIMING_KEY;
  1264. temporaryStyles.push([easeProp, easeVal]);
  1265. node.style[easeProp] = easeVal;
  1266. }
  1267. }
  1268. if (timings.transitionDuration) {
  1269. events.push(TRANSITIONEND_EVENT);
  1270. }
  1271. if (timings.animationDuration) {
  1272. events.push(ANIMATIONEND_EVENT);
  1273. }
  1274. startTime = Date.now();
  1275. var timerTime = maxDelayTime + CLOSING_TIME_BUFFER * maxDurationTime;
  1276. var endTime = startTime + timerTime;
  1277. var animationsData = element.data(ANIMATE_TIMER_KEY) || [];
  1278. var setupFallbackTimer = true;
  1279. if (animationsData.length) {
  1280. var currentTimerData = animationsData[0];
  1281. setupFallbackTimer = endTime > currentTimerData.expectedEndTime;
  1282. if (setupFallbackTimer) {
  1283. $timeout.cancel(currentTimerData.timer);
  1284. } else {
  1285. animationsData.push(close);
  1286. }
  1287. }
  1288. if (setupFallbackTimer) {
  1289. var timer = $timeout(onAnimationExpired, timerTime, false);
  1290. animationsData[0] = {
  1291. timer: timer,
  1292. expectedEndTime: endTime
  1293. };
  1294. animationsData.push(close);
  1295. element.data(ANIMATE_TIMER_KEY, animationsData);
  1296. }
  1297. if (events.length) {
  1298. element.on(events.join(' '), onAnimationProgress);
  1299. }
  1300. if (options.to) {
  1301. if (options.cleanupStyles) {
  1302. registerRestorableStyles(restoreStyles, node, Object.keys(options.to));
  1303. }
  1304. applyAnimationToStyles(element, options);
  1305. }
  1306. }
  1307. function onAnimationExpired() {
  1308. var animationsData = element.data(ANIMATE_TIMER_KEY);
  1309. // this will be false in the event that the element was
  1310. // removed from the DOM (via a leave animation or something
  1311. // similar)
  1312. if (animationsData) {
  1313. for (var i = 1; i < animationsData.length; i++) {
  1314. animationsData[i]();
  1315. }
  1316. element.removeData(ANIMATE_TIMER_KEY);
  1317. }
  1318. }
  1319. }
  1320. };
  1321. }];
  1322. }];
  1323. var $$AnimateCssDriverProvider = ['$$animationProvider', /** @this */ function ($$animationProvider) {
  1324. $$animationProvider.drivers.push('$$animateCssDriver');
  1325. var NG_ANIMATE_SHIM_CLASS_NAME = 'ng-animate-shim';
  1326. var NG_ANIMATE_ANCHOR_CLASS_NAME = 'ng-anchor';
  1327. var NG_OUT_ANCHOR_CLASS_NAME = 'ng-anchor-out';
  1328. var NG_IN_ANCHOR_CLASS_NAME = 'ng-anchor-in';
  1329. function isDocumentFragment(node) {
  1330. return node.parentNode && node.parentNode.nodeType === 11;
  1331. }
  1332. this.$get = ['$animateCss', '$rootScope', '$$AnimateRunner', '$rootElement', '$sniffer', '$$jqLite', '$document',
  1333. function ($animateCss, $rootScope, $$AnimateRunner, $rootElement, $sniffer, $$jqLite, $document) {
  1334. // only browsers that support these properties can render animations
  1335. if (!$sniffer.animations && !$sniffer.transitions) return noop;
  1336. var bodyNode = $document[0].body;
  1337. var rootNode = getDomNode($rootElement);
  1338. var rootBodyElement = jqLite(
  1339. // this is to avoid using something that exists outside of the body
  1340. // we also special case the doc fragment case because our unit test code
  1341. // appends the $rootElement to the body after the app has been bootstrapped
  1342. isDocumentFragment(rootNode) || bodyNode.contains(rootNode) ? rootNode : bodyNode
  1343. );
  1344. return function initDriverFn(animationDetails) {
  1345. return animationDetails.from && animationDetails.to
  1346. ? prepareFromToAnchorAnimation(animationDetails.from,
  1347. animationDetails.to,
  1348. animationDetails.classes,
  1349. animationDetails.anchors)
  1350. : prepareRegularAnimation(animationDetails);
  1351. };
  1352. function filterCssClasses(classes) {
  1353. //remove all the `ng-` stuff
  1354. return classes.replace(/\bng-\S+\b/g, '');
  1355. }
  1356. function getUniqueValues(a, b) {
  1357. if (isString(a)) a = a.split(' ');
  1358. if (isString(b)) b = b.split(' ');
  1359. return a.filter(function (val) {
  1360. return b.indexOf(val) === -1;
  1361. }).join(' ');
  1362. }
  1363. function prepareAnchoredAnimation(classes, outAnchor, inAnchor) {
  1364. var clone = jqLite(getDomNode(outAnchor).cloneNode(true));
  1365. var startingClasses = filterCssClasses(getClassVal(clone));
  1366. outAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
  1367. inAnchor.addClass(NG_ANIMATE_SHIM_CLASS_NAME);
  1368. clone.addClass(NG_ANIMATE_ANCHOR_CLASS_NAME);
  1369. rootBodyElement.append(clone);
  1370. var animatorIn, animatorOut = prepareOutAnimation();
  1371. // the user may not end up using the `out` animation and
  1372. // only making use of the `in` animation or vice-versa.
  1373. // In either case we should allow this and not assume the
  1374. // animation is over unless both animations are not used.
  1375. if (!animatorOut) {
  1376. animatorIn = prepareInAnimation();
  1377. if (!animatorIn) {
  1378. return end();
  1379. }
  1380. }
  1381. var startingAnimator = animatorOut || animatorIn;
  1382. return {
  1383. start: function () {
  1384. var runner;
  1385. var currentAnimation = startingAnimator.start();
  1386. currentAnimation.done(function () {
  1387. currentAnimation = null;
  1388. if (!animatorIn) {
  1389. animatorIn = prepareInAnimation();
  1390. if (animatorIn) {
  1391. currentAnimation = animatorIn.start();
  1392. currentAnimation.done(function () {
  1393. currentAnimation = null;
  1394. end();
  1395. runner.complete();
  1396. });
  1397. return currentAnimation;
  1398. }
  1399. }
  1400. // in the event that there is no `in` animation
  1401. end();
  1402. runner.complete();
  1403. });
  1404. runner = new $$AnimateRunner({
  1405. end: endFn,
  1406. cancel: endFn
  1407. });
  1408. return runner;
  1409. function endFn() {
  1410. if (currentAnimation) {
  1411. currentAnimation.end();
  1412. }
  1413. }
  1414. }
  1415. };
  1416. function calculateAnchorStyles(anchor) {
  1417. var styles = {};
  1418. var coords = getDomNode(anchor).getBoundingClientRect();
  1419. // we iterate directly since safari messes up and doesn't return
  1420. // all the keys for the coords object when iterated
  1421. forEach(['width', 'height', 'top', 'left'], function (key) {
  1422. var value = coords[key];
  1423. switch (key) {
  1424. case 'top':
  1425. value += bodyNode.scrollTop;
  1426. break;
  1427. case 'left':
  1428. value += bodyNode.scrollLeft;
  1429. break;
  1430. }
  1431. styles[key] = Math.floor(value) + 'px';
  1432. });
  1433. return styles;
  1434. }
  1435. function prepareOutAnimation() {
  1436. var animator = $animateCss(clone, {
  1437. addClass: NG_OUT_ANCHOR_CLASS_NAME,
  1438. delay: true,
  1439. from: calculateAnchorStyles(outAnchor)
  1440. });
  1441. // read the comment within `prepareRegularAnimation` to understand
  1442. // why this check is necessary
  1443. return animator.$$willAnimate ? animator : null;
  1444. }
  1445. function getClassVal(element) {
  1446. return element.attr('class') || '';
  1447. }
  1448. function prepareInAnimation() {
  1449. var endingClasses = filterCssClasses(getClassVal(inAnchor));
  1450. var toAdd = getUniqueValues(endingClasses, startingClasses);
  1451. var toRemove = getUniqueValues(startingClasses, endingClasses);
  1452. var animator = $animateCss(clone, {
  1453. to: calculateAnchorStyles(inAnchor),
  1454. addClass: NG_IN_ANCHOR_CLASS_NAME + ' ' + toAdd,
  1455. removeClass: NG_OUT_ANCHOR_CLASS_NAME + ' ' + toRemove,
  1456. delay: true
  1457. });
  1458. // read the comment within `prepareRegularAnimation` to understand
  1459. // why this check is necessary
  1460. return animator.$$willAnimate ? animator : null;
  1461. }
  1462. function end() {
  1463. clone.remove();
  1464. outAnchor.removeClass(NG_ANIMATE_SHIM_CLASS_NAME);
  1465. inAnchor.removeClass(NG_ANIMATE_SHIM_CLASS_NAME);
  1466. }
  1467. }
  1468. function prepareFromToAnchorAnimation(from, to, classes, anchors) {
  1469. var fromAnimation = prepareRegularAnimation(from, noop);
  1470. var toAnimation = prepareRegularAnimation(to, noop);
  1471. var anchorAnimations = [];
  1472. forEach(anchors, function (anchor) {
  1473. var outElement = anchor['out'];
  1474. var inElement = anchor['in'];
  1475. var animator = prepareAnchoredAnimation(classes, outElement, inElement);
  1476. if (animator) {
  1477. anchorAnimations.push(animator);
  1478. }
  1479. });
  1480. // no point in doing anything when there are no elements to animate
  1481. if (!fromAnimation && !toAnimation && anchorAnimations.length === 0) return;
  1482. return {
  1483. start: function () {
  1484. var animationRunners = [];
  1485. if (fromAnimation) {
  1486. animationRunners.push(fromAnimation.start());
  1487. }
  1488. if (toAnimation) {
  1489. animationRunners.push(toAnimation.start());
  1490. }
  1491. forEach(anchorAnimations, function (animation) {
  1492. animationRunners.push(animation.start());
  1493. });
  1494. var runner = new $$AnimateRunner({
  1495. end: endFn,
  1496. cancel: endFn // CSS-driven animations cannot be cancelled, only ended
  1497. });
  1498. $$AnimateRunner.all(animationRunners, function (status) {
  1499. runner.complete(status);
  1500. });
  1501. return runner;
  1502. function endFn() {
  1503. forEach(animationRunners, function (runner) {
  1504. runner.end();
  1505. });
  1506. }
  1507. }
  1508. };
  1509. }
  1510. function prepareRegularAnimation(animationDetails) {
  1511. var element = animationDetails.element;
  1512. var options = animationDetails.options || {};
  1513. if (animationDetails.structural) {
  1514. options.event = animationDetails.event;
  1515. options.structural = true;
  1516. options.applyClassesEarly = true;
  1517. // we special case the leave animation since we want to ensure that
  1518. // the element is removed as soon as the animation is over. Otherwise
  1519. // a flicker might appear or the element may not be removed at all
  1520. if (animationDetails.event === 'leave') {
  1521. options.onDone = options.domOperation;
  1522. }
  1523. }
  1524. // We assign the preparationClasses as the actual animation event since
  1525. // the internals of $animateCss will just suffix the event token values
  1526. // with `-active` to trigger the animation.
  1527. if (options.preparationClasses) {
  1528. options.event = concatWithSpace(options.event, options.preparationClasses);
  1529. }
  1530. var animator = $animateCss(element, options);
  1531. // the driver lookup code inside of $$animation attempts to spawn a
  1532. // driver one by one until a driver returns a.$$willAnimate animator object.
  1533. // $animateCss will always return an object, however, it will pass in
  1534. // a flag as a hint as to whether an animation was detected or not
  1535. return animator.$$willAnimate ? animator : null;
  1536. }
  1537. }];
  1538. }];
  1539. // TODO(matsko): use caching here to speed things up for detection
  1540. // TODO(matsko): add documentation
  1541. // by the time...
  1542. var $$AnimateJsProvider = ['$animateProvider', /** @this */ function ($animateProvider) {
  1543. this.$get = ['$injector', '$$AnimateRunner', '$$jqLite',
  1544. function ($injector, $$AnimateRunner, $$jqLite) {
  1545. var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
  1546. // $animateJs(element, 'enter');
  1547. return function (element, event, classes, options) {
  1548. var animationClosed = false;
  1549. // the `classes` argument is optional and if it is not used
  1550. // then the classes will be resolved from the element's className
  1551. // property as well as options.addClass/options.removeClass.
  1552. if (arguments.length === 3 && isObject(classes)) {
  1553. options = classes;
  1554. classes = null;
  1555. }
  1556. options = prepareAnimationOptions(options);
  1557. if (!classes) {
  1558. classes = element.attr('class') || '';
  1559. if (options.addClass) {
  1560. classes += ' ' + options.addClass;
  1561. }
  1562. if (options.removeClass) {
  1563. classes += ' ' + options.removeClass;
  1564. }
  1565. }
  1566. var classesToAdd = options.addClass;
  1567. var classesToRemove = options.removeClass;
  1568. // the lookupAnimations function returns a series of animation objects that are
  1569. // matched up with one or more of the CSS classes. These animation objects are
  1570. // defined via the module.animation factory function. If nothing is detected then
  1571. // we don't return anything which then makes $animation query the next driver.
  1572. var animations = lookupAnimations(classes);
  1573. var before, after;
  1574. if (animations.length) {
  1575. var afterFn, beforeFn;
  1576. if (event === 'leave') {
  1577. beforeFn = 'leave';
  1578. afterFn = 'afterLeave'; // TODO(matsko): get rid of this
  1579. } else {
  1580. beforeFn = 'before' + event.charAt(0).toUpperCase() + event.substr(1);
  1581. afterFn = event;
  1582. }
  1583. if (event !== 'enter' && event !== 'move') {
  1584. before = packageAnimations(element, event, options, animations, beforeFn);
  1585. }
  1586. after = packageAnimations(element, event, options, animations, afterFn);
  1587. }
  1588. // no matching animations
  1589. if (!before && !after) return;
  1590. function applyOptions() {
  1591. options.domOperation();
  1592. applyAnimationClasses(element, options);
  1593. }
  1594. function close() {
  1595. animationClosed = true;
  1596. applyOptions();
  1597. applyAnimationStyles(element, options);
  1598. }
  1599. var runner;
  1600. return {
  1601. $$willAnimate: true,
  1602. end: function () {
  1603. if (runner) {
  1604. runner.end();
  1605. } else {
  1606. close();
  1607. runner = new $$AnimateRunner();
  1608. runner.complete(true);
  1609. }
  1610. return runner;
  1611. },
  1612. start: function () {
  1613. if (runner) {
  1614. return runner;
  1615. }
  1616. runner = new $$AnimateRunner();
  1617. var closeActiveAnimations;
  1618. var chain = [];
  1619. if (before) {
  1620. chain.push(function (fn) {
  1621. closeActiveAnimations = before(fn);
  1622. });
  1623. }
  1624. if (chain.length) {
  1625. chain.push(function (fn) {
  1626. applyOptions();
  1627. fn(true);
  1628. });
  1629. } else {
  1630. applyOptions();
  1631. }
  1632. if (after) {
  1633. chain.push(function (fn) {
  1634. closeActiveAnimations = after(fn);
  1635. });
  1636. }
  1637. runner.setHost({
  1638. end: function () {
  1639. endAnimations();
  1640. },
  1641. cancel: function () {
  1642. endAnimations(true);
  1643. }
  1644. });
  1645. $$AnimateRunner.chain(chain, onComplete);
  1646. return runner;
  1647. function onComplete(success) {
  1648. close(success);
  1649. runner.complete(success);
  1650. }
  1651. function endAnimations(cancelled) {
  1652. if (!animationClosed) {
  1653. (closeActiveAnimations || noop)(cancelled);
  1654. onComplete(cancelled);
  1655. }
  1656. }
  1657. }
  1658. };
  1659. function executeAnimationFn(fn, element, event, options, onDone) {
  1660. var args;
  1661. switch (event) {
  1662. case 'animate':
  1663. args = [element, options.from, options.to, onDone];
  1664. break;
  1665. case 'setClass':
  1666. args = [element, classesToAdd, classesToRemove, onDone];
  1667. break;
  1668. case 'addClass':
  1669. args = [element, classesToAdd, onDone];
  1670. break;
  1671. case 'removeClass':
  1672. args = [element, classesToRemove, onDone];
  1673. break;
  1674. default:
  1675. args = [element, onDone];
  1676. break;
  1677. }
  1678. args.push(options);
  1679. var value = fn.apply(fn, args);
  1680. if (value) {
  1681. if (isFunction(value.start)) {
  1682. value = value.start();
  1683. }
  1684. if (value instanceof $$AnimateRunner) {
  1685. value.done(onDone);
  1686. } else if (isFunction(value)) {
  1687. // optional onEnd / onCancel callback
  1688. return value;
  1689. }
  1690. }
  1691. return noop;
  1692. }
  1693. function groupEventedAnimations(element, event, options, animations, fnName) {
  1694. var operations = [];
  1695. forEach(animations, function (ani) {
  1696. var animation = ani[fnName];
  1697. if (!animation) return;
  1698. // note that all of these animations will run in parallel
  1699. operations.push(function () {
  1700. var runner;
  1701. var endProgressCb;
  1702. var resolved = false;
  1703. var onAnimationComplete = function (rejected) {
  1704. if (!resolved) {
  1705. resolved = true;
  1706. (endProgressCb || noop)(rejected);
  1707. runner.complete(!rejected);
  1708. }
  1709. };
  1710. runner = new $$AnimateRunner({
  1711. end: function () {
  1712. onAnimationComplete();
  1713. },
  1714. cancel: function () {
  1715. onAnimationComplete(true);
  1716. }
  1717. });
  1718. endProgressCb = executeAnimationFn(animation, element, event, options, function (result) {
  1719. var cancelled = result === false;
  1720. onAnimationComplete(cancelled);
  1721. });
  1722. return runner;
  1723. });
  1724. });
  1725. return operations;
  1726. }
  1727. function packageAnimations(element, event, options, animations, fnName) {
  1728. var operations = groupEventedAnimations(element, event, options, animations, fnName);
  1729. if (operations.length === 0) {
  1730. var a, b;
  1731. if (fnName === 'beforeSetClass') {
  1732. a = groupEventedAnimations(element, 'removeClass', options, animations, 'beforeRemoveClass');
  1733. b = groupEventedAnimations(element, 'addClass', options, animations, 'beforeAddClass');
  1734. } else if (fnName === 'setClass') {
  1735. a = groupEventedAnimations(element, 'removeClass', options, animations, 'removeClass');
  1736. b = groupEventedAnimations(element, 'addClass', options, animations, 'addClass');
  1737. }
  1738. if (a) {
  1739. operations = operations.concat(a);
  1740. }
  1741. if (b) {
  1742. operations = operations.concat(b);
  1743. }
  1744. }
  1745. if (operations.length === 0) return;
  1746. // TODO(matsko): add documentation
  1747. return function startAnimation(callback) {
  1748. var runners = [];
  1749. if (operations.length) {
  1750. forEach(operations, function (animateFn) {
  1751. runners.push(animateFn());
  1752. });
  1753. }
  1754. if (runners.length) {
  1755. $$AnimateRunner.all(runners, callback);
  1756. } else {
  1757. callback();
  1758. }
  1759. return function endFn(reject) {
  1760. forEach(runners, function (runner) {
  1761. if (reject) {
  1762. runner.cancel();
  1763. } else {
  1764. runner.end();
  1765. }
  1766. });
  1767. };
  1768. };
  1769. }
  1770. };
  1771. function lookupAnimations(classes) {
  1772. classes = isArray(classes) ? classes : classes.split(' ');
  1773. var matches = [], flagMap = {};
  1774. for (var i = 0; i < classes.length; i++) {
  1775. var klass = classes[i],
  1776. animationFactory = $animateProvider.$$registeredAnimations[klass];
  1777. if (animationFactory && !flagMap[klass]) {
  1778. matches.push($injector.get(animationFactory));
  1779. flagMap[klass] = true;
  1780. }
  1781. }
  1782. return matches;
  1783. }
  1784. }];
  1785. }];
  1786. var $$AnimateJsDriverProvider = ['$$animationProvider', /** @this */ function ($$animationProvider) {
  1787. $$animationProvider.drivers.push('$$animateJsDriver');
  1788. this.$get = ['$$animateJs', '$$AnimateRunner', function ($$animateJs, $$AnimateRunner) {
  1789. return function initDriverFn(animationDetails) {
  1790. if (animationDetails.from && animationDetails.to) {
  1791. var fromAnimation = prepareAnimation(animationDetails.from);
  1792. var toAnimation = prepareAnimation(animationDetails.to);
  1793. if (!fromAnimation && !toAnimation) return;
  1794. return {
  1795. start: function () {
  1796. var animationRunners = [];
  1797. if (fromAnimation) {
  1798. animationRunners.push(fromAnimation.start());
  1799. }
  1800. if (toAnimation) {
  1801. animationRunners.push(toAnimation.start());
  1802. }
  1803. $$AnimateRunner.all(animationRunners, done);
  1804. var runner = new $$AnimateRunner({
  1805. end: endFnFactory(),
  1806. cancel: endFnFactory()
  1807. });
  1808. return runner;
  1809. function endFnFactory() {
  1810. return function () {
  1811. forEach(animationRunners, function (runner) {
  1812. // at this point we cannot cancel animations for groups just yet. 1.5+
  1813. runner.end();
  1814. });
  1815. };
  1816. }
  1817. function done(status) {
  1818. runner.complete(status);
  1819. }
  1820. }
  1821. };
  1822. } else {
  1823. return prepareAnimation(animationDetails);
  1824. }
  1825. };
  1826. function prepareAnimation(animationDetails) {
  1827. // TODO(matsko): make sure to check for grouped animations and delegate down to normal animations
  1828. var element = animationDetails.element;
  1829. var event = animationDetails.event;
  1830. var options = animationDetails.options;
  1831. var classes = animationDetails.classes;
  1832. return $$animateJs(element, event, classes, options);
  1833. }
  1834. }];
  1835. }];
  1836. var NG_ANIMATE_ATTR_NAME = 'data-ng-animate';
  1837. var NG_ANIMATE_PIN_DATA = '$ngAnimatePin';
  1838. var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function ($animateProvider) {
  1839. var PRE_DIGEST_STATE = 1;
  1840. var RUNNING_STATE = 2;
  1841. var ONE_SPACE = ' ';
  1842. var rules = this.rules = {
  1843. skip: [],
  1844. cancel: [],
  1845. join: []
  1846. };
  1847. function makeTruthyCssClassMap(classString) {
  1848. if (!classString) {
  1849. return null;
  1850. }
  1851. var keys = classString.split(ONE_SPACE);
  1852. var map = Object.create(null);
  1853. forEach(keys, function (key) {
  1854. map[key] = true;
  1855. });
  1856. return map;
  1857. }
  1858. function hasMatchingClasses(newClassString, currentClassString) {
  1859. if (newClassString && currentClassString) {
  1860. var currentClassMap = makeTruthyCssClassMap(currentClassString);
  1861. return newClassString.split(ONE_SPACE).some(function (className) {
  1862. return currentClassMap[className];
  1863. });
  1864. }
  1865. }
  1866. function isAllowed(ruleType, currentAnimation, previousAnimation) {
  1867. return rules[ruleType].some(function (fn) {
  1868. return fn(currentAnimation, previousAnimation);
  1869. });
  1870. }
  1871. function hasAnimationClasses(animation, and) {
  1872. var a = (animation.addClass || '').length > 0;
  1873. var b = (animation.removeClass || '').length > 0;
  1874. return and ? a && b : a || b;
  1875. }
  1876. rules.join.push(function (newAnimation, currentAnimation) {
  1877. // if the new animation is class-based then we can just tack that on
  1878. return !newAnimation.structural && hasAnimationClasses(newAnimation);
  1879. });
  1880. rules.skip.push(function (newAnimation, currentAnimation) {
  1881. // there is no need to animate anything if no classes are being added and
  1882. // there is no structural animation that will be triggered
  1883. return !newAnimation.structural && !hasAnimationClasses(newAnimation);
  1884. });
  1885. rules.skip.push(function (newAnimation, currentAnimation) {
  1886. // why should we trigger a new structural animation if the element will
  1887. // be removed from the DOM anyway?
  1888. return currentAnimation.event === 'leave' && newAnimation.structural;
  1889. });
  1890. rules.skip.push(function (newAnimation, currentAnimation) {
  1891. // if there is an ongoing current animation then don't even bother running the class-based animation
  1892. return currentAnimation.structural && currentAnimation.state === RUNNING_STATE && !newAnimation.structural;
  1893. });
  1894. rules.cancel.push(function (newAnimation, currentAnimation) {
  1895. // there can never be two structural animations running at the same time
  1896. return currentAnimation.structural && newAnimation.structural;
  1897. });
  1898. rules.cancel.push(function (newAnimation, currentAnimation) {
  1899. // if the previous animation is already running, but the new animation will
  1900. // be triggered, but the new animation is structural
  1901. return currentAnimation.state === RUNNING_STATE && newAnimation.structural;
  1902. });
  1903. rules.cancel.push(function (newAnimation, currentAnimation) {
  1904. // cancel the animation if classes added / removed in both animation cancel each other out,
  1905. // but only if the current animation isn't structural
  1906. if (currentAnimation.structural) return false;
  1907. var nA = newAnimation.addClass;
  1908. var nR = newAnimation.removeClass;
  1909. var cA = currentAnimation.addClass;
  1910. var cR = currentAnimation.removeClass;
  1911. // early detection to save the global CPU shortage :)
  1912. if ((isUndefined(nA) && isUndefined(nR)) || (isUndefined(cA) && isUndefined(cR))) {
  1913. return false;
  1914. }
  1915. return hasMatchingClasses(nA, cR) || hasMatchingClasses(nR, cA);
  1916. });
  1917. this.$get = ['$$rAF', '$rootScope', '$rootElement', '$document', '$$Map',
  1918. '$$animation', '$$AnimateRunner', '$templateRequest', '$$jqLite', '$$forceReflow',
  1919. '$$isDocumentHidden',
  1920. function ($$rAF, $rootScope, $rootElement, $document, $$Map,
  1921. $$animation, $$AnimateRunner, $templateRequest, $$jqLite, $$forceReflow,
  1922. $$isDocumentHidden) {
  1923. var activeAnimationsLookup = new $$Map();
  1924. var disabledElementsLookup = new $$Map();
  1925. var animationsEnabled = null;
  1926. function postDigestTaskFactory() {
  1927. var postDigestCalled = false;
  1928. return function (fn) {
  1929. // we only issue a call to postDigest before
  1930. // it has first passed. This prevents any callbacks
  1931. // from not firing once the animation has completed
  1932. // since it will be out of the digest cycle.
  1933. if (postDigestCalled) {
  1934. fn();
  1935. } else {
  1936. $rootScope.$$postDigest(function () {
  1937. postDigestCalled = true;
  1938. fn();
  1939. });
  1940. }
  1941. };
  1942. }
  1943. // Wait until all directive and route-related templates are downloaded and
  1944. // compiled. The $templateRequest.totalPendingRequests variable keeps track of
  1945. // all of the remote templates being currently downloaded. If there are no
  1946. // templates currently downloading then the watcher will still fire anyway.
  1947. var deregisterWatch = $rootScope.$watch(
  1948. function () {
  1949. return $templateRequest.totalPendingRequests === 0;
  1950. },
  1951. function (isEmpty) {
  1952. if (!isEmpty) return;
  1953. deregisterWatch();
  1954. // Now that all templates have been downloaded, $animate will wait until
  1955. // the post digest queue is empty before enabling animations. By having two
  1956. // calls to $postDigest calls we can ensure that the flag is enabled at the
  1957. // very end of the post digest queue. Since all of the animations in $animate
  1958. // use $postDigest, it's important that the code below executes at the end.
  1959. // This basically means that the page is fully downloaded and compiled before
  1960. // any animations are triggered.
  1961. $rootScope.$$postDigest(function () {
  1962. $rootScope.$$postDigest(function () {
  1963. // we check for null directly in the event that the application already called
  1964. // .enabled() with whatever arguments that it provided it with
  1965. if (animationsEnabled === null) {
  1966. animationsEnabled = true;
  1967. }
  1968. });
  1969. });
  1970. }
  1971. );
  1972. var callbackRegistry = Object.create(null);
  1973. // remember that the `customFilter`/`classNameFilter` are set during the
  1974. // provider/config stage therefore we can optimize here and setup helper functions
  1975. var customFilter = $animateProvider.customFilter();
  1976. var classNameFilter = $animateProvider.classNameFilter();
  1977. var returnTrue = function () {
  1978. return true;
  1979. };
  1980. var isAnimatableByFilter = customFilter || returnTrue;
  1981. var isAnimatableClassName = !classNameFilter ? returnTrue : function (node, options) {
  1982. var className = [node.getAttribute('class'), options.addClass, options.removeClass].join(' ');
  1983. return classNameFilter.test(className);
  1984. };
  1985. var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
  1986. function normalizeAnimationDetails(element, animation) {
  1987. return mergeAnimationDetails(element, animation, {});
  1988. }
  1989. // IE9-11 has no method "contains" in SVG element and in Node.prototype. Bug #10259.
  1990. var contains = window.Node.prototype.contains || /** @this */ function (arg) {
  1991. // eslint-disable-next-line no-bitwise
  1992. return this === arg || !!(this.compareDocumentPosition(arg) & 16);
  1993. };
  1994. function findCallbacks(targetParentNode, targetNode, event) {
  1995. var matches = [];
  1996. var entries = callbackRegistry[event];
  1997. if (entries) {
  1998. forEach(entries, function (entry) {
  1999. if (contains.call(entry.node, targetNode)) {
  2000. matches.push(entry.callback);
  2001. } else if (event === 'leave' && contains.call(entry.node, targetParentNode)) {
  2002. matches.push(entry.callback);
  2003. }
  2004. });
  2005. }
  2006. return matches;
  2007. }
  2008. function filterFromRegistry(list, matchContainer, matchCallback) {
  2009. var containerNode = extractElementNode(matchContainer);
  2010. return list.filter(function (entry) {
  2011. var isMatch = entry.node === containerNode &&
  2012. (!matchCallback || entry.callback === matchCallback);
  2013. return !isMatch;
  2014. });
  2015. }
  2016. function cleanupEventListeners(phase, node) {
  2017. if (phase === 'close' && !node.parentNode) {
  2018. // If the element is not attached to a parentNode, it has been removed by
  2019. // the domOperation, and we can safely remove the event callbacks
  2020. $animate.off(node);
  2021. }
  2022. }
  2023. var $animate = {
  2024. on: function (event, container, callback) {
  2025. var node = extractElementNode(container);
  2026. callbackRegistry[event] = callbackRegistry[event] || [];
  2027. callbackRegistry[event].push({
  2028. node: node,
  2029. callback: callback
  2030. });
  2031. // Remove the callback when the element is removed from the DOM
  2032. jqLite(container).on('$destroy', function () {
  2033. var animationDetails = activeAnimationsLookup.get(node);
  2034. if (!animationDetails) {
  2035. // If there's an animation ongoing, the callback calling code will remove
  2036. // the event listeners. If we'd remove here, the callbacks would be removed
  2037. // before the animation ends
  2038. $animate.off(event, container, callback);
  2039. }
  2040. });
  2041. },
  2042. off: function (event, container, callback) {
  2043. if (arguments.length === 1 && !isString(arguments[0])) {
  2044. container = arguments[0];
  2045. for (var eventType in callbackRegistry) {
  2046. callbackRegistry[eventType] = filterFromRegistry(callbackRegistry[eventType], container);
  2047. }
  2048. return;
  2049. }
  2050. var entries = callbackRegistry[event];
  2051. if (!entries) return;
  2052. callbackRegistry[event] = arguments.length === 1
  2053. ? null
  2054. : filterFromRegistry(entries, container, callback);
  2055. },
  2056. pin: function (element, parentElement) {
  2057. assertArg(isElement(element), 'element', 'not an element');
  2058. assertArg(isElement(parentElement), 'parentElement', 'not an element');
  2059. element.data(NG_ANIMATE_PIN_DATA, parentElement);
  2060. },
  2061. push: function (element, event, options, domOperation) {
  2062. options = options || {};
  2063. options.domOperation = domOperation;
  2064. return queueAnimation(element, event, options);
  2065. },
  2066. // this method has four signatures:
  2067. // () - global getter
  2068. // (bool) - global setter
  2069. // (element) - element getter
  2070. // (element, bool) - element setter<F37>
  2071. enabled: function (element, bool) {
  2072. var argCount = arguments.length;
  2073. if (argCount === 0) {
  2074. // () - Global getter
  2075. bool = !!animationsEnabled;
  2076. } else {
  2077. var hasElement = isElement(element);
  2078. if (!hasElement) {
  2079. // (bool) - Global setter
  2080. bool = animationsEnabled = !!element;
  2081. } else {
  2082. var node = getDomNode(element);
  2083. if (argCount === 1) {
  2084. // (element) - Element getter
  2085. bool = !disabledElementsLookup.get(node);
  2086. } else {
  2087. // (element, bool) - Element setter
  2088. disabledElementsLookup.set(node, !bool);
  2089. }
  2090. }
  2091. }
  2092. return bool;
  2093. }
  2094. };
  2095. return $animate;
  2096. function queueAnimation(originalElement, event, initialOptions) {
  2097. // we always make a copy of the options since
  2098. // there should never be any side effects on
  2099. // the input data when running `$animateCss`.
  2100. var options = copy(initialOptions);
  2101. var element = stripCommentsFromElement(originalElement);
  2102. var node = getDomNode(element);
  2103. var parentNode = node && node.parentNode;
  2104. options = prepareAnimationOptions(options);
  2105. // we create a fake runner with a working promise.
  2106. // These methods will become available after the digest has passed
  2107. var runner = new $$AnimateRunner();
  2108. // this is used to trigger callbacks in postDigest mode
  2109. var runInNextPostDigestOrNow = postDigestTaskFactory();
  2110. if (isArray(options.addClass)) {
  2111. options.addClass = options.addClass.join(' ');
  2112. }
  2113. if (options.addClass && !isString(options.addClass)) {
  2114. options.addClass = null;
  2115. }
  2116. if (isArray(options.removeClass)) {
  2117. options.removeClass = options.removeClass.join(' ');
  2118. }
  2119. if (options.removeClass && !isString(options.removeClass)) {
  2120. options.removeClass = null;
  2121. }
  2122. if (options.from && !isObject(options.from)) {
  2123. options.from = null;
  2124. }
  2125. if (options.to && !isObject(options.to)) {
  2126. options.to = null;
  2127. }
  2128. // If animations are hard-disabled for the whole application there is no need to continue.
  2129. // There are also situations where a directive issues an animation for a jqLite wrapper that
  2130. // contains only comment nodes. In this case, there is no way we can perform an animation.
  2131. if (!animationsEnabled ||
  2132. !node ||
  2133. !isAnimatableByFilter(node, event, initialOptions) ||
  2134. !isAnimatableClassName(node, options)) {
  2135. close();
  2136. return runner;
  2137. }
  2138. var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
  2139. var documentHidden = $$isDocumentHidden();
  2140. // This is a hard disable of all animations the element itself, therefore there is no need to
  2141. // continue further past this point if not enabled
  2142. // Animations are also disabled if the document is currently hidden (page is not visible
  2143. // to the user), because browsers slow down or do not flush calls to requestAnimationFrame
  2144. var skipAnimations = documentHidden || disabledElementsLookup.get(node);
  2145. var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
  2146. var hasExistingAnimation = !!existingAnimation.state;
  2147. // there is no point in traversing the same collection of parent ancestors if a followup
  2148. // animation will be run on the same element that already did all that checking work
  2149. if (!skipAnimations && (!hasExistingAnimation || existingAnimation.state !== PRE_DIGEST_STATE)) {
  2150. skipAnimations = !areAnimationsAllowed(node, parentNode, event);
  2151. }
  2152. if (skipAnimations) {
  2153. // Callbacks should fire even if the document is hidden (regression fix for issue #14120)
  2154. if (documentHidden) notifyProgress(runner, event, 'start');
  2155. close();
  2156. if (documentHidden) notifyProgress(runner, event, 'close');
  2157. return runner;
  2158. }
  2159. if (isStructural) {
  2160. closeChildAnimations(node);
  2161. }
  2162. var newAnimation = {
  2163. structural: isStructural,
  2164. element: element,
  2165. event: event,
  2166. addClass: options.addClass,
  2167. removeClass: options.removeClass,
  2168. close: close,
  2169. options: options,
  2170. runner: runner
  2171. };
  2172. if (hasExistingAnimation) {
  2173. var skipAnimationFlag = isAllowed('skip', newAnimation, existingAnimation);
  2174. if (skipAnimationFlag) {
  2175. if (existingAnimation.state === RUNNING_STATE) {
  2176. close();
  2177. return runner;
  2178. } else {
  2179. mergeAnimationDetails(element, existingAnimation, newAnimation);
  2180. return existingAnimation.runner;
  2181. }
  2182. }
  2183. var cancelAnimationFlag = isAllowed('cancel', newAnimation, existingAnimation);
  2184. if (cancelAnimationFlag) {
  2185. if (existingAnimation.state === RUNNING_STATE) {
  2186. // this will end the animation right away and it is safe
  2187. // to do so since the animation is already running and the
  2188. // runner callback code will run in async
  2189. existingAnimation.runner.end();
  2190. } else if (existingAnimation.structural) {
  2191. // this means that the animation is queued into a digest, but
  2192. // hasn't started yet. Therefore it is safe to run the close
  2193. // method which will call the runner methods in async.
  2194. existingAnimation.close();
  2195. } else {
  2196. // this will merge the new animation options into existing animation options
  2197. mergeAnimationDetails(element, existingAnimation, newAnimation);
  2198. return existingAnimation.runner;
  2199. }
  2200. } else {
  2201. // a joined animation means that this animation will take over the existing one
  2202. // so an example would involve a leave animation taking over an enter. Then when
  2203. // the postDigest kicks in the enter will be ignored.
  2204. var joinAnimationFlag = isAllowed('join', newAnimation, existingAnimation);
  2205. if (joinAnimationFlag) {
  2206. if (existingAnimation.state === RUNNING_STATE) {
  2207. normalizeAnimationDetails(element, newAnimation);
  2208. } else {
  2209. applyGeneratedPreparationClasses(element, isStructural ? event : null, options);
  2210. event = newAnimation.event = existingAnimation.event;
  2211. options = mergeAnimationDetails(element, existingAnimation, newAnimation);
  2212. //we return the same runner since only the option values of this animation will
  2213. //be fed into the `existingAnimation`.
  2214. return existingAnimation.runner;
  2215. }
  2216. }
  2217. }
  2218. } else {
  2219. // normalization in this case means that it removes redundant CSS classes that
  2220. // already exist (addClass) or do not exist (removeClass) on the element
  2221. normalizeAnimationDetails(element, newAnimation);
  2222. }
  2223. // when the options are merged and cleaned up we may end up not having to do
  2224. // an animation at all, therefore we should check this before issuing a post
  2225. // digest callback. Structural animations will always run no matter what.
  2226. var isValidAnimation = newAnimation.structural;
  2227. if (!isValidAnimation) {
  2228. // animate (from/to) can be quickly checked first, otherwise we check if any classes are present
  2229. isValidAnimation = (newAnimation.event === 'animate' && Object.keys(newAnimation.options.to || {}).length > 0)
  2230. || hasAnimationClasses(newAnimation);
  2231. }
  2232. if (!isValidAnimation) {
  2233. close();
  2234. clearElementAnimationState(node);
  2235. return runner;
  2236. }
  2237. // the counter keeps track of cancelled animations
  2238. var counter = (existingAnimation.counter || 0) + 1;
  2239. newAnimation.counter = counter;
  2240. markElementAnimationState(node, PRE_DIGEST_STATE, newAnimation);
  2241. $rootScope.$$postDigest(function () {
  2242. // It is possible that the DOM nodes inside `originalElement` have been replaced. This can
  2243. // happen if the animated element is a transcluded clone and also has a `templateUrl`
  2244. // directive on it. Therefore, we must recreate `element` in order to interact with the
  2245. // actual DOM nodes.
  2246. // Note: We still need to use the old `node` for certain things, such as looking up in
  2247. // HashMaps where it was used as the key.
  2248. element = stripCommentsFromElement(originalElement);
  2249. var animationDetails = activeAnimationsLookup.get(node);
  2250. var animationCancelled = !animationDetails;
  2251. animationDetails = animationDetails || {};
  2252. // if addClass/removeClass is called before something like enter then the
  2253. // registered parent element may not be present. The code below will ensure
  2254. // that a final value for parent element is obtained
  2255. var parentElement = element.parent() || [];
  2256. // animate/structural/class-based animations all have requirements. Otherwise there
  2257. // is no point in performing an animation. The parent node must also be set.
  2258. var isValidAnimation = parentElement.length > 0
  2259. && (animationDetails.event === 'animate'
  2260. || animationDetails.structural
  2261. || hasAnimationClasses(animationDetails));
  2262. // this means that the previous animation was cancelled
  2263. // even if the follow-up animation is the same event
  2264. if (animationCancelled || animationDetails.counter !== counter || !isValidAnimation) {
  2265. // if another animation did not take over then we need
  2266. // to make sure that the domOperation and options are
  2267. // handled accordingly
  2268. if (animationCancelled) {
  2269. applyAnimationClasses(element, options);
  2270. applyAnimationStyles(element, options);
  2271. }
  2272. // if the event changed from something like enter to leave then we do
  2273. // it, otherwise if it's the same then the end result will be the same too
  2274. if (animationCancelled || (isStructural && animationDetails.event !== event)) {
  2275. options.domOperation();
  2276. runner.end();
  2277. }
  2278. // in the event that the element animation was not cancelled or a follow-up animation
  2279. // isn't allowed to animate from here then we need to clear the state of the element
  2280. // so that any future animations won't read the expired animation data.
  2281. if (!isValidAnimation) {
  2282. clearElementAnimationState(node);
  2283. }
  2284. return;
  2285. }
  2286. // this combined multiple class to addClass / removeClass into a setClass event
  2287. // so long as a structural event did not take over the animation
  2288. event = !animationDetails.structural && hasAnimationClasses(animationDetails, true)
  2289. ? 'setClass'
  2290. : animationDetails.event;
  2291. markElementAnimationState(node, RUNNING_STATE);
  2292. var realRunner = $$animation(element, event, animationDetails.options);
  2293. // this will update the runner's flow-control events based on
  2294. // the `realRunner` object.
  2295. runner.setHost(realRunner);
  2296. notifyProgress(runner, event, 'start', {});
  2297. realRunner.done(function (status) {
  2298. close(!status);
  2299. var animationDetails = activeAnimationsLookup.get(node);
  2300. if (animationDetails && animationDetails.counter === counter) {
  2301. clearElementAnimationState(node);
  2302. }
  2303. notifyProgress(runner, event, 'close', {});
  2304. });
  2305. });
  2306. return runner;
  2307. function notifyProgress(runner, event, phase, data) {
  2308. runInNextPostDigestOrNow(function () {
  2309. var callbacks = findCallbacks(parentNode, node, event);
  2310. if (callbacks.length) {
  2311. // do not optimize this call here to RAF because
  2312. // we don't know how heavy the callback code here will
  2313. // be and if this code is buffered then this can
  2314. // lead to a performance regression.
  2315. $$rAF(function () {
  2316. forEach(callbacks, function (callback) {
  2317. callback(element, phase, data);
  2318. });
  2319. cleanupEventListeners(phase, node);
  2320. });
  2321. } else {
  2322. cleanupEventListeners(phase, node);
  2323. }
  2324. });
  2325. runner.progress(event, phase, data);
  2326. }
  2327. function close(reject) {
  2328. clearGeneratedClasses(element, options);
  2329. applyAnimationClasses(element, options);
  2330. applyAnimationStyles(element, options);
  2331. options.domOperation();
  2332. runner.complete(!reject);
  2333. }
  2334. }
  2335. function closeChildAnimations(node) {
  2336. var children = node.querySelectorAll('[' + NG_ANIMATE_ATTR_NAME + ']');
  2337. forEach(children, function (child) {
  2338. var state = parseInt(child.getAttribute(NG_ANIMATE_ATTR_NAME), 10);
  2339. var animationDetails = activeAnimationsLookup.get(child);
  2340. if (animationDetails) {
  2341. switch (state) {
  2342. case RUNNING_STATE:
  2343. animationDetails.runner.end();
  2344. /* falls through */
  2345. case PRE_DIGEST_STATE:
  2346. activeAnimationsLookup.delete(child);
  2347. break;
  2348. }
  2349. }
  2350. });
  2351. }
  2352. function clearElementAnimationState(node) {
  2353. node.removeAttribute(NG_ANIMATE_ATTR_NAME);
  2354. activeAnimationsLookup.delete(node);
  2355. }
  2356. /**
  2357. * This fn returns false if any of the following is true:
  2358. * a) animations on any parent element are disabled, and animations on the element aren't explicitly allowed
  2359. * b) a parent element has an ongoing structural animation, and animateChildren is false
  2360. * c) the element is not a child of the body
  2361. * d) the element is not a child of the $rootElement
  2362. */
  2363. function areAnimationsAllowed(node, parentNode, event) {
  2364. var bodyNode = $document[0].body;
  2365. var rootNode = getDomNode($rootElement);
  2366. var bodyNodeDetected = (node === bodyNode) || node.nodeName === 'HTML';
  2367. var rootNodeDetected = (node === rootNode);
  2368. var parentAnimationDetected = false;
  2369. var elementDisabled = disabledElementsLookup.get(node);
  2370. var animateChildren;
  2371. var parentHost = jqLite.data(node, NG_ANIMATE_PIN_DATA);
  2372. if (parentHost) {
  2373. parentNode = getDomNode(parentHost);
  2374. }
  2375. while (parentNode) {
  2376. if (!rootNodeDetected) {
  2377. // angular doesn't want to attempt to animate elements outside of the application
  2378. // therefore we need to ensure that the rootElement is an ancestor of the current element
  2379. rootNodeDetected = (parentNode === rootNode);
  2380. }
  2381. if (parentNode.nodeType !== ELEMENT_NODE) {
  2382. // no point in inspecting the #document element
  2383. break;
  2384. }
  2385. var details = activeAnimationsLookup.get(parentNode) || {};
  2386. // either an enter, leave or move animation will commence
  2387. // therefore we can't allow any animations to take place
  2388. // but if a parent animation is class-based then that's ok
  2389. if (!parentAnimationDetected) {
  2390. var parentNodeDisabled = disabledElementsLookup.get(parentNode);
  2391. if (parentNodeDisabled === true && elementDisabled !== false) {
  2392. // disable animations if the user hasn't explicitly enabled animations on the
  2393. // current element
  2394. elementDisabled = true;
  2395. // element is disabled via parent element, no need to check anything else
  2396. break;
  2397. } else if (parentNodeDisabled === false) {
  2398. elementDisabled = false;
  2399. }
  2400. parentAnimationDetected = details.structural;
  2401. }
  2402. if (isUndefined(animateChildren) || animateChildren === true) {
  2403. var value = jqLite.data(parentNode, NG_ANIMATE_CHILDREN_DATA);
  2404. if (isDefined(value)) {
  2405. animateChildren = value;
  2406. }
  2407. }
  2408. // there is no need to continue traversing at this point
  2409. if (parentAnimationDetected && animateChildren === false) break;
  2410. if (!bodyNodeDetected) {
  2411. // we also need to ensure that the element is or will be a part of the body element
  2412. // otherwise it is pointless to even issue an animation to be rendered
  2413. bodyNodeDetected = (parentNode === bodyNode);
  2414. }
  2415. if (bodyNodeDetected && rootNodeDetected) {
  2416. // If both body and root have been found, any other checks are pointless,
  2417. // as no animation data should live outside the application
  2418. break;
  2419. }
  2420. if (!rootNodeDetected) {
  2421. // If `rootNode` is not detected, check if `parentNode` is pinned to another element
  2422. parentHost = jqLite.data(parentNode, NG_ANIMATE_PIN_DATA);
  2423. if (parentHost) {
  2424. // The pin target element becomes the next parent element
  2425. parentNode = getDomNode(parentHost);
  2426. continue;
  2427. }
  2428. }
  2429. parentNode = parentNode.parentNode;
  2430. }
  2431. var allowAnimation = (!parentAnimationDetected || animateChildren) && elementDisabled !== true;
  2432. return allowAnimation && rootNodeDetected && bodyNodeDetected;
  2433. }
  2434. function markElementAnimationState(node, state, details) {
  2435. details = details || {};
  2436. details.state = state;
  2437. node.setAttribute(NG_ANIMATE_ATTR_NAME, state);
  2438. var oldValue = activeAnimationsLookup.get(node);
  2439. var newValue = oldValue
  2440. ? extend(oldValue, details)
  2441. : details;
  2442. activeAnimationsLookup.set(node, newValue);
  2443. }
  2444. }];
  2445. }];
  2446. /* exported $$AnimationProvider */
  2447. var $$AnimationProvider = ['$animateProvider', /** @this */ function ($animateProvider) {
  2448. var NG_ANIMATE_REF_ATTR = 'ng-animate-ref';
  2449. var drivers = this.drivers = [];
  2450. var RUNNER_STORAGE_KEY = '$$animationRunner';
  2451. function setRunner(element, runner) {
  2452. element.data(RUNNER_STORAGE_KEY, runner);
  2453. }
  2454. function removeRunner(element) {
  2455. element.removeData(RUNNER_STORAGE_KEY);
  2456. }
  2457. function getRunner(element) {
  2458. return element.data(RUNNER_STORAGE_KEY);
  2459. }
  2460. this.$get = ['$$jqLite', '$rootScope', '$injector', '$$AnimateRunner', '$$Map', '$$rAFScheduler',
  2461. function ($$jqLite, $rootScope, $injector, $$AnimateRunner, $$Map, $$rAFScheduler) {
  2462. var animationQueue = [];
  2463. var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
  2464. function sortAnimations(animations) {
  2465. var tree = {children: []};
  2466. var i, lookup = new $$Map();
  2467. // this is done first beforehand so that the map
  2468. // is filled with a list of the elements that will be animated
  2469. for (i = 0; i < animations.length; i++) {
  2470. var animation = animations[i];
  2471. lookup.set(animation.domNode, animations[i] = {
  2472. domNode: animation.domNode,
  2473. fn: animation.fn,
  2474. children: []
  2475. });
  2476. }
  2477. for (i = 0; i < animations.length; i++) {
  2478. processNode(animations[i]);
  2479. }
  2480. return flatten(tree);
  2481. function processNode(entry) {
  2482. if (entry.processed) return entry;
  2483. entry.processed = true;
  2484. var elementNode = entry.domNode;
  2485. var parentNode = elementNode.parentNode;
  2486. lookup.set(elementNode, entry);
  2487. var parentEntry;
  2488. while (parentNode) {
  2489. parentEntry = lookup.get(parentNode);
  2490. if (parentEntry) {
  2491. if (!parentEntry.processed) {
  2492. parentEntry = processNode(parentEntry);
  2493. }
  2494. break;
  2495. }
  2496. parentNode = parentNode.parentNode;
  2497. }
  2498. (parentEntry || tree).children.push(entry);
  2499. return entry;
  2500. }
  2501. function flatten(tree) {
  2502. var result = [];
  2503. var queue = [];
  2504. var i;
  2505. for (i = 0; i < tree.children.length; i++) {
  2506. queue.push(tree.children[i]);
  2507. }
  2508. var remainingLevelEntries = queue.length;
  2509. var nextLevelEntries = 0;
  2510. var row = [];
  2511. for (i = 0; i < queue.length; i++) {
  2512. var entry = queue[i];
  2513. if (remainingLevelEntries <= 0) {
  2514. remainingLevelEntries = nextLevelEntries;
  2515. nextLevelEntries = 0;
  2516. result.push(row);
  2517. row = [];
  2518. }
  2519. row.push(entry.fn);
  2520. entry.children.forEach(function (childEntry) {
  2521. nextLevelEntries++;
  2522. queue.push(childEntry);
  2523. });
  2524. remainingLevelEntries--;
  2525. }
  2526. if (row.length) {
  2527. result.push(row);
  2528. }
  2529. return result;
  2530. }
  2531. }
  2532. // TODO(matsko): document the signature in a better way
  2533. return function (element, event, options) {
  2534. options = prepareAnimationOptions(options);
  2535. var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
  2536. // there is no animation at the current moment, however
  2537. // these runner methods will get later updated with the
  2538. // methods leading into the driver's end/cancel methods
  2539. // for now they just stop the animation from starting
  2540. var runner = new $$AnimateRunner({
  2541. end: function () {
  2542. close();
  2543. },
  2544. cancel: function () {
  2545. close(true);
  2546. }
  2547. });
  2548. if (!drivers.length) {
  2549. close();
  2550. return runner;
  2551. }
  2552. setRunner(element, runner);
  2553. var classes = mergeClasses(element.attr('class'), mergeClasses(options.addClass, options.removeClass));
  2554. var tempClasses = options.tempClasses;
  2555. if (tempClasses) {
  2556. classes += ' ' + tempClasses;
  2557. options.tempClasses = null;
  2558. }
  2559. var prepareClassName;
  2560. if (isStructural) {
  2561. prepareClassName = 'ng-' + event + PREPARE_CLASS_SUFFIX;
  2562. $$jqLite.addClass(element, prepareClassName);
  2563. }
  2564. animationQueue.push({
  2565. // this data is used by the postDigest code and passed into
  2566. // the driver step function
  2567. element: element,
  2568. classes: classes,
  2569. event: event,
  2570. structural: isStructural,
  2571. options: options,
  2572. beforeStart: beforeStart,
  2573. close: close
  2574. });
  2575. element.on('$destroy', handleDestroyedElement);
  2576. // we only want there to be one function called within the post digest
  2577. // block. This way we can group animations for all the animations that
  2578. // were apart of the same postDigest flush call.
  2579. if (animationQueue.length > 1) return runner;
  2580. $rootScope.$$postDigest(function () {
  2581. var animations = [];
  2582. forEach(animationQueue, function (entry) {
  2583. // the element was destroyed early on which removed the runner
  2584. // form its storage. This means we can't animate this element
  2585. // at all and it already has been closed due to destruction.
  2586. if (getRunner(entry.element)) {
  2587. animations.push(entry);
  2588. } else {
  2589. entry.close();
  2590. }
  2591. });
  2592. // now any future animations will be in another postDigest
  2593. animationQueue.length = 0;
  2594. var groupedAnimations = groupAnimations(animations);
  2595. var toBeSortedAnimations = [];
  2596. forEach(groupedAnimations, function (animationEntry) {
  2597. toBeSortedAnimations.push({
  2598. domNode: getDomNode(animationEntry.from ? animationEntry.from.element : animationEntry.element),
  2599. fn: function triggerAnimationStart() {
  2600. // it's important that we apply the `ng-animate` CSS class and the
  2601. // temporary classes before we do any driver invoking since these
  2602. // CSS classes may be required for proper CSS detection.
  2603. animationEntry.beforeStart();
  2604. var startAnimationFn, closeFn = animationEntry.close;
  2605. // in the event that the element was removed before the digest runs or
  2606. // during the RAF sequencing then we should not trigger the animation.
  2607. var targetElement = animationEntry.anchors
  2608. ? (animationEntry.from.element || animationEntry.to.element)
  2609. : animationEntry.element;
  2610. if (getRunner(targetElement)) {
  2611. var operation = invokeFirstDriver(animationEntry);
  2612. if (operation) {
  2613. startAnimationFn = operation.start;
  2614. }
  2615. }
  2616. if (!startAnimationFn) {
  2617. closeFn();
  2618. } else {
  2619. var animationRunner = startAnimationFn();
  2620. animationRunner.done(function (status) {
  2621. closeFn(!status);
  2622. });
  2623. updateAnimationRunners(animationEntry, animationRunner);
  2624. }
  2625. }
  2626. });
  2627. });
  2628. // we need to sort each of the animations in order of parent to child
  2629. // relationships. This ensures that the child classes are applied at the
  2630. // right time.
  2631. $$rAFScheduler(sortAnimations(toBeSortedAnimations));
  2632. });
  2633. return runner;
  2634. // TODO(matsko): change to reference nodes
  2635. function getAnchorNodes(node) {
  2636. var SELECTOR = '[' + NG_ANIMATE_REF_ATTR + ']';
  2637. var items = node.hasAttribute(NG_ANIMATE_REF_ATTR)
  2638. ? [node]
  2639. : node.querySelectorAll(SELECTOR);
  2640. var anchors = [];
  2641. forEach(items, function (node) {
  2642. var attr = node.getAttribute(NG_ANIMATE_REF_ATTR);
  2643. if (attr && attr.length) {
  2644. anchors.push(node);
  2645. }
  2646. });
  2647. return anchors;
  2648. }
  2649. function groupAnimations(animations) {
  2650. var preparedAnimations = [];
  2651. var refLookup = {};
  2652. forEach(animations, function (animation, index) {
  2653. var element = animation.element;
  2654. var node = getDomNode(element);
  2655. var event = animation.event;
  2656. var enterOrMove = ['enter', 'move'].indexOf(event) >= 0;
  2657. var anchorNodes = animation.structural ? getAnchorNodes(node) : [];
  2658. if (anchorNodes.length) {
  2659. var direction = enterOrMove ? 'to' : 'from';
  2660. forEach(anchorNodes, function (anchor) {
  2661. var key = anchor.getAttribute(NG_ANIMATE_REF_ATTR);
  2662. refLookup[key] = refLookup[key] || {};
  2663. refLookup[key][direction] = {
  2664. animationID: index,
  2665. element: jqLite(anchor)
  2666. };
  2667. });
  2668. } else {
  2669. preparedAnimations.push(animation);
  2670. }
  2671. });
  2672. var usedIndicesLookup = {};
  2673. var anchorGroups = {};
  2674. forEach(refLookup, function (operations, key) {
  2675. var from = operations.from;
  2676. var to = operations.to;
  2677. if (!from || !to) {
  2678. // only one of these is set therefore we can't have an
  2679. // anchor animation since all three pieces are required
  2680. var index = from ? from.animationID : to.animationID;
  2681. var indexKey = index.toString();
  2682. if (!usedIndicesLookup[indexKey]) {
  2683. usedIndicesLookup[indexKey] = true;
  2684. preparedAnimations.push(animations[index]);
  2685. }
  2686. return;
  2687. }
  2688. var fromAnimation = animations[from.animationID];
  2689. var toAnimation = animations[to.animationID];
  2690. var lookupKey = from.animationID.toString();
  2691. if (!anchorGroups[lookupKey]) {
  2692. var group = anchorGroups[lookupKey] = {
  2693. structural: true,
  2694. beforeStart: function () {
  2695. fromAnimation.beforeStart();
  2696. toAnimation.beforeStart();
  2697. },
  2698. close: function () {
  2699. fromAnimation.close();
  2700. toAnimation.close();
  2701. },
  2702. classes: cssClassesIntersection(fromAnimation.classes, toAnimation.classes),
  2703. from: fromAnimation,
  2704. to: toAnimation,
  2705. anchors: [] // TODO(matsko): change to reference nodes
  2706. };
  2707. // the anchor animations require that the from and to elements both have at least
  2708. // one shared CSS class which effectively marries the two elements together to use
  2709. // the same animation driver and to properly sequence the anchor animation.
  2710. if (group.classes.length) {
  2711. preparedAnimations.push(group);
  2712. } else {
  2713. preparedAnimations.push(fromAnimation);
  2714. preparedAnimations.push(toAnimation);
  2715. }
  2716. }
  2717. anchorGroups[lookupKey].anchors.push({
  2718. 'out': from.element, 'in': to.element
  2719. });
  2720. });
  2721. return preparedAnimations;
  2722. }
  2723. function cssClassesIntersection(a, b) {
  2724. a = a.split(' ');
  2725. b = b.split(' ');
  2726. var matches = [];
  2727. for (var i = 0; i < a.length; i++) {
  2728. var aa = a[i];
  2729. if (aa.substring(0, 3) === 'ng-') continue;
  2730. for (var j = 0; j < b.length; j++) {
  2731. if (aa === b[j]) {
  2732. matches.push(aa);
  2733. break;
  2734. }
  2735. }
  2736. }
  2737. return matches.join(' ');
  2738. }
  2739. function invokeFirstDriver(animationDetails) {
  2740. // we loop in reverse order since the more general drivers (like CSS and JS)
  2741. // may attempt more elements, but custom drivers are more particular
  2742. for (var i = drivers.length - 1; i >= 0; i--) {
  2743. var driverName = drivers[i];
  2744. var factory = $injector.get(driverName);
  2745. var driver = factory(animationDetails);
  2746. if (driver) {
  2747. return driver;
  2748. }
  2749. }
  2750. }
  2751. function beforeStart() {
  2752. element.addClass(NG_ANIMATE_CLASSNAME);
  2753. if (tempClasses) {
  2754. $$jqLite.addClass(element, tempClasses);
  2755. }
  2756. if (prepareClassName) {
  2757. $$jqLite.removeClass(element, prepareClassName);
  2758. prepareClassName = null;
  2759. }
  2760. }
  2761. function updateAnimationRunners(animation, newRunner) {
  2762. if (animation.from && animation.to) {
  2763. update(animation.from.element);
  2764. update(animation.to.element);
  2765. } else {
  2766. update(animation.element);
  2767. }
  2768. function update(element) {
  2769. var runner = getRunner(element);
  2770. if (runner) runner.setHost(newRunner);
  2771. }
  2772. }
  2773. function handleDestroyedElement() {
  2774. var runner = getRunner(element);
  2775. if (runner && (event !== 'leave' || !options.$$domOperationFired)) {
  2776. runner.end();
  2777. }
  2778. }
  2779. function close(rejected) {
  2780. element.off('$destroy', handleDestroyedElement);
  2781. removeRunner(element);
  2782. applyAnimationClasses(element, options);
  2783. applyAnimationStyles(element, options);
  2784. options.domOperation();
  2785. if (tempClasses) {
  2786. $$jqLite.removeClass(element, tempClasses);
  2787. }
  2788. element.removeClass(NG_ANIMATE_CLASSNAME);
  2789. runner.complete(!rejected);
  2790. }
  2791. };
  2792. }];
  2793. }];
  2794. /**
  2795. * @ngdoc directive
  2796. * @name ngAnimateSwap
  2797. * @restrict A
  2798. * @scope
  2799. *
  2800. * @description
  2801. *
  2802. * ngAnimateSwap is a animation-oriented directive that allows for the container to
  2803. * be removed and entered in whenever the associated expression changes. A
  2804. * common usecase for this directive is a rotating banner or slider component which
  2805. * contains one image being present at a time. When the active image changes
  2806. * then the old image will perform a `leave` animation and the new element
  2807. * will be inserted via an `enter` animation.
  2808. *
  2809. * @animations
  2810. * | Animation | Occurs |
  2811. * |----------------------------------|--------------------------------------|
  2812. * | {@link ng.$animate#enter enter} | when the new element is inserted to the DOM |
  2813. * | {@link ng.$animate#leave leave} | when the old element is removed from the DOM |
  2814. *
  2815. * @example
  2816. * <example name="ngAnimateSwap-directive" module="ngAnimateSwapExample"
  2817. * deps="angular-animate.js"
  2818. * animations="true" fixBase="true">
  2819. * <file name="index.html">
  2820. * <div class="container" ng-controller="AppCtrl">
  2821. * <div ng-animate-swap="number" class="cell swap-animation" ng-class="colorClass(number)">
  2822. * {{ number }}
  2823. * </div>
  2824. * </div>
  2825. * </file>
  2826. * <file name="script.js">
  2827. * angular.module('ngAnimateSwapExample', ['ngAnimate'])
  2828. * .controller('AppCtrl', ['$scope', '$interval', function($scope, $interval) {
  2829. * $scope.number = 0;
  2830. * $interval(function() {
  2831. * $scope.number++;
  2832. * }, 1000);
  2833. *
  2834. * var colors = ['red','blue','green','yellow','orange'];
  2835. * $scope.colorClass = function(number) {
  2836. * return colors[number % colors.length];
  2837. * };
  2838. * }]);
  2839. * </file>
  2840. * <file name="animations.css">
  2841. * .container {
  2842. * height:250px;
  2843. * width:250px;
  2844. * position:relative;
  2845. * overflow:hidden;
  2846. * border:2px solid black;
  2847. * }
  2848. * .container .cell {
  2849. * font-size:150px;
  2850. * text-align:center;
  2851. * line-height:250px;
  2852. * position:absolute;
  2853. * top:0;
  2854. * left:0;
  2855. * right:0;
  2856. * border-bottom:2px solid black;
  2857. * }
  2858. * .swap-animation.ng-enter, .swap-animation.ng-leave {
  2859. * transition:0.5s linear all;
  2860. * }
  2861. * .swap-animation.ng-enter {
  2862. * top:-250px;
  2863. * }
  2864. * .swap-animation.ng-enter-active {
  2865. * top:0px;
  2866. * }
  2867. * .swap-animation.ng-leave {
  2868. * top:0px;
  2869. * }
  2870. * .swap-animation.ng-leave-active {
  2871. * top:250px;
  2872. * }
  2873. * .red { background:red; }
  2874. * .green { background:green; }
  2875. * .blue { background:blue; }
  2876. * .yellow { background:yellow; }
  2877. * .orange { background:orange; }
  2878. * </file>
  2879. * </example>
  2880. */
  2881. var ngAnimateSwapDirective = ['$animate', '$rootScope', function ($animate, $rootScope) {
  2882. return {
  2883. restrict: 'A',
  2884. transclude: 'element',
  2885. terminal: true,
  2886. priority: 600, // we use 600 here to ensure that the directive is caught before others
  2887. link: function (scope, $element, attrs, ctrl, $transclude) {
  2888. var previousElement, previousScope;
  2889. scope.$watchCollection(attrs.ngAnimateSwap || attrs['for'], function (value) {
  2890. if (previousElement) {
  2891. $animate.leave(previousElement);
  2892. }
  2893. if (previousScope) {
  2894. previousScope.$destroy();
  2895. previousScope = null;
  2896. }
  2897. if (value || value === 0) {
  2898. previousScope = scope.$new();
  2899. $transclude(previousScope, function (element) {
  2900. previousElement = element;
  2901. $animate.enter(element, null, $element);
  2902. });
  2903. }
  2904. });
  2905. }
  2906. };
  2907. }];
  2908. /**
  2909. * @ngdoc module
  2910. * @name ngAnimate
  2911. * @description
  2912. *
  2913. * The `ngAnimate` module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via
  2914. * callback hooks. Animations are not enabled by default, however, by including `ngAnimate` the animation hooks are enabled for an Angular app.
  2915. *
  2916. * <div doc-module-components="ngAnimate"></div>
  2917. *
  2918. * # Usage
  2919. * Simply put, there are two ways to make use of animations when ngAnimate is used: by using **CSS** and **JavaScript**. The former works purely based
  2920. * using CSS (by using matching CSS selectors/styles) and the latter triggers animations that are registered via `module.animation()`. For
  2921. * both CSS and JS animations the sole requirement is to have a matching `CSS class` that exists both in the registered animation and within
  2922. * the HTML element that the animation will be triggered on.
  2923. *
  2924. * ## Directive Support
  2925. * The following directives are "animation aware":
  2926. *
  2927. * | Directive | Supported Animations |
  2928. * |----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
  2929. * | {@link ng.directive:ngRepeat#animations ngRepeat} | enter, leave and move |
  2930. * | {@link ngRoute.directive:ngView#animations ngView} | enter and leave |
  2931. * | {@link ng.directive:ngInclude#animations ngInclude} | enter and leave |
  2932. * | {@link ng.directive:ngSwitch#animations ngSwitch} | enter and leave |
  2933. * | {@link ng.directive:ngIf#animations ngIf} | enter and leave |
  2934. * | {@link ng.directive:ngClass#animations ngClass} | add and remove (the CSS class(es) present) |
  2935. * | {@link ng.directive:ngShow#animations ngShow} & {@link ng.directive:ngHide#animations ngHide} | add and remove (the ng-hide class value) |
  2936. * | {@link ng.directive:form#animation-hooks form} & {@link ng.directive:ngModel#animation-hooks ngModel} | add and remove (dirty, pristine, valid, invalid & all other validations) |
  2937. * | {@link module:ngMessages#animations ngMessages} | add and remove (ng-active & ng-inactive) |
  2938. * | {@link module:ngMessages#animations ngMessage} | enter and leave |
  2939. *
  2940. * (More information can be found by visiting each the documentation associated with each directive.)
  2941. *
  2942. * ## CSS-based Animations
  2943. *
  2944. * CSS-based animations with ngAnimate are unique since they require no JavaScript code at all. By using a CSS class that we reference between our HTML
  2945. * and CSS code we can create an animation that will be picked up by Angular when an underlying directive performs an operation.
  2946. *
  2947. * The example below shows how an `enter` animation can be made possible on an element using `ng-if`:
  2948. *
  2949. * ```html
  2950. * <div ng-if="bool" class="fade">
  2951. * Fade me in out
  2952. * </div>
  2953. * <button ng-click="bool=true">Fade In!</button>
  2954. * <button ng-click="bool=false">Fade Out!</button>
  2955. * ```
  2956. *
  2957. * Notice the CSS class **fade**? We can now create the CSS transition code that references this class:
  2958. *
  2959. * ```css
  2960. * /&#42; The starting CSS styles for the enter animation &#42;/
  2961. * .fade.ng-enter {
  2962. * transition:0.5s linear all;
  2963. * opacity:0;
  2964. * }
  2965. *
  2966. * /&#42; The finishing CSS styles for the enter animation &#42;/
  2967. * .fade.ng-enter.ng-enter-active {
  2968. * opacity:1;
  2969. * }
  2970. * ```
  2971. *
  2972. * The key thing to remember here is that, depending on the animation event (which each of the directives above trigger depending on what's going on) two
  2973. * generated CSS classes will be applied to the element; in the example above we have `.ng-enter` and `.ng-enter-active`. For CSS transitions, the transition
  2974. * code **must** be defined within the starting CSS class (in this case `.ng-enter`). The destination class is what the transition will animate towards.
  2975. *
  2976. * If for example we wanted to create animations for `leave` and `move` (ngRepeat triggers move) then we can do so using the same CSS naming conventions:
  2977. *
  2978. * ```css
  2979. * /&#42; now the element will fade out before it is removed from the DOM &#42;/
  2980. * .fade.ng-leave {
  2981. * transition:0.5s linear all;
  2982. * opacity:1;
  2983. * }
  2984. * .fade.ng-leave.ng-leave-active {
  2985. * opacity:0;
  2986. * }
  2987. * ```
  2988. *
  2989. * We can also make use of **CSS Keyframes** by referencing the keyframe animation within the starting CSS class:
  2990. *
  2991. * ```css
  2992. * /&#42; there is no need to define anything inside of the destination
  2993. * CSS class since the keyframe will take charge of the animation &#42;/
  2994. * .fade.ng-leave {
  2995. * animation: my_fade_animation 0.5s linear;
  2996. * -webkit-animation: my_fade_animation 0.5s linear;
  2997. * }
  2998. *
  2999. * @keyframes my_fade_animation {
  3000. * from { opacity:1; }
  3001. * to { opacity:0; }
  3002. * }
  3003. *
  3004. * @-webkit-keyframes my_fade_animation {
  3005. * from { opacity:1; }
  3006. * to { opacity:0; }
  3007. * }
  3008. * ```
  3009. *
  3010. * Feel free also mix transitions and keyframes together as well as any other CSS classes on the same element.
  3011. *
  3012. * ### CSS Class-based Animations
  3013. *
  3014. * Class-based animations (animations that are triggered via `ngClass`, `ngShow`, `ngHide` and some other directives) have a slightly different
  3015. * naming convention. Class-based animations are basic enough that a standard transition or keyframe can be referenced on the class being added
  3016. * and removed.
  3017. *
  3018. * For example if we wanted to do a CSS animation for `ngHide` then we place an animation on the `.ng-hide` CSS class:
  3019. *
  3020. * ```html
  3021. * <div ng-show="bool" class="fade">
  3022. * Show and hide me
  3023. * </div>
  3024. * <button ng-click="bool=!bool">Toggle</button>
  3025. *
  3026. * <style>
  3027. * .fade.ng-hide {
  3028. * transition:0.5s linear all;
  3029. * opacity:0;
  3030. * }
  3031. * </style>
  3032. * ```
  3033. *
  3034. * All that is going on here with ngShow/ngHide behind the scenes is the `.ng-hide` class is added/removed (when the hidden state is valid). Since
  3035. * ngShow and ngHide are animation aware then we can match up a transition and ngAnimate handles the rest.
  3036. *
  3037. * In addition the addition and removal of the CSS class, ngAnimate also provides two helper methods that we can use to further decorate the animation
  3038. * with CSS styles.
  3039. *
  3040. * ```html
  3041. * <div ng-class="{on:onOff}" class="highlight">
  3042. * Highlight this box
  3043. * </div>
  3044. * <button ng-click="onOff=!onOff">Toggle</button>
  3045. *
  3046. * <style>
  3047. * .highlight {
  3048. * transition:0.5s linear all;
  3049. * }
  3050. * .highlight.on-add {
  3051. * background:white;
  3052. * }
  3053. * .highlight.on {
  3054. * background:yellow;
  3055. * }
  3056. * .highlight.on-remove {
  3057. * background:black;
  3058. * }
  3059. * </style>
  3060. * ```
  3061. *
  3062. * We can also make use of CSS keyframes by placing them within the CSS classes.
  3063. *
  3064. *
  3065. * ### CSS Staggering Animations
  3066. * A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a
  3067. * curtain-like effect. The ngAnimate module (versions >=1.2) supports staggering animations and the stagger effect can be
  3068. * performed by creating a **ng-EVENT-stagger** CSS class and attaching that class to the base CSS class used for
  3069. * the animation. The style property expected within the stagger class can either be a **transition-delay** or an
  3070. * **animation-delay** property (or both if your animation contains both transitions and keyframe animations).
  3071. *
  3072. * ```css
  3073. * .my-animation.ng-enter {
  3074. * /&#42; standard transition code &#42;/
  3075. * transition: 1s linear all;
  3076. * opacity:0;
  3077. * }
  3078. * .my-animation.ng-enter-stagger {
  3079. * /&#42; this will have a 100ms delay between each successive leave animation &#42;/
  3080. * transition-delay: 0.1s;
  3081. *
  3082. * /&#42; As of 1.4.4, this must always be set: it signals ngAnimate
  3083. * to not accidentally inherit a delay property from another CSS class &#42;/
  3084. * transition-duration: 0s;
  3085. *
  3086. * /&#42; if you are using animations instead of transitions you should configure as follows:
  3087. * animation-delay: 0.1s;
  3088. * animation-duration: 0s; &#42;/
  3089. * }
  3090. * .my-animation.ng-enter.ng-enter-active {
  3091. * /&#42; standard transition styles &#42;/
  3092. * opacity:1;
  3093. * }
  3094. * ```
  3095. *
  3096. * Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations
  3097. * on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this
  3098. * are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation
  3099. * will also be reset if one or more animation frames have passed since the multiple calls to `$animate` were fired.
  3100. *
  3101. * The following code will issue the **ng-leave-stagger** event on the element provided:
  3102. *
  3103. * ```js
  3104. * var kids = parent.children();
  3105. *
  3106. * $animate.leave(kids[0]); //stagger index=0
  3107. * $animate.leave(kids[1]); //stagger index=1
  3108. * $animate.leave(kids[2]); //stagger index=2
  3109. * $animate.leave(kids[3]); //stagger index=3
  3110. * $animate.leave(kids[4]); //stagger index=4
  3111. *
  3112. * window.requestAnimationFrame(function() {
  3113. * //stagger has reset itself
  3114. * $animate.leave(kids[5]); //stagger index=0
  3115. * $animate.leave(kids[6]); //stagger index=1
  3116. *
  3117. * $scope.$digest();
  3118. * });
  3119. * ```
  3120. *
  3121. * Stagger animations are currently only supported within CSS-defined animations.
  3122. *
  3123. * ### The `ng-animate` CSS class
  3124. *
  3125. * When ngAnimate is animating an element it will apply the `ng-animate` CSS class to the element for the duration of the animation.
  3126. * This is a temporary CSS class and it will be removed once the animation is over (for both JavaScript and CSS-based animations).
  3127. *
  3128. * Therefore, animations can be applied to an element using this temporary class directly via CSS.
  3129. *
  3130. * ```css
  3131. * .zipper.ng-animate {
  3132. * transition:0.5s linear all;
  3133. * }
  3134. * .zipper.ng-enter {
  3135. * opacity:0;
  3136. * }
  3137. * .zipper.ng-enter.ng-enter-active {
  3138. * opacity:1;
  3139. * }
  3140. * .zipper.ng-leave {
  3141. * opacity:1;
  3142. * }
  3143. * .zipper.ng-leave.ng-leave-active {
  3144. * opacity:0;
  3145. * }
  3146. * ```
  3147. *
  3148. * (Note that the `ng-animate` CSS class is reserved and it cannot be applied on an element directly since ngAnimate will always remove
  3149. * the CSS class once an animation has completed.)
  3150. *
  3151. *
  3152. * ### The `ng-[event]-prepare` class
  3153. *
  3154. * This is a special class that can be used to prevent unwanted flickering / flash of content before
  3155. * the actual animation starts. The class is added as soon as an animation is initialized, but removed
  3156. * before the actual animation starts (after waiting for a $digest).
  3157. * It is also only added for *structural* animations (`enter`, `move`, and `leave`).
  3158. *
  3159. * In practice, flickering can appear when nesting elements with structural animations such as `ngIf`
  3160. * into elements that have class-based animations such as `ngClass`.
  3161. *
  3162. * ```html
  3163. * <div ng-class="{red: myProp}">
  3164. * <div ng-class="{blue: myProp}">
  3165. * <div class="message" ng-if="myProp"></div>
  3166. * </div>
  3167. * </div>
  3168. * ```
  3169. *
  3170. * It is possible that during the `enter` animation, the `.message` div will be briefly visible before it starts animating.
  3171. * In that case, you can add styles to the CSS that make sure the element stays hidden before the animation starts:
  3172. *
  3173. * ```css
  3174. * .message.ng-enter-prepare {
  3175. * opacity: 0;
  3176. * }
  3177. *
  3178. * ```
  3179. *
  3180. * ## JavaScript-based Animations
  3181. *
  3182. * ngAnimate also allows for animations to be consumed by JavaScript code. The approach is similar to CSS-based animations (where there is a shared
  3183. * CSS class that is referenced in our HTML code) but in addition we need to register the JavaScript animation on the module. By making use of the
  3184. * `module.animation()` module function we can register the animation.
  3185. *
  3186. * Let's see an example of a enter/leave animation using `ngRepeat`:
  3187. *
  3188. * ```html
  3189. * <div ng-repeat="item in items" class="slide">
  3190. * {{ item }}
  3191. * </div>
  3192. * ```
  3193. *
  3194. * See the **slide** CSS class? Let's use that class to define an animation that we'll structure in our module code by using `module.animation`:
  3195. *
  3196. * ```js
  3197. * myModule.animation('.slide', [function() {
  3198. * return {
  3199. * // make note that other events (like addClass/removeClass)
  3200. * // have different function input parameters
  3201. * enter: function(element, doneFn) {
  3202. * jQuery(element).fadeIn(1000, doneFn);
  3203. *
  3204. * // remember to call doneFn so that angular
  3205. * // knows that the animation has concluded
  3206. * },
  3207. *
  3208. * move: function(element, doneFn) {
  3209. * jQuery(element).fadeIn(1000, doneFn);
  3210. * },
  3211. *
  3212. * leave: function(element, doneFn) {
  3213. * jQuery(element).fadeOut(1000, doneFn);
  3214. * }
  3215. * }
  3216. * }]);
  3217. * ```
  3218. *
  3219. * The nice thing about JS-based animations is that we can inject other services and make use of advanced animation libraries such as
  3220. * greensock.js and velocity.js.
  3221. *
  3222. * If our animation code class-based (meaning that something like `ngClass`, `ngHide` and `ngShow` triggers it) then we can still define
  3223. * our animations inside of the same registered animation, however, the function input arguments are a bit different:
  3224. *
  3225. * ```html
  3226. * <div ng-class="color" class="colorful">
  3227. * this box is moody
  3228. * </div>
  3229. * <button ng-click="color='red'">Change to red</button>
  3230. * <button ng-click="color='blue'">Change to blue</button>
  3231. * <button ng-click="color='green'">Change to green</button>
  3232. * ```
  3233. *
  3234. * ```js
  3235. * myModule.animation('.colorful', [function() {
  3236. * return {
  3237. * addClass: function(element, className, doneFn) {
  3238. * // do some cool animation and call the doneFn
  3239. * },
  3240. * removeClass: function(element, className, doneFn) {
  3241. * // do some cool animation and call the doneFn
  3242. * },
  3243. * setClass: function(element, addedClass, removedClass, doneFn) {
  3244. * // do some cool animation and call the doneFn
  3245. * }
  3246. * }
  3247. * }]);
  3248. * ```
  3249. *
  3250. * ## CSS + JS Animations Together
  3251. *
  3252. * AngularJS 1.4 and higher has taken steps to make the amalgamation of CSS and JS animations more flexible. However, unlike earlier versions of Angular,
  3253. * defining CSS and JS animations to work off of the same CSS class will not work anymore. Therefore the example below will only result in **JS animations taking
  3254. * charge of the animation**:
  3255. *
  3256. * ```html
  3257. * <div ng-if="bool" class="slide">
  3258. * Slide in and out
  3259. * </div>
  3260. * ```
  3261. *
  3262. * ```js
  3263. * myModule.animation('.slide', [function() {
  3264. * return {
  3265. * enter: function(element, doneFn) {
  3266. * jQuery(element).slideIn(1000, doneFn);
  3267. * }
  3268. * }
  3269. * }]);
  3270. * ```
  3271. *
  3272. * ```css
  3273. * .slide.ng-enter {
  3274. * transition:0.5s linear all;
  3275. * transform:translateY(-100px);
  3276. * }
  3277. * .slide.ng-enter.ng-enter-active {
  3278. * transform:translateY(0);
  3279. * }
  3280. * ```
  3281. *
  3282. * Does this mean that CSS and JS animations cannot be used together? Do JS-based animations always have higher priority? We can make up for the
  3283. * lack of CSS animations by using the `$animateCss` service to trigger our own tweaked-out, CSS-based animations directly from
  3284. * our own JS-based animation code:
  3285. *
  3286. * ```js
  3287. * myModule.animation('.slide', ['$animateCss', function($animateCss) {
  3288. * return {
  3289. * enter: function(element) {
  3290. * // this will trigger `.slide.ng-enter` and `.slide.ng-enter-active`.
  3291. * return $animateCss(element, {
  3292. * event: 'enter',
  3293. * structural: true
  3294. * });
  3295. * }
  3296. * }
  3297. * }]);
  3298. * ```
  3299. *
  3300. * The nice thing here is that we can save bandwidth by sticking to our CSS-based animation code and we don't need to rely on a 3rd-party animation framework.
  3301. *
  3302. * The `$animateCss` service is very powerful since we can feed in all kinds of extra properties that will be evaluated and fed into a CSS transition or
  3303. * keyframe animation. For example if we wanted to animate the height of an element while adding and removing classes then we can do so by providing that
  3304. * data into `$animateCss` directly:
  3305. *
  3306. * ```js
  3307. * myModule.animation('.slide', ['$animateCss', function($animateCss) {
  3308. * return {
  3309. * enter: function(element) {
  3310. * return $animateCss(element, {
  3311. * event: 'enter',
  3312. * structural: true,
  3313. * addClass: 'maroon-setting',
  3314. * from: { height:0 },
  3315. * to: { height: 200 }
  3316. * });
  3317. * }
  3318. * }
  3319. * }]);
  3320. * ```
  3321. *
  3322. * Now we can fill in the rest via our transition CSS code:
  3323. *
  3324. * ```css
  3325. * /&#42; the transition tells ngAnimate to make the animation happen &#42;/
  3326. * .slide.ng-enter { transition:0.5s linear all; }
  3327. *
  3328. * /&#42; this extra CSS class will be absorbed into the transition
  3329. * since the $animateCss code is adding the class &#42;/
  3330. * .maroon-setting { background:red; }
  3331. * ```
  3332. *
  3333. * And `$animateCss` will figure out the rest. Just make sure to have the `done()` callback fire the `doneFn` function to signal when the animation is over.
  3334. *
  3335. * To learn more about what's possible be sure to visit the {@link ngAnimate.$animateCss $animateCss service}.
  3336. *
  3337. * ## Animation Anchoring (via `ng-animate-ref`)
  3338. *
  3339. * ngAnimate in AngularJS 1.4 comes packed with the ability to cross-animate elements between
  3340. * structural areas of an application (like views) by pairing up elements using an attribute
  3341. * called `ng-animate-ref`.
  3342. *
  3343. * Let's say for example we have two views that are managed by `ng-view` and we want to show
  3344. * that there is a relationship between two components situated in within these views. By using the
  3345. * `ng-animate-ref` attribute we can identify that the two components are paired together and we
  3346. * can then attach an animation, which is triggered when the view changes.
  3347. *
  3348. * Say for example we have the following template code:
  3349. *
  3350. * ```html
  3351. * <!-- index.html -->
  3352. * <div ng-view class="view-animation">
  3353. * </div>
  3354. *
  3355. * <!-- home.html -->
  3356. * <a href="#/banner-page">
  3357. * <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
  3358. * </a>
  3359. *
  3360. * <!-- banner-page.html -->
  3361. * <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
  3362. * ```
  3363. *
  3364. * Now, when the view changes (once the link is clicked), ngAnimate will examine the
  3365. * HTML contents to see if there is a match reference between any components in the view
  3366. * that is leaving and the view that is entering. It will scan both the view which is being
  3367. * removed (leave) and inserted (enter) to see if there are any paired DOM elements that
  3368. * contain a matching ref value.
  3369. *
  3370. * The two images match since they share the same ref value. ngAnimate will now create a
  3371. * transport element (which is a clone of the first image element) and it will then attempt
  3372. * to animate to the position of the second image element in the next view. For the animation to
  3373. * work a special CSS class called `ng-anchor` will be added to the transported element.
  3374. *
  3375. * We can now attach a transition onto the `.banner.ng-anchor` CSS class and then
  3376. * ngAnimate will handle the entire transition for us as well as the addition and removal of
  3377. * any changes of CSS classes between the elements:
  3378. *
  3379. * ```css
  3380. * .banner.ng-anchor {
  3381. * /&#42; this animation will last for 1 second since there are
  3382. * two phases to the animation (an `in` and an `out` phase) &#42;/
  3383. * transition:0.5s linear all;
  3384. * }
  3385. * ```
  3386. *
  3387. * We also **must** include animations for the views that are being entered and removed
  3388. * (otherwise anchoring wouldn't be possible since the new view would be inserted right away).
  3389. *
  3390. * ```css
  3391. * .view-animation.ng-enter, .view-animation.ng-leave {
  3392. * transition:0.5s linear all;
  3393. * position:fixed;
  3394. * left:0;
  3395. * top:0;
  3396. * width:100%;
  3397. * }
  3398. * .view-animation.ng-enter {
  3399. * transform:translateX(100%);
  3400. * }
  3401. * .view-animation.ng-leave,
  3402. * .view-animation.ng-enter.ng-enter-active {
  3403. * transform:translateX(0%);
  3404. * }
  3405. * .view-animation.ng-leave.ng-leave-active {
  3406. * transform:translateX(-100%);
  3407. * }
  3408. * ```
  3409. *
  3410. * Now we can jump back to the anchor animation. When the animation happens, there are two stages that occur:
  3411. * an `out` and an `in` stage. The `out` stage happens first and that is when the element is animated away
  3412. * from its origin. Once that animation is over then the `in` stage occurs which animates the
  3413. * element to its destination. The reason why there are two animations is to give enough time
  3414. * for the enter animation on the new element to be ready.
  3415. *
  3416. * The example above sets up a transition for both the in and out phases, but we can also target the out or
  3417. * in phases directly via `ng-anchor-out` and `ng-anchor-in`.
  3418. *
  3419. * ```css
  3420. * .banner.ng-anchor-out {
  3421. * transition: 0.5s linear all;
  3422. *
  3423. * /&#42; the scale will be applied during the out animation,
  3424. * but will be animated away when the in animation runs &#42;/
  3425. * transform: scale(1.2);
  3426. * }
  3427. *
  3428. * .banner.ng-anchor-in {
  3429. * transition: 1s linear all;
  3430. * }
  3431. * ```
  3432. *
  3433. *
  3434. *
  3435. *
  3436. * ### Anchoring Demo
  3437. *
  3438. <example module="anchoringExample"
  3439. name="anchoringExample"
  3440. id="anchoringExample"
  3441. deps="angular-animate.js;angular-route.js"
  3442. animations="true">
  3443. <file name="index.html">
  3444. <a href="#!/">Home</a>
  3445. <hr />
  3446. <div class="view-container">
  3447. <div ng-view class="view"></div>
  3448. </div>
  3449. </file>
  3450. <file name="script.js">
  3451. angular.module('anchoringExample', ['ngAnimate', 'ngRoute'])
  3452. .config(['$routeProvider', function($routeProvider) {
  3453. $routeProvider.when('/', {
  3454. templateUrl: 'home.html',
  3455. controller: 'HomeController as home'
  3456. });
  3457. $routeProvider.when('/profile/:id', {
  3458. templateUrl: 'profile.html',
  3459. controller: 'ProfileController as profile'
  3460. });
  3461. }])
  3462. .run(['$rootScope', function($rootScope) {
  3463. $rootScope.records = [
  3464. { id: 1, title: 'Miss Beulah Roob' },
  3465. { id: 2, title: 'Trent Morissette' },
  3466. { id: 3, title: 'Miss Ava Pouros' },
  3467. { id: 4, title: 'Rod Pouros' },
  3468. { id: 5, title: 'Abdul Rice' },
  3469. { id: 6, title: 'Laurie Rutherford Sr.' },
  3470. { id: 7, title: 'Nakia McLaughlin' },
  3471. { id: 8, title: 'Jordon Blanda DVM' },
  3472. { id: 9, title: 'Rhoda Hand' },
  3473. { id: 10, title: 'Alexandrea Sauer' }
  3474. ];
  3475. }])
  3476. .controller('HomeController', [function() {
  3477. //empty
  3478. }])
  3479. .controller('ProfileController', ['$rootScope', '$routeParams',
  3480. function ProfileController($rootScope, $routeParams) {
  3481. var index = parseInt($routeParams.id, 10);
  3482. var record = $rootScope.records[index - 1];
  3483. this.title = record.title;
  3484. this.id = record.id;
  3485. }]);
  3486. </file>
  3487. <file name="home.html">
  3488. <h2>Welcome to the home page</h1>
  3489. <p>Please click on an element</p>
  3490. <a class="record"
  3491. ng-href="#!/profile/{{ record.id }}"
  3492. ng-animate-ref="{{ record.id }}"
  3493. ng-repeat="record in records">
  3494. {{ record.title }}
  3495. </a>
  3496. </file>
  3497. <file name="profile.html">
  3498. <div class="profile record" ng-animate-ref="{{ profile.id }}">
  3499. {{ profile.title }}
  3500. </div>
  3501. </file>
  3502. <file name="animations.css">
  3503. .record {
  3504. display:block;
  3505. font-size:20px;
  3506. }
  3507. .profile {
  3508. background:black;
  3509. color:white;
  3510. font-size:100px;
  3511. }
  3512. .view-container {
  3513. position:relative;
  3514. }
  3515. .view-container > .view.ng-animate {
  3516. position:absolute;
  3517. top:0;
  3518. left:0;
  3519. width:100%;
  3520. min-height:500px;
  3521. }
  3522. .view.ng-enter, .view.ng-leave,
  3523. .record.ng-anchor {
  3524. transition:0.5s linear all;
  3525. }
  3526. .view.ng-enter {
  3527. transform:translateX(100%);
  3528. }
  3529. .view.ng-enter.ng-enter-active, .view.ng-leave {
  3530. transform:translateX(0%);
  3531. }
  3532. .view.ng-leave.ng-leave-active {
  3533. transform:translateX(-100%);
  3534. }
  3535. .record.ng-anchor-out {
  3536. background:red;
  3537. }
  3538. </file>
  3539. </example>
  3540. *
  3541. * ### How is the element transported?
  3542. *
  3543. * When an anchor animation occurs, ngAnimate will clone the starting element and position it exactly where the starting
  3544. * element is located on screen via absolute positioning. The cloned element will be placed inside of the root element
  3545. * of the application (where ng-app was defined) and all of the CSS classes of the starting element will be applied. The
  3546. * element will then animate into the `out` and `in` animations and will eventually reach the coordinates and match
  3547. * the dimensions of the destination element. During the entire animation a CSS class of `.ng-animate-shim` will be applied
  3548. * to both the starting and destination elements in order to hide them from being visible (the CSS styling for the class
  3549. * is: `visibility:hidden`). Once the anchor reaches its destination then it will be removed and the destination element
  3550. * will become visible since the shim class will be removed.
  3551. *
  3552. * ### How is the morphing handled?
  3553. *
  3554. * CSS Anchoring relies on transitions and keyframes and the internal code is intelligent enough to figure out
  3555. * what CSS classes differ between the starting element and the destination element. These different CSS classes
  3556. * will be added/removed on the anchor element and a transition will be applied (the transition that is provided
  3557. * in the anchor class). Long story short, ngAnimate will figure out what classes to add and remove which will
  3558. * make the transition of the element as smooth and automatic as possible. Be sure to use simple CSS classes that
  3559. * do not rely on DOM nesting structure so that the anchor element appears the same as the starting element (since
  3560. * the cloned element is placed inside of root element which is likely close to the body element).
  3561. *
  3562. * Note that if the root element is on the `<html>` element then the cloned node will be placed inside of body.
  3563. *
  3564. *
  3565. * ## Using $animate in your directive code
  3566. *
  3567. * So far we've explored how to feed in animations into an Angular application, but how do we trigger animations within our own directives in our application?
  3568. * By injecting the `$animate` service into our directive code, we can trigger structural and class-based hooks which can then be consumed by animations. Let's
  3569. * imagine we have a greeting box that shows and hides itself when the data changes
  3570. *
  3571. * ```html
  3572. * <greeting-box active="onOrOff">Hi there</greeting-box>
  3573. * ```
  3574. *
  3575. * ```js
  3576. * ngModule.directive('greetingBox', ['$animate', function($animate) {
  3577. * return function(scope, element, attrs) {
  3578. * attrs.$observe('active', function(value) {
  3579. * value ? $animate.addClass(element, 'on') : $animate.removeClass(element, 'on');
  3580. * });
  3581. * });
  3582. * }]);
  3583. * ```
  3584. *
  3585. * Now the `on` CSS class is added and removed on the greeting box component. Now if we add a CSS class on top of the greeting box element
  3586. * in our HTML code then we can trigger a CSS or JS animation to happen.
  3587. *
  3588. * ```css
  3589. * /&#42; normally we would create a CSS class to reference on the element &#42;/
  3590. * greeting-box.on { transition:0.5s linear all; background:green; color:white; }
  3591. * ```
  3592. *
  3593. * The `$animate` service contains a variety of other methods like `enter`, `leave`, `animate` and `setClass`. To learn more about what's
  3594. * possible be sure to visit the {@link ng.$animate $animate service API page}.
  3595. *
  3596. *
  3597. * ## Callbacks and Promises
  3598. *
  3599. * When `$animate` is called it returns a promise that can be used to capture when the animation has ended. Therefore if we were to trigger
  3600. * an animation (within our directive code) then we can continue performing directive and scope related activities after the animation has
  3601. * ended by chaining onto the returned promise that animation method returns.
  3602. *
  3603. * ```js
  3604. * // somewhere within the depths of the directive
  3605. * $animate.enter(element, parent).then(function() {
  3606. * //the animation has completed
  3607. * });
  3608. * ```
  3609. *
  3610. * (Note that earlier versions of Angular prior to v1.4 required the promise code to be wrapped using `$scope.$apply(...)`. This is not the case
  3611. * anymore.)
  3612. *
  3613. * In addition to the animation promise, we can also make use of animation-related callbacks within our directives and controller code by registering
  3614. * an event listener using the `$animate` service. Let's say for example that an animation was triggered on our view
  3615. * routing controller to hook into that:
  3616. *
  3617. * ```js
  3618. * ngModule.controller('HomePageController', ['$animate', function($animate) {
  3619. * $animate.on('enter', ngViewElement, function(element) {
  3620. * // the animation for this route has completed
  3621. * }]);
  3622. * }])
  3623. * ```
  3624. *
  3625. * (Note that you will need to trigger a digest within the callback to get angular to notice any scope-related changes.)
  3626. */
  3627. var copy;
  3628. var extend;
  3629. var forEach;
  3630. var isArray;
  3631. var isDefined;
  3632. var isElement;
  3633. var isFunction;
  3634. var isObject;
  3635. var isString;
  3636. var isUndefined;
  3637. var jqLite;
  3638. var noop;
  3639. /**
  3640. * @ngdoc service
  3641. * @name $animate
  3642. * @kind object
  3643. *
  3644. * @description
  3645. * The ngAnimate `$animate` service documentation is the same for the core `$animate` service.
  3646. *
  3647. * Click here {@link ng.$animate to learn more about animations with `$animate`}.
  3648. */
  3649. angular.module('ngAnimate', [], function initAngularHelpers() {
  3650. // Access helpers from angular core.
  3651. // Do it inside a `config` block to ensure `window.angular` is available.
  3652. noop = angular.noop;
  3653. copy = angular.copy;
  3654. extend = angular.extend;
  3655. jqLite = angular.element;
  3656. forEach = angular.forEach;
  3657. isArray = angular.isArray;
  3658. isString = angular.isString;
  3659. isObject = angular.isObject;
  3660. isUndefined = angular.isUndefined;
  3661. isDefined = angular.isDefined;
  3662. isFunction = angular.isFunction;
  3663. isElement = angular.isElement;
  3664. })
  3665. .info({angularVersion: '1.6.5'})
  3666. .directive('ngAnimateSwap', ngAnimateSwapDirective)
  3667. .directive('ngAnimateChildren', $$AnimateChildrenDirective)
  3668. .factory('$$rAFScheduler', $$rAFSchedulerFactory)
  3669. .provider('$$animateQueue', $$AnimateQueueProvider)
  3670. .provider('$$animation', $$AnimationProvider)
  3671. .provider('$animateCss', $AnimateCssProvider)
  3672. .provider('$$animateCssDriver', $$AnimateCssDriverProvider)
  3673. .provider('$$animateJs', $$AnimateJsProvider)
  3674. .provider('$$animateJsDriver', $$AnimateJsDriverProvider);
  3675. })(window, window.angular);