-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition.js
308 lines (281 loc) · 7.53 KB
/
transition.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var _ = require('../util')
var queue = require('./queue')
var addClass = _.addClass
var removeClass = _.removeClass
var transitionEndEvent = _.transitionEndEvent
var animationEndEvent = _.animationEndEvent
var transDurationProp = _.transitionProp + 'Duration'
var animDurationProp = _.animationProp + 'Duration'
var TYPE_TRANSITION = 1
var TYPE_ANIMATION = 2
/**
* A Transition object that encapsulates the state and logic
* of the transition.
*
* @param {Element} el
* @param {String} id
* @param {Object} hooks
* @param {Vue} vm
*/
function Transition (el, id, hooks, vm) {
this.el = el
this.enterClass = id + '-enter'
this.leaveClass = id + '-leave'
this.hooks = hooks
this.vm = vm
// async state
this.pendingCssEvent =
this.pendingCssCb =
this.cancel =
this.pendingJsCb =
this.op =
this.cb = null
this.typeCache = {}
// bind
var self = this
;['enterNextTick', 'enterDone', 'leaveNextTick', 'leaveDone']
.forEach(function (m) {
self[m] = _.bind(self[m], self)
})
}
var p = Transition.prototype
/**
* Start an entering transition.
*
* 1. enter transition triggered
* 2. call beforeEnter hook
* 3. add enter class
* 4. insert/show element
* 5. call enter hook (with possible explicit js callback)
* 6. reflow
* 7. based on transition type:
* - transition:
* remove class now, wait for transitionend,
* then done if there's no explicit js callback.
* - animation:
* wait for animationend, remove class,
* then done if there's no explicit js callback.
* - no css transition:
* done now if there's no explicit js callback.
* 8. wait for either done or js callback, then call
* afterEnter hook.
*
* @param {Function} op - insert/show the element
* @param {Function} [cb]
*/
p.enter = function (op, cb) {
this.cancelPending()
this.callHook('beforeEnter')
this.cb = cb
addClass(this.el, this.enterClass)
op()
this.callHookWithCb('enter')
this.cancel = this.hooks && this.hooks.enterCancelled
queue.push(this.enterNextTick)
}
/**
* The "nextTick" phase of an entering transition, which is
* to be pushed into a queue and executed after a reflow so
* that removing the class can trigger a CSS transition.
*/
p.enterNextTick = function () {
var type = this.getCssTransitionType(this.enterClass)
var enterDone = this.enterDone
if (type === TYPE_TRANSITION) {
// trigger transition by removing enter class now
removeClass(this.el, this.enterClass)
this.setupCssCb(transitionEndEvent, enterDone)
} else if (type === TYPE_ANIMATION) {
this.setupCssCb(animationEndEvent, enterDone)
} else if (!this.pendingJsCb) {
enterDone()
}
}
/**
* The "cleanup" phase of an entering transition.
*/
p.enterDone = function () {
this.cancel = this.pendingJsCb = null
removeClass(this.el, this.enterClass)
this.callHook('afterEnter')
if (this.cb) this.cb()
}
/**
* Start a leaving transition.
*
* 1. leave transition triggered.
* 2. call beforeLeave hook
* 3. add leave class (trigger css transition)
* 4. call leave hook (with possible explicit js callback)
* 5. reflow if no explicit js callback is provided
* 6. based on transition type:
* - transition or animation:
* wait for end event, remove class, then done if
* there's no explicit js callback.
* - no css transition:
* done if there's no explicit js callback.
* 7. wait for either done or js callback, then call
* afterLeave hook.
*
* @param {Function} op - remove/hide the element
* @param {Function} [cb]
*/
p.leave = function (op, cb) {
this.cancelPending()
this.callHook('beforeLeave')
this.op = op
this.cb = cb
addClass(this.el, this.leaveClass)
this.callHookWithCb('leave')
this.cancel = this.hooks && this.hooks.enterCancelled
// only need to do leaveNextTick if there's no explicit
// js callback
if (!this.pendingJsCb) {
queue.push(this.leaveNextTick)
}
}
/**
* The "nextTick" phase of a leaving transition.
*/
p.leaveNextTick = function () {
var type = this.getCssTransitionType(this.leaveClass)
if (type) {
var event = type === TYPE_TRANSITION
? transitionEndEvent
: animationEndEvent
this.setupCssCb(event, this.leaveDone)
} else {
this.leaveDone()
}
}
/**
* The "cleanup" phase of a leaving transition.
*/
p.leaveDone = function () {
this.cancel = this.pendingJsCb = null
this.op()
removeClass(this.el, this.leaveClass)
this.callHook('afterLeave')
if (this.cb) this.cb()
}
/**
* Cancel any pending callbacks from a previously running
* but not finished transition.
*/
p.cancelPending = function () {
this.op = this.cb = null
var hasPending = false
if (this.pendingCssCb) {
hasPending = true
_.off(this.el, this.pendingCssEvent, this.pendingCssCb)
this.pendingCssEvent = this.pendingCssCb = null
}
if (this.pendingJsCb) {
hasPending = true
this.pendingJsCb.cancel()
this.pendingJsCb = null
}
if (hasPending) {
removeClass(this.el, this.enterClass)
removeClass(this.el, this.leaveClass)
}
if (this.cancel) {
this.cancel.call(this.vm, this.el)
this.cancel = null
}
}
/**
* Call a user-provided synchronous hook function.
*
* @param {String} type
*/
p.callHook = function (type) {
if (this.hooks && this.hooks[type]) {
this.hooks[type].call(this.vm, this.el)
}
}
/**
* Call a user-provided, potentially-async hook function.
* We check for the length of arguments to see if the hook
* expects a `done` callback. If true, the transition's end
* will be determined by when the user calls that callback;
* otherwise, the end is determined by the CSS transition or
* animation.
*
* @param {String} type
*/
p.callHookWithCb = function (type) {
var hook = this.hooks && this.hooks[type]
if (hook) {
if (hook.length > 1) {
this.pendingJsCb = _.cancellable(this[type + 'Done'])
}
hook.call(this.vm, this.el, this.pendingJsCb)
}
}
/**
* Get an element's transition type based on the
* calculated styles.
*
* @param {String} className
* @return {Number}
*/
p.getCssTransitionType = function (className) {
/* istanbul ignore if */
if (
!transitionEndEvent ||
// skip CSS transitions if page is not visible -
// this solves the issue of transitionend events not
// firing until the page is visible again.
// pageVisibility API is supported in IE10+, same as
// CSS transitions.
document.hidden ||
// explicit js-only transition
(this.hooks && this.hooks.css === false)
) {
return
}
var type = this.typeCache[className]
if (type) return type
var inlineStyles = this.el.style
var computedStyles = window.getComputedStyle(this.el)
var transDuration =
inlineStyles[transDurationProp] ||
computedStyles[transDurationProp]
if (transDuration && transDuration !== '0s') {
type = TYPE_TRANSITION
} else {
var animDuration =
inlineStyles[animDurationProp] ||
computedStyles[animDurationProp]
if (animDuration && animDuration !== '0s') {
type = TYPE_ANIMATION
}
}
if (type) {
this.typeCache[className] = type
}
return type
}
/**
* Setup a CSS transitionend/animationend callback.
*
* @param {String} event
* @param {Function} cb
*/
p.setupCssCb = function (event, cb) {
this.pendingCssEvent = event
var self = this
var el = this.el
var onEnd = this.pendingCssCb = function (e) {
if (e.target === el) {
_.off(el, event, onEnd)
self.pendingCssEvent = self.pendingCssCb = null
if (!self.pendingJsCb && cb) {
cb()
}
}
}
_.on(el, event, onEnd)
}
module.exports = Transition