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
|
|
|
*
|
2012-01-07 17:18:08 +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
|
2012-01-07 17:20:49 +00:00
|
|
|
* jquery.ui.interaction.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-11-23 17:03:09 +00:00
|
|
|
$.widget( "ui.draggable", $.ui.interaction, {
|
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: {
|
2012-01-23 02:51:46 +00:00
|
|
|
appendTo: null,
|
2012-01-14 21:15:02 +00:00
|
|
|
handle: null,
|
2011-11-27 22:55:04 +00:00
|
|
|
helper: null
|
2008-11-21 04:01:33 +00:00
|
|
|
},
|
|
|
|
|
2011-10-14 18:19:04 +00:00
|
|
|
// dragEl: element being dragged (original or helper)
|
2011-10-26 01:54:03 +00:00
|
|
|
// position: final CSS position of dragEl
|
2011-10-14 18:19:04 +00:00
|
|
|
// offset: offset of dragEl
|
2012-01-21 02:17:11 +00:00
|
|
|
// startPointer: pageX/Y of the mousedown (offset of pointer)
|
2011-10-14 18:19:04 +00:00
|
|
|
// startPosition: CSS position prior to drag start
|
|
|
|
// startOffset: offset prior to drag start
|
2011-10-26 01:54:03 +00:00
|
|
|
// tempPosition: overridable CSS position of dragEl
|
2011-10-14 18:19:04 +00:00
|
|
|
// overflowOffset: offset of scroll parent
|
2011-10-26 01:54:03 +00:00
|
|
|
// overflow: object containing width and height keys of scroll parent
|
2012-01-23 02:51:46 +00:00
|
|
|
// domPosition: object containing original parent and index when using
|
|
|
|
// appendTo option without a helper
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
_create: function() {
|
2011-11-23 17:32:55 +00:00
|
|
|
this._super();
|
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-01 16:38:48 +00:00
|
|
|
},
|
2008-11-23 17:42:24 +00:00
|
|
|
|
2012-01-14 20:49:58 +00:00
|
|
|
/** interaction interface **/
|
2011-10-26 01:54:03 +00:00
|
|
|
|
2012-01-14 21:15:02 +00:00
|
|
|
_isValidTarget: function( element ) {
|
|
|
|
return this.options.handle ? element.is( this.options.handle ) : true;
|
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_start: function( event, pointerPosition ) {
|
2012-01-23 02:51:46 +00:00
|
|
|
var offset;
|
|
|
|
|
2011-10-01 16:38:48 +00:00
|
|
|
// The actual dragging element, should always be a jQuery object
|
2012-01-15 20:18:12 +00:00
|
|
|
this.dragEl = this.options.helper ?
|
|
|
|
this._createHelper( pointerPosition ) :
|
|
|
|
this.element;
|
2011-11-06 22:42:35 +00:00
|
|
|
|
2012-01-23 02:51:46 +00:00
|
|
|
// _createHelper() ensures that helpers are in the correct position
|
|
|
|
// in the DOM, but we need to handle appendTo when there is no helper
|
|
|
|
if ( this.options.appendTo && this.dragEl === this.element ) {
|
|
|
|
this.domPosition = {
|
|
|
|
parent: this.element.parent(),
|
|
|
|
index: this.element.index()
|
|
|
|
};
|
|
|
|
offset = this.dragEl.offset();
|
|
|
|
this.dragEl
|
|
|
|
.appendTo( this.options.appendTo )
|
|
|
|
.offset( offset );
|
|
|
|
}
|
|
|
|
|
2011-10-26 01:54:03 +00:00
|
|
|
this.cssPosition = this.dragEl.css( "position" );
|
2011-11-23 14:03:02 +00:00
|
|
|
this.scrollParent = this.element.scrollParent();
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2012-01-21 02:17:11 +00:00
|
|
|
// Cache starting positions
|
|
|
|
this.startPointer = pointerPosition;
|
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-10-26 01:54:03 +00:00
|
|
|
// Cache the offset of scrollParent, if required for _handleScrolling
|
2012-01-07 17:18:08 +00:00
|
|
|
if ( this.scrollParent[0] !== this.document[0] && this.scrollParent[0].tagName !== "HTML" ) {
|
2011-10-26 01:54:03 +00:00
|
|
|
this.overflowOffset = this.scrollParent.offset();
|
|
|
|
}
|
|
|
|
|
2012-01-07 17:18:08 +00:00
|
|
|
this.overflow = {
|
|
|
|
height: this.scrollParent[0] === this.document[0] ?
|
|
|
|
this.window.height() : this.scrollParent.height(),
|
|
|
|
width: this.scrollParent[0] === this.document[0] ?
|
|
|
|
this.window.width() : this.scrollParent.width()
|
|
|
|
};
|
2011-10-14 18:19:04 +00:00
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
this._preparePosition( pointerPosition );
|
2011-10-26 01:54:03 +00:00
|
|
|
|
2012-01-08 01:44:51 +00:00
|
|
|
// If user cancels start, don't allow dragging
|
2012-01-15 18:34:46 +00:00
|
|
|
if ( this._trigger( "start", event, this._uiHash( pointerPosition ) ) === false ) {
|
2011-11-23 17:03:09 +00:00
|
|
|
return false;
|
2011-10-26 01:54:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 00:48:04 +00:00
|
|
|
this._blockFrames();
|
2012-01-09 02:48:53 +00:00
|
|
|
this._setCss();
|
2011-09-28 01:33:38 +00:00
|
|
|
},
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_move: function( event, pointerPosition ) {
|
|
|
|
this._preparePosition( pointerPosition );
|
2011-10-26 01:54:03 +00:00
|
|
|
|
2012-01-08 01:44:51 +00:00
|
|
|
// If user cancels drag, don't move the element
|
2012-01-15 18:34:46 +00:00
|
|
|
if ( this._trigger( "drag", event, this._uiHash( pointerPosition ) ) === false ) {
|
2011-10-26 01:54:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
this._setCss();
|
2011-10-26 01:54:03 +00:00
|
|
|
|
|
|
|
// Scroll the scrollParent, if needed
|
2012-01-15 17:57:39 +00:00
|
|
|
this._handleScrolling( pointerPosition );
|
2011-10-26 01:54:03 +00:00
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_stop: function( event, pointerPosition ) {
|
2012-01-23 02:51:46 +00:00
|
|
|
var parent, next;
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
this._preparePosition( pointerPosition );
|
2011-10-26 01:54:03 +00:00
|
|
|
|
2012-01-21 03:07:52 +00:00
|
|
|
// If user cancels stop, leave helper there
|
2012-01-15 18:34:46 +00:00
|
|
|
if ( this._trigger( "stop", event, this._uiHash( pointerPosition ) ) !== false ) {
|
2011-10-26 01:54:03 +00:00
|
|
|
if ( this.options.helper ) {
|
|
|
|
this.dragEl.remove();
|
|
|
|
}
|
2012-01-23 02:51:46 +00:00
|
|
|
if ( this.domPosition ) {
|
|
|
|
parent = this.domPosition.parent;
|
|
|
|
next = parent.children().eq( this.domPosition.index );
|
|
|
|
if ( next.length ) {
|
|
|
|
next.before( this.element );
|
|
|
|
} else {
|
|
|
|
parent.append( this.element );
|
|
|
|
}
|
|
|
|
this.element.offset( this.offset );
|
|
|
|
}
|
2011-10-26 01:54:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 00:48:04 +00:00
|
|
|
this._unblockFrames();
|
2011-10-26 01:54:03 +00:00
|
|
|
},
|
|
|
|
|
2012-01-14 20:49:58 +00:00
|
|
|
/** internal **/
|
|
|
|
|
2012-01-15 20:18:12 +00:00
|
|
|
_createHelper: function( pointerPosition ) {
|
|
|
|
var helper,
|
|
|
|
offset = this.element.offset(),
|
|
|
|
xPos = (pointerPosition.x - offset.left) / this.element.outerWidth(),
|
|
|
|
yPos = (pointerPosition.y - offset.top) / this.element.outerHeight();
|
|
|
|
|
|
|
|
// clone
|
|
|
|
if ( this.options.helper === true ) {
|
|
|
|
helper = this.element.clone()
|
|
|
|
.removeAttr( "id" )
|
|
|
|
.find( "[id]" )
|
|
|
|
.removeAttr( "id" )
|
|
|
|
.end();
|
|
|
|
} else {
|
|
|
|
// TODO: figure out the signature for this; see #4957
|
|
|
|
helper = $( this.options.helper() );
|
|
|
|
}
|
|
|
|
|
2012-01-23 02:51:46 +00:00
|
|
|
// Ensure the helper is in the DOM; obey the appendTo option if it exists
|
|
|
|
if ( this.options.appendTo || !helper.closest( "body" ).length ) {
|
|
|
|
helper.appendTo( this.options.appendTo || this.document[0].body );
|
|
|
|
}
|
|
|
|
|
2012-01-15 20:18:12 +00:00
|
|
|
return helper
|
|
|
|
// Helper must be absolute to function properly
|
|
|
|
.css( "position", "absolute" )
|
|
|
|
.offset({
|
|
|
|
left: pointerPosition.x - helper.outerWidth() * xPos,
|
|
|
|
top: pointerPosition.y - helper.outerHeight() * yPos
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-01-14 20:49:58 +00:00
|
|
|
_getPosition: function() {
|
|
|
|
var left, top, position,
|
|
|
|
scrollTop = this.scrollParent.scrollTop(),
|
|
|
|
scrollLeft = this.scrollParent.scrollLeft();
|
|
|
|
|
|
|
|
// If fixed or absolute
|
|
|
|
if ( this.cssPosition !== "relative" ) {
|
|
|
|
position = this.dragEl.position();
|
|
|
|
|
|
|
|
// Take into account scrollbar
|
|
|
|
position.top -= scrollTop;
|
|
|
|
position.left -= scrollLeft;
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using relative, css values are checked
|
|
|
|
// Otherwise the position wouldn't account for padding on ancestors
|
|
|
|
left = this.dragEl.css( "left" );
|
|
|
|
top = this.dragEl.css( "top" );
|
|
|
|
|
|
|
|
// Webkit will give back auto if there is no explicit value
|
|
|
|
left = ( left === "auto" ) ? 0: parseInt( left, 10 );
|
|
|
|
top = ( top === "auto" ) ? 0: parseInt( top, 10 );
|
|
|
|
|
|
|
|
return {
|
|
|
|
left: left - scrollLeft,
|
|
|
|
top: top - scrollTop
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-01-15 17:57:39 +00:00
|
|
|
_handleScrolling: function( pointerPosition ) {
|
2012-01-14 20:49:58 +00:00
|
|
|
var scrollTop = this.scrollParent.scrollTop(),
|
|
|
|
scrollLeft = this.scrollParent.scrollLeft(),
|
|
|
|
scrollSensitivity = 20,
|
|
|
|
baseSpeed = 5,
|
|
|
|
speed = function( distance ) {
|
|
|
|
return baseSpeed + Math.round( distance / 2 );
|
|
|
|
},
|
|
|
|
// overflowOffset is only set when scrollParent is not doc/html
|
|
|
|
overflowLeft = this.overflowOffset ?
|
|
|
|
this.overflowOffset.left :
|
|
|
|
scrollLeft,
|
|
|
|
overflowTop = this.overflowOffset ?
|
|
|
|
this.overflowOffset.top :
|
|
|
|
scrollTop,
|
2012-01-15 17:57:39 +00:00
|
|
|
xRight = this.overflow.width + overflowLeft - pointerPosition.x,
|
|
|
|
xLeft = pointerPosition.x- overflowLeft,
|
|
|
|
yBottom = this.overflow.height + overflowTop - pointerPosition.y,
|
|
|
|
yTop = pointerPosition.y - overflowTop;
|
2012-01-14 20:49:58 +00:00
|
|
|
|
|
|
|
// Handle vertical scrolling
|
|
|
|
if ( yBottom < scrollSensitivity ) {
|
|
|
|
this.scrollParent.scrollTop( scrollTop +
|
|
|
|
speed( scrollSensitivity - yBottom ) );
|
|
|
|
} else if ( yTop < scrollSensitivity ) {
|
|
|
|
this.scrollParent.scrollTop( scrollTop -
|
|
|
|
speed( scrollSensitivity - yTop ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle horizontal scrolling
|
|
|
|
if ( xRight < scrollSensitivity ) {
|
|
|
|
this.scrollParent.scrollLeft( scrollLeft +
|
|
|
|
speed( scrollSensitivity - xRight ) );
|
|
|
|
} else if ( xLeft < scrollSensitivity ) {
|
|
|
|
this.scrollParent.scrollLeft( scrollLeft -
|
|
|
|
speed( scrollSensitivity - xLeft ) );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-10-26 01:54:03 +00:00
|
|
|
// Uses event to determine new position of draggable, before any override from callbacks
|
2011-11-27 22:27:55 +00:00
|
|
|
// TODO: handle absolute element inside relative parent like a relative element
|
2012-01-15 17:57:39 +00:00
|
|
|
_preparePosition: function( pointerPosition ) {
|
2012-01-21 02:17:11 +00:00
|
|
|
var leftDiff = pointerPosition.x - this.startPointer.x,
|
|
|
|
topDiff = pointerPosition.y - this.startPointer.y,
|
2011-10-14 18:19:04 +00:00
|
|
|
newLeft = leftDiff + this.startPosition.left,
|
|
|
|
newTop = topDiff + this.startPosition.top;
|
2008-11-21 04:01:33 +00:00
|
|
|
|
2011-10-26 01:54:03 +00:00
|
|
|
// Save off new values for .css() in various callbacks using this function
|
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-10-26 01:54:03 +00:00
|
|
|
// Save off values to compare user override against automatic coordinates
|
|
|
|
this.tempPosition = {
|
|
|
|
left: newLeft,
|
|
|
|
top: newTop
|
2011-11-06 21:39:17 +00:00
|
|
|
};
|
2011-11-18 00:48:04 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
// Refresh offset cache with new positions
|
2011-11-13 22:14:19 +00:00
|
|
|
this.offset.left = this.startOffset.left + leftDiff;
|
|
|
|
this.offset.top = this.startOffset.top + topDiff;
|
2011-10-26 01:54:03 +00:00
|
|
|
},
|
2008-06-04 02:34:33 +00:00
|
|
|
|
2012-01-09 02:48:53 +00:00
|
|
|
// Places draggable where event, or user via event/callback, indicates
|
|
|
|
_setCss: function() {
|
2012-01-14 20:49:58 +00:00
|
|
|
var newLeft = this.position.left,
|
|
|
|
newTop = this.position.top;
|
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
|
2012-01-14 20:49:58 +00:00
|
|
|
if ( this.tempPosition.left !== this.position.left ||
|
|
|
|
this.tempPosition.top !== this.position.top ) {
|
2012-01-07 17:18:08 +00:00
|
|
|
// Reset offset based on difference of expected and overridden values
|
2012-01-14 20:49:58 +00:00
|
|
|
this.offset.left += newLeft - this.tempPosition.left;
|
|
|
|
this.offset.top += newTop - this.tempPosition.top;
|
2011-09-28 01:33:38 +00:00
|
|
|
}
|
2011-10-14 18:19:04 +00:00
|
|
|
|
|
|
|
// TODO: does this work with nested scrollable parents?
|
|
|
|
if ( this.cssPosition !== "fixed" ) {
|
2012-01-14 20:49:58 +00:00
|
|
|
newLeft += this.scrollParent.scrollLeft();
|
|
|
|
newTop += this.scrollParent.scrollTop();
|
2011-10-09 04:54:47 +00:00
|
|
|
}
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2011-09-28 01:33:38 +00:00
|
|
|
this.dragEl.css({
|
2011-11-18 00:48:04 +00:00
|
|
|
left: newLeft,
|
|
|
|
top: newTop
|
2011-09-28 01:33:38 +00:00
|
|
|
});
|
|
|
|
},
|
2008-11-10 05:18:20 +00:00
|
|
|
|
2012-01-15 18:34:46 +00:00
|
|
|
_uiHash: function( pointerPosition ) {
|
2011-11-06 21:20:56 +00:00
|
|
|
var ret = {
|
2012-01-21 02:17:11 +00:00
|
|
|
startPosition: $.extend( {}, this.startPosition ),
|
|
|
|
startOffset: $.extend( {}, this.startOffset ),
|
|
|
|
startPointer: $.extend( {}, this.startPointer ),
|
2011-11-13 22:14:19 +00:00
|
|
|
position: this.position,
|
2012-01-15 18:34:46 +00:00
|
|
|
offset: this.offset,
|
|
|
|
pointer: pointerPosition
|
2008-06-04 02:34:33 +00:00
|
|
|
};
|
2011-11-06 22:42:35 +00:00
|
|
|
|
2011-11-06 21:20:56 +00:00
|
|
|
if ( this.options.helper ) {
|
|
|
|
ret.helper = this.dragEl;
|
|
|
|
}
|
2011-11-06 22:42:35 +00:00
|
|
|
|
2011-11-06 21:20:56 +00:00
|
|
|
return ret;
|
2011-11-06 22:42:35 +00:00
|
|
|
},
|
|
|
|
|
2011-11-23 14:03:02 +00:00
|
|
|
_blockFrames: function() {
|
|
|
|
var body = this.document[0].body;
|
|
|
|
|
|
|
|
this.iframeBlocks = this.document.find( "iframe" ).map(function() {
|
|
|
|
var iframe = $( this ),
|
|
|
|
iframeOffset = iframe.offset();
|
|
|
|
|
|
|
|
return $( "<div>" )
|
|
|
|
.css({
|
|
|
|
position: "absolute",
|
|
|
|
width: iframe.outerWidth(),
|
|
|
|
height: iframe.outerHeight(),
|
|
|
|
top: iframeOffset.top,
|
|
|
|
left: iframeOffset.left
|
|
|
|
})
|
|
|
|
.appendTo( body )[0];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-11-06 22:42:35 +00:00
|
|
|
_unblockFrames: function() {
|
2011-11-27 22:27:55 +00:00
|
|
|
if ( this.iframeBlocks ) {
|
2011-11-18 00:48:04 +00:00
|
|
|
this.iframeBlocks.remove();
|
|
|
|
delete this.iframeBlocks;
|
2011-11-06 22:42:35 +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 );
|