Skip to content

Commit 13355ad

Browse files
committed
use css transform by default.
1 parent ea999e4 commit 13355ad

File tree

1 file changed

+89
-32
lines changed

1 file changed

+89
-32
lines changed

ui/sortable.js

Lines changed: 89 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,43 @@
3131
}
3232
}(function( $ ) {
3333

34+
transforms = {
35+
'webkitTransform':'-webkit-transform',
36+
'OTransform':'-o-transform',
37+
'msTransform':'-ms-transform',
38+
'MozTransform':'-moz-transform',
39+
'transform':'transform'
40+
};
41+
42+
function hasTransform(transform){
43+
var el = document.createElement('p'),
44+
has3d;
45+
46+
// Add it to the body to get the computed style
47+
document.body.insertBefore(el, null);
48+
49+
for(var t in transforms){
50+
if (transforms.hasOwnProperty(t)) {
51+
if( el.style[t] !== undefined ){
52+
el.style[t] = transform;
53+
has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
54+
}
55+
}
56+
}
57+
58+
document.body.removeChild(el);
59+
60+
return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
61+
}
62+
63+
var has3d, hasTranslate;
64+
$(function() {
65+
has3d = hasTransform('translate3d(1px,1px,1px)');
66+
hasTranslate = hasTransform('translate(1px,1px,1px)');
67+
});
68+
3469
return $.widget("ui.sortable", $.ui.mouse, {
35-
version: "@VERSION",
70+
version: "1.11.3",
3671
widgetEventPrefix: "sort",
3772
ready: false,
3873
options: {
@@ -83,12 +118,17 @@ return $.widget("ui.sortable", $.ui.mouse, {
83118
},
84119

85120
_create: function() {
121+
122+
var o = this.options;
86123
this.containerCache = {};
87124
this.element.addClass("ui-sortable");
88125

89126
//Get the items
90127
this.refresh();
91128

129+
//Let's determine if the items are being displayed horizontally
130+
this.floating = this.items.length ? o.axis === "x" || this._isFloating(this.items[0].item) : false;
131+
92132
//Let's determine the parent's offset
93133
this.offset = this.element.offset();
94134

@@ -223,6 +263,9 @@ return $.widget("ui.sortable", $.ui.mouse, {
223263
relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
224264
});
225265

266+
//Create the placeholder
267+
this._createPlaceholder();
268+
226269
// Only after we got the offset, we can change the helper's position to absolute
227270
// TODO: Still need to figure out a way to make relative sorting possible
228271
this.helper.css("position", "absolute");
@@ -233,6 +276,10 @@ return $.widget("ui.sortable", $.ui.mouse, {
233276
this.originalPageX = event.pageX;
234277
this.originalPageY = event.pageY;
235278

279+
// Set helper's initial position
280+
this.helper[0].style.left = this.originalPosition.left + "px";
281+
this.helper[0].style.top = this.originalPosition.top + "px";
282+
236283
//Adjust the mouse offset relative to the helper if "cursorAt" is supplied
237284
(o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
238285

@@ -244,9 +291,6 @@ return $.widget("ui.sortable", $.ui.mouse, {
244291
this.currentItem.hide();
245292
}
246293

247-
//Create the placeholder
248-
this._createPlaceholder();
249-
250294
//Set a containment if given in the options
251295
if(o.containment) {
252296
this._setContainment();
@@ -368,11 +412,37 @@ return $.widget("ui.sortable", $.ui.mouse, {
368412
this.positionAbs = this._convertPositionTo("absolute");
369413

370414
//Set the helper position
371-
if(!this.options.axis || this.options.axis !== "y") {
372-
this.helper[0].style.left = this.position.left+"px";
373-
}
374-
if(!this.options.axis || this.options.axis !== "x") {
375-
this.helper[0].style.top = this.position.top+"px";
415+
if (has3d || hasTranslate) {
416+
var dx = this.position.left - this.originalPosition.left;
417+
var dy = this.position.top - this.originalPosition.top;
418+
var translate = has3d ? "translate3d(" : "translate(";
419+
420+
if(!this.options.axis || this.options.axis !== "y") {
421+
translate += dx + "px,";
422+
} else {
423+
translate += "0,";
424+
}
425+
426+
if(!this.options.axis || this.options.axis !== "x") {
427+
translate += dy + "px";
428+
} else {
429+
translate += "0";
430+
}
431+
432+
if (has3d) {
433+
translate += ",0)";
434+
}
435+
436+
for (t in transforms) {
437+
this.helper[0].style[t] = translate;
438+
}
439+
} else {
440+
if(!this.options.axis || this.options.axis !== "y") {
441+
this.helper[0].style.left = this.position.left+"px";
442+
}
443+
if(!this.options.axis || this.options.axis !== "x") {
444+
this.helper[0].style.top = this.position.top+"px";
445+
}
376446
}
377447

378448
//Rearrange
@@ -732,11 +802,6 @@ return $.widget("ui.sortable", $.ui.mouse, {
732802

733803
refreshPositions: function(fast) {
734804

735-
// Determine whether items are being displayed horizontally
736-
this.floating = this.items.length ?
737-
this.options.axis === "x" || this._isFloating( this.items[ 0 ].item ) :
738-
false;
739-
740805
//This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change
741806
if(this.offsetParent && this.helper) {
742807
this.offset.parent = this._getParentOffset();
@@ -794,13 +859,12 @@ return $.widget("ui.sortable", $.ui.mouse, {
794859
.addClass(className || that.currentItem[0].className+" ui-sortable-placeholder")
795860
.removeClass("ui-sortable-helper");
796861

797-
if ( nodeName === "tbody" ) {
798-
that._createTrPlaceholder(
799-
that.currentItem.find( "tr" ).eq( 0 ),
800-
$( "<tr>", that.document[ 0 ] ).appendTo( element )
801-
);
802-
} else if ( nodeName === "tr" ) {
803-
that._createTrPlaceholder( that.currentItem, element );
862+
if ( nodeName === "tr" ) {
863+
that.currentItem.children().each(function() {
864+
$( "<td>&#160;</td>", that.document[0] )
865+
.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
866+
.appendTo( element );
867+
});
804868
} else if ( nodeName === "img" ) {
805869
element.attr( "src", that.currentItem.attr( "src" ) );
806870
}
@@ -837,16 +901,6 @@ return $.widget("ui.sortable", $.ui.mouse, {
837901

838902
},
839903

840-
_createTrPlaceholder: function( sourceTr, targetTr ) {
841-
var that = this;
842-
843-
sourceTr.children().each(function() {
844-
$( "<td>&#160;</td>", that.document[ 0 ] )
845-
.attr( "colspan", $( this ).attr( "colspan" ) || 1 )
846-
.appendTo( targetTr );
847-
});
848-
},
849-
850904
_contactContainers: function(event) {
851905
var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, cur, nearBottom, floating, axis,
852906
innermostContainer = null,
@@ -1198,7 +1252,6 @@ return $.widget("ui.sortable", $.ui.mouse, {
11981252
},
11991253

12001254
_clear: function(event, noPropagation) {
1201-
12021255
this.reverting = false;
12031256
// We delay all events that have to be triggered to after the point where the placeholder has been removed and
12041257
// everything else normalized again
@@ -1213,6 +1266,10 @@ return $.widget("ui.sortable", $.ui.mouse, {
12131266
this._noFinalSort = null;
12141267

12151268
if(this.helper[0] === this.currentItem[0]) {
1269+
for (t in transforms) {
1270+
this.helper[0].style[t] = '';
1271+
}
1272+
12161273
for(i in this._storedCSS) {
12171274
if(this._storedCSS[i] === "auto" || this._storedCSS[i] === "static") {
12181275
this._storedCSS[i] = "";

0 commit comments

Comments
 (0)