-
Notifications
You must be signed in to change notification settings - Fork 14
/
angular-velocity.js
278 lines (225 loc) · 7.7 KB
/
angular-velocity.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
(function () {
'use strict';
var CLASS_ANIM_ADD = 0,
CLASS_ANIM_REMOVE = 1,
Velocity = window.Velocity || (window.jQuery && jQuery.Velocity);
// Check we have velocity and the UI pack
if (!Velocity || !Velocity.RegisterEffect) {
throw "Velocity and Velocity UI Pack plugin required, please include the relevant JS files. Get Velocity with: bower install velocity";
}
// Utility to normalize the UI pack transition name
function normalizeTransitionName(animation) {
return animation.replace('.', '-');
}
// Utility to create class name for a sequence
function animationToClassName(animation) {
return 'velocity-' + normalizeTransitionName(animation);
}
// Utility to create an opposites class name for a sequence
function animationToOppositesClassName(animation) {
return 'velocity-opposites-' + normalizeTransitionName(animation);
}
// Utility to create an enter transition class name
function animationToEnterClassName(animation) {
return 'velocity-enter-' + normalizeTransitionName(animation);
}
// Utility to create a leave transition class name
function animationToLeaveClassName(animation) {
return 'velocity-leave-' + normalizeTransitionName(animation);
}
// Utility to parse out velocity options
function getVelocityOpts($parse, $el, done) {
var optsAttrVal = $el.attr('data-velocity-opts'),
scope = $el.scope(),
opts = {
complete: done
},
userOpts;
if (optsAttrVal) {
userOpts = $parse(optsAttrVal)(scope);
angular.extend(opts, userOpts);
if (userOpts.begin) {
opts.begin = function () {
var args = Array.prototype.slice.call(arguments, 0);
scope.$apply(function () {
userOpts.begin.apply(args[0], args);
});
};
}
if (userOpts.complete) {
opts.complete = function () {
var args = Array.prototype.slice.call(arguments, 0);
scope.$apply(function () {
userOpts.complete.apply(args[0], args);
});
scope = null;
done();
};
}
}
return opts;
}
// Utility for queueing animations together.
// Staggering isn't natively supported by ngAnimate for JS animations yet, so
// we use the workaround from: http://www.yearofmoo.com/2013/12/staggering-animations-in-angularjs.html
function newAnimationQueue($timeout) {
var queue = {
enter: [],
leave: []
};
return function queueAllAnimations(event, element, done, onComplete) {
var eventQueue = queue[event];
eventQueue.push({
element : element,
done : done
});
eventQueue.timer && $timeout.cancel(eventQueue.timer);
eventQueue.timer = $timeout(function() {
var elms = [],
doneFns = [],
onDone;
angular.forEach(eventQueue, function(item) {
item && elms.push(item.element);
doneFns.push(item.done);
});
onDone = function() {
angular.forEach(doneFns, function(fn) {
fn();
});
};
onComplete(elms, onDone);
queue[event] = [];
}, 10, false);
return function() {
queue[event] = [];
};
};
}
function makeCancelFunctionFor($el) {
return function (cancel) {
if (cancel) {
Velocity($el, 'stop');
}
};
}
// Factory for the angular animation effect function (move animations)
function makeAnimFor(animation, $parse) {
return function ($el, done) {
var opts = getVelocityOpts($parse, $el, done);
Velocity($el, animation, opts);
return makeCancelFunctionFor($el);
};
}
// Factory for the angular animation effect function (class animations (ng-hide))
function makeClassAnimFor(addOrRemove, addAnim, removeAnim, $parse) {
return function ($el, className, done) {
var animation = addAnim,
opts;
if ('ng-hide' === className) {
animation = (addOrRemove === CLASS_ANIM_ADD) ? removeAnim : animation;
opts = getVelocityOpts($parse, $el, done);
Velocity($el, animation, opts);
return makeCancelFunctionFor($el);
}
};
}
// Factory for the angular animation effect function (grouped animations)
function makeGroupedAnimFor(animation, event, queueFn, $parse) {
return function ($el, done) {
var cancel;
// Hide element so it doesn't briefly show whilst queue is built
if (event === 'enter') {
$el.css('opacity', 0);
}
cancel = queueFn(event, $el[0], done, function(elements, onQueueDone) {
var opts = getVelocityOpts($parse, $el, onQueueDone);
Velocity(elements, animation, opts);
});
return function onClose(cancelled) {
cancelled && cancel();
};
};
}
// Factory for making animations
function makeAngularAnimationFor(animation) {
return ['$timeout', '$parse', function ($timeout, $parse) {
var queueFn = newAnimationQueue($timeout);
return {
'enter': makeGroupedAnimFor(animation, 'enter', queueFn, $parse),
'leave': makeGroupedAnimFor(animation, 'leave', queueFn, $parse),
'move': makeAnimFor(animation, $parse),
'addClass': makeClassAnimFor(CLASS_ANIM_ADD, animation, animation, $parse),
'removeClass': makeClassAnimFor(CLASS_ANIM_REMOVE, animation, animation, $parse)
};
}];
}
// Factory for making enter/show animations
function makeEnterAnimationFor(animation) {
return ['$timeout', '$parse', function ($timeout, $parse) {
var queueFn = newAnimationQueue($timeout);
return {
'enter': makeGroupedAnimFor(animation, 'enter', queueFn, $parse),
'removeClass': makeClassAnimFor(CLASS_ANIM_REMOVE, animation, animation, $parse)
};
}];
}
// Factory for making leave/hide animations
function makeLeaveAnimationFor(animation) {
return ['$timeout', '$parse', function ($timeout, $parse) {
var queueFn = newAnimationQueue($timeout);
return {
'leave': makeGroupedAnimFor(animation, 'leave', queueFn, $parse),
'addClass': makeClassAnimFor(CLASS_ANIM_ADD, animation, animation, $parse)
};
}];
}
// Factory for making opposite animations
function makeOppositesAnimationFor(animation) {
return ['$timeout', '$parse', function ($timeout, $parse) {
var queueFn = newAnimationQueue($timeout);
var opp = animation.replace('In', 'Out');
if (opp.indexOf('Down') > -1) {
opp = opp.replace('Down', 'Up');
}
else if (opp.indexOf('Up') > -1) {
opp = opp.replace('Up', 'Down');
}
else if (opp.indexOf('Left') > -1) {
opp = opp.replace('Left', 'Right');
}
else if (opp.indexOf('Right') > -1) {
opp = opp.replace('Right', 'Left');
}
return {
'enter': makeGroupedAnimFor(animation, 'enter', queueFn, $parse),
'leave': makeGroupedAnimFor(opp, 'leave', queueFn, $parse),
'move': makeAnimFor(animation, $parse),
'addClass': makeClassAnimFor(CLASS_ANIM_ADD, animation, opp, $parse),
'removeClass': makeClassAnimFor(CLASS_ANIM_REMOVE, animation, opp, $parse)
};
}];
}
// Use the factories to define animations for all velocity's sequences
angular
.module('angular-velocity', ['ngAnimate'])
.config(['$animateProvider', function ($animateProvider) {
// Use the factories to define animations for all velocity's sequences
angular.forEach(Velocity.Redirects, function (_, animation) {
var IN = 'In',
OUT = 'Out',
selector = '.' + animationToClassName(animation),
oppositesSelector = '.' + animationToOppositesClassName(animation),
enterSelector, leaveSelector;
$animateProvider.register(selector, makeAngularAnimationFor(animation));
if (animation.substr(-2) === IN) {
enterSelector = '.' + animationToEnterClassName(animation);
$animateProvider.register(oppositesSelector, makeOppositesAnimationFor(animation));
$animateProvider.register(enterSelector, makeEnterAnimationFor(animation));
}
else if (animation.substr(-3) === OUT) {
leaveSelector = '.' + animationToLeaveClassName(animation);
$animateProvider.register(leaveSelector, makeLeaveAnimationFor(animation));
}
});
}]);
})();