transition.js
7.3 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { warn } from 'core/util/debug'
import { extend, once, noop } from 'shared/util'
import { activeInstance } from 'core/instance/lifecycle'
import { resolveTransition } from 'web/runtime/transition-util'
export default {
create: enter,
activate: enter,
remove: leave
}
function enter (_, vnode) {
const el = vnode.elm
// call leave callback now
if (el._leaveCb) {
el._leaveCb.cancelled = true
el._leaveCb()
}
const data = resolveTransition(vnode.data.transition)
if (!data) {
return
}
/* istanbul ignore if */
if (el._enterCb) {
return
}
const {
enterClass,
enterToClass,
enterActiveClass,
appearClass,
appearToClass,
appearActiveClass,
beforeEnter,
enter,
afterEnter,
enterCancelled,
beforeAppear,
appear,
afterAppear,
appearCancelled
} = data
let context = activeInstance
let transitionNode = activeInstance.$vnode
while (transitionNode && transitionNode.parent) {
transitionNode = transitionNode.parent
context = transitionNode.context
}
const isAppear = !context._isMounted || !vnode.isRootInsert
if (isAppear && !appear && appear !== '') {
return
}
const startClass = isAppear ? appearClass : enterClass
const toClass = isAppear ? appearToClass : enterToClass
const activeClass = isAppear ? appearActiveClass : enterActiveClass
const beforeEnterHook = isAppear ? (beforeAppear || beforeEnter) : beforeEnter
const enterHook = isAppear ? (typeof appear === 'function' ? appear : enter) : enter
const afterEnterHook = isAppear ? (afterAppear || afterEnter) : afterEnter
const enterCancelledHook = isAppear ? (appearCancelled || enterCancelled) : enterCancelled
const userWantsControl =
enterHook &&
// enterHook may be a bound method which exposes
// the length of original fn as _length
(enterHook._length || enterHook.length) > 1
const stylesheet = vnode.context.$options.style || {}
const startState = stylesheet[startClass]
const transitionProperties = (stylesheet['@TRANSITION'] && stylesheet['@TRANSITION'][activeClass]) || {}
const endState = getEnterTargetState(el, stylesheet, startClass, toClass, activeClass)
const needAnimation = Object.keys(endState).length > 0
const cb = el._enterCb = once(() => {
if (cb.cancelled) {
enterCancelledHook && enterCancelledHook(el)
} else {
afterEnterHook && afterEnterHook(el)
}
el._enterCb = null
})
// We need to wait until the native element has been inserted, but currently
// there's no API to do that. So we have to wait "one frame" - not entirely
// sure if this is guaranteed to be enough (e.g. on slow devices?)
setTimeout(() => {
const parent = el.parentNode
const pendingNode = parent && parent._pending && parent._pending[vnode.key]
if (pendingNode &&
pendingNode.context === vnode.context &&
pendingNode.tag === vnode.tag &&
pendingNode.elm._leaveCb
) {
pendingNode.elm._leaveCb()
}
enterHook && enterHook(el, cb)
if (needAnimation) {
const animation = vnode.context.$requireWeexModule('animation')
animation.transition(el.ref, {
styles: endState,
duration: transitionProperties.duration || 0,
delay: transitionProperties.delay || 0,
timingFunction: transitionProperties.timingFunction || 'linear'
}, userWantsControl ? noop : cb)
} else if (!userWantsControl) {
cb()
}
}, 16)
// start enter transition
beforeEnterHook && beforeEnterHook(el)
if (startState) {
if (typeof el.setStyles === 'function') {
el.setStyles(startState)
} else {
for (const key in startState) {
el.setStyle(key, startState[key])
}
}
}
if (!needAnimation && !userWantsControl) {
cb()
}
}
function leave (vnode, rm) {
const el = vnode.elm
// call enter callback now
if (el._enterCb) {
el._enterCb.cancelled = true
el._enterCb()
}
const data = resolveTransition(vnode.data.transition)
if (!data) {
return rm()
}
if (el._leaveCb) {
return
}
const {
leaveClass,
leaveToClass,
leaveActiveClass,
beforeLeave,
leave,
afterLeave,
leaveCancelled,
delayLeave
} = data
const userWantsControl =
leave &&
// leave hook may be a bound method which exposes
// the length of original fn as _length
(leave._length || leave.length) > 1
const stylesheet = vnode.context.$options.style || {}
const startState = stylesheet[leaveClass]
const endState = stylesheet[leaveToClass] || stylesheet[leaveActiveClass]
const transitionProperties = (stylesheet['@TRANSITION'] && stylesheet['@TRANSITION'][leaveActiveClass]) || {}
const cb = el._leaveCb = once(() => {
if (el.parentNode && el.parentNode._pending) {
el.parentNode._pending[vnode.key] = null
}
if (cb.cancelled) {
leaveCancelled && leaveCancelled(el)
} else {
rm()
afterLeave && afterLeave(el)
}
el._leaveCb = null
})
if (delayLeave) {
delayLeave(performLeave)
} else {
performLeave()
}
function performLeave () {
const animation = vnode.context.$requireWeexModule('animation')
// the delayed leave may have already been cancelled
if (cb.cancelled) {
return
}
// record leaving element
if (!vnode.data.show) {
(el.parentNode._pending || (el.parentNode._pending = {}))[vnode.key] = vnode
}
beforeLeave && beforeLeave(el)
if (startState) {
animation.transition(el.ref, {
styles: startState
}, next)
} else {
next()
}
function next () {
animation.transition(el.ref, {
styles: endState,
duration: transitionProperties.duration || 0,
delay: transitionProperties.delay || 0,
timingFunction: transitionProperties.timingFunction || 'linear'
}, userWantsControl ? noop : cb)
}
leave && leave(el, cb)
if (!endState && !userWantsControl) {
cb()
}
}
}
// determine the target animation style for an entering transition.
function getEnterTargetState (el, stylesheet, startClass, endClass, activeClass) {
const targetState = {}
const startState = stylesheet[startClass]
const endState = stylesheet[endClass]
const activeState = stylesheet[activeClass]
// 1. fallback to element's default styling
if (startState) {
for (const key in startState) {
targetState[key] = el.style[key]
if (
process.env.NODE_ENV !== 'production' &&
targetState[key] == null &&
(!activeState || activeState[key] == null) &&
(!endState || endState[key] == null)
) {
warn(
`transition property "${key}" is declared in enter starting class (.${startClass}), ` +
`but not declared anywhere in enter ending class (.${endClass}), ` +
`enter active cass (.${activeClass}) or the element's default styling. ` +
`Note in Weex, CSS properties need explicit values to be transitionable.`
)
}
}
}
// 2. if state is mixed in active state, extract them while excluding
// transition properties
if (activeState) {
for (const key in activeState) {
if (key.indexOf('transition') !== 0) {
targetState[key] = activeState[key]
}
}
}
// 3. explicit endState has highest priority
if (endState) {
extend(targetState, endState)
}
return targetState
}