forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition.js
226 lines (200 loc) · 5.67 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
var endEvents = sniffEndEvents(),
config = require('./config'),
// batch enter animations so we only force the layout once
Batcher = require('./batcher'),
batcher = new Batcher(),
// cache timer functions
setTO = window.setTimeout,
clearTO = window.clearTimeout,
// exit codes for testing
codes = {
CSS_E : 1,
CSS_L : 2,
JS_E : 3,
JS_L : 4,
CSS_SKIP : -1,
JS_SKIP : -2,
JS_SKIP_E : -3,
JS_SKIP_L : -4,
INIT : -5,
SKIP : -6
}
// force layout before triggering transitions/animations
batcher._preFlush = function () {
/* jshint unused: false */
var f = document.body.offsetHeight
}
/**
* stage:
* 1 = enter
* 2 = leave
*/
var transition = module.exports = function (el, stage, cb, compiler) {
var changeState = function () {
cb()
compiler.execHook(stage > 0 ? 'attached' : 'detached')
}
if (compiler.init) {
changeState()
return codes.INIT
}
var hasTransition = el.vue_trans === '',
hasAnimation = el.vue_anim === '',
effectId = el.vue_effect
if (effectId) {
return applyTransitionFunctions(
el,
stage,
changeState,
effectId,
compiler
)
} else if (hasTransition || hasAnimation) {
return applyTransitionClass(
el,
stage,
changeState,
hasAnimation
)
} else {
changeState()
return codes.SKIP
}
}
transition.codes = codes
/**
* Togggle a CSS class to trigger transition
*/
function applyTransitionClass (el, stage, changeState, hasAnimation) {
if (!endEvents.trans) {
changeState()
return codes.CSS_SKIP
}
// if the browser supports transition,
// it must have classList...
var onEnd,
classList = el.classList,
existingCallback = el.vue_trans_cb,
enterClass = config.enterClass,
leaveClass = config.leaveClass,
endEvent = hasAnimation ? endEvents.anim : endEvents.trans
// cancel unfinished callbacks and jobs
if (existingCallback) {
el.removeEventListener(endEvent, existingCallback)
classList.remove(enterClass)
classList.remove(leaveClass)
el.vue_trans_cb = null
}
if (stage > 0) { // enter
// set to enter state before appending
classList.add(enterClass)
// append
changeState()
// trigger transition
if (!hasAnimation) {
batcher.push({
execute: function () {
classList.remove(enterClass)
}
})
} else {
onEnd = function (e) {
if (e.target === el) {
el.removeEventListener(endEvent, onEnd)
el.vue_trans_cb = null
classList.remove(enterClass)
}
}
el.addEventListener(endEvent, onEnd)
el.vue_trans_cb = onEnd
}
return codes.CSS_E
} else { // leave
if (el.offsetWidth || el.offsetHeight) {
// trigger hide transition
classList.add(leaveClass)
onEnd = function (e) {
if (e.target === el) {
el.removeEventListener(endEvent, onEnd)
el.vue_trans_cb = null
// actually remove node here
changeState()
classList.remove(leaveClass)
}
}
// attach transition end listener
el.addEventListener(endEvent, onEnd)
el.vue_trans_cb = onEnd
} else {
// directly remove invisible elements
changeState()
}
return codes.CSS_L
}
}
function applyTransitionFunctions (el, stage, changeState, effectId, compiler) {
var funcs = compiler.getOption('effects', effectId)
if (!funcs) {
changeState()
return codes.JS_SKIP
}
var enter = funcs.enter,
leave = funcs.leave,
timeouts = el.vue_timeouts
// clear previous timeouts
if (timeouts) {
var i = timeouts.length
while (i--) {
clearTO(timeouts[i])
}
}
timeouts = el.vue_timeouts = []
function timeout (cb, delay) {
var id = setTO(function () {
cb()
timeouts.splice(timeouts.indexOf(id), 1)
if (!timeouts.length) {
el.vue_timeouts = null
}
}, delay)
timeouts.push(id)
}
if (stage > 0) { // enter
if (typeof enter !== 'function') {
changeState()
return codes.JS_SKIP_E
}
enter(el, changeState, timeout)
return codes.JS_E
} else { // leave
if (typeof leave !== 'function') {
changeState()
return codes.JS_SKIP_L
}
leave(el, changeState, timeout)
return codes.JS_L
}
}
/**
* Sniff proper transition end event name
*/
function sniffEndEvents () {
var el = document.createElement('vue'),
defaultEvent = 'transitionend',
events = {
'transition' : defaultEvent,
'mozTransition' : defaultEvent,
'webkitTransition' : 'webkitTransitionEnd'
},
ret = {}
for (var name in events) {
if (el.style[name] !== undefined) {
ret.trans = events[name]
break
}
}
ret.anim = el.style.animation === ''
? 'animationend'
: 'webkitAnimationEnd'
return ret
}