| 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
309
310
311
312
313
314
315
316
317 |
1×
7×
7×
7×
7×
7×
7×
6×
6×
6×
6×
6×
1×
19×
19×
18×
18×
18×
18×
2×
2×
16×
16×
16×
1×
1×
15×
15×
15×
1×
1×
1×
14×
15×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
4×
4×
4×
4×
4×
4×
4×
1×
16×
16×
1×
15×
1×
1×
1×
19×
19×
2×
2×
1×
1×
3×
3×
14×
14×
14×
14×
1×
14×
14×
14×
5×
5×
2×
2×
5×
5×
5×
5×
5×
14×
14×
10×
1×
4×
4×
4×
4×
2×
2×
2×
2×
4×
4×
4×
4×
4×
4×
4×
4×
1×
16×
16×
16×
3×
3×
3×
3×
3×
3×
3×
3×
13×
13×
1×
5×
5×
1×
4×
4×
8×
1×
1×
1×
1×
6×
6×
1×
6×
6×
6×
6×
| import { avalon, window, Cache } from '../seed/core'
import { cssDiff } from '../directives/css'
import {
css3,
animation,
transition,
animationEndEvent,
transitionEndEvent
} from './detect'
var effectDir = avalon.directive('effect', {
priority: 5,
diff: function (effect) {
var vdom = this.node
Iif (typeof effect === 'string') {
this.value = effect = {
is: effect
}
avalon.warn('ms-effect的指令值不再支持字符串,必须是一个对象')
}
this.value = vdom.effect = effect
var ok = cssDiff.call(this, effect, this.oldValue)
var me = this
if (ok) {
setTimeout(function () {
vdom.animating = true
effectDir.update.call(me, vdom, vdom.effect)
})
vdom.animating = false
return true
}
return false
},
update: function (vdom, change, opts) {
var dom = vdom.dom
if (dom && dom.nodeType === 1) {
//要求配置对象必须指定is属性,action必须是布尔或enter,leave,move
var option = change || opts
var is = option.is
var globalOption = avalon.effects[is]
if (!globalOption) {//如果没有定义特效
avalon.warn(is + ' effect is undefined')
return
}
var finalOption = {}
var action = actionMaps[option.action]
if (typeof Effect.prototype[action] !== 'function') {
avalon.warn('action is undefined')
return
}
//必须预定义特效
var effect = new avalon.Effect(dom)
avalon.mix(finalOption, globalOption, option, { action })
if (finalOption.queue) {
animationQueue.push(function () {
effect[action](finalOption)
})
callNextAnimation()
} else {
effect[action](finalOption)
}
return true
}
}
})
let move = 'move'
let leave = 'leave'
let enter = 'enter'
var actionMaps = {
'true': enter,
'false': leave,
enter,
leave,
move,
'undefined': enter
}
var animationQueue = []
export function callNextAnimation() {
var fn = animationQueue[0]
Eif (fn) {
fn()
}
}
avalon.effects = {}
avalon.effect = function (name, opts) {
var definition = avalon.effects[name] = (opts || {})
Eif (css3 && definition.css !== false) {
patchObject(definition, 'enterClass', name + '-enter')
patchObject(definition, 'enterActiveClass', definition.enterClass + '-active')
patchObject(definition, 'leaveClass', name + '-leave')
patchObject(definition, 'leaveActiveClass', definition.leaveClass + '-active')
}
return definition
}
function patchObject(obj, name, value) {
Eif (!obj[name]) {
obj[name] = value
}
}
var Effect = function (dom) {
this.dom = dom
}
avalon.Effect = Effect
Effect.prototype = {
enter: createAction('Enter'),
leave: createAction('Leave'),
move: createAction('Move')
}
function execHooks(options, name, el) {
var fns = [].concat(options[name])
for (var i = 0, fn; fn = fns[i++];) {
Eif (typeof fn === 'function') {
fn(el)
}
}
}
var staggerCache = new Cache(128)
function createAction(action) {
var lower = action.toLowerCase()
return function (option) {
var dom = this.dom
var elem = avalon(dom)
//处理与ms-for指令相关的stagger
//========BEGIN=====
var staggerTime = isFinite(option.stagger) ? option.stagger * 1000 : 0
if (staggerTime) {
Iif (option.staggerKey) {
var stagger = staggerCache.get(option.staggerKey) ||
staggerCache.put(option.staggerKey, {
count: 0,
items: 0
})
stagger.count++
stagger.items++
}
}
var staggerIndex = stagger && stagger.count || 0
//=======END==========
var stopAnimationID
var animationDone = function (e) {
var isOk = e !== false
if (--dom.__ms_effect_ === 0) {
avalon.unbind(dom, transitionEndEvent)
avalon.unbind(dom, animationEndEvent)
}
clearTimeout(stopAnimationID)
var dirWord = isOk ? 'Done' : 'Abort'
execHooks(option, 'on' + action + dirWord, dom)
Iif (stagger) {
if (--stagger.items === 0) {
stagger.count = 0
}
}
Iif (option.queue) {
animationQueue.shift()
callNextAnimation()
}
}
//执行开始前的钩子
execHooks(option, 'onBefore' + action, dom)
if (option[lower]) {
//使用JS方式执行动画
option[lower](dom, function (ok) {
animationDone(ok !== false)
})
} else Eif (css3) {
//使用CSS3方式执行动画
elem.addClass(option[lower + 'Class'])
elem.removeClass(getNeedRemoved(option, lower))
if (!dom.__ms_effect_) {
//绑定动画结束事件
elem.bind(transitionEndEvent, animationDone)
elem.bind(animationEndEvent, animationDone)
dom.__ms_effect_ = 1
} else {
dom.__ms_effect_++
}
setTimeout(function () {
//用xxx-active代替xxx类名的方式 触发CSS3动画
var time = avalon.root.offsetWidth === NaN
elem.addClass(option[lower + 'ActiveClass'])
//计算动画时长
time = getAnimationTime(dom)
Iif (!time === 0) {
//立即结束动画
animationDone(false)
} else Eif (!staggerTime) {
//如果动画超出时长还没有调用结束事件,这可能是元素被移除了
//如果强制结束动画
stopAnimationID = setTimeout(function () {
animationDone(false)
}, time + 32)
}
}, 17 + staggerTime * staggerIndex)// = 1000/60
}
}
}
avalon.applyEffect = function (dom, vdom, opts) {
var cb = opts.cb
var curEffect = vdom.effect
if (curEffect && dom && dom.nodeType === 1) {
var hook = opts.hook
var old = curEffect[hook]
Eif (cb) {
Iif (Array.isArray(old)) {
old.push(cb)
} else Iif (old) {
curEffect[hook] = [old, cb]
} else {
curEffect[hook] = [cb]
}
}
getAction(opts)
avalon.directives.effect.update(vdom, curEffect, avalon.shadowCopy({}, opts))
} else Eif (cb) {
cb(dom)
}
}
/**
* 获取方向
*/
export function getAction(opts) {
Eif (!opts.action) {
return opts.action = opts.hook.replace(/^on/, '').replace(/Done$/, '').toLowerCase()
}
}
/**
* 需要移除的类名
*/
function getNeedRemoved(options, name) {
var name = name === 'leave' ? 'enter' : 'leave'
return Array(name + 'Class', name + 'ActiveClass').map(function (cls) {
return options[cls]
}).join(' ')
}
/**
* 计算动画长度
*/
var transitionDuration = avalon.cssName('transition-duration')
var animationDuration = avalon.cssName('animation-duration')
var rsecond = /\d+s$/
export function toMillisecond(str) {
var ratio = rsecond.test(str) ? 1000 : 1
return parseFloat(str) * ratio
}
export function getAnimationTime(dom) {
var computedStyles = window.getComputedStyle(dom,null)
var tranDuration = computedStyles[transitionDuration]
var animDuration = computedStyles[animationDuration]
return toMillisecond(tranDuration) || toMillisecond(animDuration)
}
/**
*
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="dist/avalon.js"></script>
<script>
avalon.effect('animate')
var vm = avalon.define({
$id: 'ani',
a: true
})
</script>
<style>
.animate-enter, .animate-leave{
width:100px;
height:100px;
background: #29b6f6;
transition:all 2s;
-moz-transition: all 2s;
-webkit-transition: all 2s;
-o-transition:all 2s;
}
.animate-enter-active, .animate-leave{
width:300px;
height:300px;
}
.animate-leave-active{
width:100px;
height:100px;
}
</style>
</head>
<body>
<div :controller='ani' >
<p><input type='button' value='click' :click='@a =!@a'></p>
<div :effect="{is:'animate',action:@a}"></div>
</div>
</body>
</html>
*
*/ |