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