-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationMixin.jsx
More file actions
93 lines (85 loc) · 3.63 KB
/
AnimationMixin.jsx
File metadata and controls
93 lines (85 loc) · 3.63 KB
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
/*jslint node:true,esnext:true*/
'use strict';
var React = require('react');
var $ = require('jquery');
var TICK = 17,
TRANSITION_TIME = .3;
module.exports = {
getInitialState: function () {
return {
animating: false,
setupTimeout: -1
};
},
componentWillReceiveProps: function (nextProps) {
var willAnimate = true,
oldStyle,
currentStyle,
i,
setupTimeout;
if (this.state.animating) {
if (this.ignoreAnimationInterruptions) {
return;
}
if (this.animationInterrupted) {
// If we were interrupted before calling setupAnimation, we do not
// want to call that twice, so cancel the waiting one
clearTimeout(this.state.setupTimeout);
// We do not want to call the completed callback for the interrupted
// animation, since it did not complete
this.refs.animatedElement.getDOMNode().
removeEventListener(this.transitionEndCallback);
oldStyle = $.extend({}, this.refs.animatedElement.props.style);
oldStyle.transition = 'none';
this.setState({animatedElementStyle: oldStyle});
// Collect the current style animation, so the interrupted handler
// can set whatever it needs to appropriately.
currentStyle = {};
for (i = 0; i < this.state.stylePropsToAnimate.length; i = i + 1) {
// jQuery happens to use getComputedStyle, which gets the actual
// style rather than the final style for the transition
currentStyle[this.state.stylePropsToAnimate[i]] =
$(this.refs.animatedElement.getDOMNode()).
css(this.state.stylePropsToAnimate[i]);
// stateToSet[this.state.stylePropsToAnimate[i]] =
// getComputedStyle(this.refs.animatedElement.getDOMNode(),
// null).getPropertyValue(this.state.stylePropsToAnimate[i]);
}
this.animationInterrupted(nextProps, currentStyle);
}
} else {
if (this.componentWillAnimate) {
// Allow the animation to be cancelled
willAnimate = !this.componentWillAnimate(nextProps);
}
}
if (willAnimate) {
// Save the timeout so it can be cancelled if need be
setupTimeout = setTimeout(this.setupAnimation, TICK);
this.setState({animating: true, setupTimeout: setupTimeout});
this.refs.animatedElement.getDOMNode().
addEventListener('transitionend', this.transitionEndCallback);
}
},
transitionEndCallback: function () {
var oldStyle = $.extend({}, this.refs.animatedElement.props.style);
oldStyle.transition = 'none';
this.setState({
animatedElementStyle: oldStyle,
animating: false
});
if (this.animationCompleted) {
this.animationCompleted();
}
this.refs.animatedElement.getDOMNode().
removeEventListener('transitionend', this.transitionEndCallback);
},
setupAnimation: function () {
var oldStyle = $.extend({}, this.refs.animatedElement.props.style),
newTransition = this.state.stylePropsToAnimate.concat('').
join(' ' + TRANSITION_TIME + 's ease,').slice(0, -1);
oldStyle.transition = newTransition;
this.setState({animatedElementStyle: oldStyle});
this.componentAnimationIsReady();
}
};