-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathriotAnimate.js
144 lines (109 loc) · 4.26 KB
/
riotAnimate.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
/**
* Animate Riot v2 Components
* @author Sartaj
* @contributor Nate Chapman
* @license MIT 2015
*/
/**
* The Animate Mixin
*/
var OptsAnimateMixin = {
init: function() {
this.on('mount', function() {
createAnimation.call(this, 'enter');
var animationEnded = function(e) {
var item = e.target;
if (this.enterClasses) {
this.enterClasses.forEach(function(classToAnimate) {
item.classList.remove(classToAnimate);
});
}
if (this.leaveClasses) {
this.leaveClasses.forEach(function(classToAnimate) {
item.classList.remove(classToAnimate);
});
}
// remove animate.css class
item.classList.remove('animated');
// Remove all other classes
item.classList.remove('riot-enter-active');
item.classList.remove('riot-enter');
item.classList.remove('riot-animate');
// Remove all other classes
item.classList.remove('riot-leave-active');
item.classList.remove('riot-leave');
item.classList.remove('riot-animate');
var delay = item.getAttribute('animate-delay');
if (delay) {
item.style.animationDelay = '';
item.style.transitionDelay = '';
}
var duration = item.getAttribute('animate-duration');
if (duration) {
item.style.animationDuration = '';
item.style.transitionDuration = '';
}
}.bind(this)
// After animation has ended
this.root.addEventListener("transitionend", animationEnded, false);
this.root.addEventListener("animationend", animationEnded, false);
});
},
animatedUnmount: function(keepParent) {
createAnimation.call(this, 'leave');
var waitFor = 1;
var unmount = function(e) {
e.target.style.visibility = 'hidden';
if (waitFor === this.animatedDomElements.length) {
this.unmount(keepParent);
} else {
waitFor++;
}
}.bind(this)
// After animation has ended
this.root.addEventListener("transitionend", unmount, false);
this.root.addEventListener("animationend", unmount, false)
}
};
function createAnimation(ANIMATION_NAME) {
// Get attributes you want to animate
this.animatedDomElements = this.root.querySelectorAll('[animate]');
for (var x = 0; x < this.animatedDomElements.length; x++) {
// Item to add animations to
var item = this.animatedDomElements[x];
// Get attribute string
var attributeToAnimate = item.getAttribute('animate-' + ANIMATION_NAME);
if (!attributeToAnimate) {
var attributeToAnimate = item.getAttribute('animate');
}
// Get all space separated classes
this.enterClasses = attributeToAnimate.split(' ');
// Add those classes to this item
this.enterClasses.forEach(function(classToAnimate) {
item.classList.add(classToAnimate);
});
var delay = item.getAttribute('animate-delay');
if (delay) {
item.style.animationDelay = delay;
item.style.transitionDelay = delay;
}
var duration = item.getAttribute('animate-duration');
if (duration) {
item.style.animationDuration = duration;
item.style.transitionDuration = duration;
}
// Add initial enter animation class
item.classList.add('riot-' + ANIMATION_NAME);
}
// Add animation for enter after 1 frame
window.requestAnimationFrame(function() {
for (var x = 0; x < this.animatedDomElements.length; x++) {
var item = this.animatedDomElements[x];
// animate.css support
item.classList.add('animated');
item.classList.add('riot-animate');
item.classList.add('riot-' + ANIMATION_NAME + '-active');
}
}.bind(this));
}
module.exports = OptsAnimateMixin;