forked from vuejs/vue-animated-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-animated-list.js
133 lines (127 loc) · 4.36 KB
/
vue-animated-list.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.VueAnimatedList = factory());
}(this, function () { 'use strict';
function install (Vue) {
var _ = Vue.util
var transitionEndEvent = _.transitionEndEvent
var addClass = _.addClass
var removeClass = _.removeClass
var on = _.on
var off = _.off
var transitionProp = _.transitionProp
// patch v-for
var vFor = Vue.directive('for')
var diff = vFor.diff
vFor.diff = function () {
var needMoveTransition = prepareMoveTransition(this.frags)
diff.apply(this, arguments)
if (needMoveTransition) {
applyMoveTransition(this.frags)
}
}
/**
* Check if move transitions are needed, and if so,
* record the bounding client rects for each item.
*
* @param {Array<Fragment>|undefined} frags
* @return {Boolean|undefined}
*/
function prepareMoveTransition (frags) {
var transition =
transitionEndEvent && // css transition supported?
frags && frags.length && // has frags to be moved?
frags[0].node.__v_trans // has transitions?
if (transition) {
var node = frags[0].node
var moveClass = transition.id + '-move'
var moving = node._pendingMoveCb
var hasTransition = false
if (!moving) {
// sniff whether element has a transition duration for transform
// with the move class applied
addClass(node, moveClass)
var type = transition.getCssTransitionType(moveClass)
if (type === 'transition') {
var computedStyles = window.getComputedStyle(node)
var transitionedProps = computedStyles[transitionProp + 'Property']
if (/\btransform(,|$)/.test(transitionedProps)) {
hasTransition = true
}
}
removeClass(node, moveClass)
}
if (moving || hasTransition) {
frags.forEach(function (frag) {
frag._oldPos = frag.node.getBoundingClientRect()
})
return true
}
}
}
/**
* Apply move transitions.
* Calculate new target positions after the move, then apply the
* FLIP technique to trigger CSS transforms.
*
* @param {Array<Fragment>} frags
*/
function applyMoveTransition (frags) {
frags.forEach(function (frag) {
frag._newPos = frag.node.getBoundingClientRect()
})
frags.forEach(function (frag) {
var node = frag.node
var oldPos = frag._oldPos
if (!oldPos) return
if (!frag.moved) {
// transition busting to ensure correct bounding rect:
// if an element has an ongoing transition and not "reinserted",
// the bounding rect will not be calculated at its target position,
// but rather an in-transition position.
var p = node.parentNode
var next = node.nextSibling
p.removeChild(node)
p.insertBefore(node, next)
}
var dx = oldPos.left - frag._newPos.left
var dy = oldPos.top - frag._newPos.top
if (dx !== 0 || dy !== 0) {
frag.moved = true
node.style.transform =
node.style.WebkitTransform =
'translate(' + dx + 'px, ' + dy + 'px)'
node.style.transitionDuration = '0s'
} else {
frag.moved = false
}
})
Vue.nextTick(function () {
var f = document.documentElement.offsetHeight
frags.forEach(function (frag) {
var node = frag.node
var moveClass = node.__v_trans.id + '-move'
if (frag.moved) {
addClass(node, moveClass)
node.style.transform = node.style.WebkitTransform = ''
node.style.transitionDuration = ''
if (node._pendingMoveCb) {
off(node, transitionEndEvent, node._pendingMoveCb)
}
node._pendingMoveCb = function cb () {
off(node, transitionEndEvent, cb)
node._pendingMoveCb = null
removeClass(node, moveClass)
}
on(node, transitionEndEvent, node._pendingMoveCb)
}
})
})
}
}
if (typeof Vue !== 'undefined') {
Vue.use(install)
}
return install
}));