]> source.dussan.org Git - jquery-ui.git/commitdiff
Draggable: Ensure helper is positioned even if its the element itself
authorMike Sherov <mike.sherov@gmail.com>
Fri, 15 Aug 2014 01:17:28 +0000 (21:17 -0400)
committerMike Sherov <mike.sherov@gmail.com>
Tue, 19 Aug 2014 19:15:21 +0000 (15:15 -0400)
Fixes #9446

tests/unit/draggable/draggable_options.js
ui/draggable.js

index 1b7dc2b3bb3bc0198cf9a98c6cf4877f83c448f8..8f4bd3bbbd920a18a7ce9524c5464705101aee08 100644 (file)
@@ -701,6 +701,25 @@ test( "helper, default, switching after initialization", function() {
        TestHelpers.draggable.shouldMove( element, "helper: original" );
 });
 
+// http://bugs.jqueryui.com/ticket/9446
+// Draggable: helper function cannot emulate default behavior
+test( "helper, function returning original element", function() {
+       expect( 1 );
+
+       var element = $( "#draggable1" ).css( "position", "static" ).draggable({
+               helper: function() {
+                       return this;
+               }
+       });
+
+       TestHelpers.draggable.testDragHelperOffset( element, 100, 100, 100, 100, "original element is draggable" );
+
+       element.simulate( "drag", {
+               dx: 100,
+               dy: 100
+       });
+});
+
 function testHelperPosition( scrollPositions, position, helper, scrollElements, scrollElementsTitle ) {
        test( "{ helper: '" + helper + "' }, " + position + ", with scroll offset on " + scrollElementsTitle, function() {
                expect( scrollPositions.length * 2 );
index 826098feb86aaf34f8b1d06de6f7dfbb8d0f83d5..1529e94aa8def3d34c9ee80a21617474ab496029 100644 (file)
@@ -61,8 +61,8 @@ $.widget("ui.draggable", $.ui.mouse, {
        },
        _create: function() {
 
-               if (this.options.helper === "original" && !(/^(?:r|a|f)/).test(this.element.css("position"))) {
-                       this.element[0].style.position = "relative";
+               if ( this.options.helper === "original" ) {
+                       this._setPositionRelative();
                }
                if (this.options.addClasses){
                        this.element.addClass("ui-draggable");
@@ -345,12 +345,24 @@ $.widget("ui.draggable", $.ui.mouse, {
        _createHelper: function(event) {
 
                var o = this.options,
-                       helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[ 0 ], [ event ])) : (o.helper === "clone" ? this.element.clone().removeAttr("id") : this.element);
+                       helperIsFunction = $.isFunction( o.helper ),
+                       helper = helperIsFunction ?
+                               $( o.helper.apply( this.element[ 0 ], [ event ] ) ) :
+                               ( o.helper === "clone" ?
+                                       this.element.clone().removeAttr( "id" ) :
+                                       this.element );
 
                if (!helper.parents("body").length) {
                        helper.appendTo((o.appendTo === "parent" ? this.element[0].parentNode : o.appendTo));
                }
 
+               // http://bugs.jqueryui.com/ticket/9446
+               // a helper function can return the original element
+               // which wouldn't have been set to relative in _create
+               if ( helperIsFunction && helper[ 0 ] === this.element[ 0 ] ) {
+                       this._setPositionRelative();
+               }
+
                if (helper[0] !== this.element[0] && !(/(fixed|absolute)/).test(helper.css("position"))) {
                        helper.css("position", "absolute");
                }
@@ -359,6 +371,12 @@ $.widget("ui.draggable", $.ui.mouse, {
 
        },
 
+       _setPositionRelative: function() {
+               if ( !( /^(?:r|a|f)/ ).test( this.element.css( "position" ) ) ) {
+                       this.element[ 0 ].style.position = "relative";
+               }
+       },
+
        _adjustOffsetFromHelper: function(obj) {
                if (typeof obj === "string") {
                        obj = obj.split(" ");