From 6358695df18722d8c7e99437365db42cf4957626 Mon Sep 17 00:00:00 2001 From: TJ VanToll Date: Sat, 26 Jan 2013 15:31:27 -0500 Subject: Draggable: Fix border containment. Fixed #5569 - Draggable: Containment incorrectly calculates padding and border --- ui/jquery.ui.draggable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index 7c1fb3361..56b8fc77d 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -421,8 +421,8 @@ $.widget("ui.draggable", $.ui.mouse, { this.containment = [ (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0), (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0), - (over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left - this.margins.right, - (over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top - this.margins.bottom + (over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderRightWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left - this.margins.right, + (over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderBottomWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top - this.margins.bottom ]; this.relative_container = c; -- cgit v1.2.3 From cdff72efed495d7a17c551578079619712758793 Mon Sep 17 00:00:00 2001 From: Scott González Date: Fri, 8 Mar 2013 10:08:46 -0500 Subject: Draggable: Account for descendants in handle. --- tests/unit/draggable/draggable.html | 2 +- tests/unit/draggable/draggable_options.js | 28 ++-------------------------- ui/jquery.ui.draggable.js | 14 +++----------- 3 files changed, 6 insertions(+), 38 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/draggable/draggable.html b/tests/unit/draggable/draggable.html index 6380f3e85..3d1d3326e 100644 --- a/tests/unit/draggable/draggable.html +++ b/tests/unit/draggable/draggable.html @@ -48,7 +48,7 @@
Relative
-
Absolute
+
Absolute
Absolute
diff --git a/tests/unit/draggable/draggable_options.js b/tests/unit/draggable/draggable_options.js index 2e9f85c24..a71bf57cb 100644 --- a/tests/unit/draggable/draggable_options.js +++ b/tests/unit/draggable/draggable_options.js @@ -603,39 +603,15 @@ test( "grid, switching after initialization", function() { }); test( "{ handle: 'span' }", function() { - expect( 2 ); + expect( 3 ); var element = $( "#draggable2" ).draggable({ handle: "span" }); TestHelpers.draggable.testDrag( element, "#draggable2 span", 50, 50, 50, 50, "drag span" ); + TestHelpers.draggable.testDrag( element, "#draggable2 span em", 50, 50, 50, 50, "drag span child" ); TestHelpers.draggable.shouldNotMove( element, "drag element" ); }); -/* -test( "{ handle: Selectors }, matching parent selector", function() { - - expect( 4 ); - - var element = $( "#draggable2" ).draggable({ handle: "span a" }); - - $( "#qunit-fixture" ).append( "" ); - - element.find( "span" ).append( "" ); - - $( "#wrapping a" ).append( element ); - - TestHelpers.draggable.testDrag( element, "#draggable2 span a", 50, 50, 50, 50, "drag span child" ); - TestHelpers.draggable.shouldNotMove( $( "#wrapping a" ) ); - - $( "#draggable2" ).draggable( "option", "handle", "span > a" ); - $( "#draggable2" ).find( "a" ).append( "" ); - - TestHelpers.draggable.testDrag( element, $( "#draggable2 span a" ).first(), 50, 50, 50, 50, "drag span child" ); - TestHelpers.draggable.shouldNotMove( $( "#draggable2 span a" ).last() ); - -}); -*/ - test( "handle, default, switching after initialization", function() { expect( 6 ); diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index 56b8fc77d..5762d3171 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -281,17 +281,9 @@ $.widget("ui.draggable", $.ui.mouse, { }, _getHandle: function(event) { - - var handle = !this.options.handle || !$(this.options.handle, this.element).length ? true : false; - - this.element.find( this.options.handle ).each(function() { - if(this === event.target) { - handle = true; - } - }); - - return handle; - + return this.options.handle ? + !!$( event.target ).closest( this.element.find( this.options.handle ) ).length : + true; }, _createHelper: function(event) { -- cgit v1.2.3 From 82f588e82b887fdcb2406da2c5dfedc2f13ebdc9 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Thu, 14 Mar 2013 13:21:24 -0500 Subject: Draggable: Fix double offset bug when scrolling. Fixes #6817 - Draggable: auto scroll goes double distance when dragging --- tests/unit/draggable/draggable_options.js | 34 +++++++++++++++++++++++++++++++ ui/jquery.ui.draggable.js | 21 +++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/draggable/draggable_options.js b/tests/unit/draggable/draggable_options.js index a71bf57cb..83fbc1712 100644 --- a/tests/unit/draggable/draggable_options.js +++ b/tests/unit/draggable/draggable_options.js @@ -1103,6 +1103,40 @@ test( "scroll, scrollSensitivity, and scrollSpeed", function() { TestHelpers.draggable.restoreScroll( document ); }); +test( "#6817: auto scroll goes double distance when dragging", function() { + expect( 2 ); + + var offsetBefore, + distance = 10, + viewportHeight = $( window ).height(), + element = $( "#draggable1" ).draggable({ + scroll: true, + stop: function( e, ui ) { + equal( ui.offset.top, newY, "offset of item matches pointer position after scroll" ); + equal( ui.offset.top - offsetBefore.top, distance, "offset of item only moves expected distance after scroll" ); + } + }), + scrollSensitivity = element.draggable( "option", "scrollSensitivity" ), + oldY = viewportHeight - scrollSensitivity, + newY = oldY + distance; + + element.offset({ + top: oldY, + left: 1 + }); + + offsetBefore = element.offset(); + + element.simulate( "drag", { + handle: "corner", + dx: 1, + y: newY, + moves: 1 + }); + + TestHelpers.draggable.restoreScroll( document ); +}); + test( "snap, snapMode, and snapTolerance", function() { expect( 9 ); diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index 5762d3171..a75f9e9f3 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -135,6 +135,9 @@ $.widget("ui.draggable", $.ui.mouse, { left: this.offset.left - this.margins.left }; + //Reset scroll cache + this.offset.scroll = false; + $.extend(this.offset, { click: { //Where the click happened, relative to the element left: event.pageX - this.offset.left, @@ -433,18 +436,23 @@ $.widget("ui.draggable", $.ui.mouse, { var mod = d === "absolute" ? 1 : -1, scroll = this.cssPosition === "absolute" && !(this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName); + //Cache the scroll + if (!this.offset.scroll) { + this.offset.scroll = {top : scroll.scrollTop(), left : scroll.scrollLeft()}; + } + return { top: ( pos.top + // The absolute mouse position this.offset.relative.top * mod + // Only for relative positioned nodes: Relative offset from element to offset parent this.offset.parent.top * mod - // The offsetParent's offset without borders (offset + border) - ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod) + ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) ) * mod) ), left: ( pos.left + // The absolute mouse position this.offset.relative.left * mod + // Only for relative positioned nodes: Relative offset from element to offset parent this.offset.parent.left * mod - // The offsetParent's offset without borders (offset + border) - ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod) + ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : this.offset.scroll.left ) * mod) ) }; @@ -459,6 +467,11 @@ $.widget("ui.draggable", $.ui.mouse, { pageX = event.pageX, pageY = event.pageY; + //Cache the scroll + if (!this.offset.scroll) { + this.offset.scroll = {top : scroll.scrollTop(), left : scroll.scrollLeft()}; + } + /* * - Position constraining - * Constrain the position to a mix of grid, containment. @@ -508,14 +521,14 @@ $.widget("ui.draggable", $.ui.mouse, { this.offset.click.top - // Click offset (relative to the element) this.offset.relative.top - // Only for relative positioned nodes: Relative offset from element to offset parent this.offset.parent.top + // The offsetParent's offset without borders (offset + border) - ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) )) + ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )) ), left: ( pageX - // The absolute mouse position this.offset.click.left - // Click offset (relative to the element) this.offset.relative.left - // Only for relative positioned nodes: Relative offset from element to offset parent this.offset.parent.left + // The offsetParent's offset without borders (offset + border) - ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() )) + ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : this.offset.scroll.left )) ) }; -- cgit v1.2.3 From bd126a9c1cfcbc9d0fd370af25cfa0eab294fc4e Mon Sep 17 00:00:00 2001 From: Zbigniew Motyka Date: Mon, 29 Oct 2012 09:55:54 +0100 Subject: Draggable: modified snapping algorithm to use edges and corners. Fixed #8165 - Draggable: Snapping doesn't take top/left into account properly --- tests/unit/draggable/draggable_options.js | 37 +++++++++++++++++++++++++++++++ ui/jquery.ui.draggable.js | 3 +-- 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/draggable/draggable_options.js b/tests/unit/draggable/draggable_options.js index 83fbc1712..2cb7ec22b 100644 --- a/tests/unit/draggable/draggable_options.js +++ b/tests/unit/draggable/draggable_options.js @@ -1257,6 +1257,43 @@ test( "snap, snapMode, and snapTolerance", function() { deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" ); }); +test( "#8165: Snapping large rectangles to small rectangles doesn't snap properly", function() { + expect( 1 ); + + var snapTolerance = 20, + y = 1, + element = $( "#draggable1" ) + .css({ + width: "50px", + height: "200px" + }).offset({ + top: y, + left: 1 + }), + element2 = $( "#draggable2" ) + .css({ + width: "50px", + height: "50px" + }).offset({ + top: y + snapTolerance + 1, + left: 200 + }), + newX = element2.offset().left - element.outerWidth() - snapTolerance + 1; + + $( "#draggable1, #draggable2" ).draggable({ + snap: true, + snapTolerance: snapTolerance + }); + + element.simulate( "drag", { + handle: "corner", + x: newX, + moves: 1 + }); + + notDeepEqual( element.offset(), { top: y, left: newX }, "snaps even if only a side (not a corner) is inside the snapTolerance" ); +}); + test( "stack", function() { expect( 2 ); diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index a75f9e9f3..4c8a9d5d5 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -850,8 +850,7 @@ $.ui.plugin.add("draggable", "snap", { t = inst.snapElements[i].top; b = t + inst.snapElements[i].height; - //Yes, I know, this is insane ;) - if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) { + if(x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d) { if(inst.snapElements[i].snapping) { (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item }))); } -- cgit v1.2.3 From 9d8af804ad4cebe434d420b29467c596809a7cca Mon Sep 17 00:00:00 2001 From: Mike Sherov Date: Sat, 16 Mar 2013 14:36:06 -0400 Subject: Draggable: make sure snap elements are in the document before snapping. Fixes #8459 - Draggable: element can snap to an element that was removed during drag. --- tests/unit/draggable/draggable_options.js | 33 +++++++++++++++++++++++++++++++ ui/jquery.ui.draggable.js | 14 +++---------- 2 files changed, 36 insertions(+), 11 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/draggable/draggable_options.js b/tests/unit/draggable/draggable_options.js index 2cb7ec22b..f4fca0b2e 100644 --- a/tests/unit/draggable/draggable_options.js +++ b/tests/unit/draggable/draggable_options.js @@ -1257,6 +1257,39 @@ test( "snap, snapMode, and snapTolerance", function() { deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap on the inner snapTolerance area when snapMode is outer" ); }); +test( "#8459: element can snap to an element that was removed during drag", function() { + expect( 1 ); + + var newX, newY, + snapTolerance = 15, + element = $( "#draggable1" ).draggable({ + snap: true, + snapMode: "both", + snapTolerance: snapTolerance, + start: function() { + element2.remove(); + } + }), + element2 = $( "#draggable2" ).draggable(); + + element.offset({ + top: 1, + left: 1 + }); + + newX = element2.offset().left - element.outerWidth() - snapTolerance + 1; + newY = element2.offset().top; + + element.simulate( "drag", { + handle: "corner", + x: newX, + y: newY, + moves: 1 + }); + + deepEqual( element.offset(), { top: newY, left: newX }, "doesn't snap to a removed element" ); +}); + test( "#8165: Snapping large rectangles to small rectangles doesn't snap properly", function() { expect( 1 ); diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index 4c8a9d5d5..605425859 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -217,9 +217,7 @@ $.widget("ui.draggable", $.ui.mouse, { _mouseStop: function(event) { //If we are using droppables, inform the manager about the drop - var element, - that = this, - elementInDom = false, + var that = this, dropped = false; if ($.ui.ddmanager && !this.options.dropBehaviour) { dropped = $.ui.ddmanager.drop(this, event); @@ -232,13 +230,7 @@ $.widget("ui.draggable", $.ui.mouse, { } //if the original element is no longer in the DOM don't bother to continue (see #8269) - element = this.element[0]; - while ( element && (element = element.parentNode) ) { - if (element === document ) { - elementInDom = true; - } - } - if ( !elementInDom && this.options.helper === "original" ) { + if ( this.options.helper === "original" && !$.contains( this.element[ 0 ].ownerDocument, this.element[ 0 ] ) ) { return false; } @@ -850,7 +842,7 @@ $.ui.plugin.add("draggable", "snap", { t = inst.snapElements[i].top; b = t + inst.snapElements[i].height; - if(x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d) { + if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) { if(inst.snapElements[i].snapping) { (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item }))); } -- cgit v1.2.3 From 4fce29e9356d7cbe2ccbdc29a391603580e852de Mon Sep 17 00:00:00 2001 From: Corey Frang Date: Wed, 30 Jan 2013 08:32:48 -0600 Subject: Widgets: Updating to use instance method on bridge --- tests/unit/widget/widget_core.js | 7 +++++-- ui/jquery.ui.autocomplete.js | 2 +- ui/jquery.ui.draggable.js | 28 ++++++++++++++-------------- ui/jquery.ui.resizable.js | 22 +++++++++++----------- ui/jquery.ui.widget.js | 8 ++++---- 5 files changed, 35 insertions(+), 32 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index 0f6692c3c..eaea8d8be 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -626,7 +626,7 @@ test( ".widget() - overriden", function() { }); test( ".instance()", function() { - expect( 1 ); + expect( 2 ); var div, _test = function() {}; @@ -634,7 +634,10 @@ test( ".instance()", function() { _create: function() {}, _test: _test }); - div = $( "
" ).testWidget(); + + div = $( "
" ); + equal( div.testWidget( "instance" ), undefined ); + div.testWidget(); equal( div.testWidget( "instance" ), div.data( "ui-testWidget" ) ); }); diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index b3a05da0b..92ddc3a12 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -201,7 +201,7 @@ $.widget( "ui.autocomplete", { role: null }) .hide() - .data( "ui-menu" ); + .menu( "instance" ); this._on( this.menu.element, { mousedown: function( event ) { diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index 605425859..a16b022d7 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -563,7 +563,7 @@ $.widget("ui.draggable", $.ui.mouse, { $.ui.plugin.add("draggable", "connectToSortable", { start: function(event, ui) { - var inst = $(this).data("ui-draggable"), o = inst.options, + var inst = $(this).draggable( "instance" ), o = inst.options, uiSortable = $.extend({}, ui, { item: inst.element }); inst.sortables = []; $(o.connectToSortable).each(function() { @@ -582,7 +582,7 @@ $.ui.plugin.add("draggable", "connectToSortable", { stop: function(event, ui) { //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper - var inst = $(this).data("ui-draggable"), + var inst = $(this).draggable( "instance" ), uiSortable = $.extend({}, ui, { item: inst.element }); $.each(inst.sortables, function() { @@ -618,7 +618,7 @@ $.ui.plugin.add("draggable", "connectToSortable", { }, drag: function(event, ui) { - var inst = $(this).data("ui-draggable"), that = this; + var inst = $(this).draggable( "instance" ), that = this; $.each(inst.sortables, function() { @@ -719,14 +719,14 @@ $.ui.plugin.add("draggable", "connectToSortable", { $.ui.plugin.add("draggable", "cursor", { start: function() { - var t = $("body"), o = $(this).data("ui-draggable").options; + var t = $("body"), o = $(this).draggable( "instance" ).options; if (t.css("cursor")) { o._cursor = t.css("cursor"); } t.css("cursor", o.cursor); }, stop: function() { - var o = $(this).data("ui-draggable").options; + var o = $(this).draggable( "instance" ).options; if (o._cursor) { $("body").css("cursor", o._cursor); } @@ -735,14 +735,14 @@ $.ui.plugin.add("draggable", "cursor", { $.ui.plugin.add("draggable", "opacity", { start: function(event, ui) { - var t = $(ui.helper), o = $(this).data("ui-draggable").options; + var t = $(ui.helper), o = $(this).draggable( "instance" ).options; if(t.css("opacity")) { o._opacity = t.css("opacity"); } t.css("opacity", o.opacity); }, stop: function(event, ui) { - var o = $(this).data("ui-draggable").options; + var o = $(this).draggable( "instance" ).options; if(o._opacity) { $(ui.helper).css("opacity", o._opacity); } @@ -751,14 +751,14 @@ $.ui.plugin.add("draggable", "opacity", { $.ui.plugin.add("draggable", "scroll", { start: function() { - var i = $(this).data("ui-draggable"); + var i = $(this).draggable( "instance" ); if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") { i.overflowOffset = i.scrollParent.offset(); } }, drag: function( event ) { - var i = $(this).data("ui-draggable"), o = i.options, scrolled = false; + var i = $(this).draggable( "instance" ), o = i.options, scrolled = false; if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") { @@ -808,7 +808,7 @@ $.ui.plugin.add("draggable", "scroll", { $.ui.plugin.add("draggable", "snap", { start: function() { - var i = $(this).data("ui-draggable"), + var i = $(this).draggable( "instance" ), o = i.options; i.snapElements = []; @@ -829,7 +829,7 @@ $.ui.plugin.add("draggable", "snap", { drag: function(event, ui) { var ts, bs, ls, rs, l, r, t, b, i, first, - inst = $(this).data("ui-draggable"), + inst = $(this).draggable( "instance" ), o = inst.options, d = o.snapTolerance, x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width, @@ -903,7 +903,7 @@ $.ui.plugin.add("draggable", "snap", { $.ui.plugin.add("draggable", "stack", { start: function() { var min, - o = this.data("ui-draggable").options, + o = $(this).draggable( "instance" ).options, group = $.makeArray($(o.stack)).sort(function(a,b) { return (parseInt($(a).css("zIndex"),10) || 0) - (parseInt($(b).css("zIndex"),10) || 0); }); @@ -920,14 +920,14 @@ $.ui.plugin.add("draggable", "stack", { $.ui.plugin.add("draggable", "zIndex", { start: function(event, ui) { - var t = $(ui.helper), o = $(this).data("ui-draggable").options; + var t = $(ui.helper), o = $(this).draggable( "instance" ).options; if(t.css("zIndex")) { o._zIndex = t.css("zIndex"); } t.css("zIndex", o.zIndex); }, stop: function(event, ui) { - var o = $(this).data("ui-draggable").options; + var o = $(this).draggable( "instance" ).options; if(o._zIndex) { $(ui.helper).css("zIndex", o._zIndex); } diff --git a/ui/jquery.ui.resizable.js b/ui/jquery.ui.resizable.js index 2468dcaa6..761ea058b 100644 --- a/ui/jquery.ui.resizable.js +++ b/ui/jquery.ui.resizable.js @@ -81,7 +81,7 @@ $.widget("ui.resizable", $.ui.mouse, { //Overwrite the original this.element this.element = this.element.parent().data( - "ui-resizable", this.element.data("ui-resizable") + "ui-resizable", this.element.resizable( "instance" ) ); this.elementIsWrapper = true; @@ -651,7 +651,7 @@ $.widget("ui.resizable", $.ui.mouse, { $.ui.plugin.add("resizable", "animate", { stop: function( event ) { - var that = $(this).data("ui-resizable"), + var that = $(this).resizable( "instance" ), o = that.options, pr = that._proportionallyResizeElements, ista = pr.length && (/textarea/i).test(pr[0].nodeName), @@ -693,7 +693,7 @@ $.ui.plugin.add("resizable", "containment", { start: function() { var element, p, co, ch, cw, width, height, - that = $(this).data("ui-resizable"), + that = $(this).resizable( "instance" ), o = that.options, el = that.element, oc = o.containment, @@ -739,7 +739,7 @@ $.ui.plugin.add("resizable", "containment", { resize: function( event ) { var woset, hoset, isParent, isOffsetRelative, - that = $(this).data("ui-resizable"), + that = $(this).resizable( "instance" ), o = that.options, co = that.containerOffset, cp = that.position, pRatio = that._aspectRatio || event.shiftKey, @@ -794,7 +794,7 @@ $.ui.plugin.add("resizable", "containment", { }, stop: function(){ - var that = $(this).data("ui-resizable"), + var that = $(this).resizable( "instance" ), o = that.options, co = that.containerOffset, cop = that.containerPosition, @@ -818,7 +818,7 @@ $.ui.plugin.add("resizable", "containment", { $.ui.plugin.add("resizable", "alsoResize", { start: function () { - var that = $(this).data("ui-resizable"), + var that = $(this).resizable( "instance" ), o = that.options, _store = function (exp) { $(exp).each(function() { @@ -839,7 +839,7 @@ $.ui.plugin.add("resizable", "alsoResize", { }, resize: function (event, ui) { - var that = $(this).data("ui-resizable"), + var that = $(this).resizable( "instance" ), o = that.options, os = that.originalSize, op = that.originalPosition, @@ -880,7 +880,7 @@ $.ui.plugin.add("resizable", "ghost", { start: function() { - var that = $(this).data("ui-resizable"), o = that.options, cs = that.size; + var that = $(this).resizable( "instance" ), o = that.options, cs = that.size; that.ghost = that.originalElement.clone(); that.ghost @@ -893,14 +893,14 @@ $.ui.plugin.add("resizable", "ghost", { }, resize: function(){ - var that = $(this).data("ui-resizable"); + var that = $(this).resizable( "instance" ); if (that.ghost) { that.ghost.css({ position: "relative", height: that.size.height, width: that.size.width }); } }, stop: function() { - var that = $(this).data("ui-resizable"); + var that = $(this).resizable( "instance" ); if (that.ghost && that.helper) { that.helper.get(0).removeChild(that.ghost.get(0)); } @@ -911,7 +911,7 @@ $.ui.plugin.add("resizable", "ghost", { $.ui.plugin.add("resizable", "grid", { resize: function() { - var that = $(this).data("ui-resizable"), + var that = $(this).resizable( "instance" ), o = that.options, cs = that.size, os = that.originalSize, diff --git a/ui/jquery.ui.widget.js b/ui/jquery.ui.widget.js index ca097e02b..741ac74d7 100644 --- a/ui/jquery.ui.widget.js +++ b/ui/jquery.ui.widget.js @@ -178,14 +178,14 @@ $.widget.bridge = function( name, object ) { this.each(function() { var methodValue, instance = $.data( this, fullName ); - if ( !instance ) { - return $.error( "cannot call methods on " + name + " prior to initialization; " + - "attempted to call method '" + options + "'" ); - } if ( options === "instance" ) { returnValue = instance; return false; } + if ( !instance ) { + return $.error( "cannot call methods on " + name + " prior to initialization; " + + "attempted to call method '" + options + "'" ); + } if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) { return $.error( "no such method '" + options + "' for " + name + " widget instance" ); } -- cgit v1.2.3 From 90135b27f896506776f3ce9fdbd27c9e957b924a Mon Sep 17 00:00:00 2001 From: Jörn Zaefferer Date: Tue, 19 Mar 2013 16:53:35 +0100 Subject: Widget tests: Use instance method. Also replace a few instances of $.data in widget implementations. --- tests/unit/autocomplete/autocomplete_core.js | 6 ++-- tests/unit/dialog/dialog_events.js | 4 +-- tests/unit/draggable/draggable_events.js | 2 +- tests/unit/slider/slider_options.js | 4 +-- tests/unit/tabs/tabs_core.js | 6 ++-- tests/unit/tabs/tabs_options.js | 4 +-- tests/unit/widget/widget_core.js | 41 ++++++++++++++-------------- ui/jquery.ui.draggable.js | 2 +- ui/jquery.ui.droppable.js | 6 ++-- 9 files changed, 38 insertions(+), 37 deletions(-) (limited to 'ui/jquery.ui.draggable.js') diff --git a/tests/unit/autocomplete/autocomplete_core.js b/tests/unit/autocomplete/autocomplete_core.js index 845a3b19d..a9fc1143c 100644 --- a/tests/unit/autocomplete/autocomplete_core.js +++ b/tests/unit/autocomplete/autocomplete_core.js @@ -99,7 +99,7 @@ test( "allow form submit on enter when menu is not active", function() { delay: 0, minLength: 0 }); - element.data( "ui-autocomplete" )._move = function() { + element.autocomplete( "instance" )._move = function() { didMove = true; }; element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } ); @@ -114,7 +114,7 @@ test( "allow form submit on enter when menu is not active", function() { delay: 0, minLength: 0 }); - element.data( "ui-autocomplete" )._move = function() { + element.autocomplete( "instance" )._move = function() { ok( true, "repsond to arrow" ); }; element.autocomplete( "search" ); @@ -156,7 +156,7 @@ test( "ARIA", function() { var element = $( "#autocomplete" ).autocomplete({ source: [ "java", "javascript" ] }), - liveRegion = element.data( "ui-autocomplete" ).liveRegion; + liveRegion = element.autocomplete( "instance" ).liveRegion; equal( liveRegion.text(), "", "Empty live region on create" ); diff --git a/tests/unit/dialog/dialog_events.js b/tests/unit/dialog/dialog_events.js index 7bcd5673e..1384500f6 100644 --- a/tests/unit/dialog/dialog_events.js +++ b/tests/unit/dialog/dialog_events.js @@ -11,7 +11,7 @@ test("open", function() { var element = $("
"); element.dialog({ open: function(ev, ui) { - ok(element.data("ui-dialog")._isOpen, "interal _isOpen flag is set"); + ok(element.dialog( "instance" )._isOpen, "interal _isOpen flag is set"); ok(true, "autoOpen: true fires open callback"); equal(this, element[0], "context of callback"); equal(ev.type, "dialogopen", "event type in callback"); @@ -30,7 +30,7 @@ test("open", function() { deepEqual(ui, {}, "ui hash in callback"); } }).bind("dialogopen", function(ev, ui) { - ok(element.data("ui-dialog")._isOpen, "interal _isOpen flag is set"); + ok(element.dialog( "instance" )._isOpen, "interal _isOpen flag is set"); ok(true, "dialog('open') fires open event"); equal(this, element[0], "context of event"); deepEqual(ui, {}, "ui hash in event"); diff --git a/tests/unit/draggable/draggable_events.js b/tests/unit/draggable/draggable_events.js index 199561be3..bbaaaeb15 100644 --- a/tests/unit/draggable/draggable_events.js +++ b/tests/unit/draggable/draggable_events.js @@ -117,7 +117,7 @@ test( "stopping the stop callback", function() { dy: 10 }); - ok( element.data("ui-draggable").helper, "the clone should not be deleted if the stop callback is stopped" ); + ok( element.draggable( "instance" ).helper, "the clone should not be deleted if the stop callback is stopped" ); }); diff --git a/tests/unit/slider/slider_options.js b/tests/unit/slider/slider_options.js index f46dbde99..3a6f55390 100644 --- a/tests/unit/slider/slider_options.js +++ b/tests/unit/slider/slider_options.js @@ -185,8 +185,8 @@ test( "values", function() { }); notStrictEqual( - ranges.eq( 0 ).data( "ui-slider" ).options.values, - ranges.eq( 1 ).data( "ui-slider" ).options.values, + ranges.eq( 0 ).slider( "instance" ).options.values, + ranges.eq( 1 ).slider( "instance" ).options.values, "multiple range sliders should not have a reference to the same options.values array" ); diff --git a/tests/unit/tabs/tabs_core.js b/tests/unit/tabs/tabs_core.js index 5f70206dc..cc4f0460c 100644 --- a/tests/unit/tabs/tabs_core.js +++ b/tests/unit/tabs/tabs_core.js @@ -33,7 +33,7 @@ test( "nested list", function() { expect( 1 ); var element = $( "#tabs6" ).tabs(); - equal( element.data( "ui-tabs" ).anchors.length, 2, "should contain 2 tab" ); + equal( element.tabs( "instance" ).anchors.length, 2, "should contain 2 tab" ); }); test( "disconnected from DOM", function() { @@ -155,7 +155,7 @@ asyncTest( "keyboard support - LEFT, RIGHT, UP, DOWN, HOME, END, SPACE, ENTER", panels = element.find( ".ui-tabs-panel" ), keyCode = $.ui.keyCode; - element.data( "ui-tabs" ).delay = 50; + element.tabs( "instance" ).delay = 50; equal( tabs.filter( ".ui-state-focus" ).length, 0, "no tabs focused on init" ); tabs.eq( 0 ).simulate( "focus" ); @@ -306,7 +306,7 @@ asyncTest( "keyboard support - CTRL navigation", function() { panels = element.find( ".ui-tabs-panel" ), keyCode = $.ui.keyCode; - element.data( "ui-tabs" ).delay = 50; + element.tabs( "instance" ).delay = 50; equal( tabs.filter( ".ui-state-focus" ).length, 0, "no tabs focused on init" ); tabs.eq( 0 ).simulate( "focus" ); diff --git a/tests/unit/tabs/tabs_options.js b/tests/unit/tabs/tabs_options.js index 02a2fea34..51212b8f0 100644 --- a/tests/unit/tabs/tabs_options.js +++ b/tests/unit/tabs/tabs_options.js @@ -314,7 +314,7 @@ test( "hide and show: false", function() { show: false, hide: false }), - widget = element.data( "ui-tabs" ), + widget = element.tabs( "instance" ), panels = element.find( ".ui-tabs-panel" ); widget._show = function() { ok( false, "_show() called" ); @@ -335,7 +335,7 @@ asyncTest( "hide and show - animation", function() { show: "drop", hide: 2000 }), - widget = element.data( "ui-tabs" ), + widget = element.tabs( "instance" ), panels = element.find( ".ui-tabs-panel" ); widget._show = function( element, options, callback ) { strictEqual( element[ 0 ], panels[ 1 ], "correct element in _show()" ); diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js index eaea8d8be..f9a6a456d 100644 --- a/tests/unit/widget/widget_core.js +++ b/tests/unit/widget/widget_core.js @@ -43,28 +43,28 @@ test( "element normalization", function() { // workaround for core ticket #8381 this.element.appendTo( "#qunit-fixture" ); ok( this.element.is( "div" ), "generated div" ); - deepEqual( this.element.data( "ui-testWidget" ), this, "instance stored in .data()" ); + deepEqual( this.element.testWidget( "instance" ), this, "instance stored in .data()" ); }; $.ui.testWidget(); $.ui.testWidget.prototype.defaultElement = ""; $.ui.testWidget.prototype._create = function() { ok( this.element.is( "span[data-test=pass]" ), "generated span with properties" ); - deepEqual( this.element.data( "ui-testWidget" ), this, "instace stored in .data()" ); + deepEqual( this.element.testWidget( "instance" ), this, "instace stored in .data()" ); }; $.ui.testWidget(); elem = $( "" ); $.ui.testWidget.prototype._create = function() { deepEqual( this.element[ 0 ], elem[ 0 ], "from element" ); - deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" ); + deepEqual( elem.testWidget( "instance" ), this, "instace stored in .data()" ); }; $.ui.testWidget( {}, elem[ 0 ] ); elem = $( "
" ); $.ui.testWidget.prototype._create = function() { deepEqual( this.element[ 0 ], elem[ 0 ], "from jQuery object" ); - deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" ); + deepEqual( elem.testWidget( "instance" ), this, "instace stored in .data()" ); }; $.ui.testWidget( {}, elem ); @@ -72,7 +72,7 @@ test( "element normalization", function() { .appendTo( "#qunit-fixture" ); $.ui.testWidget.prototype._create = function() { deepEqual( this.element[ 0 ], elem[ 0 ], "from selector" ); - deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" ); + deepEqual( elem.testWidget( "instance" ), this, "instace stored in .data()" ); }; $.ui.testWidget( {}, "#element-normalization-selector" ); @@ -133,7 +133,7 @@ test( "jQuery usage", function() { .testWidget(); shouldCreate = false; - instance = elem.data( "ui-testWidget" ); + instance = elem.testWidget( "instance" ); equal( typeof instance, "object", "instance stored in .data(pluginName)" ); equal( instance.element[0], elem[0], "element stored on widget" ); ret = elem.testWidget( "methodWithParams", "value1", "value2" ); @@ -149,7 +149,7 @@ test( "jQuery usage", function() { equal( ret.end(), elem, "stack preserved" ); elem.testWidget( "destroy" ); - equal( elem.data( "ui-testWidget" ), null ); + equal( elem.testWidget( "instance" ), null ); }); test( "direct usage", function() { @@ -185,7 +185,7 @@ test( "direct usage", function() { instance = new $.ui.testWidget( {}, elem ); shouldCreate = false; - equal( $( elem ).data( "ui-testWidget" ), instance, + equal( $( elem ).testWidget( "instance" ), instance, "instance stored in .data(pluginName)" ); equal( instance.element[ 0 ], elem, "element stored on widget" ); @@ -405,7 +405,7 @@ test( "._super()", function() { } }); - instance = $( "
" ).testWidget3().data( "ui-testWidget3" ); + instance = $( "
" ).testWidget3().testWidget3( "instance" ); instance.method( 5 ); delete $.ui.testWidget3; delete $.ui.testWidget2; @@ -442,7 +442,7 @@ test( "._superApply()", function() { } }); - instance = $( "
" ).testWidget3().data( "ui-testWidget3" ); + instance = $( "
" ).testWidget3().testWidget3( "instance" ); instance.method( 5, 10 ); delete $.ui.testWidget3; delete $.ui.testWidget2; @@ -560,7 +560,7 @@ test( ".option() - deep option setter", function() { $.widget( "ui.testWidget", {} ); var div = $( "
" ).testWidget(); function deepOption( from, to, msg ) { - div.data( "ui-testWidget" ).options.foo = from; + div.testWidget( "instance" ).options.foo = from; $.ui.testWidget.prototype._setOption = function( key, value ) { deepEqual( key, "foo", msg + ": key" ); deepEqual( value, to, msg + ": value" ); @@ -638,7 +638,7 @@ test( ".instance()", function() { div = $( "
" ); equal( div.testWidget( "instance" ), undefined ); div.testWidget(); - equal( div.testWidget( "instance" ), div.data( "ui-testWidget" ) ); + equal( div.testWidget( "instance" ), div.testWidget( "instance" ) ); }); test( "._on() to element (default)", function() { @@ -856,7 +856,7 @@ test( "_on() to common element", function() { ok( true, "handler triggered" ); } }); - var widget = $( "#widget" ).testWidget().data( "ui-testWidget" ); + var widget = $( "#widget" ).testWidget().testWidget( "instance" ); $( "#widget-wrapper" ).testWidget(); widget.destroy(); $( document ).trigger( "customevent" ); @@ -868,7 +868,7 @@ test( "_off() - single event", function() { $.widget( "ui.testWidget", {} ); var shouldTriggerWidget, shouldTriggerOther, element = $( "#widget" ), - widget = element.testWidget().data( "ui-testWidget" ); + widget = element.testWidget().testWidget( "instance" ); widget._on( element, { foo: function() { ok( shouldTriggerWidget, "foo called from _on" ); }}); @@ -889,7 +889,7 @@ test( "_off() - multiple events", function() { $.widget( "ui.testWidget", {} ); var shouldTriggerWidget, shouldTriggerOther, element = $( "#widget" ), - widget = element.testWidget().data( "ui-testWidget" ); + widget = element.testWidget().testWidget( "instance" ); widget._on( element, { foo: function() { ok( shouldTriggerWidget, "foo called from _on" ); @@ -917,7 +917,7 @@ test( "_off() - all events", function() { $.widget( "ui.testWidget", {} ); var shouldTriggerWidget, shouldTriggerOther, element = $( "#widget" ), - widget = element.testWidget().data( "ui-testWidget" ); + widget = element.testWidget().testWidget( "instance" ); widget._on( element, { foo: function() { ok( shouldTriggerWidget, "foo called from _on" ); @@ -1023,7 +1023,7 @@ test( "._trigger() - no event, no ui", function() { deepEqual( ui, {}, "empty ui hash passed" ); handlers.push( this ); }); - deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), true, + deepEqual( $( "#widget" ).testWidget( "instance" )._trigger( "foo" ), true, "_trigger returns true when event is not cancelled" ); deepEqual( handlers, [ $( "#widget" )[ 0 ], @@ -1051,7 +1051,7 @@ test( "._trigger() - cancelled event", function() { ok( true, "event was triggered" ); return false; }); - deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false, + deepEqual( $( "#widget" ).testWidget( "instance" )._trigger( "foo" ), false, "_trigger returns false when event is cancelled" ); }); @@ -1066,7 +1066,7 @@ test( "._trigger() - cancelled callback", function() { return false; } }); - deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false, + deepEqual( $( "#widget" ).testWidget( "instance" )._trigger( "foo" ), false, "_trigger returns false when callback returns false" ); }); @@ -1400,7 +1400,7 @@ asyncTest( "_delay", function() { }); test( "$.widget.bridge()", function() { - expect( 9 ); + expect( 10 ); var instance, ret, elem = $( "
" ); @@ -1428,6 +1428,7 @@ test( "$.widget.bridge()", function() { strictEqual( elem.testWidget({ foo: "bar" }), elem, "plugin returns original jQuery object" ); instance = elem.data( "testWidget" ); equal( typeof instance, "object", "instance stored in .data(pluginName)" ); + equal( typeof elem.testWidget( "instance" ), "object", "also retrievable via instance method" ); ret = elem.testWidget( "method", "value1" ); equal( ret, elem, "jQuery object returned from method call" ); diff --git a/ui/jquery.ui.draggable.js b/ui/jquery.ui.draggable.js index a16b022d7..e5fc069b9 100644 --- a/ui/jquery.ui.draggable.js +++ b/ui/jquery.ui.draggable.js @@ -567,7 +567,7 @@ $.ui.plugin.add("draggable", "connectToSortable", { uiSortable = $.extend({}, ui, { item: inst.element }); inst.sortables = []; $(o.connectToSortable).each(function() { - var sortable = $.data(this, "ui-sortable"); + var sortable = $( this ).sortable( "instance" ); if (sortable && !sortable.options.disabled) { inst.sortables.push({ instance: sortable, diff --git a/ui/jquery.ui.droppable.js b/ui/jquery.ui.droppable.js index 34342d33d..6bc4b594d 100644 --- a/ui/jquery.ui.droppable.js +++ b/ui/jquery.ui.droppable.js @@ -152,7 +152,7 @@ $.widget("ui.droppable", { } this.element.find(":data(ui-droppable)").not(".ui-draggable-dragging").each(function() { - var inst = $.data(this, "ui-droppable"); + var inst = $( this ).droppable( "instance" ); if( inst.options.greedy && !inst.options.disabled && @@ -331,11 +331,11 @@ $.ui.ddmanager = { // find droppable parents with same scope scope = this.options.scope; parent = this.element.parents(":data(ui-droppable)").filter(function () { - return $.data(this, "ui-droppable").options.scope === scope; + return $(this).droppable( "instance" ).options.scope === scope; }); if (parent.length) { - parentInstance = $.data(parent[0], "ui-droppable"); + parentInstance = $( parent[ 0 ] ).droppable( "instance" ); parentInstance.greedyChild = (c === "isover"); } } -- cgit v1.2.3