Skip to content

Commit 1ca93b4

Browse files
committed
Draggable: Added in iframeFix option
1 parent 7941f7c commit 1ca93b4

File tree

1 file changed

+73
-19
lines changed

1 file changed

+73
-19
lines changed

ui/jquery.ui.draggable.js

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ $.widget( "ui.draggable", {
2020
options: {
2121
helper: false,
2222
scrollSensitivity: 20,
23-
scrollSpeed: 20
23+
scrollSpeed: 20,
24+
iframeFix: false
2425
},
2526

2627
// dragEl: element being dragged (original or helper)
@@ -33,6 +34,36 @@ $.widget( "ui.draggable", {
3334
// overflowOffset: offset of scroll parent
3435
// overflow: object containing width and height keys of scroll parent
3536

37+
_blockFrames: function() {
38+
39+
var iframes = $('iframe'),
40+
widget = this;
41+
42+
this.iframeBlocks = $('');
43+
44+
iframes.each( function() {
45+
46+
var iframe = $(this),
47+
width = iframe.outerWidth(),
48+
height = iframe.outerHeight(),
49+
iframeOffset = iframe.offset(),
50+
block = $('<div />');
51+
52+
block.css({
53+
position: 'absolute',
54+
width: width+'px',
55+
height: height+'px',
56+
top: iframeOffset.top+'px',
57+
left: iframeOffset.left+'px'
58+
})
59+
.appendTo( widget.document[0].body );
60+
61+
widget.iframeBlocks = widget.iframeBlocks.add( block );
62+
63+
});
64+
65+
},
66+
3667
_create: function() {
3768
this.scrollParent = this.element.scrollParent();
3869

@@ -78,48 +109,48 @@ $.widget( "ui.draggable", {
78109
_handleScrolling: function( event ) {
79110
var scrollTop = this.scrollParent.scrollTop(),
80111
scrollLeft = this.scrollParent.scrollLeft();
81-
112+
82113
// overflowOffset is only set when scrollParent is not doc/html
83114
if ( !this.overflowOffset ) {
84-
115+
85116
// Handle vertical scrolling
86117
if ( ( ( this.overflow.height + scrollTop ) - event.pageY ) < this.options.scrollSensitivity ) {
87118
this.scrollParent.scrollTop( scrollTop + this.options.scrollSpeed );
88119
}
89120
else if ( event.pageY < ( scrollTop + this.options.scrollSensitivity ) ) {
90121
this.scrollParent.scrollTop( scrollTop - this.options.scrollSpeed );
91122
}
92-
123+
93124
// Handle horizontal scrolling
94125
if ( ( ( this.overflow.width + scrollLeft ) - event.pageX ) < this.options.scrollSensitivity ) {
95126
this.scrollParent.scrollLeft( scrollLeft + this.options.scrollSpeed );
96127
}
97128
else if ( event.pageX < ( scrollLeft + this.options.scrollSensitivity ) ) {
98129
this.scrollParent.scrollLeft( scrollLeft - this.options.scrollSpeed );
99130
}
100-
131+
101132
} else {
102-
103-
133+
134+
104135
// Handle vertical scrolling
105136
if ( ( event.pageY + this.options.scrollSensitivity ) > ( this.overflow.height + this.overflowOffset.top ) ) {
106137
this.scrollParent.scrollTop( scrollTop + this.options.scrollSpeed );
107138
}
108139
else if ( ( event.pageY - this.options.scrollSensitivity ) < this.overflowOffset.top ) {
109140
this.scrollParent.scrollTop( scrollTop - this.options.scrollSpeed );
110141
}
111-
142+
112143
// Handle horizontal scrolling
113144
if ( ( event.pageX + this.options.scrollSensitivity ) > ( this.overflow.width + this.overflowOffset.left ) ) {
114145
this.scrollParent.scrollLeft( scrollLeft + this.options.scrollSpeed );
115146
}
116147
else if ( ( event.pageX - this.options.scrollSensitivity ) < this.overflowOffset.left ) {
117148
this.scrollParent.scrollLeft( scrollLeft - this.options.scrollSpeed );
118149
}
119-
120-
150+
151+
121152
}
122-
153+
123154
},
124155

125156
_mouseDown: function( event ) {
@@ -131,6 +162,10 @@ $.widget( "ui.draggable", {
131162
// The actual dragging element, should always be a jQuery object
132163
this.dragEl = this.element;
133164

165+
if ( this.options.iframeFix === true ) {
166+
this._blockFrames();
167+
}
168+
134169
// Helper required
135170
if ( this.options.helper ) {
136171
// clone
@@ -151,7 +186,7 @@ $.widget( "ui.draggable", {
151186
.appendTo( this.document[0].body )
152187
.offset( this.element.offset() );
153188
}
154-
189+
155190
this.cssPosition = this.dragEl.css( "position" );
156191

157192
// Cache starting absolute and relative positions
@@ -183,7 +218,7 @@ $.widget( "ui.draggable", {
183218
this._preparePosition( event );
184219

185220
allowed = this._trigger( "start", event, this._uiHash() );
186-
221+
187222
// If user stops propagation, leave helper there ( if there's one ), disallow any CSS changes
188223
if ( allowed !== true ) {
189224
this.document.unbind( "." + this.widgetName );
@@ -200,12 +235,12 @@ $.widget( "ui.draggable", {
200235

201236
_mouseMove: function( event ) {
202237
var newLeft, newTop, allowed;
203-
238+
204239
this._preparePosition( event );
205240

206241
allowed = this._trigger( "drag", event, this._uiHash() );
207-
208-
242+
243+
209244
// If user stops propagation, leave helper there ( if there's one ), disallow any CSS changes
210245
if ( allowed !== true ) {
211246
this.document.unbind( "." + this.widgetName );
@@ -219,7 +254,7 @@ $.widget( "ui.draggable", {
219254
},
220255

221256
_mouseUp: function( event ) {
222-
257+
223258
var allowed;
224259

225260
this._preparePosition( event );
@@ -238,6 +273,11 @@ $.widget( "ui.draggable", {
238273
}
239274

240275
this.document.unbind( "." + this.widgetName );
276+
277+
if ( this.options.iframeFix === true ) {
278+
this._unblockFrames();
279+
}
280+
241281
},
242282

243283
// Uses event to determine new position of draggable, before any override from callbacks
@@ -296,13 +336,27 @@ $.widget( "ui.draggable", {
296336
position: this.position
297337
// offset: this.offset
298338
};
299-
339+
300340
if ( this.options.helper ) {
301341
ret.helper = this.dragEl;
302342
}
303-
343+
304344
return ret;
345+
346+
},
347+
348+
_unblockFrames: function() {
349+
350+
if ( !this.iframeBlocks || !this.iframeBlocks.length ) {
351+
return;
352+
}
305353

354+
this.iframeBlocks.each( function() {
355+
356+
$(this).remove();
357+
358+
});
359+
306360
}
307361
});
308362

0 commit comments

Comments
 (0)