-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathController.js
142 lines (130 loc) · 4.36 KB
/
Controller.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
export default class Controller {
constructor(
animationSource,
options,
target,
onNeedReattachAllAninmations,
remove
) {
this._animationSource = animationSource
this._target = target
this._options = options
this._onNeedReattachAllAninmations = onNeedReattachAllAninmations
this._remove = remove
this._commandsWaitingForAttach = []
}
//Not documented. For internal usage. (animachine)
replaceAnimationSource(animationSource) {
if (this._gsapAnimation) {
this._gsapAnimation.kill()
this._gsapAnimation = undefined
this._animationSource = animationSource
this._onNeedReattachAllAninmations()
}
else {//it's not attached yet
this._animationSource = animationSource
}
}
attach() {
if (this._gsapAnimation) {
let time = this._gsapAnimation.time()
let paused = this._gsapAnimation.paused()
let reversed = this._gsapAnimation.reversed()
this._gsapAnimation
.invalidate()
.restart(false, true) //suppress events
.time(time, true) //suppress events - http://greensock.com/docs/#/HTML5/GSAP/TimelineMax/time/
if (paused) {
this._gsapAnimation.pause(null, true)//suppress events
}
if (reversed) {
this._gsapAnimation.reverse(null, true)//suppress events
}
}
else {
this._gsapAnimation = this._animationSource({
target: this._target,
options: this._options,
})
if (__DEV__) {
if (
!this._gsapAnimation ||
typeof this._gsapAnimation.play !== 'function'
) {
throw Error(`[react-gsap-enhancer] The return value of the animation `
+ `source doesn't seems to be a GSAP Animation`
+ `\nCheck out this animation source: \n${this._animationSource}`
+ `\nbecause it returned this value: ${this._gsapAnimation}`
+ `\n\n`
+ `If you're using something like TweenMax.staggerTo() witch returns`
+ ` an array of GSAP Animations please use Timeline (like`
+ ` TimelineMax.staggerTo()) instead. It has the same effect`
+ ` but returns one object.`)
}
}
}
this._commandsWaitingForAttach
.splice(0)
.forEach(({fnName, args}) => this[fnName](...args))
}
kill() {
if (this._gsapAnimation) {
this._gsapAnimation.kill()
}
this._remove(this)
}
}
const EXPOSED_METHODS = [
'currentLabel', 'delay', 'duration', 'endTime', 'eventCallback', 'from',
'fromTo', 'getLabelAfter', 'getLabelArray', 'getLabelBefore', 'getLabelTime',
'invalidate', 'isActive', 'pause', 'paused', 'play', 'progress', 'restart',
'resume', 'reverse', 'reversed', 'seek', 'startTime', 'time', 'timeScale',
'totalDuration', 'totalProgress', 'totalTime', 'tweenFromTo', 'tweenTo'
]
const ONLY_GETTER_METHODS = [
'delay',
'duration',
'startTime',
'totalDuration',
'totalProgress',
'totalTime',
'endTime'
]
function bindAPI() {
EXPOSED_METHODS
//remove duplications
.filter((item, pos, arr) => arr.indexOf(item) === pos)
.forEach(fnName => {
Controller.prototype[fnName] = function (...args) {
let result
const onlyGetter = ONLY_GETTER_METHODS.indexOf(fnName) !== -1
if (!this._gsapAnimation) {
//if the animation doesn't attached yet, schedule the API call
this._commandsWaitingForAttach.push({fnName, args})
}
else if (typeof this._gsapAnimation[fnName] === 'function') {
if (__DEV__) {
if (onlyGetter && args.length !== 0) {
console.warn(
`[react-gsap-enhancer] controller.${fnName} is only a getter `
+ `but it looks like you tried to use as a getter by calling `
+ `it with the following arguments: "${args}"`
)
}
}
result = onlyGetter
? this._gsapAnimation[fnName]()
: this._gsapAnimation[fnName](...args)
}
else {
throw Error(
`[react-gsap-enhancer] Animation source has no method: '${fnName}.'`
+ `\nYou maybe tryed to use an only TweenMax method on TweenLite instance`
+ `\nCheck GSAP docs for more detailes: http://greensock.com/docs/#/HTML5/GSAP/`
)
}
return result === this._gsapAnimation ? this : result
}
})
}
bindAPI()