This repository was archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAutoAnimations.js
176 lines (150 loc) · 6.6 KB
/
AutoAnimations.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
/*
AUTO ANIMATIONS
Automatically animate all inserted and removed DOM elements with good defaults:
- Slide down/up for elements with display=block & position=static
- Fade in/out for all others
- Elements with existing CSS transitions or animations will be left alone
- Uses velocity.js to assure smooth 60fps animations
USAGE: Just include this script and add an animation duration to your css, e.g.
* { animation-duration: .2s; }
EXCEPTIONS: If you don't want some element to automatically animate (e.g. external libraries) just set its animation-duration to 0s, e.g. :
.SomeCustomComponent, .SomeCustomComponent * {animation-duration: 0s; }
*/
(function(){
// Configuration needed by AutoAnimations. Put it in the CSS instead
//document.styleSheets[0].insertRule("* {animation-duration: .5s}", 0);
"use strict";
//already loaded exit
if (HTMLElement.prototype._insertBefore) {
return;
}
// load Velocity.js if needed
var velocityURL = "//cdn.rawgit.com/julianshapiro/velocity/1.2.3/velocity.min.js";
if (window.Velocity || (window.$ && $.Velocity)) {
AutoAnimations();
} else if (window.require){
require([velocityURL], function (Velocity) {
AutoAnimations();
});
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", velocityURL);
xmlhttp.onreadystatechange = function(){
if ((xmlhttp.status == 200) && (xmlhttp.readyState == 4)) {
eval(xmlhttp.responseText);
AutoAnimations();
}};
xmlhttp.send();
}
function AutoAnimations() {
var Velocity = window.Velocity || $.Velocity;
// Override native methods (yup, this is hard core)
HTMLElement.prototype._appendChild = HTMLElement.prototype.appendChild;
HTMLElement.prototype.appendChild = function() {
var result = this._appendChild.apply( this, arguments );
showElement.apply( this, arguments );
return result;
};
HTMLElement.prototype._insertBefore = HTMLElement.prototype.insertBefore;
HTMLElement.prototype.insertBefore = function() {
var result = this._insertBefore.apply( this, arguments );
showElement.apply( this, arguments );
return result;
};
HTMLElement.prototype._replaceChild = HTMLElement.prototype.replaceChild;
HTMLElement.prototype.replaceChild = function() {
return swapElements.apply( this, arguments );
};
HTMLElement.prototype._removeChild = HTMLElement.prototype.removeChild;
HTMLElement.prototype.removeChild = function() {
hideElement.apply( this, arguments );
};
function showElement(node) {
toggleElement(node, true);
}
function hideElement(node) {
if (isAnimating(node)) {
// probably we're inside a removechild cycle
if (node.nextElementSibling) {
do {
node = node.nextElementSibling;
} while (node.nextElementSibling && isAnimating(node));
}
if (isAnimating(node)) {
if (node.previousElementSibling) {
do {
node = node.previousElementSibling;
} while (node.previousElementSibling && isAnimating(node));
}
}
}
if (!isAnimating(node)) {
toggleElement(node, false, function(){
if (node.parentNode) {
node.parentNode._removeChild(node);
}
});
}
}
function isAnimating(node) {
var velocityData = Velocity.Utilities.data(node);
return velocityData && velocityData.velocity && velocityData.velocity.isAnimating;
}
function swapElements(newChild, oldChild) {
// REACT uses noscript often
if (oldChild.tagName == "NOSCRIPT") {
//treat this as a show
oldChild.parentNode._replaceChild(newChild, oldChild);
toggleElement(newChild, true);
} else if (newChild.tagName == "NOSCRIPT"){
//treat this as a hide
toggleElement(oldChild, false, function() {
if(oldChild.parentNode)
oldChild.parentNode._replaceChild(newChild, oldChild);
});
} else {
oldChild.parentNode._replaceChild(newChild, oldChild);
}
}
function toggleElement(node, show, onComplete) {
// Do not animate some elements
if (!node.parentNode || node.parentElement.dataset.reactid == ".0"
|| node.parentElement.tagName == "HEAD" || node.parentNode.parentNode == null
|| !(node instanceof HTMLElement)) {
if (onComplete)
onComplete();
return;
}
var style = window.getComputedStyle(node);
// Only animate elements with animation-duration that do not have any
// other animations or transitions properties set
if (style.animationDuration == "0s" || style.transform != "none"
|| style.animationName != "none" || style.transitionDuration != "0s" ) {
if (onComplete)
onComplete();
return;
}
var animationProperties = {
duration: parseFloat(style.animationDuration) * 1000,
complete: onComplete,
display: style.display //keep display value
};
//inline, inline-block fixed and absolute will fade instead of slide
if (style.position != "static" || style.float != "none" || style.display.indexOf("inline") != -1) {
if (show) {
node.style.display = 'none';
Velocity.Redirects.fadeIn(node, animationProperties);
} else {
Velocity.Redirects.fadeOut(node, animationProperties);
}
} else {
if (show) {
node.style.display = 'none';
Velocity.Redirects.slideDown(node, animationProperties);
} else {
Velocity.Redirects.slideUp(node, animationProperties);
}
}
}
}
})();