forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.js
399 lines (335 loc) · 12.7 KB
/
animation.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
'use strict';
var $$AnimationProvider = ['$animateProvider', function($animateProvider) {
var NG_ANIMATE_REF_ATTR = 'ng-animate-ref';
var drivers = this.drivers = [];
var RUNNER_STORAGE_KEY = '$$animationRunner';
function setRunner(element, runner) {
element.data(RUNNER_STORAGE_KEY, runner);
}
function removeRunner(element) {
element.removeData(RUNNER_STORAGE_KEY);
}
function getRunner(element) {
return element.data(RUNNER_STORAGE_KEY);
}
this.$get = ['$$jqLite', '$rootScope', '$injector', '$$AnimateRunner', '$$HashMap', '$$rAFScheduler',
function($$jqLite, $rootScope, $injector, $$AnimateRunner, $$HashMap, $$rAFScheduler) {
var animationQueue = [];
var applyAnimationClasses = applyAnimationClassesFactory($$jqLite);
function sortAnimations(animations) {
var tree = { children: [] };
var i, lookup = new $$HashMap();
// this is done first beforehand so that the hashmap
// is filled with a list of the elements that will be animated
for (i = 0; i < animations.length; i++) {
var animation = animations[i];
lookup.put(animation.domNode, animations[i] = {
domNode: animation.domNode,
fn: animation.fn,
children: []
});
}
for (i = 0; i < animations.length; i++) {
processNode(animations[i]);
}
return flatten(tree);
function processNode(entry) {
if (entry.processed) return entry;
entry.processed = true;
var elementNode = entry.domNode;
var parentNode = elementNode.parentNode;
lookup.put(elementNode, entry);
var parentEntry;
while (parentNode) {
parentEntry = lookup.get(parentNode);
if (parentEntry) {
if (!parentEntry.processed) {
parentEntry = processNode(parentEntry);
}
break;
}
parentNode = parentNode.parentNode;
}
(parentEntry || tree).children.push(entry);
return entry;
}
function flatten(tree) {
var result = [];
var queue = [];
var i;
for (i = 0; i < tree.children.length; i++) {
queue.push(tree.children[i]);
}
var remainingLevelEntries = queue.length;
var nextLevelEntries = 0;
var row = [];
for (i = 0; i < queue.length; i++) {
var entry = queue[i];
if (remainingLevelEntries <= 0) {
remainingLevelEntries = nextLevelEntries;
nextLevelEntries = 0;
result.push(row);
row = [];
}
row.push(entry.fn);
entry.children.forEach(function(childEntry) {
nextLevelEntries++;
queue.push(childEntry);
});
remainingLevelEntries--;
}
if (row.length) {
result.push(row);
}
return result;
}
}
// TODO(matsko): document the signature in a better way
return function(element, event, options) {
options = prepareAnimationOptions(options);
var isStructural = ['enter', 'move', 'leave'].indexOf(event) >= 0;
// there is no animation at the current moment, however
// these runner methods will get later updated with the
// methods leading into the driver's end/cancel methods
// for now they just stop the animation from starting
var runner = new $$AnimateRunner({
end: function() { close(); },
cancel: function() { close(true); }
});
if (!drivers.length) {
close();
return runner;
}
setRunner(element, runner);
var classes = mergeClasses(element.attr('class'), mergeClasses(options.addClass, options.removeClass));
var tempClasses = options.tempClasses;
if (tempClasses) {
classes += ' ' + tempClasses;
options.tempClasses = null;
}
animationQueue.push({
// this data is used by the postDigest code and passed into
// the driver step function
element: element,
classes: classes,
event: event,
structural: isStructural,
options: options,
beforeStart: beforeStart,
close: close
});
element.on('$destroy', handleDestroyedElement);
// we only want there to be one function called within the post digest
// block. This way we can group animations for all the animations that
// were apart of the same postDigest flush call.
if (animationQueue.length > 1) return runner;
$rootScope.$$postDigest(function() {
var animations = [];
forEach(animationQueue, function(entry) {
// the element was destroyed early on which removed the runner
// form its storage. This means we can't animate this element
// at all and it already has been closed due to destruction.
if (getRunner(entry.element)) {
animations.push(entry);
} else {
entry.close();
}
});
// now any future animations will be in another postDigest
animationQueue.length = 0;
var groupedAnimations = groupAnimations(animations);
var toBeSortedAnimations = [];
forEach(groupedAnimations, function(animationEntry) {
toBeSortedAnimations.push({
domNode: getDomNode(animationEntry.from ? animationEntry.from.element : animationEntry.element),
fn: function triggerAnimationStart() {
// it's important that we apply the `ng-animate` CSS class and the
// temporary classes before we do any driver invoking since these
// CSS classes may be required for proper CSS detection.
animationEntry.beforeStart();
var startAnimationFn, closeFn = animationEntry.close;
// in the event that the element was removed before the digest runs or
// during the RAF sequencing then we should not trigger the animation.
var targetElement = animationEntry.anchors
? (animationEntry.from.element || animationEntry.to.element)
: animationEntry.element;
if (getRunner(targetElement)) {
var operation = invokeFirstDriver(animationEntry);
if (operation) {
startAnimationFn = operation.start;
}
}
if (!startAnimationFn) {
closeFn();
} else {
var animationRunner = startAnimationFn();
animationRunner.done(function(status) {
closeFn(!status);
});
updateAnimationRunners(animationEntry, animationRunner);
}
}
});
});
// we need to sort each of the animations in order of parent to child
// relationships. This ensures that the child classes are applied at the
// right time.
$$rAFScheduler(sortAnimations(toBeSortedAnimations));
});
return runner;
// TODO(matsko): change to reference nodes
function getAnchorNodes(node) {
var SELECTOR = '[' + NG_ANIMATE_REF_ATTR + ']';
var items = node.hasAttribute(NG_ANIMATE_REF_ATTR)
? [node]
: node.querySelectorAll(SELECTOR);
var anchors = [];
forEach(items, function(node) {
var attr = node.getAttribute(NG_ANIMATE_REF_ATTR);
if (attr && attr.length) {
anchors.push(node);
}
});
return anchors;
}
function groupAnimations(animations) {
var preparedAnimations = [];
var refLookup = {};
forEach(animations, function(animation, index) {
var element = animation.element;
var node = getDomNode(element);
var event = animation.event;
var enterOrMove = ['enter', 'move'].indexOf(event) >= 0;
var anchorNodes = animation.structural ? getAnchorNodes(node) : [];
if (anchorNodes.length) {
var direction = enterOrMove ? 'to' : 'from';
forEach(anchorNodes, function(anchor) {
var key = anchor.getAttribute(NG_ANIMATE_REF_ATTR);
refLookup[key] = refLookup[key] || {};
refLookup[key][direction] = {
animationID: index,
element: jqLite(anchor)
};
});
} else {
preparedAnimations.push(animation);
}
});
var usedIndicesLookup = {};
var anchorGroups = {};
forEach(refLookup, function(operations, key) {
var from = operations.from;
var to = operations.to;
if (!from || !to) {
// only one of these is set therefore we can't have an
// anchor animation since all three pieces are required
var index = from ? from.animationID : to.animationID;
var indexKey = index.toString();
if (!usedIndicesLookup[indexKey]) {
usedIndicesLookup[indexKey] = true;
preparedAnimations.push(animations[index]);
}
return;
}
var fromAnimation = animations[from.animationID];
var toAnimation = animations[to.animationID];
var lookupKey = from.animationID.toString();
if (!anchorGroups[lookupKey]) {
var group = anchorGroups[lookupKey] = {
structural: true,
beforeStart: function() {
fromAnimation.beforeStart();
toAnimation.beforeStart();
},
close: function() {
fromAnimation.close();
toAnimation.close();
},
classes: cssClassesIntersection(fromAnimation.classes, toAnimation.classes),
from: fromAnimation,
to: toAnimation,
anchors: [] // TODO(matsko): change to reference nodes
};
// the anchor animations require that the from and to elements both have at least
// one shared CSS class which effictively marries the two elements together to use
// the same animation driver and to properly sequence the anchor animation.
if (group.classes.length) {
preparedAnimations.push(group);
} else {
preparedAnimations.push(fromAnimation);
preparedAnimations.push(toAnimation);
}
}
anchorGroups[lookupKey].anchors.push({
'out': from.element, 'in': to.element
});
});
return preparedAnimations;
}
function cssClassesIntersection(a,b) {
a = a.split(' ');
b = b.split(' ');
var matches = [];
for (var i = 0; i < a.length; i++) {
var aa = a[i];
if (aa.substring(0,3) === 'ng-') continue;
for (var j = 0; j < b.length; j++) {
if (aa === b[j]) {
matches.push(aa);
break;
}
}
}
return matches.join(' ');
}
function invokeFirstDriver(animationDetails) {
// we loop in reverse order since the more general drivers (like CSS and JS)
// may attempt more elements, but custom drivers are more particular
for (var i = drivers.length - 1; i >= 0; i--) {
var driverName = drivers[i];
if (!$injector.has(driverName)) continue; // TODO(matsko): remove this check
var factory = $injector.get(driverName);
var driver = factory(animationDetails);
if (driver) {
return driver;
}
}
}
function beforeStart() {
element.addClass(NG_ANIMATE_CLASSNAME);
if (tempClasses) {
$$jqLite.addClass(element, tempClasses);
}
}
function updateAnimationRunners(animation, newRunner) {
if (animation.from && animation.to) {
update(animation.from.element);
update(animation.to.element);
} else {
update(animation.element);
}
function update(element) {
getRunner(element).setHost(newRunner);
}
}
function handleDestroyedElement() {
var runner = getRunner(element);
if (runner && (event !== 'leave' || !options.$$domOperationFired)) {
runner.end();
}
}
function close(rejected) { // jshint ignore:line
element.off('$destroy', handleDestroyedElement);
removeRunner(element);
applyAnimationClasses(element, options);
applyAnimationStyles(element, options);
options.domOperation();
if (tempClasses) {
$$jqLite.removeClass(element, tempClasses);
}
element.removeClass(NG_ANIMATE_CLASSNAME);
runner.complete(!rejected);
}
};
}];
}];