This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
/
animate.js
2128 lines (1949 loc) · 102 KB
/
animate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/* jshint maxlen: false */
/**
* @ngdoc module
* @name ngAnimate
* @description
*
* The `ngAnimate` module provides support for JavaScript, CSS3 transition and CSS3 keyframe animation hooks within existing core and custom directives.
*
* <div doc-module-components="ngAnimate"></div>
*
* # Usage
*
* To see animations in action, all that is required is to define the appropriate CSS classes
* or to register a JavaScript animation via the `myModule.animation()` function. The directives that support animation automatically are:
* `ngRepeat`, `ngInclude`, `ngIf`, `ngSwitch`, `ngShow`, `ngHide`, `ngView` and `ngClass`. Custom directives can take advantage of animation
* by using the `$animate` service.
*
* Below is a more detailed breakdown of the supported animation events provided by pre-existing ng directives:
*
* | Directive | Supported Animations |
* |----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
* | {@link ng.directive:ngRepeat#animations ngRepeat} | enter, leave and move |
* | {@link ngRoute.directive:ngView#animations ngView} | enter and leave |
* | {@link ng.directive:ngInclude#animations ngInclude} | enter and leave |
* | {@link ng.directive:ngSwitch#animations ngSwitch} | enter and leave |
* | {@link ng.directive:ngIf#animations ngIf} | enter and leave |
* | {@link ng.directive:ngClass#animations ngClass} | add and remove (the CSS class(es) present) |
* | {@link ng.directive:ngShow#animations ngShow} & {@link ng.directive:ngHide#animations ngHide} | add and remove (the ng-hide class value) |
* | {@link ng.directive:form#animation-hooks form} & {@link ng.directive:ngModel#animation-hooks ngModel} | add and remove (dirty, pristine, valid, invalid & all other validations) |
* | {@link module:ngMessages#animations ngMessages} | add and remove (ng-active & ng-inactive) |
* | {@link module:ngMessages#animations ngMessage} | enter and leave |
*
* You can find out more information about animations upon visiting each directive page.
*
* Below is an example of how to apply animations to a directive that supports animation hooks:
*
* ```html
* <style type="text/css">
* .slide.ng-enter, .slide.ng-leave {
* -webkit-transition:0.5s linear all;
* transition:0.5s linear all;
* }
*
* .slide.ng-enter { } /* starting animations for enter */
* .slide.ng-enter.ng-enter-active { } /* terminal animations for enter */
* .slide.ng-leave { } /* starting animations for leave */
* .slide.ng-leave.ng-leave-active { } /* terminal animations for leave */
* </style>
*
* <!--
* the animate service will automatically add .ng-enter and .ng-leave to the element
* to trigger the CSS transition/animations
* -->
* <ANY class="slide" ng-include="..."></ANY>
* ```
*
* Keep in mind that, by default, if an animation is running, any child elements cannot be animated
* until the parent element's animation has completed. This blocking feature can be overridden by
* placing the `ng-animate-children` attribute on a parent container tag.
*
* ```html
* <div class="slide-animation" ng-if="on" ng-animate-children>
* <div class="fade-animation" ng-if="on">
* <div class="explode-animation" ng-if="on">
* ...
* </div>
* </div>
* </div>
* ```
*
* When the `on` expression value changes and an animation is triggered then each of the elements within
* will all animate without the block being applied to child elements.
*
* ## Are animations run when the application starts?
* No they are not. When an application is bootstrapped Angular will disable animations from running to avoid
* a frenzy of animations from being triggered as soon as the browser has rendered the screen. For this to work,
* Angular will wait for two digest cycles until enabling animations. From there on, any animation-triggering
* layout changes in the application will trigger animations as normal.
*
* In addition, upon bootstrap, if the routing system or any directives or load remote data (via $http) then Angular
* will automatically extend the wait time to enable animations once **all** of the outbound HTTP requests
* are complete.
*
* ## CSS-defined Animations
* The animate service will automatically apply two CSS classes to the animated element and these two CSS classes
* are designed to contain the start and end CSS styling. Both CSS transitions and keyframe animations are supported
* and can be used to play along with this naming structure.
*
* The following code below demonstrates how to perform animations using **CSS transitions** with Angular:
*
* ```html
* <style type="text/css">
* /*
* The animate class is apart of the element and the ng-enter class
* is attached to the element once the enter animation event is triggered
* */
* .reveal-animation.ng-enter {
* -webkit-transition: 1s linear all; /* Safari/Chrome */
* transition: 1s linear all; /* All other modern browsers and IE10+ */
*
* /* The animation preparation code */
* opacity: 0;
* }
*
* /*
* Keep in mind that you want to combine both CSS
* classes together to avoid any CSS-specificity
* conflicts
* */
* .reveal-animation.ng-enter.ng-enter-active {
* /* The animation code itself */
* opacity: 1;
* }
* </style>
*
* <div class="view-container">
* <div ng-view class="reveal-animation"></div>
* </div>
* ```
*
* The following code below demonstrates how to perform animations using **CSS animations** with Angular:
*
* ```html
* <style type="text/css">
* .reveal-animation.ng-enter {
* -webkit-animation: enter_sequence 1s linear; /* Safari/Chrome */
* animation: enter_sequence 1s linear; /* IE10+ and Future Browsers */
* }
* @-webkit-keyframes enter_sequence {
* from { opacity:0; }
* to { opacity:1; }
* }
* @keyframes enter_sequence {
* from { opacity:0; }
* to { opacity:1; }
* }
* </style>
*
* <div class="view-container">
* <div ng-view class="reveal-animation"></div>
* </div>
* ```
*
* Both CSS3 animations and transitions can be used together and the animate service will figure out the correct duration and delay timing.
*
* Upon DOM mutation, the event class is added first (something like `ng-enter`), then the browser prepares itself to add
* the active class (in this case `ng-enter-active`) which then triggers the animation. The animation module will automatically
* detect the CSS code to determine when the animation ends. Once the animation is over then both CSS classes will be
* removed from the DOM. If a browser does not support CSS transitions or CSS animations then the animation will start and end
* immediately resulting in a DOM element that is at its final state. This final state is when the DOM element
* has no CSS transition/animation classes applied to it.
*
* ### Structural transition animations
*
* Structural transitions (such as enter, leave and move) will always apply a `0s none` transition
* value to force the browser into rendering the styles defined in the setup (`.ng-enter`, `.ng-leave`
* or `.ng-move`) class. This means that any active transition animations operating on the element
* will be cut off to make way for the enter, leave or move animation.
*
* ### Class-based transition animations
*
* Class-based transitions refer to transition animations that are triggered when a CSS class is
* added to or removed from the element (via `$animate.addClass`, `$animate.removeClass`,
* `$animate.setClass`, or by directives such as `ngClass`, `ngModel` and `form`).
* They are different when compared to structural animations since they **do not cancel existing
* animations** nor do they **block successive transitions** from rendering on the same element.
* This distinction allows for **multiple class-based transitions** to be performed on the same element.
*
* In addition to ngAnimate supporting the default (natural) functionality of class-based transition
* animations, ngAnimate also decorates the element with starting and ending CSS classes to aid the
* developer in further styling the element throughout the transition animation. Earlier versions
* of ngAnimate may have caused natural CSS transitions to break and not render properly due to
* $animate temporarily blocking transitions using `0s none` in order to allow the setup CSS class
* (the `-add` or `-remove` class) to be applied without triggering an animation. However, as of
* **version 1.3**, this workaround has been removed with ngAnimate and all non-ngAnimate CSS
* class transitions are compatible with ngAnimate.
*
* There is, however, one special case when dealing with class-based transitions in ngAnimate.
* When rendering class-based transitions that make use of the setup and active CSS classes
* (e.g. `.fade-add` and `.fade-add-active` for when `.fade` is added) be sure to define
* the transition value **on the active CSS class** and not the setup class.
*
* ```css
* .fade-add {
* /* remember to place a 0s transition here
* to ensure that the styles are applied instantly
* even if the element already has a transition style */
* transition:0s linear all;
*
* /* starting CSS styles */
* opacity:1;
* }
* .fade-add.fade-add-active {
* /* this will be the length of the animation */
* transition:1s linear all;
* opacity:0;
* }
* ```
*
* The setup CSS class (in this case `.fade-add`) also has a transition style property, however, it
* has a duration of zero. This may not be required, however, incase the browser is unable to render
* the styling present in this CSS class instantly then it could be that the browser is attempting
* to perform an unnecessary transition.
*
* This workaround, however, does not apply to standard class-based transitions that are rendered
* when a CSS class containing a transition is applied to an element:
*
* ```css
* /* this works as expected */
* .fade {
* transition:1s linear all;
* opacity:0;
* }
* ```
*
* Please keep this in mind when coding the CSS markup that will be used within class-based transitions.
* Also, try not to mix the two class-based animation flavors together since the CSS code may become
* overly complex.
*
*
* ### Preventing Collisions With Third Party Libraries
*
* Some third-party frameworks place animation duration defaults across many element or className
* selectors in order to make their code small and reuseable. This can lead to issues with ngAnimate, which
* is expecting actual animations on these elements and has to wait for their completion.
*
* You can prevent this unwanted behavior by using a prefix on all your animation classes:
*
* ```css
* /* prefixed with animate- */
* .animate-fade-add.animate-fade-add-active {
* transition:1s linear all;
* opacity:0;
* }
* ```
*
* You then configure `$animate` to enforce this prefix:
*
* ```js
* $animateProvider.classNameFilter(/animate-/);
* ```
* </div>
*
* ### CSS Staggering Animations
* A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a
* curtain-like effect. The ngAnimate module (versions >=1.2) supports staggering animations and the stagger effect can be
* performed by creating a **ng-EVENT-stagger** CSS class and attaching that class to the base CSS class used for
* the animation. The style property expected within the stagger class can either be a **transition-delay** or an
* **animation-delay** property (or both if your animation contains both transitions and keyframe animations).
*
* ```css
* .my-animation.ng-enter {
* /* standard transition code */
* -webkit-transition: 1s linear all;
* transition: 1s linear all;
* opacity:0;
* }
* .my-animation.ng-enter-stagger {
* /* this will have a 100ms delay between each successive leave animation */
* -webkit-transition-delay: 0.1s;
* transition-delay: 0.1s;
*
* /* in case the stagger doesn't work then these two values
* must be set to 0 to avoid an accidental CSS inheritance */
* -webkit-transition-duration: 0s;
* transition-duration: 0s;
* }
* .my-animation.ng-enter.ng-enter-active {
* /* standard transition styles */
* opacity:1;
* }
* ```
*
* Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations
* on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this
* are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation
* will also be reset if more than 10ms has passed after the last animation has been fired.
*
* The following code will issue the **ng-leave-stagger** event on the element provided:
*
* ```js
* var kids = parent.children();
*
* $animate.leave(kids[0]); //stagger index=0
* $animate.leave(kids[1]); //stagger index=1
* $animate.leave(kids[2]); //stagger index=2
* $animate.leave(kids[3]); //stagger index=3
* $animate.leave(kids[4]); //stagger index=4
*
* $timeout(function() {
* //stagger has reset itself
* $animate.leave(kids[5]); //stagger index=0
* $animate.leave(kids[6]); //stagger index=1
* }, 100, false);
* ```
*
* Stagger animations are currently only supported within CSS-defined animations.
*
* ## JavaScript-defined Animations
* In the event that you do not want to use CSS3 transitions or CSS3 animations or if you wish to offer animations on browsers that do not
* yet support CSS transitions/animations, then you can make use of JavaScript animations defined inside of your AngularJS module.
*
* ```js
* //!annotate="YourApp" Your AngularJS Module|Replace this or ngModule with the module that you used to define your application.
* var ngModule = angular.module('YourApp', ['ngAnimate']);
* ngModule.animation('.my-crazy-animation', function() {
* return {
* enter: function(element, done) {
* //run the animation here and call done when the animation is complete
* return function(cancelled) {
* //this (optional) function will be called when the animation
* //completes or when the animation is cancelled (the cancelled
* //flag will be set to true if cancelled).
* };
* },
* leave: function(element, done) { },
* move: function(element, done) { },
*
* //animation that can be triggered before the class is added
* beforeAddClass: function(element, className, done) { },
*
* //animation that can be triggered after the class is added
* addClass: function(element, className, done) { },
*
* //animation that can be triggered before the class is removed
* beforeRemoveClass: function(element, className, done) { },
*
* //animation that can be triggered after the class is removed
* removeClass: function(element, className, done) { }
* };
* });
* ```
*
* JavaScript-defined animations are created with a CSS-like class selector and a collection of events which are set to run
* a javascript callback function. When an animation is triggered, $animate will look for a matching animation which fits
* the element's CSS class attribute value and then run the matching animation event function (if found).
* In other words, if the CSS classes present on the animated element match any of the JavaScript animations then the callback function will
* be executed. It should be also noted that only simple, single class selectors are allowed (compound class selectors are not supported).
*
* Within a JavaScript animation, an object containing various event callback animation functions is expected to be returned.
* As explained above, these callbacks are triggered based on the animation event. Therefore if an enter animation is run,
* and the JavaScript animation is found, then the enter callback will handle that animation (in addition to the CSS keyframe animation
* or transition code that is defined via a stylesheet).
*
*
* ### Applying Directive-specific Styles to an Animation
* In some cases a directive or service may want to provide `$animate` with extra details that the animation will
* include into its animation. Let's say for example we wanted to render an animation that animates an element
* towards the mouse coordinates as to where the user clicked last. By collecting the X/Y coordinates of the click
* (via the event parameter) we can set the `top` and `left` styles into an object and pass that into our function
* call to `$animate.addClass`.
*
* ```js
* canvas.on('click', function(e) {
* $animate.addClass(element, 'on', {
* to: {
* left : e.client.x + 'px',
* top : e.client.y + 'px'
* }
* }):
* });
* ```
*
* Now when the animation runs, and a transition or keyframe animation is picked up, then the animation itself will
* also include and transition the styling of the `left` and `top` properties into its running animation. If we want
* to provide some starting animation values then we can do so by placing the starting animations styles into an object
* called `from` in the same object as the `to` animations.
*
* ```js
* canvas.on('click', function(e) {
* $animate.addClass(element, 'on', {
* from: {
* position: 'absolute',
* left: '0px',
* top: '0px'
* },
* to: {
* left : e.client.x + 'px',
* top : e.client.y + 'px'
* }
* }):
* });
* ```
*
* Once the animation is complete or cancelled then the union of both the before and after styles are applied to the
* element. If `ngAnimate` is not present then the styles will be applied immediately.
*
*/
angular.module('ngAnimate', ['ng'])
/**
* @ngdoc provider
* @name $animateProvider
* @description
*
* The `$animateProvider` allows developers to register JavaScript animation event handlers directly inside of a module.
* When an animation is triggered, the $animate service will query the $animate service to find any animations that match
* the provided name value.
*
* Requires the {@link ngAnimate `ngAnimate`} module to be installed.
*
* Please visit the {@link ngAnimate `ngAnimate`} module overview page learn more about how to use animations in your application.
*
*/
.directive('ngAnimateChildren', function() {
var NG_ANIMATE_CHILDREN = '$$ngAnimateChildren';
return function(scope, element, attrs) {
var val = attrs.ngAnimateChildren;
if (angular.isString(val) && val.length === 0) { //empty attribute
element.data(NG_ANIMATE_CHILDREN, true);
} else {
scope.$watch(val, function(value) {
element.data(NG_ANIMATE_CHILDREN, !!value);
});
}
};
})
//this private service is only used within CSS-enabled animations
//IE8 + IE9 do not support rAF natively, but that is fine since they
//also don't support transitions and keyframes which means that the code
//below will never be used by the two browsers.
.factory('$$animateReflow', ['$$rAF', '$document', function($$rAF, $document) {
var bod = $document[0].body;
return function(fn) {
//the returned function acts as the cancellation function
return $$rAF(function() {
//the line below will force the browser to perform a repaint
//so that all the animated elements within the animation frame
//will be properly updated and drawn on screen. This is
//required to perform multi-class CSS based animations with
//Firefox. DO NOT REMOVE THIS LINE.
var a = bod.offsetWidth + 1;
fn();
});
};
}])
.config(['$provide', '$animateProvider', function($provide, $animateProvider) {
var noop = angular.noop;
var forEach = angular.forEach;
var selectors = $animateProvider.$$selectors;
var isArray = angular.isArray;
var isString = angular.isString;
var isObject = angular.isObject;
var ELEMENT_NODE = 1;
var NG_ANIMATE_STATE = '$$ngAnimateState';
var NG_ANIMATE_CHILDREN = '$$ngAnimateChildren';
var NG_ANIMATE_CLASS_NAME = 'ng-animate';
var rootAnimateState = {running: true};
function extractElementNode(element) {
for (var i = 0; i < element.length; i++) {
var elm = element[i];
if (elm.nodeType == ELEMENT_NODE) {
return elm;
}
}
}
function prepareElement(element) {
return element && angular.element(element);
}
function stripCommentsFromElement(element) {
return angular.element(extractElementNode(element));
}
function isMatchingElement(elm1, elm2) {
return extractElementNode(elm1) == extractElementNode(elm2);
}
var $$jqLite;
$provide.decorator('$animate',
['$delegate', '$$q', '$injector', '$sniffer', '$rootElement', '$$asyncCallback', '$rootScope', '$document', '$templateRequest', '$$jqLite',
function($delegate, $$q, $injector, $sniffer, $rootElement, $$asyncCallback, $rootScope, $document, $templateRequest, $$$jqLite) {
$$jqLite = $$$jqLite;
$rootElement.data(NG_ANIMATE_STATE, rootAnimateState);
// Wait until all directive and route-related templates are downloaded and
// compiled. The $templateRequest.totalPendingRequests variable keeps track of
// all of the remote templates being currently downloaded. If there are no
// templates currently downloading then the watcher will still fire anyway.
var deregisterWatch = $rootScope.$watch(
function() { return $templateRequest.totalPendingRequests; },
function(val, oldVal) {
if (val !== 0) return;
deregisterWatch();
// Now that all templates have been downloaded, $animate will wait until
// the post digest queue is empty before enabling animations. By having two
// calls to $postDigest calls we can ensure that the flag is enabled at the
// very end of the post digest queue. Since all of the animations in $animate
// use $postDigest, it's important that the code below executes at the end.
// This basically means that the page is fully downloaded and compiled before
// any animations are triggered.
$rootScope.$$postDigest(function() {
$rootScope.$$postDigest(function() {
rootAnimateState.running = false;
});
});
}
);
var globalAnimationCounter = 0;
var classNameFilter = $animateProvider.classNameFilter();
var isAnimatableClassName = !classNameFilter
? function() { return true; }
: function(className) {
return classNameFilter.test(className);
};
function classBasedAnimationsBlocked(element, setter) {
var data = element.data(NG_ANIMATE_STATE) || {};
if (setter) {
data.running = true;
data.structural = true;
element.data(NG_ANIMATE_STATE, data);
}
return data.disabled || (data.running && data.structural);
}
function runAnimationPostDigest(fn) {
var cancelFn, defer = $$q.defer();
defer.promise.$$cancelFn = function() {
cancelFn && cancelFn();
};
$rootScope.$$postDigest(function() {
cancelFn = fn(function() {
defer.resolve();
});
});
return defer.promise;
}
function parseAnimateOptions(options) {
// some plugin code may still be passing in the callback
// function as the last param for the $animate methods so
// it's best to only allow string or array values for now
if (isObject(options)) {
if (options.tempClasses && isString(options.tempClasses)) {
options.tempClasses = options.tempClasses.split(/\s+/);
}
return options;
}
}
function resolveElementClasses(element, cache, runningAnimations) {
runningAnimations = runningAnimations || {};
var lookup = {};
forEach(runningAnimations, function(data, selector) {
forEach(selector.split(' '), function(s) {
lookup[s]=data;
});
});
var hasClasses = Object.create(null);
forEach((element.attr('class') || '').split(/\s+/), function(className) {
hasClasses[className] = true;
});
var toAdd = [], toRemove = [];
forEach((cache && cache.classes) || [], function(status, className) {
var hasClass = hasClasses[className];
var matchingAnimation = lookup[className] || {};
// When addClass and removeClass is called then $animate will check to
// see if addClass and removeClass cancel each other out. When there are
// more calls to removeClass than addClass then the count falls below 0
// and then the removeClass animation will be allowed. Otherwise if the
// count is above 0 then that means an addClass animation will commence.
// Once an animation is allowed then the code will also check to see if
// there exists any on-going animation that is already adding or remvoing
// the matching CSS class.
if (status === false) {
//does it have the class or will it have the class
if (hasClass || matchingAnimation.event == 'addClass') {
toRemove.push(className);
}
} else if (status === true) {
//is the class missing or will it be removed?
if (!hasClass || matchingAnimation.event == 'removeClass') {
toAdd.push(className);
}
}
});
return (toAdd.length + toRemove.length) > 0 && [toAdd.join(' '), toRemove.join(' ')];
}
function lookup(name) {
if (name) {
var matches = [],
flagMap = {},
classes = name.substr(1).split('.');
//the empty string value is the default animation
//operation which performs CSS transition and keyframe
//animations sniffing. This is always included for each
//element animation procedure if the browser supports
//transitions and/or keyframe animations. The default
//animation is added to the top of the list to prevent
//any previous animations from affecting the element styling
//prior to the element being animated.
if ($sniffer.transitions || $sniffer.animations) {
matches.push($injector.get(selectors['']));
}
for (var i=0; i < classes.length; i++) {
var klass = classes[i],
selectorFactoryName = selectors[klass];
if (selectorFactoryName && !flagMap[klass]) {
matches.push($injector.get(selectorFactoryName));
flagMap[klass] = true;
}
}
return matches;
}
}
function animationRunner(element, animationEvent, className, options) {
//transcluded directives may sometimes fire an animation using only comment nodes
//best to catch this early on to prevent any animation operations from occurring
var node = element[0];
if (!node) {
return;
}
if (options) {
options.to = options.to || {};
options.from = options.from || {};
}
var classNameAdd;
var classNameRemove;
if (isArray(className)) {
classNameAdd = className[0];
classNameRemove = className[1];
if (!classNameAdd) {
className = classNameRemove;
animationEvent = 'removeClass';
} else if (!classNameRemove) {
className = classNameAdd;
animationEvent = 'addClass';
} else {
className = classNameAdd + ' ' + classNameRemove;
}
}
var isSetClassOperation = animationEvent == 'setClass';
var isClassBased = isSetClassOperation
|| animationEvent == 'addClass'
|| animationEvent == 'removeClass'
|| animationEvent == 'animate';
var currentClassName = element.attr('class');
var classes = currentClassName + ' ' + className;
if (!isAnimatableClassName(classes)) {
return;
}
var beforeComplete = noop,
beforeCancel = [],
before = [],
afterComplete = noop,
afterCancel = [],
after = [];
var animationLookup = (' ' + classes).replace(/\s+/g,'.');
forEach(lookup(animationLookup), function(animationFactory) {
var created = registerAnimation(animationFactory, animationEvent);
if (!created && isSetClassOperation) {
registerAnimation(animationFactory, 'addClass');
registerAnimation(animationFactory, 'removeClass');
}
});
function registerAnimation(animationFactory, event) {
var afterFn = animationFactory[event];
var beforeFn = animationFactory['before' + event.charAt(0).toUpperCase() + event.substr(1)];
if (afterFn || beforeFn) {
if (event == 'leave') {
beforeFn = afterFn;
//when set as null then animation knows to skip this phase
afterFn = null;
}
after.push({
event: event, fn: afterFn
});
before.push({
event: event, fn: beforeFn
});
return true;
}
}
function run(fns, cancellations, allCompleteFn) {
var animations = [];
forEach(fns, function(animation) {
animation.fn && animations.push(animation);
});
var count = 0;
function afterAnimationComplete(index) {
if (cancellations) {
(cancellations[index] || noop)();
if (++count < animations.length) return;
cancellations = null;
}
allCompleteFn();
}
//The code below adds directly to the array in order to work with
//both sync and async animations. Sync animations are when the done()
//operation is called right away. DO NOT REFACTOR!
forEach(animations, function(animation, index) {
var progress = function() {
afterAnimationComplete(index);
};
switch (animation.event) {
case 'setClass':
cancellations.push(animation.fn(element, classNameAdd, classNameRemove, progress, options));
break;
case 'animate':
cancellations.push(animation.fn(element, className, options.from, options.to, progress));
break;
case 'addClass':
cancellations.push(animation.fn(element, classNameAdd || className, progress, options));
break;
case 'removeClass':
cancellations.push(animation.fn(element, classNameRemove || className, progress, options));
break;
default:
cancellations.push(animation.fn(element, progress, options));
break;
}
});
if (cancellations && cancellations.length === 0) {
allCompleteFn();
}
}
return {
node: node,
event: animationEvent,
className: className,
isClassBased: isClassBased,
isSetClassOperation: isSetClassOperation,
applyStyles: function() {
if (options) {
element.css(angular.extend(options.from || {}, options.to || {}));
}
},
before: function(allCompleteFn) {
beforeComplete = allCompleteFn;
run(before, beforeCancel, function() {
beforeComplete = noop;
allCompleteFn();
});
},
after: function(allCompleteFn) {
afterComplete = allCompleteFn;
run(after, afterCancel, function() {
afterComplete = noop;
allCompleteFn();
});
},
cancel: function() {
if (beforeCancel) {
forEach(beforeCancel, function(cancelFn) {
(cancelFn || noop)(true);
});
beforeComplete(true);
}
if (afterCancel) {
forEach(afterCancel, function(cancelFn) {
(cancelFn || noop)(true);
});
afterComplete(true);
}
}
};
}
/**
* @ngdoc service
* @name $animate
* @kind object
*
* @description
* The `$animate` service provides animation detection support while performing DOM operations (enter, leave and move) as well as during addClass and removeClass operations.
* When any of these operations are run, the $animate service
* will examine any JavaScript-defined animations (which are defined by using the $animateProvider provider object)
* as well as any CSS-defined animations against the CSS classes present on the element once the DOM operation is run.
*
* The `$animate` service is used behind the scenes with pre-existing directives and animation with these directives
* will work out of the box without any extra configuration.
*
* Requires the {@link ngAnimate `ngAnimate`} module to be installed.
*
* Please visit the {@link ngAnimate `ngAnimate`} module overview page learn more about how to use animations in your application.
* ## Callback Promises
* With AngularJS 1.3, each of the animation methods, on the `$animate` service, return a promise when called. The
* promise itself is then resolved once the animation has completed itself, has been cancelled or has been
* skipped due to animations being disabled. (Note that even if the animation is cancelled it will still
* call the resolve function of the animation.)
*
* ```js
* $animate.enter(element, container).then(function() {
* //...this is called once the animation is complete...
* });
* ```
*
* Also note that, due to the nature of the callback promise, if any Angular-specific code (like changing the scope,
* location of the page, etc...) is executed within the callback promise then be sure to wrap the code using
* `$scope.$apply(...)`;
*
* ```js
* $animate.leave(element).then(function() {
* $scope.$apply(function() {
* $location.path('/new-page');
* });
* });
* ```
*
* An animation can also be cancelled by calling the `$animate.cancel(promise)` method with the provided
* promise that was returned when the animation was started.
*
* ```js
* var promise = $animate.addClass(element, 'super-long-animation');
* promise.then(function() {
* //this will still be called even if cancelled
* });
*
* element.on('click', function() {
* //tooo lazy to wait for the animation to end
* $animate.cancel(promise);
* });
* ```
*
* (Keep in mind that the promise cancellation is unique to `$animate` since promises in
* general cannot be cancelled.)
*
*/
return {
/**
* @ngdoc method
* @name $animate#animate
* @kind function
*
* @description
* Performs an inline animation on the element which applies the provided `to` and `from` CSS styles to the element.
* If any detected CSS transition, keyframe or JavaScript matches the provided `className` value then the animation
* will take on the provided styles. For example, if a transition animation is set for the given className then the
* provided `from` and `to` styles will be applied alongside the given transition. If a JavaScript animation is
* detected then the provided styles will be given in as function paramters.
*
* ```js
* ngModule.animation('.my-inline-animation', function() {
* return {
* animate : function(element, className, from, to, done) {
* //styles
* }
* }
* });
* ```
*
* Below is a breakdown of each step that occurs during the `animate` animation:
*
* | Animation Step | What the element class attribute looks like |
* |-----------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------|
* | 1. `$animate.animate(...)` is called | `class="my-animation"` |
* | 2. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` |
* | 3. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` |
* | 4. the `className` class value is added to the element | `class="my-animation ng-animate className"` |
* | 5. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate className"` |
* | 6. `$animate` blocks all CSS transitions on the element to ensure the `.className` class styling is applied right away| `class="my-animation ng-animate className"` |
* | 7. `$animate` applies the provided collection of `from` CSS styles to the element | `class="my-animation ng-animate className"` |
* | 8. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate className"` |
* | 9. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate className"` |
* | 10. the `className-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate className className-active"` |
* | 11. `$animate` applies the collection of `to` CSS styles to the element which are then handled by the transition | `class="my-animation ng-animate className className-active"` |
* | 12. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate className className-active"` |
* | 13. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` |
* | 14. The returned promise is resolved. | `class="my-animation"` |
*
* @param {DOMElement} element the element that will be the focus of the enter animation
* @param {object} from a collection of CSS styles that will be applied to the element at the start of the animation
* @param {object} to a collection of CSS styles that the element will animate towards
* @param {string=} className an optional CSS class that will be added to the element for the duration of the animation (the default class is `ng-inline-animate`)
* @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation
* @return {Promise} the animation callback promise
*/
animate: function(element, from, to, className, options) {
className = className || 'ng-inline-animate';
options = parseAnimateOptions(options) || {};
options.from = to ? from : null;
options.to = to ? to : from;
return runAnimationPostDigest(function(done) {
return performAnimation('animate', className, stripCommentsFromElement(element), null, null, noop, options, done);
});
},
/**
* @ngdoc method
* @name $animate#enter
* @kind function
*
* @description
* Appends the element to the parentElement element that resides in the document and then runs the enter animation. Once
* the animation is started, the following CSS classes will be present on the element for the duration of the animation:
*
* Below is a breakdown of each step that occurs during enter animation:
*
* | Animation Step | What the element class attribute looks like |
* |-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
* | 1. `$animate.enter(...)` is called | `class="my-animation"` |
* | 2. element is inserted into the `parentElement` element or beside the `afterElement` element | `class="my-animation"` |
* | 3. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` |
* | 4. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` |
* | 5. the `.ng-enter` class is added to the element | `class="my-animation ng-animate ng-enter"` |
* | 6. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate ng-enter"` |
* | 7. `$animate` blocks all CSS transitions on the element to ensure the `.ng-enter` class styling is applied right away | `class="my-animation ng-animate ng-enter"` |
* | 8. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate ng-enter"` |
* | 9. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate ng-enter"` |
* | 10. the `.ng-enter-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate ng-enter ng-enter-active"` |
* | 11. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate ng-enter ng-enter-active"` |
* | 12. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` |
* | 13. The returned promise is resolved. | `class="my-animation"` |
*
* @param {DOMElement} element the element that will be the focus of the enter animation
* @param {DOMElement} parentElement the parent element of the element that will be the focus of the enter animation
* @param {DOMElement} afterElement the sibling element (which is the previous element) of the element that will be the focus of the enter animation
* @param {object=} options an optional collection of options that will be picked up by the CSS transition/animation
* @return {Promise} the animation callback promise
*/
enter: function(element, parentElement, afterElement, options) {
options = parseAnimateOptions(options);
element = angular.element(element);
parentElement = prepareElement(parentElement);
afterElement = prepareElement(afterElement);
classBasedAnimationsBlocked(element, true);
$delegate.enter(element, parentElement, afterElement);
return runAnimationPostDigest(function(done) {
return performAnimation('enter', 'ng-enter', stripCommentsFromElement(element), parentElement, afterElement, noop, options, done);
});
},
/**
* @ngdoc method
* @name $animate#leave
* @kind function
*
* @description
* Runs the leave animation operation and, upon completion, removes the element from the DOM. Once
* the animation is started, the following CSS classes will be added for the duration of the animation:
*
* Below is a breakdown of each step that occurs during leave animation:
*
* | Animation Step | What the element class attribute looks like |
* |-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|
* | 1. `$animate.leave(...)` is called | `class="my-animation"` |
* | 2. `$animate` runs the JavaScript-defined animations detected on the element | `class="my-animation ng-animate"` |
* | 3. `$animate` waits for the next digest to start the animation | `class="my-animation ng-animate"` |
* | 4. the `.ng-leave` class is added to the element | `class="my-animation ng-animate ng-leave"` |
* | 5. `$animate` scans the element styles to get the CSS transition/animation duration and delay | `class="my-animation ng-animate ng-leave"` |
* | 6. `$animate` blocks all CSS transitions on the element to ensure the `.ng-leave` class styling is applied right away | `class="my-animation ng-animate ng-leave"` |
* | 7. `$animate` waits for a single animation frame (this performs a reflow) | `class="my-animation ng-animate ng-leave"` |
* | 8. `$animate` removes the CSS transition block placed on the element | `class="my-animation ng-animate ng-leave"` |
* | 9. the `.ng-leave-active` class is added (this triggers the CSS transition/animation) | `class="my-animation ng-animate ng-leave ng-leave-active"` |
* | 10. `$animate` waits for the animation to complete (via events and timeout) | `class="my-animation ng-animate ng-leave ng-leave-active"` |
* | 11. The animation ends and all generated CSS classes are removed from the element | `class="my-animation"` |
* | 12. The element is removed from the DOM | ... |
* | 13. The returned promise is resolved. | ... |
*
* @param {DOMElement} element the element that will be the focus of the leave animation
* @param {object=} options an optional collection of styles that will be picked up by the CSS transition/animation
* @return {Promise} the animation callback promise
*/
leave: function(element, options) {
options = parseAnimateOptions(options);
element = angular.element(element);
cancelChildAnimations(element);
classBasedAnimationsBlocked(element, true);
return runAnimationPostDigest(function(done) {
return performAnimation('leave', 'ng-leave', stripCommentsFromElement(element), null, null, function() {
$delegate.leave(element);
}, options, done);
});
},