Skip to content

Commit 4792d5b

Browse files
committed
Draggable: Cleanup.
1 parent dac3ddd commit 4792d5b

File tree

1 file changed

+34
-76
lines changed

1 file changed

+34
-76
lines changed

ui/jquery.ui.draggable.js

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ $.widget( "ui.draggable", {
1818
widgetEventPrefix: "drag",
1919

2020
options: {
21-
helper: false,
21+
helper: null,
22+
// TODO: remove scroll options
2223
scrollSensitivity: 20,
23-
scrollSpeed: 20,
24-
iframeFix: false
24+
scrollSpeed: 20
2525
},
2626

2727
// dragEl: element being dragged (original or helper)
@@ -34,37 +34,28 @@ $.widget( "ui.draggable", {
3434
// overflowOffset: offset of scroll parent
3535
// overflow: object containing width and height keys of scroll parent
3636

37+
// TODO: move next to _unblockFrames()
3738
_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-
39+
var body = this.document[0].body;
40+
41+
this.iframeBlocks = this.document.find( "iframe" ).map(function() {
42+
var iframe = $( this ),
43+
iframeOffset = iframe.offset();
44+
45+
return $( "<div>" )
46+
.css({
47+
position: "absolute",
48+
width: iframe.outerWidth(),
49+
height: iframe.outerHeight(),
50+
top: iframeOffset.top,
51+
left: iframeOffset.left
52+
})
53+
.appendTo( body )[0];
6354
});
64-
6555
},
6656

6757
_create: function() {
58+
// TODO: move to drag start in case DOM changes
6859
this.scrollParent = this.element.scrollParent();
6960

7061
// Static position elements can't be moved with top/left
@@ -128,10 +119,7 @@ $.widget( "ui.draggable", {
128119
else if ( event.pageX < ( scrollLeft + this.options.scrollSensitivity ) ) {
129120
this.scrollParent.scrollLeft( scrollLeft - this.options.scrollSpeed );
130121
}
131-
132122
} else {
133-
134-
135123
// Handle vertical scrolling
136124
if ( ( event.pageY + this.options.scrollSensitivity ) > ( this.overflow.height + this.overflowOffset.top ) ) {
137125
this.scrollParent.scrollTop( scrollTop + this.options.scrollSpeed );
@@ -147,25 +135,18 @@ $.widget( "ui.draggable", {
147135
else if ( ( event.pageX - this.options.scrollSensitivity ) < this.overflowOffset.left ) {
148136
this.scrollParent.scrollLeft( scrollLeft - this.options.scrollSpeed );
149137
}
150-
151-
152138
}
153-
154139
},
155140

156141
_mouseDown: function( event ) {
157-
var newLeft, newTop, allowed;
142+
var newLeft, newTop;
158143

159144
// Prevent text selection, among other things
160145
event.preventDefault();
161146

162147
// The actual dragging element, should always be a jQuery object
163148
this.dragEl = this.element;
164149

165-
if ( this.options.iframeFix === true ) {
166-
this._blockFrames();
167-
}
168-
169150
// Helper required
170151
if ( this.options.helper ) {
171152
// clone
@@ -217,14 +198,13 @@ $.widget( "ui.draggable", {
217198

218199
this._preparePosition( event );
219200

220-
allowed = this._trigger( "start", event, this._uiHash() );
221-
222201
// If user stops propagation, leave helper there ( if there's one ), disallow any CSS changes
223-
if ( allowed !== true ) {
202+
if ( this._trigger( "start", event, this._uiHash() ) === false ) {
224203
this.document.unbind( "." + this.widgetName );
225204
return;
226205
}
227206

207+
this._blockFrames();
228208
this._setCss( event );
229209

230210
this._bind( this.document, {
@@ -234,15 +214,12 @@ $.widget( "ui.draggable", {
234214
},
235215

236216
_mouseMove: function( event ) {
237-
var newLeft, newTop, allowed;
217+
var newLeft, newTop;
238218

239219
this._preparePosition( event );
240220

241-
allowed = this._trigger( "drag", event, this._uiHash() );
242-
243-
244221
// If user stops propagation, leave helper there ( if there's one ), disallow any CSS changes
245-
if ( allowed !== true ) {
222+
if ( this._trigger( "drag", event, this._uiHash() ) === false ) {
246223
this.document.unbind( "." + this.widgetName );
247224
return;
248225
}
@@ -254,30 +231,18 @@ $.widget( "ui.draggable", {
254231
},
255232

256233
_mouseUp: function( event ) {
257-
258-
var allowed;
259-
260234
this._preparePosition( event );
261235

262-
allowed = this._trigger( "stop", event, this._uiHash() );
263-
264236
// If user stops propagation, leave helper there, disallow any CSS changes
265-
if ( allowed === true ) {
266-
237+
if ( this._trigger( "stop", event, this._uiHash() ) === false ) {
267238
this._setCss( event );
268-
269239
if ( this.options.helper ) {
270240
this.dragEl.remove();
271241
}
272-
273242
}
274243

275244
this.document.unbind( "." + this.widgetName );
276-
277-
if ( this.options.iframeFix === true ) {
278-
this._unblockFrames();
279-
}
280-
245+
this._unblockFrames();
281246
},
282247

283248
// Uses event to determine new position of draggable, before any override from callbacks
@@ -298,7 +263,7 @@ $.widget( "ui.draggable", {
298263
left: newLeft,
299264
top: newTop
300265
};
301-
266+
302267
// Refresh offset cache with new positions
303268
this.offset.left = this.startOffset.left + leftDiff;
304269
this.offset.top = this.startOffset.top + topDiff;
@@ -326,8 +291,8 @@ $.widget( "ui.draggable", {
326291
}
327292

328293
this.dragEl.css({
329-
left: newLeft + "px",
330-
top: newTop + "px"
294+
left: newLeft,
295+
top: newTop
331296
});
332297
},
333298

@@ -337,26 +302,19 @@ $.widget( "ui.draggable", {
337302
offset: this.offset
338303
};
339304

305+
// TODO: should we always set the helper?
340306
if ( this.options.helper ) {
341307
ret.helper = this.dragEl;
342308
}
343309

344310
return ret;
345-
346311
},
347312

348313
_unblockFrames: function() {
349-
350-
if ( !this.iframeBlocks || !this.iframeBlocks.length ) {
351-
return;
314+
if ( this.iframeBlocks ) {
315+
this.iframeBlocks.remove();
316+
delete this.iframeBlocks;
352317
}
353-
354-
this.iframeBlocks.each( function() {
355-
356-
$(this).remove();
357-
358-
});
359-
360318
}
361319
});
362320

0 commit comments

Comments
 (0)