-
Notifications
You must be signed in to change notification settings - Fork 0
/
rl-velocity.js
105 lines (77 loc) · 3.02 KB
/
rl-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
/*! rl-velocity - v0.3.2 - 2014-07-10
* https://github.com/rosslavery/rl-velocity
* Copyright (c) 2014 Ross Lavery <[email protected]>; License: MIT */
(function(angular) {
'use strict';
var Container = (window.jQuery || window.Zepto || window);
if (!Container.Velocity || !Container.Velocity.Utilities || !Container.Velocity.RegisterUI) {
throw new Error('Velocity UI Pack: Velocity must be loaded first. Aborting.');
}
Container.rlVelocity = angular.module('rl.velocity', ['ngAnimate'])
.constant('rlVelocityConfig', {
duration: 300
})
.factory('VelocityUtils', ['rlVelocityConfig', function(rlVelocityConfig) {
return {
_parseClassList: function(classList) {
var parsedOptions = {};
angular.forEach(classList, function(currClass) {
if (currClass.indexOf('velocity-duration-') > -1) {
parsedOptions.duration = currClass.split('velocity-duration-')[1];
}
});
return parsedOptions;
},
_createAngularAnimation: function(animation) {
var self = this;
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');
}
return {
enter: self._createVelocityAnimation(animation),
leave: self._createVelocityAnimation(opp),
move: self._createVelocityAnimation(animation),
beforeAddClass: self._createVelocityClassAnimation(opp),
removeClass: self._createVelocityClassAnimation(animation)
};
},
_createVelocityAnimation: function(animation) {
var self = this;
return function($el, done) {
var parsedOptions = self._parseClassList($el[0].classList);
var options = angular.extend(rlVelocityConfig, parsedOptions);
Container.Velocity.animate($el, animation, options).then(done);
};
},
_createVelocityClassAnimation: function(animation) {
var self = this;
return function ($el, className, done) {
var parsedOptions = self._parseClassList($el[0].classList);
var options = angular.extend(rlVelocityConfig, parsedOptions);
if (className === 'ng-hide') {
Container.Velocity.animate($el, animation, options).then(done);
}
else {
done();
}
};
}
};
}]);
// Convert animation name to class name
function _getClassName(animation) {
return '.velocity-' + animation.replace('.', '-');
}
// Iterate through the packaged effects to register them as Angular animations
var className;
angular.forEach(Container.Velocity.RegisterUI.packagedEffects, function(animationProps, animationName) {
className = _getClassName(animationName);
Container.rlVelocity.animation(className, ['VelocityUtils', function(VelocityUtils) {
return VelocityUtils._createAngularAnimation(animationName);
}]);
});
})(angular);