@angular-wave/angular.ts 0.0.53 → 0.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,933 @@
1
+ /\*\*
2
+
3
+ - @ngdoc module
4
+ - @name ng.animate
5
+ - @description
6
+ -
7
+ - The `ng.animate` module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via
8
+ - callback hooks. Animations are not enabled by default, however, by including `ng.animate` the animation hooks are enabled for an AngularJS app.
9
+ -
10
+ - ## Usage
11
+ - Simply put, there are two ways to make use of animations when ng.animate is used: by using **CSS** and **JavaScript**. The former works purely based
12
+ - using CSS (by using matching CSS selectors/styles) and the latter triggers animations that are registered via `module.animation()`. For
13
+ - both CSS and JS animations the sole requirement is to have a matching `CSS class` that exists both in the registered animation and within
14
+ - the HTML element that the animation will be triggered on.
15
+ -
16
+ - ## Directive Support
17
+ - The following directives are "animation aware":
18
+ -
19
+ - | Directive | Supported Animations |
20
+ - |-------------------------------------------------------------------------------|---------------------------------------------------------------------------|
21
+ - | {@link ng.directive:form#animations form / ngForm} | add and remove ({@link ng.directive:form#css-classes various classes}) |
22
+ - | {@link ng.animate.directive:ng.animateSwap#animations ng.animateSwap} | enter and leave |
23
+ - | {@link ng.directive:ngClass#animations ngClass / class}​}} | add and remove |
24
+ - | {@link ng.directive:ngClassEven#animations ngClassEven} | add and remove |
25
+ - | {@link ng.directive:ngClassOdd#animations ngClassOdd} | add and remove |
26
+ - | {@link ng.directive:ngHide#animations ngHide} | add and remove (the `ng-hide` class) |
27
+ - | {@link ng.directive:ngIf#animations ngIf} | enter and leave |
28
+ - | {@link ng.directive:ngInclude#animations ngInclude} | enter and leave |
29
+ - | {@link module:ngMessages#animations ngMessage / ngMessageExp} | enter and leave |
30
+ - | {@link module:ngMessages#animations ngMessages} | add and remove (the `ng-active`/`ng-inactive` classes) |
31
+ - | {@link ng.directive:ngModel#animations ngModel} | add and remove ({@link ng.directive:ngModel#css-classes various classes}) |
32
+ - | {@link ng.directive:ngRepeat#animations ngRepeat} | enter, leave, and move |
33
+ - | {@link ng.directive:ngShow#animations ngShow} | add and remove (the `ng-hide` class) |
34
+ - | {@link ng.directive:ngSwitch#animations ngSwitch} | enter and leave |
35
+ - | {@link ngRoute.directive:ngView#animations ngView} | enter and leave |
36
+ -
37
+ - (More information can be found by visiting the documentation associated with each directive.)
38
+ -
39
+ - For a full breakdown of the steps involved during each animation event, refer to the
40
+ - {@link ng.$animate `$animate` API docs}.
41
+ -
42
+ - ## CSS-based Animations
43
+ -
44
+ - CSS-based animations with ng.animate are unique since they require no JavaScript code at all. By using a CSS class that we reference between our HTML
45
+ - and CSS code we can create an animation that will be picked up by AngularJS when an underlying directive performs an operation.
46
+ -
47
+ - The example below shows how an `enter` animation can be made possible on an element using `ng-if`:
48
+ -
49
+ - ```html
50
+
51
+ ```
52
+
53
+ - <div ng-if="bool" class="fade">
54
+ - Fade me in out
55
+ - </div>
56
+ - <button ng-click="bool=true">Fade In!</button>
57
+ - <button ng-click="bool=false">Fade Out!</button>
58
+ - ```
59
+
60
+ ```
61
+
62
+ -
63
+ - Notice the CSS class **fade**? We can now create the CSS transition code that references this class:
64
+ -
65
+ - ```css
66
+
67
+ ```
68
+
69
+ - /\* The starting CSS styles for the enter animation \*/
70
+ - .fade.ng-enter {
71
+ - transition:0.5s linear all;
72
+ - opacity:0;
73
+ - }
74
+ -
75
+ - /\* The finishing CSS styles for the enter animation \*/
76
+ - .fade.ng-enter.ng-enter-active {
77
+ - opacity:1;
78
+ - }
79
+ - ```
80
+
81
+ ```
82
+
83
+ -
84
+ - 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
85
+ - 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
86
+ - code **must** be defined within the starting CSS class (in this case `.ng-enter`). The destination class is what the transition will animate towards.
87
+ -
88
+ - 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:
89
+ -
90
+ - ```css
91
+
92
+ ```
93
+
94
+ - /\* now the element will fade out before it is removed from the DOM \*/
95
+ - .fade.ng-leave {
96
+ - transition:0.5s linear all;
97
+ - opacity:1;
98
+ - }
99
+ - .fade.ng-leave.ng-leave-active {
100
+ - opacity:0;
101
+ - }
102
+ - ```
103
+
104
+ ```
105
+
106
+ -
107
+ - We can also make use of **CSS Keyframes** by referencing the keyframe animation within the starting CSS class:
108
+ -
109
+ - ```css
110
+
111
+ ```
112
+
113
+ - /\* there is no need to define anything inside of the destination
114
+ - CSS class since the keyframe will take charge of the animation \*/
115
+ - .fade.ng-leave {
116
+ - animation: my_fade_animation 0.5s linear;
117
+ - -webkit-animation: my_fade_animation 0.5s linear;
118
+ - }
119
+ -
120
+ - @keyframes my_fade_animation {
121
+ - from { opacity:1; }
122
+ - to { opacity:0; }
123
+ - }
124
+ -
125
+ - @-webkit-keyframes my_fade_animation {
126
+ - from { opacity:1; }
127
+ - to { opacity:0; }
128
+ - }
129
+ - ```
130
+
131
+ ```
132
+
133
+ -
134
+ - Feel free also mix transitions and keyframes together as well as any other CSS classes on the same element.
135
+ -
136
+ - ### CSS Class-based Animations
137
+ -
138
+ - Class-based animations (animations that are triggered via `ngClass`, `ngShow`, `ngHide` and some other directives) have a slightly different
139
+ - naming convention. Class-based animations are basic enough that a standard transition or keyframe can be referenced on the class being added
140
+ - and removed.
141
+ -
142
+ - For example if we wanted to do a CSS animation for `ngHide` then we place an animation on the `.ng-hide` CSS class:
143
+ -
144
+ - ```html
145
+
146
+ ```
147
+
148
+ - <div ng-show="bool" class="fade">
149
+ - Show and hide me
150
+ - </div>
151
+ - <button ng-click="bool=!bool">Toggle</button>
152
+ -
153
+ - <style>
154
+ - .fade.ng-hide {
155
+ - transition:0.5s linear all;
156
+ - opacity:0;
157
+ - }
158
+ - </style>
159
+ - ```
160
+
161
+ ```
162
+
163
+ -
164
+ - 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
165
+ - ngShow and ngHide are animation aware then we can match up a transition and ng.animate handles the rest.
166
+ -
167
+ - In addition the addition and removal of the CSS class, ng.animate also provides two helper methods that we can use to further decorate the animation
168
+ - with CSS styles.
169
+ -
170
+ - ```html
171
+
172
+ ```
173
+
174
+ - <div ng-class="{on:onOff}" class="highlight">
175
+ - Highlight this box
176
+ - </div>
177
+ - <button ng-click="onOff=!onOff">Toggle</button>
178
+ -
179
+ - <style>
180
+ - .highlight {
181
+ - transition:0.5s linear all;
182
+ - }
183
+ - .highlight.on-add {
184
+ - background:white;
185
+ - }
186
+ - .highlight.on {
187
+ - background:yellow;
188
+ - }
189
+ - .highlight.on-remove {
190
+ - background:black;
191
+ - }
192
+ - </style>
193
+ - ```
194
+
195
+ ```
196
+
197
+ -
198
+ - We can also make use of CSS keyframes by placing them within the CSS classes.
199
+ -
200
+ -
201
+ - ### CSS Staggering Animations
202
+ - A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a
203
+ - curtain-like effect. The ng.animate module (versions >=1.2) supports staggering animations and the stagger effect can be
204
+ - performed by creating a **ng-EVENT-stagger** CSS class and attaching that class to the base CSS class used for
205
+ - the animation. The style property expected within the stagger class can either be a **transition-delay** or an
206
+ - **animation-delay** property (or both if your animation contains both transitions and keyframe animations).
207
+ -
208
+ - ```css
209
+
210
+ ```
211
+
212
+ - .my-animation.ng-enter {
213
+ - /\* standard transition code \*/
214
+ - transition: 1s linear all;
215
+ - opacity:0;
216
+ - }
217
+ - .my-animation.ng-enter-stagger {
218
+ - /\* this will have a 100ms delay between each successive leave animation \*/
219
+ - transition-delay: 0.1s;
220
+ -
221
+ - /\* As of 1.4.4, this must always be set: it signals ng.animate
222
+ - to not accidentally inherit a delay property from another CSS class &#42;/
223
+ - transition-duration: 0s;
224
+ -
225
+ - /\* if you are using animations instead of transitions you should configure as follows:
226
+ - animation-delay: 0.1s;
227
+ - animation-duration: 0s; &#42;/
228
+ - }
229
+ - .my-animation.ng-enter.ng-enter-active {
230
+ - /\* standard transition styles \*/
231
+ - opacity:1;
232
+ - }
233
+ - ```
234
+
235
+ ```
236
+
237
+ -
238
+ - Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations
239
+ - on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this
240
+ - are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation
241
+ - will also be reset if one or more animation frames have passed since the multiple calls to `$animate` were fired.
242
+ -
243
+ - The following code will issue the **ng-leave-stagger** event on the element provided:
244
+ -
245
+ - ```js
246
+
247
+ ```
248
+
249
+ - let kids = parent.children();
250
+ -
251
+ - $animate.leave(kids[0]); //stagger index=0
252
+ - $animate.leave(kids[1]); //stagger index=1
253
+ - $animate.leave(kids[2]); //stagger index=2
254
+ - $animate.leave(kids[3]); //stagger index=3
255
+ - $animate.leave(kids[4]); //stagger index=4
256
+ -
257
+ - window.requestAnimationFrame(function() {
258
+ - //stagger has reset itself
259
+ - $animate.leave(kids[5]); //stagger index=0
260
+ - $animate.leave(kids[6]); //stagger index=1
261
+ -
262
+ - $scope.$digest();
263
+ - });
264
+ - ```
265
+
266
+ ```
267
+
268
+ -
269
+ - Stagger animations are currently only supported within CSS-defined animations.
270
+ -
271
+ - ### The `ng-animate` CSS class
272
+ -
273
+ - When ng.animate is animating an element it will apply the `ng-animate` CSS class to the element for the duration of the animation.
274
+ - This is a temporary CSS class and it will be removed once the animation is over (for both JavaScript and CSS-based animations).
275
+ -
276
+ - Therefore, animations can be applied to an element using this temporary class directly via CSS.
277
+ -
278
+ - ```css
279
+
280
+ ```
281
+
282
+ - .zipper.ng-animate {
283
+ - transition:0.5s linear all;
284
+ - }
285
+ - .zipper.ng-enter {
286
+ - opacity:0;
287
+ - }
288
+ - .zipper.ng-enter.ng-enter-active {
289
+ - opacity:1;
290
+ - }
291
+ - .zipper.ng-leave {
292
+ - opacity:1;
293
+ - }
294
+ - .zipper.ng-leave.ng-leave-active {
295
+ - opacity:0;
296
+ - }
297
+ - ```
298
+
299
+ ```
300
+
301
+ -
302
+ - (Note that the `ng-animate` CSS class is reserved and it cannot be applied on an element directly since ng.animate will always remove
303
+ - the CSS class once an animation has completed.)
304
+ -
305
+ -
306
+ - ### The `ng-[event]-prepare` class
307
+ -
308
+ - This is a special class that can be used to prevent unwanted flickering / flash of content before
309
+ - the actual animation starts. The class is added as soon as an animation is initialized, but removed
310
+ - before the actual animation starts (after waiting for a $digest).
311
+ - It is also only added for _structural_ animations (`enter`, `move`, and `leave`).
312
+ -
313
+ - In practice, flickering can appear when nesting elements with structural animations such as `ngIf`
314
+ - into elements that have class-based animations such as `ngClass`.
315
+ -
316
+ - ```html
317
+
318
+ ```
319
+
320
+ - <div ng-class="{red: myProp}">
321
+ - <div ng-class="{blue: myProp}">
322
+ - <div class="message" ng-if="myProp"></div>
323
+ - </div>
324
+ - </div>
325
+ - ```
326
+
327
+ ```
328
+
329
+ -
330
+ - It is possible that during the `enter` animation, the `.message` div will be briefly visible before it starts animating.
331
+ - In that case, you can add styles to the CSS that make sure the element stays hidden before the animation starts:
332
+ -
333
+ - ```css
334
+
335
+ ```
336
+
337
+ - .message.ng-enter-prepare {
338
+ - opacity: 0;
339
+ - }
340
+ - ```
341
+
342
+ ```
343
+
344
+ -
345
+ - ### Animating between value changes
346
+ -
347
+ - Sometimes you need to animate between different expression states, whose values
348
+ - don't necessary need to be known or referenced in CSS styles.
349
+ - Unless possible with another {@link ng.animate#directive-support "animation aware" directive},
350
+ - that specific use case can always be covered with {@link ng.animate.directive:ng.animateSwap} as
351
+ - can be seen in {@link ng.animate.directive:ng.animateSwap#examples this example}.
352
+ -
353
+ - Note that {@link ng.animate.directive:ng.animateSwap} is a _structural directive_, which means it
354
+ - creates a new instance of the element (including any other/child directives it may have) and
355
+ - links it to a new scope every time _swap_ happens. In some cases this might not be desirable
356
+ - (e.g. for performance reasons, or when you wish to retain internal state on the original
357
+ - element instance).
358
+ -
359
+ - ## JavaScript-based Animations
360
+ -
361
+ - ng.animate also allows for animations to be consumed by JavaScript code. The approach is similar to CSS-based animations (where there is a shared
362
+ - 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
363
+ - `module.animation()` module function we can register the animation.
364
+ -
365
+ - Let's see an example of a enter/leave animation using `ngRepeat`:
366
+ -
367
+ - ```html
368
+
369
+ ```
370
+
371
+ - <div ng-repeat="item in items" class="slide">
372
+ - {{ item }}
373
+ - </div>
374
+ - ```
375
+
376
+ ```
377
+
378
+ -
379
+ - 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`:
380
+ -
381
+ - ```js
382
+
383
+ ```
384
+
385
+ - myModule.animation('.slide', [function() {
386
+ - return {
387
+ - // make note that other events (like addClass/removeClass)
388
+ - // have different function input parameters
389
+ - enter: function(element, doneFn) {
390
+ - jQuery(element).fadeIn(1000, doneFn);
391
+ -
392
+ - // remember to call doneFn so that AngularJS
393
+ - // knows that the animation has concluded
394
+ - },
395
+ -
396
+ - move: function(element, doneFn) {
397
+ - jQuery(element).fadeIn(1000, doneFn);
398
+ - },
399
+ -
400
+ - leave: function(element, doneFn) {
401
+ - jQuery(element).fadeOut(1000, doneFn);
402
+ - }
403
+ - }
404
+ - }]);
405
+ - ```
406
+
407
+ ```
408
+
409
+ -
410
+ - The nice thing about JS-based animations is that we can inject other services and make use of advanced animation libraries such as
411
+ - greensock.js and velocity.js.
412
+ -
413
+ - If our animation code class-based (meaning that something like `ngClass`, `ngHide` and `ngShow` triggers it) then we can still define
414
+ - our animations inside of the same registered animation, however, the function input arguments are a bit different:
415
+ -
416
+ - ```html
417
+
418
+ ```
419
+
420
+ - <div ng-class="color" class="colorful">
421
+ - this box is moody
422
+ - </div>
423
+ - <button ng-click="color='red'">Change to red</button>
424
+ - <button ng-click="color='blue'">Change to blue</button>
425
+ - <button ng-click="color='green'">Change to green</button>
426
+ - ```
427
+
428
+ ```
429
+
430
+ -
431
+ - ```js
432
+
433
+ ```
434
+
435
+ - myModule.animation('.colorful', [function() {
436
+ - return {
437
+ - addClass: function(element, className, doneFn) {
438
+ - // do some cool animation and call the doneFn
439
+ - },
440
+ - removeClass: function(element, className, doneFn) {
441
+ - // do some cool animation and call the doneFn
442
+ - },
443
+ - setClass: function(element, addedClass, removedClass, doneFn) {
444
+ - // do some cool animation and call the doneFn
445
+ - }
446
+ - }
447
+ - }]);
448
+ - ```
449
+
450
+ ```
451
+
452
+ -
453
+ - ## CSS + JS Animations Together
454
+ -
455
+ - AngularJS 1.4 and higher has taken steps to make the amalgamation of CSS and JS animations more flexible. However, unlike earlier versions of AngularJS,
456
+ - 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
457
+ - charge of the animation\*\*:
458
+ -
459
+ - ```html
460
+
461
+ ```
462
+
463
+ - <div ng-if="bool" class="slide">
464
+ - Slide in and out
465
+ - </div>
466
+ - ```
467
+
468
+ ```
469
+
470
+ -
471
+ - ```js
472
+
473
+ ```
474
+
475
+ - myModule.animation('.slide', [function() {
476
+ - return {
477
+ - enter: function(element, doneFn) {
478
+ - jQuery(element).slideIn(1000, doneFn);
479
+ - }
480
+ - }
481
+ - }]);
482
+ - ```
483
+
484
+ ```
485
+
486
+ -
487
+ - ```css
488
+
489
+ ```
490
+
491
+ - .slide.ng-enter {
492
+ - transition:0.5s linear all;
493
+ - transform:translateY(-100px);
494
+ - }
495
+ - .slide.ng-enter.ng-enter-active {
496
+ - transform:translateY(0);
497
+ - }
498
+ - ```
499
+
500
+ ```
501
+
502
+ -
503
+ - 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
504
+ - lack of CSS animations by using the `$animateCss` service to trigger our own tweaked-out, CSS-based animations directly from
505
+ - our own JS-based animation code:
506
+ -
507
+ - ```js
508
+
509
+ ```
510
+
511
+ - myModule.animation('.slide', ['$animateCss', function($animateCss) {
512
+ - return {
513
+ - enter: function(element) {
514
+ - // this will trigger `.slide.ng-enter` and `.slide.ng-enter-active`.
515
+ - return $animateCss(element, {
516
+ - event: 'enter',
517
+ - structural: true
518
+ - });
519
+ - }
520
+ - }
521
+ - }]);
522
+ - ```
523
+
524
+ ```
525
+
526
+ -
527
+ - 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.
528
+ -
529
+ - 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
530
+ - 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
531
+ - data into `$animateCss` directly:
532
+ -
533
+ - ```js
534
+
535
+ ```
536
+
537
+ - myModule.animation('.slide', ['$animateCss', function($animateCss) {
538
+ - return {
539
+ - enter: function(element) {
540
+ - return $animateCss(element, {
541
+ - event: 'enter',
542
+ - structural: true,
543
+ - addClass: 'maroon-setting',
544
+ - from: { height:0 },
545
+ - to: { height: 200 }
546
+ - });
547
+ - }
548
+ - }
549
+ - }]);
550
+ - ```
551
+
552
+ ```
553
+
554
+ -
555
+ - Now we can fill in the rest via our transition CSS code:
556
+ -
557
+ - ```css
558
+
559
+ ```
560
+
561
+ - /\* the transition tells ng.animate to make the animation happen \*/
562
+ - .slide.ng-enter { transition:0.5s linear all; }
563
+ -
564
+ - /\* this extra CSS class will be absorbed into the transition
565
+ - since the $animateCss code is adding the class \*/
566
+ - .maroon-setting { background:red; }
567
+ - ```
568
+
569
+ ```
570
+
571
+ -
572
+ - 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.
573
+ -
574
+ - To learn more about what's possible be sure to visit the {@link ng.animate.$animateCss $animateCss service}.
575
+ -
576
+ - ## Animation Anchoring (via `ng-animate-ref`)
577
+ -
578
+ - ng.animate in AngularJS 1.4 comes packed with the ability to cross-animate elements between
579
+ - structural areas of an application (like views) by pairing up elements using an attribute
580
+ - called `ng-animate-ref`.
581
+ -
582
+ - Let's say for example we have two views that are managed by `ng-view` and we want to show
583
+ - that there is a relationship between two components situated in within these views. By using the
584
+ - `ng-animate-ref` attribute we can identify that the two components are paired together and we
585
+ - can then attach an animation, which is triggered when the view changes.
586
+ -
587
+ - Say for example we have the following template code:
588
+ -
589
+ - ```html
590
+
591
+ ```
592
+
593
+ - <!-- index.html -->
594
+ - <div ng-view class="view-animation">
595
+ - </div>
596
+ -
597
+ - <!-- home.html -->
598
+ - <a href="#/banner-page">
599
+ - <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
600
+ - </a>
601
+ -
602
+ - <!-- banner-page.html -->
603
+ - <img src="./banner.jpg" class="banner" ng-animate-ref="banner">
604
+ - ```
605
+
606
+ ```
607
+
608
+ -
609
+ - Now, when the view changes (once the link is clicked), ng.animate will examine the
610
+ - HTML contents to see if there is a match reference between any components in the view
611
+ - that is leaving and the view that is entering. It will scan both the view which is being
612
+ - removed (leave) and inserted (enter) to see if there are any paired DOM elements that
613
+ - contain a matching ref value.
614
+ -
615
+ - The two images match since they share the same ref value. ng.animate will now create a
616
+ - transport element (which is a clone of the first image element) and it will then attempt
617
+ - to animate to the position of the second image element in the next view. For the animation to
618
+ - work a special CSS class called `ng-anchor` will be added to the transported element.
619
+ -
620
+ - We can now attach a transition onto the `.banner.ng-anchor` CSS class and then
621
+ - ng.animate will handle the entire transition for us as well as the addition and removal of
622
+ - any changes of CSS classes between the elements:
623
+ -
624
+ - ```css
625
+
626
+ ```
627
+
628
+ - .banner.ng-anchor {
629
+ - /\* this animation will last for 1 second since there are
630
+ - two phases to the animation (an `in` and an `out` phase) &#42;/
631
+ - transition:0.5s linear all;
632
+ - }
633
+ - ```
634
+
635
+ ```
636
+
637
+ -
638
+ - We also **must** include animations for the views that are being entered and removed
639
+ - (otherwise anchoring wouldn't be possible since the new view would be inserted right away).
640
+ -
641
+ - ```css
642
+
643
+ ```
644
+
645
+ - .view-animation.ng-enter, .view-animation.ng-leave {
646
+ - transition:0.5s linear all;
647
+ - position:fixed;
648
+ - left:0;
649
+ - top:0;
650
+ - width:100%;
651
+ - }
652
+ - .view-animation.ng-enter {
653
+ - transform:translateX(100%);
654
+ - }
655
+ - .view-animation.ng-leave,
656
+ - .view-animation.ng-enter.ng-enter-active {
657
+ - transform:translateX(0%);
658
+ - }
659
+ - .view-animation.ng-leave.ng-leave-active {
660
+ - transform:translateX(-100%);
661
+ - }
662
+ - ```
663
+
664
+ ```
665
+
666
+ -
667
+ - Now we can jump back to the anchor animation. When the animation happens, there are two stages that occur:
668
+ - an `out` and an `in` stage. The `out` stage happens first and that is when the element is animated away
669
+ - from its origin. Once that animation is over then the `in` stage occurs which animates the
670
+ - element to its destination. The reason why there are two animations is to give enough time
671
+ - for the enter animation on the new element to be ready.
672
+ -
673
+ - The example above sets up a transition for both the in and out phases, but we can also target the out or
674
+ - in phases directly via `ng-anchor-out` and `ng-anchor-in`.
675
+ -
676
+ - ```css
677
+
678
+ ```
679
+
680
+ - .banner.ng-anchor-out {
681
+ - transition: 0.5s linear all;
682
+ -
683
+ - /\* the scale will be applied during the out animation,
684
+ - but will be animated away when the in animation runs &#42;/
685
+ - transform: scale(1.2);
686
+ - }
687
+ -
688
+ - .banner.ng-anchor-in {
689
+ - transition: 1s linear all;
690
+ - }
691
+ - ```
692
+
693
+ ```
694
+
695
+ -
696
+ -
697
+ -
698
+ -
699
+ - ### Anchoring Demo
700
+ - <example module="anchoringExample"
701
+ name="anchoringExample"
702
+ id="anchoringExample"
703
+ deps="angular-animate.js;angular-route.js"
704
+ animations="true">
705
+ <file name="index.html">
706
+ <a href="#!/">Home</a>
707
+ <hr />
708
+ <div class="view-container">
709
+ <div ng-view class="view"></div>
710
+ </div>
711
+ </file>
712
+ <file name="script.js">
713
+ angular.module('anchoringExample', ['ng.animate', 'ngRoute'])
714
+ .config(['$routeProvider', function($routeProvider) {
715
+ $routeProvider.when('/', {
716
+ templateUrl: 'home.html',
717
+ controller: 'HomeController as home'
718
+ });
719
+ $routeProvider.when('/profile/:id', {
720
+ templateUrl: 'profile.html',
721
+ controller: 'ProfileController as profile'
722
+ });
723
+ }])
724
+ .run(['$rootScope', function($rootScope) {
725
+ $rootScope.records = [
726
+ { id: 1, title: 'Miss Beulah Roob' },
727
+ { id: 2, title: 'Trent Morissette' },
728
+ { id: 3, title: 'Miss Ava Pouros' },
729
+ { id: 4, title: 'Rod Pouros' },
730
+ { id: 5, title: 'Abdul Rice' },
731
+ { id: 6, title: 'Laurie Rutherford Sr.' },
732
+ { id: 7, title: 'Nakia McLaughlin' },
733
+ { id: 8, title: 'Jordon Blanda DVM' },
734
+ { id: 9, title: 'Rhoda Hand' },
735
+ { id: 10, title: 'Alexandrea Sauer' }
736
+ ];
737
+ }])
738
+ .controller('HomeController', [function() {
739
+ //empty
740
+ }])
741
+ .controller('ProfileController', ['$rootScope', '$routeParams',
742
+ function ProfileController($rootScope, $routeParams) {
743
+ let index = parseInt($routeParams.id, 10);
744
+ let record = $rootScope.records[index - 1];
745
+
746
+ this.title = record.title;
747
+ this.id = record.id;
748
+ }]);
749
+
750
+ </file>
751
+ <file name="home.html">
752
+ <h2>Welcome to the home page</h1>
753
+ <p>Please click on an element</p>
754
+ <a class="record"
755
+ ng-href="#!/profile/{{ record.id }}"
756
+ ng-animate-ref="{{ record.id }}"
757
+ ng-repeat="record in records">
758
+ {{ record.title }}
759
+ </a>
760
+ </file>
761
+ <file name="profile.html">
762
+ <div class="profile record" ng-animate-ref="{{ profile.id }}">
763
+ {{ profile.title }}
764
+ </div>
765
+ </file>
766
+ <file name="animations.css">
767
+ .record {
768
+ display:block;
769
+ font-size:20px;
770
+ }
771
+ .profile {
772
+ background:black;
773
+ color:white;
774
+ font-size:100px;
775
+ }
776
+ .view-container {
777
+ position:relative;
778
+ }
779
+ .view-container > .view.ng-animate {
780
+ position:absolute;
781
+ top:0;
782
+ left:0;
783
+ width:100%;
784
+ min-height:500px;
785
+ }
786
+ .view.ng-enter, .view.ng-leave,
787
+ .record.ng-anchor {
788
+ transition:0.5s linear all;
789
+ }
790
+ .view.ng-enter {
791
+ transform:translateX(100%);
792
+ }
793
+ .view.ng-enter.ng-enter-active, .view.ng-leave {
794
+ transform:translateX(0%);
795
+ }
796
+ .view.ng-leave.ng-leave-active {
797
+ transform:translateX(-100%);
798
+ }
799
+ .record.ng-anchor-out {
800
+ background:red;
801
+ }
802
+ </file>
803
+ </example>
804
+
805
+ -
806
+ - ### How is the element transported?
807
+ -
808
+ - When an anchor animation occurs, ng.animate will clone the starting element and position it exactly where the starting
809
+ - element is located on screen via absolute positioning. The cloned element will be placed inside of the root element
810
+ - of the application (where ng-app was defined) and all of the CSS classes of the starting element will be applied. The
811
+ - element will then animate into the `out` and `in` animations and will eventually reach the coordinates and match
812
+ - the dimensions of the destination element. During the entire animation a CSS class of `.ng-animate-shim` will be applied
813
+ - to both the starting and destination elements in order to hide them from being visible (the CSS styling for the class
814
+ - is: `visibility:hidden`). Once the anchor reaches its destination then it will be removed and the destination element
815
+ - will become visible since the shim class will be removed.
816
+ -
817
+ - ### How is the morphing handled?
818
+ -
819
+ - CSS Anchoring relies on transitions and keyframes and the internal code is intelligent enough to figure out
820
+ - what CSS classes differ between the starting element and the destination element. These different CSS classes
821
+ - will be added/removed on the anchor element and a transition will be applied (the transition that is provided
822
+ - in the anchor class). Long story short, ng.animate will figure out what classes to add and remove which will
823
+ - make the transition of the element as smooth and automatic as possible. Be sure to use simple CSS classes that
824
+ - do not rely on DOM nesting structure so that the anchor element appears the same as the starting element (since
825
+ - the cloned element is placed inside of root element which is likely close to the body element).
826
+ -
827
+ - Note that if the root element is on the `<html>` element then the cloned node will be placed inside of body.
828
+ -
829
+ -
830
+ - ## Using $animate in your directive code
831
+ -
832
+ - So far we've explored how to feed in animations into an AngularJS application, but how do we trigger animations within our own directives in our application?
833
+ - 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
834
+ - imagine we have a greeting box that shows and hides itself when the data changes
835
+ -
836
+ - ```html
837
+
838
+ ```
839
+
840
+ - <greeting-box active="onOrOff">Hi there</greeting-box>
841
+ - ```
842
+
843
+ ```
844
+
845
+ -
846
+ - ```js
847
+
848
+ ```
849
+
850
+ - ngModule.directive('greetingBox', ['$animate', function($animate) {
851
+ - return function(scope, element, attrs) {
852
+ - attrs.$observe('active', function(value) {
853
+ - value ? $animate.addClass(element, 'on') : $animate.removeClass(element, 'on');
854
+ - });
855
+ - });
856
+ - }]);
857
+ - ```
858
+
859
+ ```
860
+
861
+ -
862
+ - 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
863
+ - in our HTML code then we can trigger a CSS or JS animation to happen.
864
+ -
865
+ - ```css
866
+
867
+ ```
868
+
869
+ - /\* normally we would create a CSS class to reference on the element \*/
870
+ - greeting-box.on { transition:0.5s linear all; background:green; color:white; }
871
+ - ```
872
+
873
+ ```
874
+
875
+ -
876
+ - The `$animate` service contains a variety of other methods like `enter`, `leave`, `animate` and `setClass`. To learn more about what's
877
+ - possible be sure to visit the {@link ng.$animate $animate service API page}.
878
+ -
879
+ -
880
+ - ## Callbacks and Promises
881
+ -
882
+ - 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
883
+ - an animation (within our directive code) then we can continue performing directive and scope related activities after the animation has
884
+ - ended by chaining onto the returned promise that animation method returns.
885
+ -
886
+ - ```js
887
+
888
+ ```
889
+
890
+ - // somewhere within the depths of the directive
891
+ - $animate.enter(element, parent).then(function() {
892
+ - //the animation has completed
893
+ - });
894
+ - ```
895
+
896
+ ```
897
+
898
+ -
899
+ - (Note that earlier versions of AngularJS prior to v1.4 required the promise code to be wrapped using `$scope.$apply(...)`. This is not the case
900
+ - anymore.)
901
+ -
902
+ - In addition to the animation promise, we can also make use of animation-related callbacks within our directives and controller code by registering
903
+ - an event listener using the `$animate` service. Let's say for example that an animation was triggered on our view
904
+ - routing controller to hook into that:
905
+ -
906
+ - ```js
907
+
908
+ ```
909
+
910
+ - ngModule.controller('HomePageController', ['$animate', function($animate) {
911
+ - $animate.on('enter', ngViewElement, function(element) {
912
+ - // the animation for this route has completed
913
+ - }]);
914
+ - }])
915
+ - ```
916
+
917
+ ```
918
+
919
+ -
920
+ - (Note that you will need to trigger a digest within the callback to get AngularJS to notice any scope-related changes.)
921
+ \*/
922
+
923
+ /\*\*
924
+
925
+ - @ngdoc service
926
+ - @name $animate
927
+ - @kind object
928
+ -
929
+ - @description
930
+ - The ng.animate `$animate` service documentation is the same for the core `$animate` service.
931
+ -
932
+ - Click here {@link ng.$animate to learn more about animations with `$animate`}.
933
+ \*/