2008-06-04 02:34:33 +00:00
|
|
|
/*
|
2011-10-14 18:19:04 +00:00
|
|
|
* jQuery UI Draggable @VERSION
|
2008-06-04 02:34:33 +00:00
|
|
|
*
|
2011-01-17 14:13:18 +00:00
|
|
|
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2008-11-10 05:18:20 +00:00
|
|
|
*
|
2011-10-14 18:19:04 +00:00
|
|
|
* http://docs.jquery.com/UI/Draggable
|
2008-06-04 02:34:33 +00:00
|
|
|
*
|
|
|
|
* Depends:
|
2009-09-17 10:39:12 +00:00
|
|
|
* jquery.ui.core.js
|
2009-12-22 19:51:24 +00:00
|
|
|
* jquery.ui.widget.js
|
2008-06-04 02:34:33 +00:00
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
(function( $, undefined ) {
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2011-09-28 02:02:07 +00:00
|
|
|
$.widget( "ui.draggable", {
|
2011-10-14 18:19:04 +00:00
|
|
|
version: "@VERSION",
|
2010-02-05 03:03:50 +00:00
|
|
|
widgetEventPrefix: "drag",
|
2011-09-28 01:33:38 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
options: {
|
2011-10-14 18:19:04 +00:00
|
|
|
helper: false,
|
|
|
|
scrollSensitivity: 20,
|
|
|
|
scrollSpeed: 20
|
2008-11-21 04:01:33 +00:00
|
|
|
},
|
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// dragEl: element being dragged (original or helper)
|
|
|
|
// position: CSS position of dragEl
|
|
|
|
// offset: offset of dragEl
|
|
|
|
// startCoords: clientX/Y of the mousedown (offset of pointer)
|
|
|
|
// startPosition: CSS position prior to drag start
|
|
|
|
// startOffset: offset prior to drag start
|
|
|
|
// overflowOffset: offset of scroll parent
|
|
|
|
// overflowHeight: height of scroll parent
|
|
|
|
// overflowWidth: width of scroll parent
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
_create: function() {
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: add these to the base widget
|
|
|
|
this.doc = $( this.element[0].ownerDocument );
|
|
|
|
this.win = $( this.doc[0].defaultView );
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
this.scrollParent = this.element.scrollParent();
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// Static position elements can't be moved with top/left
|
2011-09-28 01:33:38 +00:00
|
|
|
if ( this.element.css( "position" ) === "static" ) {
|
|
|
|
this.element.css( "position", "relative" );
|
|
|
|
}
|
2011-10-09 04:54:47 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: use _bind()
|
|
|
|
this.element.bind( "mousedown." + this.widgetName, $.proxy( this, "_mouseDown" ) );
|
2011-10-01 16:38:48 +00:00
|
|
|
},
|
2008-11-23 17:42:24 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: why is relative handled differently than fixed/absolute?
|
|
|
|
_getPosition: function() {
|
2011-10-09 04:54:47 +00:00
|
|
|
var left, top, position,
|
2011-10-14 18:19:04 +00:00
|
|
|
scrollTop = this.scrollParent.scrollTop(),
|
|
|
|
scrollLeft = this.scrollParent.scrollLeft();
|
2008-11-28 15:43:32 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// If fixed or absolute
|
2011-10-09 04:54:47 +00:00
|
|
|
if ( this.cssPosition !== "relative" ) {
|
2011-09-28 01:33:38 +00:00
|
|
|
position = this.dragEl.position();
|
2008-11-28 15:43:32 +00:00
|
|
|
|
2011-10-09 04:54:47 +00:00
|
|
|
// Take into account scrollbar
|
2011-10-14 18:19:04 +00:00
|
|
|
position.top -= scrollTop;
|
|
|
|
position.left -= scrollLeft
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
return position;
|
2011-10-01 16:38:48 +00:00
|
|
|
}
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// When using relative, css values are checked
|
2011-09-28 01:33:38 +00:00
|
|
|
left = this.dragEl.css( "left" );
|
2011-10-01 16:05:36 +00:00
|
|
|
top = this.dragEl.css( "top" );
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// Webkit will give back auto if there is nothing inline yet
|
2011-10-01 16:05:36 +00:00
|
|
|
left = ( left === "auto" ) ? 0: parseInt( left, 10 );
|
|
|
|
top = ( top === "auto" ) ? 0: parseInt( top, 10 );
|
2011-09-28 01:33:38 +00:00
|
|
|
|
|
|
|
return {
|
2011-10-09 04:54:47 +00:00
|
|
|
left: left - scrollLeft,
|
|
|
|
top: top - scrollTop
|
2011-09-28 01:33:38 +00:00
|
|
|
};
|
|
|
|
},
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-01 16:05:36 +00:00
|
|
|
_mouseDown: function( event ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
// Prevent text selection, among other things
|
|
|
|
event.preventDefault();
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-01 16:38:48 +00:00
|
|
|
// The actual dragging element, should always be a jQuery object
|
2011-09-28 01:33:38 +00:00
|
|
|
this.dragEl = this.element;
|
2011-10-09 04:54:47 +00:00
|
|
|
this.cssPosition = this.dragEl.css( "position" );
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// Helper required
|
|
|
|
if ( this.options.helper ) {
|
|
|
|
// clone
|
2011-10-01 16:38:48 +00:00
|
|
|
if ( this.options.helper === true ) {
|
|
|
|
// If source element has an ID, change ID of helper to avoid overlap
|
2011-10-14 18:19:04 +00:00
|
|
|
this.dragEl = this.element.clone();
|
2011-10-01 16:38:48 +00:00
|
|
|
if ( this.element.attr( "id" ) ) {
|
|
|
|
this.dragEl.attr( "id", this.element.attr( "id" ) + "-" + this.widgetName );
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: figure out the signature for this; see #4957
|
|
|
|
this.dragEl = $( this.options.helper() );
|
2011-10-01 16:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dragEl
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: should we move this to the stylesheet and use a class?
|
|
|
|
.css( "position", "absolute" )
|
|
|
|
.appendTo( this.doc[0].body )
|
|
|
|
.offset( this.element.offset() );
|
2011-10-01 16:38:48 +00:00
|
|
|
}
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// Cache starting absolute and relative positions
|
2011-10-14 18:19:04 +00:00
|
|
|
this.startPosition = this._getPosition();
|
2011-10-01 16:05:36 +00:00
|
|
|
this.startOffset = this.dragEl.offset();
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// Cache current position and offset
|
|
|
|
this.position = $.extend( {}, this.startPosition );
|
2011-10-01 16:05:36 +00:00
|
|
|
this.offset = $.extend( {}, this.startOffset );
|
2009-01-22 13:10:18 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
this.startCoords = {
|
2011-10-01 16:05:36 +00:00
|
|
|
left: event.clientX,
|
|
|
|
top: event.clientY
|
2011-09-28 01:33:38 +00:00
|
|
|
};
|
2009-01-22 13:10:18 +00:00
|
|
|
|
2011-10-09 04:54:47 +00:00
|
|
|
// Cache the offset of scrollParent
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: store overflow height/width in a hash instead of separate properties
|
2011-10-09 04:54:47 +00:00
|
|
|
this.overflowOffset = this.scrollParent.offset();
|
2011-10-14 18:19:04 +00:00
|
|
|
this.overflowHeight = ( this.scrollParent[0] === this.doc[0] ) ?
|
|
|
|
this.win.height() : this.scrollParent.height();
|
|
|
|
this.overflowWidth = ( this.scrollParent[0] === this.doc[0] ) ?
|
|
|
|
this.win.width() : this.scrollParent.width();
|
|
|
|
|
|
|
|
// TODO: allow modifying position, just like during drag
|
|
|
|
this._trigger( "start", event, this._uiHash() );
|
|
|
|
|
|
|
|
// TODO: use ._bind()
|
|
|
|
// TODO: rename _bind() to _on(); add _off()
|
|
|
|
this.doc
|
|
|
|
.bind( "mousemove." + this.widgetName, $.proxy( this, "_mouseMove" ) )
|
|
|
|
.bind( "mouseup." + this.widgetName, $.proxy( this, "_mouseUp" ) );
|
2011-09-28 01:33:38 +00:00
|
|
|
},
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-10-01 16:05:36 +00:00
|
|
|
_mouseMove: function( event ) {
|
2011-09-28 01:33:38 +00:00
|
|
|
var leftDiff = event.clientX - this.startCoords.left,
|
2011-10-14 18:19:04 +00:00
|
|
|
topDiff = event.clientY - this.startCoords.top,
|
|
|
|
newLeft = leftDiff + this.startPosition.left,
|
|
|
|
newTop = topDiff + this.startPosition.top;
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
this.position = {
|
2011-10-01 16:05:36 +00:00
|
|
|
left: newLeft,
|
|
|
|
top: newTop
|
2011-09-28 01:33:38 +00:00
|
|
|
};
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// Refresh offset cache with new positions
|
|
|
|
this.offset.left = this.startOffset.left + newLeft;
|
2011-10-01 16:05:36 +00:00
|
|
|
this.offset.top = this.startOffset.top + newTop;
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
this._trigger( "drag", event, this._uiHash() );
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// User overriding left/top so shortcut math is no longer valid
|
|
|
|
if ( newLeft !== this.position.left || newTop !== this.position.top ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
// TODO: can we just store the previous offset values
|
|
|
|
// and not go through .offset()?
|
2011-09-28 01:33:38 +00:00
|
|
|
// refresh offset using slower functions
|
2011-10-01 16:05:36 +00:00
|
|
|
this.offset = this.dragEl.offset();
|
2011-09-28 01:33:38 +00:00
|
|
|
}
|
2011-10-14 18:19:04 +00:00
|
|
|
|
2011-10-09 04:54:47 +00:00
|
|
|
newLeft = this.position.left;
|
|
|
|
newTop = this.position.top;
|
2011-10-14 18:19:04 +00:00
|
|
|
|
|
|
|
// TODO: does this work with nested scrollable parents?
|
|
|
|
if ( this.cssPosition !== "fixed" ) {
|
2011-10-09 04:54:47 +00:00
|
|
|
newLeft = newLeft + this.scrollParent.scrollLeft();
|
|
|
|
newTop = newTop + this.scrollParent.scrollTop();
|
|
|
|
}
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
this.dragEl.css({
|
2011-10-09 04:54:47 +00:00
|
|
|
left: newLeft + "px",
|
|
|
|
top: newTop + "px"
|
2011-09-28 01:33:38 +00:00
|
|
|
});
|
2011-10-14 18:19:04 +00:00
|
|
|
|
2011-10-09 04:54:47 +00:00
|
|
|
// Scroll the scrollParent, if needed
|
|
|
|
this._handleScrolling( event );
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleScrolling: function( event ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
var scrollTop = this.doc.scrollTop(),
|
|
|
|
scrollLeft = this.doc.scrollLeft();
|
2011-10-09 04:54:47 +00:00
|
|
|
|
|
|
|
// Handle vertical scrolling
|
|
|
|
if ( ( ( this.overflowHeight + scrollTop ) - event.pageY ) < this.options.scrollSensitivity ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
this.doc.scrollTop( scrollTop + this.options.scrollSpeed );
|
2011-10-09 04:54:47 +00:00
|
|
|
}
|
2011-10-14 18:19:04 +00:00
|
|
|
|
2011-10-09 04:54:47 +00:00
|
|
|
// Handle horizontal scrolling
|
|
|
|
if ( ( ( this.overflowWidth + scrollLeft ) - event.pageX ) < this.options.scrollSensitivity ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
this.doc.scrollLeft( scrollLeft + this.options.scrollSpeed );
|
2011-10-09 04:54:47 +00:00
|
|
|
}
|
2011-09-28 01:33:38 +00:00
|
|
|
},
|
2009-01-24 01:36:22 +00:00
|
|
|
|
2011-10-01 16:05:36 +00:00
|
|
|
_mouseUp: function( event ) {
|
2011-10-14 18:19:04 +00:00
|
|
|
this._trigger( "stop", event, this._uiHash() );
|
2009-01-24 01:36:22 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
if ( this.options.helper ) {
|
2011-09-28 01:33:38 +00:00
|
|
|
this.dragEl.remove();
|
|
|
|
}
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
this.doc
|
|
|
|
.unbind( "mousemove." + this.widgetName )
|
|
|
|
.unbind( "mouseup." + this.widgetName );
|
2011-09-28 01:33:38 +00:00
|
|
|
},
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
_uiHash: function( event ) {
|
2011-09-28 01:10:59 +00:00
|
|
|
return {
|
2011-10-01 16:05:36 +00:00
|
|
|
position: this.position,
|
|
|
|
offset: this.offset
|
2008-06-04 02:34:33 +00:00
|
|
|
};
|
2011-09-28 01:33:38 +00:00
|
|
|
}
|
2008-06-04 02:34:33 +00:00
|
|
|
});
|
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
})( jQuery );
|