15
15
*/
16
16
( function ( $ , undefined ) {
17
17
18
- // create a shallow copy of an object
18
+ // Create a shallow copy of an object
19
19
function copy ( obj ) {
20
20
var prop ,
21
21
ret = { } ;
@@ -57,12 +57,12 @@ $.widget( "ui.draggable", $.ui.interaction, {
57
57
// appendTo option without a helper
58
58
// dragDimensions: saved off width and height used for various options
59
59
60
+ scrollSensitivity : 20 ,
61
+ scrollSpeed : 5 ,
62
+
60
63
_create : function ( ) {
61
64
this . _super ( ) ;
62
65
63
- this . scrollSensitivity = 20 ;
64
- this . scrollSpeed = 5 ;
65
-
66
66
// Static position elements can't be moved with top/left
67
67
if ( this . element . css ( "position" ) === "static" ) {
68
68
this . element . css ( "position" , "relative" ) ;
@@ -74,11 +74,14 @@ $.widget( "ui.draggable", $.ui.interaction, {
74
74
/** interaction interface **/
75
75
76
76
_isValidTarget : function ( element ) {
77
- var handle = this . options . handle ? element . is ( this . element . find ( this . options . handle ) ) : true ,
78
- exclude = this . options . exclude ? element . is ( this . element . find ( this . options . exclude ) ) : false ;
79
-
80
- // Enforce boolean
81
- return ! ! ( handle && ! exclude ) ;
77
+ var handle = this . options . handle ?
78
+ element . is ( this . element . find ( this . options . handle ) ) :
79
+ true ,
80
+ exclude = this . options . exclude ?
81
+ element . is ( this . element . find ( this . options . exclude ) ) :
82
+ false ;
83
+
84
+ return handle && ! exclude ;
82
85
} ,
83
86
84
87
_start : function ( event , pointerPosition ) {
@@ -88,7 +91,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
88
91
this . dragDimensions = null ;
89
92
90
93
// The actual dragging element, should always be a jQuery object
91
- this . dragEl = ( this . options . helper === true || typeof this . options . helper === "function" ) ?
94
+ this . dragEl = ( this . options . helper === true || $ . isFunction ( this . options . helper ) ) ?
92
95
this . _createHelper ( pointerPosition ) :
93
96
this . element ;
94
97
@@ -101,7 +104,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
101
104
} ;
102
105
offset = this . dragEl . offset ( ) ;
103
106
this . dragEl
104
- . appendTo ( this . _appendToEl ( ) )
107
+ . appendTo ( this . _appendTo ( ) )
105
108
. offset ( offset ) ;
106
109
}
107
110
@@ -113,7 +116,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
113
116
this . originalOffset = this . startOffset = this . dragEl . offset ( ) ;
114
117
this . originalPointer = pointerPosition ;
115
118
116
- // If not already cached within _createHelper
119
+ // If not already cached within _createHelper()
117
120
if ( ! this . dragDimensions ) {
118
121
this . _cacheDragDimensions ( this . dragEl ) ;
119
122
}
@@ -122,15 +125,16 @@ $.widget( "ui.draggable", $.ui.interaction, {
122
125
this . position = copy ( this . startPosition ) ;
123
126
this . offset = copy ( this . startOffset ) ;
124
127
125
- // Cache the offset of scrollParent, if required for _handleScrolling
126
- if ( this . scrollParent [ 0 ] !== this . document [ 0 ] && this . scrollParent [ 0 ] . tagName !== "HTML" ) {
128
+ // Cache the offset of scrollParent, if required for _handleScrolling()
129
+ if ( this . scrollParent [ 0 ] !== this . document [ 0 ] &&
130
+ this . scrollParent [ 0 ] . tagName . toUpperCase ( ) !== "HTML" ) {
127
131
this . overflowOffset = this . scrollParent . offset ( ) ;
128
132
}
129
133
130
134
this . overflow = {
131
- height : this . scrollParent [ 0 ] === this . document [ 0 ] ?
135
+ height : this . scrollParent [ 0 ] === this . document [ 0 ] ?
132
136
this . window . height ( ) : this . scrollParent . height ( ) ,
133
- width : this . scrollParent [ 0 ] === this . document [ 0 ] ?
137
+ width : this . scrollParent [ 0 ] === this . document [ 0 ] ?
134
138
this . window . width ( ) : this . scrollParent . width ( )
135
139
} ;
136
140
@@ -140,15 +144,15 @@ $.widget( "ui.draggable", $.ui.interaction, {
140
144
if ( this . _trigger ( "beforeStart" , event ,
141
145
this . _originalHash ( pointerPosition ) ) === false ) {
142
146
143
- // domPosition needs to be undone even if beforeStart is stopped
144
- // Otherwise this.dragEl will remain in the element appendTo is set to
147
+ // domPosition needs to be undone even if beforeStart is stopped,
148
+ // otherwise this.dragEl will remain in the ` appendTo` element
145
149
this . _resetDomPosition ( ) ;
146
150
return false ;
147
151
}
148
152
149
- // Save off the usual properties locally, so they can be reverted from start
150
- startCssLeft = this . dragEl . css ( "left" ) ;
151
- startCssTop = this . dragEl . css ( "top" ) ;
153
+ // Save off the usual properties locally, so they can be reverted
154
+ startCssLeft = this . dragEl . css ( "left" ) ;
155
+ startCssTop = this . dragEl . css ( "top" ) ;
152
156
startPosition = copy ( this . _getPosition ( ) ) ;
153
157
startOffset = copy ( this . offset ) ;
154
158
@@ -159,9 +163,9 @@ $.widget( "ui.draggable", $.ui.interaction, {
159
163
// If user cancels on start, don't allow dragging
160
164
if ( this . _trigger ( "start" , event ,
161
165
this . _fullHash ( pointerPosition ) ) === false ) {
162
- // domPosition needs to be undone even if start is stopped
163
- // Otherwise this.dragEl will remain in the element appendTo is set to
164
166
167
+ // domPosition needs to be undone even if start is stopped,
168
+ // otherwise this.dragEl will remain in the `appendTo` element
165
169
this . startPosition = startPosition ;
166
170
this . startOffset = startOffset ;
167
171
this . dragEl . css ( {
@@ -172,6 +176,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
172
176
this . _resetDomPosition ( ) ;
173
177
return false ;
174
178
}
179
+
175
180
this . _blockFrames ( ) ;
176
181
} ,
177
182
@@ -197,7 +202,8 @@ $.widget( "ui.draggable", $.ui.interaction, {
197
202
this . _preparePosition ( pointerPosition ) ;
198
203
199
204
// If user cancels drag, don't move the element
200
- if ( this . _trigger ( "drag" , event , this . _fullHash ( pointerPosition ) ) === false ) {
205
+ if ( this . _trigger ( "drag" , event ,
206
+ this . _fullHash ( pointerPosition ) ) === false ) {
201
207
return false ;
202
208
}
203
209
@@ -211,9 +217,10 @@ $.widget( "ui.draggable", $.ui.interaction, {
211
217
this . _preparePosition ( pointerPosition ) ;
212
218
213
219
// If user cancels stop, leave helper there
214
- if ( this . _trigger ( "stop" , event , this . _fullHash ( pointerPosition ) ) !== false ) {
220
+ if ( this . _trigger ( "stop" , event ,
221
+ this . _fullHash ( pointerPosition ) ) !== false ) {
215
222
if ( this . options . helper ) {
216
- delete this . element . data ( "ui-draggable" ) . helper ;
223
+ delete this . helper ;
217
224
this . dragEl . remove ( ) ;
218
225
}
219
226
this . _resetDomPosition ( ) ;
@@ -225,33 +232,30 @@ $.widget( "ui.draggable", $.ui.interaction, {
225
232
/** internal **/
226
233
227
234
_createHelper : function ( pointerPosition ) {
228
- var helper ,
229
- offset = this . element . offset ( ) ,
235
+ var offset = this . element . offset ( ) ,
230
236
xPos = ( pointerPosition . x - offset . left ) / this . element . outerWidth ( ) ,
231
237
yPos = ( pointerPosition . y - offset . top ) / this . element . outerHeight ( ) ;
232
238
233
- // clone
239
+ // Clone
234
240
if ( this . options . helper === true ) {
235
- helper = this . element . clone ( )
241
+ this . helper = this . element . clone ( )
236
242
. removeAttr ( "id" )
237
243
. find ( "[id]" )
238
244
. removeAttr ( "id" )
239
245
. end ( ) ;
240
246
} else {
241
247
// TODO: figure out the signature for this; see #4957
242
- helper = $ ( this . options . helper ( ) ) ;
248
+ this . helper = $ ( this . options . helper ( ) ) ;
243
249
}
244
250
245
251
// Ensure the helper is in the DOM; obey the appendTo option if it exists
246
- if ( this . options . appendTo || ! helper . closest ( "body" ) . length ) {
247
- helper . appendTo ( this . _appendToEl ( ) || this . document [ 0 ] . body ) ;
252
+ if ( this . options . appendTo || ! this . helper . closest ( "body" ) . length ) {
253
+ this . helper . appendTo ( this . _appendTo ( ) || this . document [ 0 ] . body ) ;
248
254
}
249
255
250
- this . element . data ( "ui-draggable" ) . helper = helper ;
251
-
252
- this . _cacheDragDimensions ( helper ) ;
256
+ this . _cacheDragDimensions ( this . helper ) ;
253
257
254
- return helper
258
+ return this . helper
255
259
// Helper must be absolute to function properly
256
260
. css ( "position" , "absolute" )
257
261
. offset ( {
@@ -260,15 +264,15 @@ $.widget( "ui.draggable", $.ui.interaction, {
260
264
} ) ;
261
265
} ,
262
266
263
- _cacheDragDimensions : function ( el ) {
267
+ _cacheDragDimensions : function ( element ) {
264
268
this . dragDimensions = {
265
- width : el . outerWidth ( ) ,
266
- height : el . outerHeight ( )
269
+ width : element . outerWidth ( ) ,
270
+ height : element . outerHeight ( )
267
271
} ;
268
272
} ,
269
273
270
274
// TODO: Remove after 2.0, only used for backCompat
271
- _appendToEl : function ( ) {
275
+ _appendTo : function ( ) {
272
276
return this . options . appendTo ;
273
277
} ,
274
278
@@ -288,11 +292,12 @@ $.widget( "ui.draggable", $.ui.interaction, {
288
292
return position ;
289
293
}
290
294
291
- // When using relative, css values are checked
292
- // Otherwise the position wouldn't account for padding on ancestors
295
+ // When using relative, css values are checked,
296
+ // otherwise the position wouldn't account for padding on ancestors
293
297
left = this . dragEl . css ( "left" ) ;
294
298
top = this . dragEl . css ( "top" ) ;
295
299
300
+ // TODO: add proper support comment
296
301
// Webkit will give back auto if there is no explicit value
297
302
left = ( left === "auto" ) ? 0 : parseInt ( left , 10 ) ;
298
303
top = ( top === "auto" ) ? 0 : parseInt ( top , 10 ) ;
@@ -321,7 +326,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
321
326
yBottom = this . overflow . height + overflowTop - pointerPosition . y ,
322
327
yTop = pointerPosition . y - overflowTop ,
323
328
324
- // accounts for change in scrollbar to modify "original" pointer so calc
329
+ // Accounts for change in scrollbar to modify "original" pointer
325
330
change ;
326
331
327
332
// Handle vertical scrolling
@@ -362,7 +367,8 @@ $.widget( "ui.draggable", $.ui.interaction, {
362
367
return this . scrollSpeed + Math . round ( distance / 2 ) ;
363
368
} ,
364
369
365
- // Uses event to determine new position of draggable, before any override from callbacks
370
+ // Uses event to determine new position of draggable, before any override
371
+ // from callbacks
366
372
// TODO: handle absolute element inside relative parent like a relative element
367
373
_preparePosition : function ( pointerPosition ) {
368
374
var leftDiff = pointerPosition . x - this . originalPointer . x ,
@@ -387,14 +393,15 @@ $.widget( "ui.draggable", $.ui.interaction, {
387
393
this . offset . top = this . startOffset . top + topDiff ;
388
394
} ,
389
395
390
- // Places draggable where event, or user via event/callback, indicates
396
+ // Places draggable where event indicates
391
397
_setCss : function ( ) {
392
398
var newLeft = this . position . left ,
393
399
newTop = this . position . top ;
394
400
395
401
// User overriding left/top so shortcut math is no longer valid
396
402
if ( this . tempPosition . left !== this . position . left ||
397
403
this . tempPosition . top !== this . position . top ) {
404
+
398
405
// Reset offset based on difference of expected and overridden values
399
406
this . offset . left += newLeft - this . tempPosition . left ;
400
407
this . offset . top += newTop - this . tempPosition . top ;
@@ -445,7 +452,7 @@ $.widget( "ui.draggable", $.ui.interaction, {
445
452
height : iframe . outerHeight ( )
446
453
} )
447
454
. appendTo ( iframe . parent ( ) )
448
- . offset ( iframe . offset ( ) ) [ 0 ] ;
455
+ . offset ( iframe . offset ( ) ) [ 0 ] ;
449
456
} ) ;
450
457
} ,
451
458
@@ -462,12 +469,8 @@ $.widget( "ui.draggable", $.ui.interaction, {
462
469
}
463
470
} ) ;
464
471
472
+ // Add containment option via extension
465
473
$ . widget ( "ui.draggable" , $ . ui . draggable , {
466
-
467
- // $.widget doesn't know how to handle redefinitions with a custom prefix
468
- // custom prefixes are going away anyway, so it's not worth fixing right now
469
- widgetEventPrefix : "drag" ,
470
-
471
474
options : {
472
475
containment : null
473
476
} ,
@@ -492,11 +495,11 @@ $.widget( "ui.draggable", $.ui.draggable, {
492
495
if ( $ . isArray ( container ) ) {
493
496
offset = container . offset ( ) ;
494
497
left = offset . left +
495
- ( parseFloat ( $ . css ( container [ 0 ] , "borderLeftWidth" , true ) ) || 0 ) +
496
- ( parseFloat ( $ . css ( container [ 0 ] , "paddingLeft" , true ) ) || 0 ) ;
498
+ ( parseFloat ( $ . css ( container [ 0 ] , "borderLeftWidth" , true ) ) || 0 ) +
499
+ ( parseFloat ( $ . css ( container [ 0 ] , "paddingLeft" , true ) ) || 0 ) ;
497
500
top = offset . top +
498
- ( parseFloat ( $ . css ( container [ 0 ] , "borderTopWidth" , true ) ) || 0 ) +
499
- ( parseFloat ( $ . css ( container [ 0 ] , "paddingTop" , true ) ) || 0 ) ;
501
+ ( parseFloat ( $ . css ( container [ 0 ] , "borderTopWidth" , true ) ) || 0 ) +
502
+ ( parseFloat ( $ . css ( container [ 0 ] , "paddingTop" , true ) ) || 0 ) ;
500
503
right = left + container . width ( ) ;
501
504
bottom = top + container . height ( ) ;
502
505
} else {
@@ -555,7 +558,7 @@ $.widget( "ui.draggable", $.ui.draggable, {
555
558
}
556
559
} ) ;
557
560
558
- } ) ( jQuery ) ;
561
+
559
562
560
563
// DEPRECATED
561
564
if ( $ . uiBackCompat !== false ) {
@@ -564,7 +567,7 @@ if ( $.uiBackCompat !== false ) {
564
567
$ . widget ( "ui.draggable" , $ . ui . draggable , {
565
568
566
569
// Helper passed in since _createHelper calls this before dragEl is set
567
- _appendToEl : function ( ) {
570
+ _appendTo : function ( ) {
568
571
var el = this . options . appendTo ;
569
572
570
573
// This should only happen via _createHelper
@@ -949,7 +952,7 @@ if ( $.uiBackCompat !== false ) {
949
952
scrollSpeed : 20 ,
950
953
scrollSensitivity : 20
951
954
} ,
952
- _create : function ( ) {
955
+ _create : function ( ) {
953
956
var self = this ,
954
957
handleScroll = this . _handleScrolling ,
955
958
speed = this . _speed ;
@@ -1260,3 +1263,5 @@ if ( $.uiBackCompat !== false ) {
1260
1263
}
1261
1264
} ) ;
1262
1265
}
1266
+
1267
+ } ) ( jQuery ) ;
0 commit comments