Skip to content

Commit 24756a9

Browse files
bgrinsmikesherov
authored andcommitted
Draggable: enabled draggable from within iframe. Fixed #5727 - draggable: cannot drag element inside iframe
1 parent 433ef9d commit 24756a9

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

tests/jquery.simulate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ $.extend( $.simulate.prototype, {
313313
clientY: Math.round( y )
314314
};
315315

316-
this.simulateEvent( document, "mousemove", coord );
316+
this.simulateEvent( target.ownerDocument, "mousemove", coord );
317317
}
318318

319319
if ( $.contains( document, target ) ) {

tests/unit/draggable/draggable_core.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,20 @@ test( "#5009: scroll not working with parent's position fixed", function() {
169169
});
170170
});
171171

172+
test( "#5727: draggable from iframe" , function() {
173+
expect( 2 );
174+
175+
var iframe = $( "<iframe id='iframe-draggable-container' src='about:blank'></iframe>" ).appendTo( "#qunit-fixture" ),
176+
iframeBody = iframe.contents().find( "body" ).append(
177+
"<div id='iframe-draggable-1' style='background: green; width: 200px; height: 100px;'>Relative</div>"
178+
),
179+
draggable1 = iframeBody.find( "#iframe-draggable-1" );
180+
181+
draggable1.draggable();
182+
183+
equal( draggable1.closest( iframeBody ).length, 1 );
184+
185+
TestHelpers.draggable.shouldMove( draggable1 );
186+
});
187+
172188
})( jQuery );

ui/jquery.ui.core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $.fn.extend({
6969
}).eq(0);
7070
}
7171

72-
return (/fixed/).test(this.css("position")) || !scrollParent.length ? $(document) : scrollParent;
72+
return ( /fixed/ ).test( this.css( "position") ) || !scrollParent.length ? $( this[ 0 ].ownerDocument || document ) : scrollParent;
7373
},
7474

7575
uniqueId: function() {

ui/jquery.ui.draggable.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ $.widget("ui.draggable", $.ui.mouse, {
325325
_getParentOffset: function() {
326326

327327
//Get the offsetParent and cache its position
328-
var po = this.offsetParent.offset();
328+
var po = this.offsetParent.offset(),
329+
document = this.document[ 0 ];
329330

330331
// This is a special case where we need to modify a offset calculated on start, since the following happened:
331332
// 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
@@ -383,7 +384,8 @@ $.widget("ui.draggable", $.ui.mouse, {
383384
_setContainment: function() {
384385

385386
var over, c, ce,
386-
o = this.options;
387+
o = this.options,
388+
document = this.document[ 0 ];
387389

388390
if ( !o.containment ) {
389391
this.containment = null;
@@ -444,6 +446,7 @@ $.widget("ui.draggable", $.ui.mouse, {
444446
}
445447

446448
var mod = d === "absolute" ? 1 : -1,
449+
document = this.document[ 0 ],
447450
scroll = this.cssPosition === "absolute" && !( this.scrollParent[ 0 ] !== document && $.contains( this.scrollParent[ 0 ], this.offsetParent[ 0 ] ) ) ? this.offsetParent : this.scrollParent;
448451

449452
//Cache the scroll
@@ -472,6 +475,7 @@ $.widget("ui.draggable", $.ui.mouse, {
472475

473476
var containment, co, top, left,
474477
o = this.options,
478+
document = this.document[ 0 ],
475479
scroll = this.cssPosition === "absolute" && !( this.scrollParent[ 0 ] !== document && $.contains( this.scrollParent[ 0 ], this.offsetParent[ 0 ] ) ) ? this.offsetParent : this.scrollParent,
476480
pageX = event.pageX,
477481
pageY = event.pageY;
@@ -778,17 +782,17 @@ $.ui.plugin.add("draggable", "opacity", {
778782

779783
$.ui.plugin.add("draggable", "scroll", {
780784
start: function( event, ui, i ) {
781-
if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") {
785+
if( i.scrollParent[ 0 ] !== i.document[ 0 ] && i.scrollParent[ 0 ].tagName !== "HTML" ) {
782786
i.overflowOffset = i.scrollParent.offset();
783787
}
784788
},
785789
drag: function( event, ui, i ) {
786790

787791
var o = i.options,
788-
scrolled = false;
789-
790-
if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") {
792+
scrolled = false,
793+
document = i.document[ 0 ];
791794

795+
if( i.scrollParent[ 0 ] !== document && i.scrollParent[ 0 ].tagName !== "HTML" ) {
792796
if(!o.axis || o.axis !== "x") {
793797
if((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) {
794798
i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed;

ui/jquery.ui.mouse.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $.widget("ui.mouse", {
4848
_mouseDestroy: function() {
4949
this.element.unbind("."+this.widgetName);
5050
if ( this._mouseMoveDelegate ) {
51-
$(document)
51+
this.document
5252
.unbind("mousemove."+this.widgetName, this._mouseMoveDelegate)
5353
.unbind("mouseup."+this.widgetName, this._mouseUpDelegate);
5454
}
@@ -99,9 +99,10 @@ $.widget("ui.mouse", {
9999
this._mouseUpDelegate = function(event) {
100100
return that._mouseUp(event);
101101
};
102-
$(document)
103-
.bind("mousemove."+this.widgetName, this._mouseMoveDelegate)
104-
.bind("mouseup."+this.widgetName, this._mouseUpDelegate);
102+
103+
this.document
104+
.bind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
105+
.bind( "mouseup." + this.widgetName, this._mouseUpDelegate );
105106

106107
event.preventDefault();
107108

@@ -114,6 +115,10 @@ $.widget("ui.mouse", {
114115
if ($.ui.ie && ( !document.documentMode || document.documentMode < 9 ) && !event.button) {
115116
return this._mouseUp(event);
116117
}
118+
// Iframe mouseup check - mouseup occurred in another document
119+
else if ( !event.which ) {
120+
return this._mouseUp( event );
121+
}
117122

118123
if (this._mouseStarted) {
119124
this._mouseDrag(event);
@@ -130,9 +135,9 @@ $.widget("ui.mouse", {
130135
},
131136

132137
_mouseUp: function(event) {
133-
$(document)
134-
.unbind("mousemove."+this.widgetName, this._mouseMoveDelegate)
135-
.unbind("mouseup."+this.widgetName, this._mouseUpDelegate);
138+
this.document
139+
.unbind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
140+
.unbind( "mouseup." + this.widgetName, this._mouseUpDelegate );
136141

137142
if (this._mouseStarted) {
138143
this._mouseStarted = false;
@@ -144,6 +149,7 @@ $.widget("ui.mouse", {
144149
this._mouseStop(event);
145150
}
146151

152+
mouseHandled = false;
147153
return false;
148154
},
149155

0 commit comments

Comments
 (0)