| 1 |
import { getRect, css, matrix, isRectEqual, indexOfObject } from './utils.js'; |
| 2 |
import Sortable from './Sortable.js'; |
| 3 |
|
| 4 |
export default function AnimationStateManager() { |
| 5 |
let animationStates = [], |
| 6 |
animationCallbackId; |
| 7 |
|
| 8 |
return { |
| 9 |
captureAnimationState() { |
| 10 |
animationStates = []; |
| 11 |
if (!this.options.animation) return; |
| 12 |
let children = [].slice.call(this.el.children); |
| 13 |
|
| 14 |
children.forEach(child => { |
| 15 |
if (css(child, 'display') === 'none' || child === Sortable.ghost) return; |
| 16 |
animationStates.push({ |
| 17 |
target: child, |
| 18 |
rect: getRect(child) |
| 19 |
}); |
| 20 |
let fromRect = { ...animationStates[animationStates.length - 1].rect }; |
| 21 |
|
| 22 |
// If animating: compensate for current animation |
| 23 |
if (child.thisAnimationDuration) { |
| 24 |
let childMatrix = matrix(child, true); |
| 25 |
if (childMatrix) { |
| 26 |
fromRect.top -= childMatrix.f; |
| 27 |
fromRect.left -= childMatrix.e; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
child.fromRect = fromRect; |
| 32 |
}); |
| 33 |
}, |
| 34 |
|
| 35 |
addAnimationState(state) { |
| 36 |
animationStates.push(state); |
| 37 |
}, |
| 38 |
|
| 39 |
removeAnimationState(target) { |
| 40 |
animationStates.splice(indexOfObject(animationStates, { target }), 1); |
| 41 |
}, |
| 42 |
|
| 43 |
animateAll(callback) { |
| 44 |
if (!this.options.animation) { |
| 45 |
clearTimeout(animationCallbackId); |
| 46 |
if (typeof(callback) === 'function') callback(); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
let animating = false, |
| 51 |
animationTime = 0; |
| 52 |
|
| 53 |
animationStates.forEach((state) => { |
| 54 |
let time = 0, |
| 55 |
animatingThis = false, |
| 56 |
target = state.target, |
| 57 |
fromRect = target.fromRect, |
| 58 |
toRect = getRect(target), |
| 59 |
prevFromRect = target.prevFromRect, |
| 60 |
prevToRect = target.prevToRect, |
| 61 |
animatingRect = state.rect, |
| 62 |
targetMatrix = matrix(target, true); |
| 63 |
|
| 64 |
|
| 65 |
if (targetMatrix) { |
| 66 |
// Compensate for current animation |
| 67 |
toRect.top -= targetMatrix.f; |
| 68 |
toRect.left -= targetMatrix.e; |
| 69 |
} |
| 70 |
|
| 71 |
target.toRect = toRect; |
| 72 |
|
| 73 |
if (target.thisAnimationDuration) { |
| 74 |
// Could also check if animatingRect is between fromRect and toRect |
| 75 |
if ( |
| 76 |
isRectEqual(prevFromRect, toRect) && |
| 77 |
!isRectEqual(fromRect, toRect) && |
| 78 |
// Make sure animatingRect is on line between toRect & fromRect |
| 79 |
(animatingRect.top - toRect.top) / |
| 80 |
(animatingRect.left - toRect.left) === |
| 81 |
(fromRect.top - toRect.top) / |
| 82 |
(fromRect.left - toRect.left) |
| 83 |
) { |
| 84 |
// If returning to same place as started from animation and on same axis |
| 85 |
time = calculateRealTime(animatingRect, prevFromRect, prevToRect, this.options); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// if fromRect != toRect: animate |
| 90 |
if (!isRectEqual(toRect, fromRect)) { |
| 91 |
target.prevFromRect = fromRect; |
| 92 |
target.prevToRect = toRect; |
| 93 |
|
| 94 |
if (!time) { |
| 95 |
time = this.options.animation; |
| 96 |
} |
| 97 |
this.animate( |
| 98 |
target, |
| 99 |
animatingRect, |
| 100 |
toRect, |
| 101 |
time |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
if (time) { |
| 106 |
animating = true; |
| 107 |
animationTime = Math.max(animationTime, time); |
| 108 |
clearTimeout(target.animationResetTimer); |
| 109 |
target.animationResetTimer = setTimeout(function() { |
| 110 |
target.animationTime = 0; |
| 111 |
target.prevFromRect = null; |
| 112 |
target.fromRect = null; |
| 113 |
target.prevToRect = null; |
| 114 |
target.thisAnimationDuration = null; |
| 115 |
}, time); |
| 116 |
target.thisAnimationDuration = time; |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 120 |
|
| 121 |
clearTimeout(animationCallbackId); |
| 122 |
if (!animating) { |
| 123 |
if (typeof(callback) === 'function') callback(); |
| 124 |
} else { |
| 125 |
animationCallbackId = setTimeout(function() { |
| 126 |
if (typeof(callback) === 'function') callback(); |
| 127 |
}, animationTime); |
| 128 |
} |
| 129 |
animationStates = []; |
| 130 |
}, |
| 131 |
|
| 132 |
animate(target, currentRect, toRect, duration) { |
| 133 |
if (duration) { |
| 134 |
css(target, 'transition', ''); |
| 135 |
css(target, 'transform', ''); |
| 136 |
let elMatrix = matrix(this.el), |
| 137 |
scaleX = elMatrix && elMatrix.a, |
| 138 |
scaleY = elMatrix && elMatrix.d, |
| 139 |
translateX = (currentRect.left - toRect.left) / (scaleX || 1), |
| 140 |
translateY = (currentRect.top - toRect.top) / (scaleY || 1); |
| 141 |
|
| 142 |
target.animatingX = !!translateX; |
| 143 |
target.animatingY = !!translateY; |
| 144 |
|
| 145 |
css(target, 'transform', 'translate3d(' + translateX + 'px,' + translateY + 'px,0)'); |
| 146 |
|
| 147 |
this.forRepaintDummy = repaint(target); // repaint |
| 148 |
|
| 149 |
css(target, 'transition', 'transform ' + duration + 'ms' + (this.options.easing ? ' ' + this.options.easing : '')); |
| 150 |
css(target, 'transform', 'translate3d(0,0,0)'); |
| 151 |
(typeof target.animated === 'number') && clearTimeout(target.animated); |
| 152 |
target.animated = setTimeout(function () { |
| 153 |
css(target, 'transition', ''); |
| 154 |
css(target, 'transform', ''); |
| 155 |
target.animated = false; |
| 156 |
|
| 157 |
target.animatingX = false; |
| 158 |
target.animatingY = false; |
| 159 |
}, duration); |
| 160 |
} |
| 161 |
} |
| 162 |
}; |
| 163 |
} |
| 164 |
|
| 165 |
function repaint(target) { |
| 166 |
return target.offsetWidth; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
function calculateRealTime(animatingRect, fromRect, toRect, options) { |
| 171 |
return ( |
| 172 |
Math.sqrt(Math.pow(fromRect.top - animatingRect.top, 2) + Math.pow(fromRect.left - animatingRect.left, 2)) / |
| 173 |
Math.sqrt(Math.pow(fromRect.top - toRect.top, 2) + Math.pow(fromRect.left - toRect.left, 2)) |
| 174 |
) * options.animation; |
| 175 |
} |
| 176 |
|