16
16
*/
17
17
( function ( $ , undefined ) {
18
18
19
- /*jshint onevar: false, curly: false, eqeqeq: false, laxbreak: true */
20
19
$ . widget ( "ui.droppable" , {
21
20
version : "@VERSION" ,
22
21
widgetEventPrefix : "drop" ,
@@ -31,8 +30,11 @@ $.widget("ui.droppable", {
31
30
} ,
32
31
_create : function ( ) {
33
32
34
- var o = this . options , accept = o . accept ;
35
- this . isover = 0 ; this . isout = 1 ;
33
+ var o = this . options ,
34
+ accept = o . accept ;
35
+
36
+ this . isover = false ;
37
+ this . isout = true ;
36
38
37
39
this . accept = $ . isFunction ( accept ) ? accept : function ( d ) {
38
40
return d . is ( accept ) ;
@@ -50,17 +52,21 @@ $.widget("ui.droppable", {
50
52
} ,
51
53
52
54
_destroy : function ( ) {
53
- var drop = $ . ui . ddmanager . droppables [ this . options . scope ] ;
54
- for ( var i = 0 ; i < drop . length ; i ++ )
55
- if ( drop [ i ] == this )
55
+ var i = 0 ,
56
+ drop = $ . ui . ddmanager . droppables [ this . options . scope ] ;
57
+
58
+ for ( ; i < drop . length ; i ++ ) {
59
+ if ( drop [ i ] === this ) {
56
60
drop . splice ( i , 1 ) ;
61
+ }
62
+ }
57
63
58
64
this . element . removeClass ( "ui-droppable ui-droppable-disabled" ) ;
59
65
} ,
60
66
61
67
_setOption : function ( key , value ) {
62
68
63
- if ( key == 'accept' ) {
69
+ if ( key === 'accept' ) {
64
70
this . accept = $ . isFunction ( value ) ? value : function ( d ) {
65
71
return d . is ( value ) ;
66
72
} ;
@@ -70,23 +76,37 @@ $.widget("ui.droppable", {
70
76
71
77
_activate : function ( event ) {
72
78
var draggable = $ . ui . ddmanager . current ;
73
- if ( this . options . activeClass ) this . element . addClass ( this . options . activeClass ) ;
74
- ( draggable && this . _trigger ( 'activate' , event , this . ui ( draggable ) ) ) ;
79
+ if ( this . options . activeClass ) {
80
+ this . element . addClass ( this . options . activeClass ) ;
81
+ }
82
+ if ( draggable ) {
83
+ this . _trigger ( 'activate' , event , this . ui ( draggable ) ) ;
84
+ }
75
85
} ,
76
86
77
87
_deactivate : function ( event ) {
78
88
var draggable = $ . ui . ddmanager . current ;
79
- if ( this . options . activeClass ) this . element . removeClass ( this . options . activeClass ) ;
80
- ( draggable && this . _trigger ( 'deactivate' , event , this . ui ( draggable ) ) ) ;
89
+ if ( this . options . activeClass ) {
90
+ this . element . removeClass ( this . options . activeClass ) ;
91
+ }
92
+ if ( draggable ) {
93
+ this . _trigger ( 'deactivate' , event , this . ui ( draggable ) ) ;
94
+ }
81
95
} ,
82
96
83
97
_over : function ( event ) {
84
98
85
99
var draggable = $ . ui . ddmanager . current ;
86
- if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] == this . element [ 0 ] ) return ; // Bail if draggable and droppable are same element
100
+
101
+ // Bail if draggable and droppable are same element
102
+ if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] === this . element [ 0 ] ) {
103
+ return ;
104
+ }
87
105
88
106
if ( this . accept . call ( this . element [ 0 ] , ( draggable . currentItem || draggable . element ) ) ) {
89
- if ( this . options . hoverClass ) this . element . addClass ( this . options . hoverClass ) ;
107
+ if ( this . options . hoverClass ) {
108
+ this . element . addClass ( this . options . hoverClass ) ;
109
+ }
90
110
this . _trigger ( 'over' , event , this . ui ( draggable ) ) ;
91
111
}
92
112
@@ -95,36 +115,52 @@ $.widget("ui.droppable", {
95
115
_out : function ( event ) {
96
116
97
117
var draggable = $ . ui . ddmanager . current ;
98
- if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] == this . element [ 0 ] ) return ; // Bail if draggable and droppable are same element
118
+
119
+ // Bail if draggable and droppable are same element
120
+ if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] === this . element [ 0 ] ) {
121
+ return ;
122
+ }
99
123
100
124
if ( this . accept . call ( this . element [ 0 ] , ( draggable . currentItem || draggable . element ) ) ) {
101
- if ( this . options . hoverClass ) this . element . removeClass ( this . options . hoverClass ) ;
125
+ if ( this . options . hoverClass ) {
126
+ this . element . removeClass ( this . options . hoverClass ) ;
127
+ }
102
128
this . _trigger ( 'out' , event , this . ui ( draggable ) ) ;
103
129
}
104
130
105
131
} ,
106
132
107
133
_drop : function ( event , custom ) {
108
134
109
- var draggable = custom || $ . ui . ddmanager . current ;
110
- if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] == this . element [ 0 ] ) return false ; // Bail if draggable and droppable are same element
135
+ var draggable = custom || $ . ui . ddmanager . current ,
136
+ childrenIntersection = false ;
137
+
138
+ // Bail if draggable and droppable are same element
139
+ if ( ! draggable || ( draggable . currentItem || draggable . element ) [ 0 ] === this . element [ 0 ] ) {
140
+ return false ;
141
+ }
111
142
112
- var childrenIntersection = false ;
113
143
this . element . find ( ":data(ui-droppable)" ) . not ( ".ui-draggable-dragging" ) . each ( function ( ) {
114
144
var inst = $ . data ( this , 'ui-droppable' ) ;
115
145
if (
116
- inst . options . greedy
117
- && ! inst . options . disabled
118
- && inst . options . scope == draggable . options . scope
119
- && inst . accept . call ( inst . element [ 0 ] , ( draggable . currentItem || draggable . element ) )
120
- && $ . ui . intersect ( draggable , $ . extend ( inst , { offset : inst . element . offset ( ) } ) , inst . options . tolerance )
146
+ inst . options . greedy &&
147
+ ! inst . options . disabled &&
148
+ inst . options . scope === draggable . options . scope &&
149
+ inst . accept . call ( inst . element [ 0 ] , ( draggable . currentItem || draggable . element ) ) &&
150
+ $ . ui . intersect ( draggable , $ . extend ( inst , { offset : inst . element . offset ( ) } ) , inst . options . tolerance )
121
151
) { childrenIntersection = true ; return false ; }
122
152
} ) ;
123
- if ( childrenIntersection ) return false ;
153
+ if ( childrenIntersection ) {
154
+ return false ;
155
+ }
124
156
125
157
if ( this . accept . call ( this . element [ 0 ] , ( draggable . currentItem || draggable . element ) ) ) {
126
- if ( this . options . activeClass ) this . element . removeClass ( this . options . activeClass ) ;
127
- if ( this . options . hoverClass ) this . element . removeClass ( this . options . hoverClass ) ;
158
+ if ( this . options . activeClass ) {
159
+ this . element . removeClass ( this . options . activeClass ) ;
160
+ }
161
+ if ( this . options . hoverClass ) {
162
+ this . element . removeClass ( this . options . hoverClass ) ;
163
+ }
128
164
this . _trigger ( 'drop' , event , this . ui ( draggable ) ) ;
129
165
return this . element ;
130
166
}
@@ -146,27 +182,28 @@ $.widget("ui.droppable", {
146
182
147
183
$ . ui . intersect = function ( draggable , droppable , toleranceMode ) {
148
184
149
- if ( ! droppable . offset ) return false ;
185
+ if ( ! droppable . offset ) {
186
+ return false ;
187
+ }
150
188
151
- var x1 = ( draggable . positionAbs || draggable . position . absolute ) . left , x2 = x1 + draggable . helperProportions . width ,
152
- y1 = ( draggable . positionAbs || draggable . position . absolute ) . top , y2 = y1 + draggable . helperProportions . height ;
153
- var l = droppable . offset . left , r = l + droppable . proportions . width ,
189
+ var draggableLeft , draggableTop ,
190
+ x1 = ( draggable . positionAbs || draggable . position . absolute ) . left , x2 = x1 + draggable . helperProportions . width ,
191
+ y1 = ( draggable . positionAbs || draggable . position . absolute ) . top , y2 = y1 + draggable . helperProportions . height ,
192
+ l = droppable . offset . left , r = l + droppable . proportions . width ,
154
193
t = droppable . offset . top , b = t + droppable . proportions . height ;
155
194
156
195
switch ( toleranceMode ) {
157
196
case 'fit' :
158
- return ( l <= x1 && x2 <= r
159
- && t <= y1 && y2 <= b ) ;
197
+ return ( l <= x1 && x2 <= r && t <= y1 && y2 <= b ) ;
160
198
case 'intersect' :
161
- return ( l < x1 + ( draggable . helperProportions . width / 2 ) // Right Half
162
- && x2 - ( draggable . helperProportions . width / 2 ) < r // Left Half
163
- && t < y1 + ( draggable . helperProportions . height / 2 ) // Bottom Half
164
- && y2 - ( draggable . helperProportions . height / 2 ) < b ) ; // Top Half
199
+ return ( l < x1 + ( draggable . helperProportions . width / 2 ) && // Right Half
200
+ x2 - ( draggable . helperProportions . width / 2 ) < r && // Left Half
201
+ t < y1 + ( draggable . helperProportions . height / 2 ) && // Bottom Half
202
+ y2 - ( draggable . helperProportions . height / 2 ) < b ) ; // Top Half
165
203
case 'pointer' :
166
- var draggableLeft = ( ( draggable . positionAbs || draggable . position . absolute ) . left + ( draggable . clickOffset || draggable . offset . click ) . left ) ,
167
- draggableTop = ( ( draggable . positionAbs || draggable . position . absolute ) . top + ( draggable . clickOffset || draggable . offset . click ) . top ) ,
168
- isOver = $ . ui . isOver ( draggableTop , draggableLeft , t , l , droppable . proportions . height , droppable . proportions . width ) ;
169
- return isOver ;
204
+ draggableLeft = ( ( draggable . positionAbs || draggable . position . absolute ) . left + ( draggable . clickOffset || draggable . offset . click ) . left ) ;
205
+ draggableTop = ( ( draggable . positionAbs || draggable . position . absolute ) . top + ( draggable . clickOffset || draggable . offset . click ) . top ) ;
206
+ return $ . ui . isOver ( draggableTop , draggableLeft , t , l , droppable . proportions . height , droppable . proportions . width ) ;
170
207
case 'touch' :
171
208
return (
172
209
( y1 >= t && y1 <= b ) || // Top edge touching
@@ -191,23 +228,35 @@ $.ui.ddmanager = {
191
228
droppables : { 'default' : [ ] } ,
192
229
prepareOffsets : function ( t , event ) {
193
230
194
- var m = $ . ui . ddmanager . droppables [ t . options . scope ] || [ ] ;
195
- var type = event ? event . type : null ; // workaround for #2317
196
- var list = ( t . currentItem || t . element ) . find ( ":data(ui-droppable)" ) . andSelf ( ) ;
231
+ var i , j ,
232
+ m = $ . ui . ddmanager . droppables [ t . options . scope ] || [ ] ,
233
+ type = event ? event . type : null , // workaround for #2317
234
+ list = ( t . currentItem || t . element ) . find ( ":data(ui-droppable)" ) . andSelf ( ) ;
197
235
198
- droppablesLoop: for ( var i = 0 ; i < m . length ; i ++ ) {
236
+ droppablesLoop: for ( i = 0 ; i < m . length ; i ++ ) {
237
+
238
+ //No disabled and non-accepted
239
+ if ( m [ i ] . options . disabled || ( t && ! m [ i ] . accept . call ( m [ i ] . element [ 0 ] , ( t . currentItem || t . element ) ) ) ) {
240
+ continue ;
241
+ }
199
242
200
- if ( m [ i ] . options . disabled || ( t && ! m [ i ] . accept . call ( m [ i ] . element [ 0 ] , ( t . currentItem || t . element ) ) ) ) continue ; //No disabled and non-accepted
201
243
// Filter out elements in the current dragged item
202
- for ( var j = 0 ; j < list . length ; j ++ ) {
203
- if ( list [ j ] == m [ i ] . element [ 0 ] ) {
244
+ for ( j = 0 ; j < list . length ; j ++ ) {
245
+ if ( list [ j ] === m [ i ] . element [ 0 ] ) {
204
246
m [ i ] . proportions . height = 0 ;
205
247
continue droppablesLoop;
206
248
}
207
249
}
208
- m [ i ] . visible = m [ i ] . element . css ( "display" ) != "none" ; if ( ! m [ i ] . visible ) continue ; //If the element is not visible, continue
209
250
210
- if ( type == "mousedown" ) m [ i ] . _activate . call ( m [ i ] , event ) ; //Activate the droppable if used directly from draggables
251
+ m [ i ] . visible = m [ i ] . element . css ( "display" ) !== "none" ;
252
+ if ( ! m [ i ] . visible ) {
253
+ continue ;
254
+ }
255
+
256
+ //Activate the droppable if used directly from draggables
257
+ if ( type === "mousedown" ) {
258
+ m [ i ] . _activate . call ( m [ i ] , event ) ;
259
+ }
211
260
212
261
m [ i ] . offset = m [ i ] . element . offset ( ) ;
213
262
m [ i ] . proportions = { width : m [ i ] . element [ 0 ] . offsetWidth , height : m [ i ] . element [ 0 ] . offsetHeight } ;
@@ -220,12 +269,16 @@ $.ui.ddmanager = {
220
269
var dropped = false ;
221
270
$ . each ( $ . ui . ddmanager . droppables [ draggable . options . scope ] || [ ] , function ( ) {
222
271
223
- if ( ! this . options ) return ;
224
- if ( ! this . options . disabled && this . visible && $ . ui . intersect ( draggable , this , this . options . tolerance ) )
272
+ if ( ! this . options ) {
273
+ return ;
274
+ }
275
+ if ( ! this . options . disabled && this . visible && $ . ui . intersect ( draggable , this , this . options . tolerance ) ) {
225
276
dropped = this . _drop . call ( this , event ) || dropped ;
277
+ }
226
278
227
279
if ( ! this . options . disabled && this . visible && this . accept . call ( this . element [ 0 ] , ( draggable . currentItem || draggable . element ) ) ) {
228
- this . isout = 1 ; this . isover = 0 ;
280
+ this . isout = true ;
281
+ this . isover = false ;
229
282
this . _deactivate . call ( this , event ) ;
230
283
}
231
284
@@ -236,51 +289,60 @@ $.ui.ddmanager = {
236
289
dragStart : function ( draggable , event ) {
237
290
//Listen for scrolling so that if the dragging causes scrolling the position of the droppables can be recalculated (see #5003)
238
291
draggable . element . parentsUntil ( "body" ) . bind ( "scroll.droppable" , function ( ) {
239
- if ( ! draggable . options . refreshPositions ) $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
292
+ if ( ! draggable . options . refreshPositions ) {
293
+ $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
294
+ }
240
295
} ) ;
241
296
} ,
242
297
drag : function ( draggable , event ) {
243
298
244
299
//If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
245
- if ( draggable . options . refreshPositions ) $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
300
+ if ( draggable . options . refreshPositions ) {
301
+ $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
302
+ }
246
303
247
304
//Run through all droppables and check their positions based on specific tolerance options
248
305
$ . each ( $ . ui . ddmanager . droppables [ draggable . options . scope ] || [ ] , function ( ) {
249
306
250
- if ( this . options . disabled || this . greedyChild || ! this . visible ) return ;
251
- var intersects = $ . ui . intersect ( draggable , this , this . options . tolerance ) ;
307
+ if ( this . options . disabled || this . greedyChild || ! this . visible ) {
308
+ return ;
309
+ }
252
310
253
- var c = ! intersects && this . isover == 1 ? 'isout' : ( intersects && this . isover === 0 ? 'isover' : null ) ;
254
- if ( ! c ) return ;
311
+ var parentInstance , scope , parent ,
312
+ intersects = $ . ui . intersect ( draggable , this , this . options . tolerance ) ,
313
+ c = ! intersects && this . isover ? 'isout' : ( intersects && ! this . isover ? 'isover' : null ) ;
314
+ if ( ! c ) {
315
+ return ;
316
+ }
255
317
256
- var parentInstance ;
257
318
if ( this . options . greedy ) {
258
319
// find droppable parents with same scope
259
- var scope = this . options . scope ;
260
- var parent = this . element . parents ( ':data(ui-droppable)' ) . filter ( function ( ) {
320
+ scope = this . options . scope ;
321
+ parent = this . element . parents ( ':data(ui-droppable)' ) . filter ( function ( ) {
261
322
return $ . data ( this , 'ui-droppable' ) . options . scope === scope ;
262
323
} ) ;
263
324
264
325
if ( parent . length ) {
265
326
parentInstance = $ . data ( parent [ 0 ] , 'ui-droppable' ) ;
266
- parentInstance . greedyChild = ( c == 'isover' ? 1 : 0 ) ;
327
+ parentInstance . greedyChild = ( c === 'isover' ) ;
267
328
}
268
329
}
269
330
270
331
// we just moved into a greedy child
271
- if ( parentInstance && c == 'isover' ) {
272
- parentInstance . isover = 0 ;
273
- parentInstance . isout = 1 ;
332
+ if ( parentInstance && c === 'isover' ) {
333
+ parentInstance . isover = false ;
334
+ parentInstance . isout = true ;
274
335
parentInstance . _out . call ( parentInstance , event ) ;
275
336
}
276
337
277
- this [ c ] = 1 ; this [ c == 'isout' ? 'isover' : 'isout' ] = 0 ;
278
- this [ c == "isover" ? "_over" : "_out" ] . call ( this , event ) ;
338
+ this [ c ] = true ;
339
+ this [ c === 'isout' ? 'isover' : 'isout' ] = false ;
340
+ this [ c === "isover" ? "_over" : "_out" ] . call ( this , event ) ;
279
341
280
342
// we just moved out of a greedy child
281
- if ( parentInstance && c == 'isout' ) {
282
- parentInstance . isout = 0 ;
283
- parentInstance . isover = 1 ;
343
+ if ( parentInstance && c === 'isout' ) {
344
+ parentInstance . isout = false ;
345
+ parentInstance . isover = true ;
284
346
parentInstance . _over . call ( parentInstance , event ) ;
285
347
}
286
348
} ) ;
@@ -289,7 +351,9 @@ $.ui.ddmanager = {
289
351
dragStop : function ( draggable , event ) {
290
352
draggable . element . parentsUntil ( "body" ) . unbind ( "scroll.droppable" ) ;
291
353
//Call prepareOffsets one final time since IE does not fire return scroll events when overflow was caused by drag (see #5003)
292
- if ( ! draggable . options . refreshPositions ) $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
354
+ if ( ! draggable . options . refreshPositions ) {
355
+ $ . ui . ddmanager . prepareOffsets ( draggable , event ) ;
356
+ }
293
357
}
294
358
} ;
295
359
0 commit comments