-
Notifications
You must be signed in to change notification settings - Fork 11
/
jquery.ui.sortable-animation.js
125 lines (108 loc) · 3.45 KB
/
jquery.ui.sortable-animation.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
/*!
* jQuery UI Sortable Animation 0.0.1
*
* Copyright 2015, Egor Sharapov
* Licensed under the MIT license.
*
* Depends:
* jquery.ui.sortable.js
*/
(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jquery", "jquery-ui"], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
var supports = {},
testProp = function (prefixes) {
var test_el = document.createElement('div'), i, l;
for (i = 0; l = prefixes.length, i < l; i++) {
if (test_el.style[prefixes[i]] != undefined) {
return prefixes[i];
}
}
return '';
},
use_css_animation = false;
// check for css-transforms support
supports['transform'] = testProp([
'transform', 'WebkitTransform',
'MozTransform', 'OTransform',
'msTransform'
]);
// check for css-transitions support
supports['transition'] = testProp([
'transition', 'WebkitTransition',
'MozTransition', 'OTransition',
'msTransition'
]);
use_css_animation = supports['transform'] && supports['transition'];
$.widget("ui.sortable", $.ui.sortable, {
options: {
// adds the new `animation` option, turned off by default.
animation: 0,
},
// called internally by sortable when sortable
// items are rearranged.
_rearrange: function (e, item) {
var $item,
props = {},
reset_props = {},
offset,
axis = $.trim(this.options.axis);
// just call the original implementation of _rearrange()
// if option `animation` is turned off
// `currentContainer` used for animating received items
// from another sortable container (`connectWith` option)
if (!parseInt(this.currentContainer.options.animation) ||
!axis
) {
return this._superApply(arguments);
}
// call original _rearrange() at first, before access to item, which may be undefined
this._superApply(arguments);
if (item == null) { return; }
$item = $(item.item[0]);
// if moved up, then move item up to its height,
// if moved down, then move item down
offset = (this.direction == 'up' ? '' : '-') + ($item[axis == 'x' ? 'width' : 'height']()) + 'px';
// prepare starting css props
if (use_css_animation) {
props[supports['transform']] = (axis == 'x' ? 'translateX' : 'translateY') + '(' + offset + ')';
} else {
props = {
position: 'relative',
};
props[axis == 'x' ? 'left' : 'top'] = offset;
}
// set starting css props on item
$item.css(props);
// if css animations are not supported
// use jQuery animations
if (use_css_animation) {
props[supports['transition']] = supports['transform'] + ' ' + this.options.animation + 'ms';
props[supports['transform']] = '';
reset_props[supports['transform']] = '';
reset_props[supports['transition']] = '';
setTimeout(function () {
$item.css(props);
}, 0);
} else {
reset_props.top = '';
reset_props.position = '';
$item.animate({
top: '',
position: ''
}, this.options.animation);
}
// after animation ends
// clear changed for animation props
setTimeout(function () {
$item.css(reset_props);
}, this.options.animation);
}
});
}));