1
1
/*
2
- * jQuery UI Draggable 2.0.0
2
+ * jQuery UI Draggable @VERSION
3
3
*
4
4
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
5
5
* Dual licensed under the MIT or GPL Version 2 licenses.
6
6
* http://jquery.org/license
7
7
*
8
- * http://docs.jquery.com/UI/Draggables
8
+ * http://docs.jquery.com/UI/Draggable
9
9
*
10
10
* Depends:
11
11
* jquery.ui.core.js
14
14
( function ( $ , undefined ) {
15
15
16
16
$ . widget ( "ui.draggable" , {
17
-
17
+ version : "@VERSION" ,
18
18
widgetEventPrefix : "drag" ,
19
19
20
20
options : {
21
-
22
- scrollSpeed : 20 ,
23
- scrollSensitivity :20 ,
24
- helper : false
25
-
21
+ helper : false ,
22
+ scrollSensitivity : 20 ,
23
+ scrollSpeed : 20
26
24
} ,
27
25
28
- // TODO: actually remove data
29
- destroy : function ( ) {
30
- return this ;
31
- } ,
26
+ // dragEl: element being dragged (original or helper)
27
+ // position: CSS position of dragEl
28
+ // offset: offset of dragEl
29
+ // startCoords: clientX/Y of the mousedown (offset of pointer)
30
+ // startPosition: CSS position prior to drag start
31
+ // startOffset: offset prior to drag start
32
+ // overflowOffset: offset of scroll parent
33
+ // overflowHeight: height of scroll parent
34
+ // overflowWidth: width of scroll parent
32
35
33
36
_create : function ( ) {
34
-
35
- // Either initialized element or the helper
36
- this . dragEl = false ,
37
-
38
- this . position = { } ,
39
- this . offset = { } ,
40
-
41
- // Start X/Y coords of mouse before drag
42
- this . startCoords = { } ,
43
-
44
- // Start position of element before drag
45
- this . startPosition = { } ,
46
-
47
- // Start offset of element before drag
48
- this . startOffset = { } ,
37
+ // TODO: add these to the base widget
38
+ this . doc = $ ( this . element [ 0 ] . ownerDocument ) ;
39
+ this . win = $ ( this . doc [ 0 ] . defaultView ) ;
49
40
50
41
this . scrollParent = this . element . scrollParent ( ) ;
51
42
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
-
61
- // Static position elements can"t be moved with top/left
43
+ // Static position elements can't be moved with top/left
62
44
if ( this . element . css ( "position" ) === "static" ) {
63
45
this . element . css ( "position" , "relative" ) ;
64
46
}
65
-
66
- // Using proxy to avoid anon functions using self to pass "this" along
67
- this . element . bind ( "mousedown." + this . widgetName , $ . proxy ( this . _mouseDown , this ) ) ;
68
-
69
- } ,
70
47
71
- _usingHelper : function ( ) {
72
- return ( this . options . helper === true || typeof this . options . helper === 'function' ) ;
48
+ // TODO: use _bind()
49
+ this . element . bind ( "mousedown." + this . widgetName , $ . proxy ( this , "_mouseDown" ) ) ;
73
50
} ,
74
51
75
- _setPosition : function ( ) {
76
-
52
+ // TODO: why is relative handled differently than fixed/absolute?
53
+ _getPosition : function ( ) {
77
54
var left , top , position ,
78
- scrollTop = this . scrollParent . scrollTop ( ) ,
79
- scrollLeft = this . scrollParent . scrollLeft ( ) ;
80
-
81
- // Helper is appended to body so offset of element is all that's needed
82
- if ( this . _usingHelper ( ) ) {
83
- return this . element . offset ( ) ;
84
- }
55
+ scrollTop = this . scrollParent . scrollTop ( ) ,
56
+ scrollLeft = this . scrollParent . scrollLeft ( ) ;
85
57
86
58
// If fixed or absolute
87
59
if ( this . cssPosition !== "relative" ) {
88
-
89
60
position = this . dragEl . position ( ) ;
90
61
91
62
// Take into account scrollbar
92
- position . top = position . top - scrollTop ;
93
- position . left = position . left - scrollLeft
63
+ position . top -= scrollTop ;
64
+ position . left -= scrollLeft
94
65
95
66
return position ;
96
-
97
67
}
98
68
99
- /** When using relative, css values are checked **/
100
-
69
+ // When using relative, css values are checked
101
70
left = this . dragEl . css ( "left" ) ;
102
71
top = this . dragEl . css ( "top" ) ;
103
72
@@ -106,64 +75,42 @@ $.widget( "ui.draggable", {
106
75
top = ( top === "auto" ) ? 0 : parseInt ( top , 10 ) ;
107
76
108
77
return {
109
-
110
78
left : left - scrollLeft ,
111
79
top : top - scrollTop
112
-
113
80
} ;
114
-
115
81
} ,
116
82
117
83
_mouseDown : function ( event ) {
118
-
119
- // Stop browser from highlighting, among other things
120
- event . preventDefault ( ) ;
84
+ // Prevent text selection, among other things
85
+ event . preventDefault ( ) ;
121
86
122
87
// The actual dragging element, should always be a jQuery object
123
88
this . dragEl = this . element ;
124
-
125
89
this . cssPosition = this . dragEl . css ( "position" ) ;
126
90
127
- // Helper required, so clone, hide, and set reference
128
- if ( this . _usingHelper ( ) ) {
129
-
130
- // If getting a cloned helper
91
+ // Helper required
92
+ if ( this . options . helper ) {
93
+ // clone
131
94
if ( this . options . helper === true ) {
132
-
133
- this . dragEl = this . element . clone ( ) ;
134
-
135
95
// If source element has an ID, change ID of helper to avoid overlap
96
+ this . dragEl = this . element . clone ( ) ;
136
97
if ( this . element . attr ( "id" ) ) {
137
-
138
98
this . dragEl . attr ( "id" , this . element . attr ( "id" ) + "-" + this . widgetName ) ;
139
-
140
99
}
141
-
142
100
} else {
143
-
144
- this . dragEl = this . options . helper ( ) ;
145
-
146
- // If function was passed, it should return a DOMElement
147
- if ( typeof this . dragEl . nodeType !== 'number' ) {
148
- throw "Helper function must return a DOMElement" ;
149
- }
150
-
151
- this . dragEl = $ ( this . dragEl ) ;
152
-
101
+ // TODO: figure out the signature for this; see #4957
102
+ this . dragEl = $ ( this . options . helper ( ) ) ;
153
103
}
154
104
155
- // Automatically make helper absolute
156
105
this . dragEl
157
- . css ( {
158
- position : "absolute"
159
- } ) ;
160
-
161
- $ ( "body" ) . append ( this . dragEl ) ;
162
-
106
+ // TODO: should we move this to the stylesheet and use a class?
107
+ . css ( "position" , "absolute" )
108
+ . appendTo ( this . doc [ 0 ] . body )
109
+ . offset ( this . element . offset ( ) ) ;
163
110
}
164
111
165
112
// Cache starting absolute and relative positions
166
- this . startPosition = this . _setPosition ( ) ;
113
+ this . startPosition = this . _getPosition ( ) ;
167
114
this . startOffset = this . dragEl . offset ( ) ;
168
115
169
116
// Cache current position and offset
@@ -176,38 +123,28 @@ $.widget( "ui.draggable", {
176
123
} ;
177
124
178
125
// Cache the offset of scrollParent
126
+ // TODO: store overflow height/width in a hash instead of separate properties
179
127
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
-
183
- this . _trigger ( "start" , event ) ;
184
-
185
- $ ( document ) . bind ( "mousemove." + this . widgetName , $ . proxy ( this . _mouseMove , this ) )
186
- . bind ( "mouseup." + this . widgetName , $ . proxy ( this . _mouseUp , this ) ) ;
187
-
188
-
189
- // Set the helper up by actual element
190
- if ( this . _usingHelper ( ) ) {
191
-
192
- // get the absolute position of element so that helper will know where to go
193
- elOffset = this . element . offset ( ) ;
194
-
195
- this . dragEl . css ( {
196
- display : "block" ,
197
- top : elOffset . top + "px" ,
198
- left : elOffset . left + "px"
199
- } ) ;
200
-
201
- }
202
-
128
+ this . overflowHeight = ( this . scrollParent [ 0 ] === this . doc [ 0 ] ) ?
129
+ this . win . height ( ) : this . scrollParent . height ( ) ;
130
+ this . overflowWidth = ( this . scrollParent [ 0 ] === this . doc [ 0 ] ) ?
131
+ this . win . width ( ) : this . scrollParent . width ( ) ;
132
+
133
+ // TODO: allow modifying position, just like during drag
134
+ this . _trigger ( "start" , event , this . _uiHash ( ) ) ;
135
+
136
+ // TODO: use ._bind()
137
+ // TODO: rename _bind() to _on(); add _off()
138
+ this . doc
139
+ . bind ( "mousemove." + this . widgetName , $ . proxy ( this , "_mouseMove" ) )
140
+ . bind ( "mouseup." + this . widgetName , $ . proxy ( this , "_mouseUp" ) ) ;
203
141
} ,
204
142
205
143
_mouseMove : function ( event ) {
206
-
207
144
var leftDiff = event . clientX - this . startCoords . left ,
208
- topDiff = event . clientY - this . startCoords . top ,
209
- newLeft = leftDiff + this . startPosition . left ,
210
- newTop = topDiff + this . startPosition . top ;
145
+ topDiff = event . clientY - this . startCoords . top ,
146
+ newLeft = leftDiff + this . startPosition . left ,
147
+ newTop = topDiff + this . startPosition . top ;
211
148
212
149
this . position = {
213
150
left : newLeft ,
@@ -218,87 +155,67 @@ $.widget( "ui.draggable", {
218
155
this . offset . left = this . startOffset . left + newLeft ;
219
156
this . offset . top = this . startOffset . top + newTop ;
220
157
221
- this . _trigger ( "drag" , event ) ;
158
+ this . _trigger ( "drag" , event , this . _uiHash ( ) ) ;
222
159
223
160
// User overriding left/top so shortcut math is no longer valid
224
161
if ( newLeft !== this . position . left || newTop !== this . position . top ) {
225
-
162
+ // TODO: can we just store the previous offset values
163
+ // and not go through .offset()?
226
164
// refresh offset using slower functions
227
165
this . offset = this . dragEl . offset ( ) ;
228
-
229
166
}
230
-
167
+
231
168
newLeft = this . position . left ;
232
169
newTop = this . position . top ;
233
-
234
- if ( this . cssPosition !== 'fixed' ) {
235
-
170
+
171
+ // TODO: does this work with nested scrollable parents?
172
+ if ( this . cssPosition !== "fixed" ) {
236
173
newLeft = newLeft + this . scrollParent . scrollLeft ( ) ;
237
174
newTop = newTop + this . scrollParent . scrollTop ( ) ;
238
-
239
175
}
240
176
241
177
this . dragEl . css ( {
242
-
243
178
left : newLeft + "px" ,
244
179
top : newTop + "px"
245
-
246
180
} ) ;
247
-
181
+
248
182
// Scroll the scrollParent, if needed
249
183
this . _handleScrolling ( event ) ;
250
-
251
184
} ,
252
185
253
186
_handleScrolling : function ( event ) {
254
-
255
- var doc = $ ( document ) ,
256
- scrollTop = doc . scrollTop ( ) ,
257
- scrollLeft = doc . scrollLeft ( ) ;
187
+ var scrollTop = this . doc . scrollTop ( ) ,
188
+ scrollLeft = this . doc . scrollLeft ( ) ;
258
189
259
190
// Handle vertical scrolling
260
191
if ( ( ( this . overflowHeight + scrollTop ) - event . pageY ) < this . options . scrollSensitivity ) {
261
- doc . scrollTop ( scrollTop + this . options . scrollSpeed ) ;
192
+ this . doc . scrollTop ( scrollTop + this . options . scrollSpeed ) ;
262
193
}
263
-
194
+
264
195
// Handle horizontal scrolling
265
196
if ( ( ( this . overflowWidth + scrollLeft ) - event . pageX ) < this . options . scrollSensitivity ) {
266
- doc . scrollLeft ( scrollLeft + this . options . scrollSpeed ) ;
197
+ this . doc . scrollLeft ( scrollLeft + this . options . scrollSpeed ) ;
267
198
}
268
-
269
199
} ,
270
200
271
201
_mouseUp : function ( event ) {
202
+ this . _trigger ( "stop" , event , this . _uiHash ( ) ) ;
272
203
273
- var doc = $ ( document ) ;
274
-
275
- this . _trigger ( "stop" , event ) ;
276
-
277
- this . startCoords = { } ;
278
-
279
- if ( this . _usingHelper ( ) ) {
204
+ if ( this . options . helper ) {
280
205
this . dragEl . remove ( ) ;
281
206
}
282
207
283
- doc . unbind ( "mousemove." + this . widgetName ) ;
284
- doc . unbind ( "mouseup ." + this . widgetName ) ;
285
-
208
+ this . doc
209
+ . unbind ( "mousemove ." + this . widgetName )
210
+ . unbind ( "mouseup." + this . widgetName ) ;
286
211
} ,
287
212
288
- _uiHash : function ( event ) {
289
-
213
+ _uiHash : function ( event ) {
290
214
return {
291
215
position : this . position ,
292
216
offset : this . offset
293
217
} ;
294
-
295
218
}
296
-
297
- } ) ;
298
-
299
- $ . extend ( $ . ui . draggable , {
300
- version : "2.0.0"
301
219
} ) ;
302
220
303
-
304
- } ) ( jQuery ) ;
221
+ } ) ( jQuery ) ;
0 commit comments