@@ -19,6 +19,8 @@ $.widget( "ui.draggable", {
19
19
20
20
options : {
21
21
22
+ scrollSpeed : 20 ,
23
+ scrollSensitivity :20 ,
22
24
helper : false
23
25
24
26
} ,
@@ -47,43 +49,48 @@ $.widget( "ui.draggable", {
47
49
48
50
this . scrollParent = this . element . scrollParent ( ) ;
49
51
52
+ // Offset of scrollParent, used for auto-scrolling
53
+ this . overflowOffset = { } ;
54
+
55
+ // Height of scrollParent, used for auto-scrolling
56
+ this . overflowHeight = 0 ;
57
+
58
+ // Width of scrollParent, used for auto-scrolling
59
+ this . overflowWidth = 0 ;
60
+
50
61
// Static position elements can"t be moved with top/left
51
62
if ( this . element . css ( "position" ) === "static" ) {
52
63
this . element . css ( "position" , "relative" ) ;
53
64
}
54
-
65
+
55
66
// Using proxy to avoid anon functions using self to pass "this" along
56
67
this . element . bind ( "mousedown." + this . widgetName , $ . proxy ( this . _mouseDown , this ) ) ;
57
68
58
69
} ,
59
-
70
+
60
71
_usingHelper : function ( ) {
61
72
return ( this . options . helper === true || typeof this . options . helper === 'function' ) ;
62
73
} ,
63
74
64
75
_setPosition : function ( ) {
65
76
66
- var left , top , position , cssPosition ;
77
+ var left , top , position ,
78
+ scrollTop = this . scrollParent . scrollTop ( ) ,
79
+ scrollLeft = this . scrollParent . scrollLeft ( ) ;
67
80
68
81
// Helper is appended to body so offset of element is all that's needed
69
82
if ( this . _usingHelper ( ) ) {
70
83
return this . element . offset ( ) ;
71
84
}
72
85
73
- cssPosition = this . dragEl . css ( "position" ) ; ;
74
-
75
86
// If fixed or absolute
76
- if ( cssPosition !== "relative" ) {
87
+ if ( this . cssPosition !== "relative" ) {
77
88
78
89
position = this . dragEl . position ( ) ;
79
90
80
- if ( cssPosition === "absolute" ) {
81
- return position ;
82
- }
83
-
84
- // Take into account scrollbar for fixed position
85
- position . top = position . top - this . scrollParent . scrollTop ( ) ;
86
- position . left = position . left - this . scrollParent . scrollLeft ( ) ;
91
+ // Take into account scrollbar
92
+ position . top = position . top - scrollTop ;
93
+ position . left = position . left - scrollLeft
87
94
88
95
return position ;
89
96
@@ -100,20 +107,22 @@ $.widget( "ui.draggable", {
100
107
101
108
return {
102
109
103
- left : left ,
104
- top : top
110
+ left : left - scrollLeft ,
111
+ top : top - scrollTop
105
112
106
113
} ;
107
114
108
115
} ,
109
116
110
117
_mouseDown : function ( event ) {
111
-
118
+
112
119
// Stop browser from highlighting, among other things
113
120
event . preventDefault ( ) ;
114
121
115
122
// The actual dragging element, should always be a jQuery object
116
123
this . dragEl = this . element ;
124
+
125
+ this . cssPosition = this . dragEl . css ( "position" ) ;
117
126
118
127
// Helper required, so clone, hide, and set reference
119
128
if ( this . _usingHelper ( ) ) {
@@ -131,16 +140,16 @@ $.widget( "ui.draggable", {
131
140
}
132
141
133
142
} else {
134
-
143
+
135
144
this . dragEl = this . options . helper ( ) ;
136
-
145
+
137
146
// If function was passed, it should return a DOMElement
138
147
if ( typeof this . dragEl . nodeType !== 'number' ) {
139
148
throw "Helper function must return a DOMElement" ;
140
149
}
141
-
150
+
142
151
this . dragEl = $ ( this . dragEl ) ;
143
-
152
+
144
153
}
145
154
146
155
// Automatically make helper absolute
@@ -166,10 +175,15 @@ $.widget( "ui.draggable", {
166
175
top : event . clientY
167
176
} ;
168
177
178
+ // Cache the offset of scrollParent
179
+ this . overflowOffset = this . scrollParent . offset ( ) ;
180
+ this . overflowHeight = ( this . scrollParent [ 0 ] === document ) ? $ ( window ) . height ( ) : this . scrollParent . height ( ) ;
181
+ this . overflowWidth = ( this . scrollParent [ 0 ] === document ) ? $ ( window ) . width ( ) : this . scrollParent . width ( ) ;
182
+
169
183
this . _trigger ( "start" , event ) ;
170
184
171
- $ ( document ) . bind ( "mousemove." + this . widgetName , $ . proxy ( this . _mouseMove , this ) ) ;
172
- $ ( document ) . bind ( "mouseup." + this . widgetName , $ . proxy ( this . _mouseUp , this ) ) ;
185
+ $ ( document ) . bind ( "mousemove." + this . widgetName , $ . proxy ( this . _mouseMove , this ) )
186
+ . bind ( "mouseup." + this . widgetName , $ . proxy ( this . _mouseUp , this ) ) ;
173
187
174
188
175
189
// Set the helper up by actual element
@@ -189,11 +203,11 @@ $.widget( "ui.draggable", {
189
203
} ,
190
204
191
205
_mouseMove : function ( event ) {
192
-
206
+
193
207
var leftDiff = event . clientX - this . startCoords . left ,
194
- topDiff = event . clientY - this . startCoords . top ,
195
- newLeft = leftDiff + this . startPosition . left ,
196
- newTop = topDiff + this . startPosition . top ;
208
+ topDiff = event . clientY - this . startCoords . top ,
209
+ newLeft = leftDiff + this . startPosition . left ,
210
+ newTop = topDiff + this . startPosition . top ;
197
211
198
212
this . position = {
199
213
left : newLeft ,
@@ -213,18 +227,51 @@ $.widget( "ui.draggable", {
213
227
this . offset = this . dragEl . offset ( ) ;
214
228
215
229
}
230
+
231
+ newLeft = this . position . left ;
232
+ newTop = this . position . top ;
233
+
234
+ if ( this . cssPosition !== 'fixed' ) {
235
+
236
+ newLeft = newLeft + this . scrollParent . scrollLeft ( ) ;
237
+ newTop = newTop + this . scrollParent . scrollTop ( ) ;
238
+
239
+ }
216
240
217
241
this . dragEl . css ( {
218
242
219
- left : this . position . left + "px" ,
220
- top : this . position . top + "px"
243
+ left : newLeft + "px" ,
244
+ top : newTop + "px"
221
245
222
246
} ) ;
247
+
248
+ // Scroll the scrollParent, if needed
249
+ this . _handleScrolling ( event ) ;
250
+
251
+ } ,
252
+
253
+ _handleScrolling : function ( event ) {
254
+
255
+ var doc = $ ( document ) ,
256
+ scrollTop = doc . scrollTop ( ) ,
257
+ scrollLeft = doc . scrollLeft ( ) ;
258
+
259
+ // Handle vertical scrolling
260
+ if ( ( ( this . overflowHeight + scrollTop ) - event . pageY ) < this . options . scrollSensitivity ) {
261
+ doc . scrollTop ( scrollTop + this . options . scrollSpeed ) ;
262
+ }
263
+
264
+ // Handle horizontal scrolling
265
+ if ( ( ( this . overflowWidth + scrollLeft ) - event . pageX ) < this . options . scrollSensitivity ) {
266
+ doc . scrollLeft ( scrollLeft + this . options . scrollSpeed ) ;
267
+ }
223
268
224
269
} ,
225
270
226
271
_mouseUp : function ( event ) {
227
272
273
+ var doc = $ ( document ) ;
274
+
228
275
this . _trigger ( "stop" , event ) ;
229
276
230
277
this . startCoords = { } ;
@@ -233,8 +280,8 @@ $.widget( "ui.draggable", {
233
280
this . dragEl . remove ( ) ;
234
281
}
235
282
236
- $ ( document ) . unbind ( "mousemove." + this . widgetName ) ;
237
- $ ( document ) . unbind ( "mouseup." + this . widgetName ) ;
283
+ doc . unbind ( "mousemove." + this . widgetName ) ;
284
+ doc . unbind ( "mouseup." + this . widgetName ) ;
238
285
239
286
} ,
240
287
0 commit comments