/*!
* jQuery UI Draggable @VERSION
* http://jqueryui.com
*
* Copyright 2014 jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*
* http://api.jqueryui.com/draggable/
*/
(function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define([
"jquery",
"./core",
"./mouse",
"./widget"
], factory );
} else {
// Browser globals
factory( jQuery );
}
}(function( $ ) {
/**
* 可拖拽小部件(Draggable Widget)<br/>
* 允许使用鼠标移动元素。<br/>
*
* 注释:让被选元素可被鼠标拖拽。如果您不只是拖拽,而是拖拽 & 放置,请查看 jQuery UI 可放置(Droppable)插件,为可拖拽元素提供了一个放置目标。<br/>
*
* @class draggable
* @module draggable
* @requires core,mouse,widget
* @constructor
* @namespace ui
*/
$.widget("ui.draggable", $.ui.mouse, {
version: "@VERSION",
widgetEventPrefix: "drag",
/**
* 一个包含小部件当前使用选项的对象。
* 在实例化时,用户提供的任何选项将会自动与 $.myNamespace.myWidget.prototype.options 中定义的默认值合并。用户指定的选项会覆盖默认值。
*
* @property options
* @type {Object}
*/
options: {
/**
* 如果设置为 false,将阻止 ui-draggable class 被添加。当在数百个元素上调用 .draggable() 时,这么设置有利于性能优化。
* @property options.addClasses
* @type {Boolean}
* @example
* //初始化带有指定 addClasses 选项的 draggable:
* $( ".selector" ).draggable({ addClasses: false });
*
*/
addClasses: true,
/**
* 当拖拽时,draggable 助手(helper)要追加到哪一个元素。<br/>
* 支持多个类型:<br/>
* jQuery:一个 jQuery 对象,包含助手(helper)要追加到的元素。<br/>
* Element:要追加助手(helper)的元素。<br/>
* Selector:一个选择器,指定哪一个元素要追加助手(helper)。<br/>
* String:字符串 "parent" 将促使助手(helper)成为 draggable 的同级。<br/>
*
* @property options.appendTo
* @type {jQuery | Element | Selector | String}
* @default "parent"
* @example
* //初始化带有指定 axis 选项的 draggable:
* $( ".selector" ).draggable({ axis: "x" });
*/
appendTo: "parent",
/**
* 约束在水平轴 (x) 或垂直轴 (y) 上拖拽。可能的值:"x", "y"。
* @property options.axis
* @type {String}
* @default false
* @example
* //初始化带有指定 appendTo 选项的 draggable:
* $( ".selector" ).draggable({ appendTo: "body" });
*/
axis: false,
/**
* 允许 draggable 放置在指定的 sortable 上。
* 如果使用了该选项,一个 draggable 可被放置在一个 sortable 列表上,然后成为列表的一部分。
* 注意:helper 选项必须设置为 "clone",以便更好地工作。
* 必须包含 可排序小部件(Sortable Widget)。
* @property options.connectToSortable
* @type {Boolean}
* @default false
* @example
* //初始化带有指定 connectToSortable 选项的 draggable:
* $( ".selector" ).draggable({ connectToSortable: "#my-sortable" });
*/
connectToSortable: false,
/**
* 约束在指定元素或区域的边界内拖拽。<br/>
* 支持多个类型:<br/>
* Selector:可拖拽元素将被包含在 selector 第一个元素的边界内。如果未找到元素,则不设置 containment。<br/>
* Element:可拖拽元素将被韩寒在元素的边界。<br/>
* String:可能的值:"parent"、"document"、"window"。<br/>
* Array:一个数组, 以形式 [ x1, y1, x2, y2 ] 定义元素的边界。<br/>
*
* @property options.containment
* @type {Selector | Element | String | Array | Boolean}
* @default false
* @example
* //初始化带有指定 containment 选项的 draggable:
* $( ".selector" ).draggable({ containment: "parent" });
* //在初始化后,获取或设置 containment 选项:
* // getter
* var containment = $( ".selector" ).draggable( "option", "containment" );
* // setter
* $( ".selector" ).draggable( "option", "containment", "parent" );
*
*/
containment: false,
/**
* 拖拽操作期间的 CSS 光标。<br/>
* 有效的 CSS 光标值包括:default、move、pointer、crosshair,等等。
*
* @property options.cursor
* @type {String}
* @default "auto"
* @example
* //初始化带有指定 cursor 选项的 draggable:
* $( ".selector" ).draggable({ cursor: "crosshair" });
*/
cursor: "auto",
/**
* 使用 cursorAt 选项来指定相对于 draggable 的另一个位置(指定一个相对于 top、right、bottom、left 的像素值)
*
* @property options.cursorAt
* @type {Object}
* @default false
* @example
* //初始化带有指定 cursorAt 选项的 draggable:
* $( ".selector" ).draggable({ cursorAt: { top: -5, left: -5} });
*/
cursorAt: false,
/**
* 对齐拖拽助手(helper)到网格,每个 x 和 y 像素。数组形式必须是 [ x, y ]。
*
* @property options.grid
* @type {Array}
* @default false
* @example
* //初始化带有指定 grid 选项的 draggable:
* $( ".selector" ).draggable({ grid: [ 50, 20 ] });
*/
grid: false,
/**
* 如果指定了该选项,则限制开始拖拽,除非鼠标在指定的元素上按下。
* 只有可拖拽(draggable)元素的后代元素才允许被拖拽。
*
* @property options.handle
* @type {Selector | Element}
* @default false
* @example
* //初始化带有指定 handle 选项的 draggable:
* $( ".selector" ).draggable({ handle: "h2" });
*/
handle: false,
/**
* 允许一个 helper 元素用于拖拽显示。<br/>
* 支持多个类型:<br/>
* String:如果设置为 "clone",元素将被克隆,且克隆将被拖拽。<br/>
* Function:一个函数,将返回拖拽时要使用的 DOMElement。<br/>
*
* @property options.helper
* @type {String | Function}
* @default false
* @example
* //初始化带有指定 helper 选项的 draggable:
* $( ".selector" ).draggable({ helper: "clone" });
* //初始化带有指定 helper function 选项的 draggable:
* $( ".selector" ).draggable({
* helper: function( event ) {
* return $( "<div class='ui-widget-header'>I'm a custom helper</div>" );
* }
* });
*/
helper: "original",
/**
* 防止拖拽期间 iframes 捕捉鼠标移动(mousemove )事件。
* 在与 cursorAt 选项结合使用时,或鼠标光标未覆盖在助手(helper)上时,非常有用。<br/>
*
* 支持多个类型:<br/>
* Boolean:当设置为 true 时,透明遮罩将被放置在页面上所有 iframes 上。<br/>
* Selector:匹配 selector 的任意 iframes 将被透明遮罩覆盖。<br/>
*
* @property options.iframeFix
* @type {Boolean | Selector}
* @default false
* @example
* //初始化带有指定 iframeFix 选项的 draggable:
* $( ".selector" ).draggable({ iframeFix: true });
*/
iframeFix: false,
/**
* 当被拖拽时助手(helper)的不透明度。
*
* @property options.opacity
* @type {Number}
* @default false
* @example
* //初始化带有指定 opacity 选项的 draggable:
* $( ".selector" ).draggable({ opacity: 0.35 });
*/
opacity: false,
/**
* 如果设置为 true,在每次鼠标移动(mousemove)时都会计算所有可放置的位置。
* 注意:这解决了高度动态的问题,但是明显降低了性能。
*
* @property options.refreshPositions
* @type {Boolean}
* @default false
* @example
* //初始化带有指定 refreshPositions 选项的 draggable:
* $( ".selector" ).draggable({ refreshPositions: true });
*/
refreshPositions: false,
/**
* 当拖拽停止时,元素是否还原到它的开始位置。<br/>
* 支持多个类型:<br/>
* Boolean:如果设置为 true,元素总会还原。<br/>
* String:如果设置为 "invalid",还原仅在 draggable 未放置在 droppable 上时发生,如果设置为 "valid" 则相反。<br/>
* Function:一个函数,确定元素是否还原到它的开始位置。该函数必须返回 true 才能还原元素。<br/>
*
* @property options.revert
* @type {Boolean | String | Function}
* @default false
* @example
* //初始化带有指定 revert 选项的 draggable:
* $( ".selector" ).draggable({ revert: true });
*/
revert: false,
/**
* 还原(revert)动画的持续时间,以毫秒计。如果 revert 选项是 false 则忽略。
*
* @property options.revertDuration
* @type {Number}
* @default 500
* @example
* //初始化带有指定 revertDuration 选项的 draggable:
* $( ".selector" ).draggable({ revertDuration: 200 });
*/
revertDuration: 500,
/**
* 用于组合配套 draggable 和 droppable 项,除了 droppable 的 accept 选项之外。
* 一个与 droppable 带有相同的 scope 值的 draggable 会被该 droppable 接受。
*
* @property options.scope
* @type {String}
* @default "default"
* @example
* //初始化带有指定 scope 选项的 draggable:
* $( ".selector" ).draggable({ scope: "tasks" });
*/
scope: "default",
/**
* 如果设置为 true,当拖拽时容器会自动滚动。
*
* @property options.scroll
* @type {Boolean}
* @default true
* @example
* //初始化带有指定 scroll 选项的 draggable:
* $( ".selector" ).draggable({ scroll: false });
*/
scroll: true,
/**
* 从要滚动的视区边缘起的距离,以像素计。距离是相对于指针的,不是相对于 draggable。
* 如果 scroll 选项是 false 则忽略。
*
* @property options.scrollSensitivity
* @type {Number}
* @default 20
* @example
* //初始化带有指定 scrollSensitivity 选项的 draggable:
* $( ".selector" ).draggable({ scroll: false });
*/
scrollSensitivity: 20,
/**
* 当鼠标指针获取到在 scrollSensitivity 距离内时,窗体滚动的速度。
* 如果 scroll 选项是 false 则忽略。
*
* @property options.scrollSpeed
* @type {Number}
* @default 20
* @example
* //初始化带有指定 scrollSpeed 选项的 draggable:
* $( ".selector" ).draggable({ scrollSpeed: 100 });
*/
scrollSpeed: 20,
/**
* 元素是否对齐到其他元素。<br/>
* 支持多个类型:<br/>
* Boolean:当设置为 true 时,元素会对齐到其它可拖拽(draggable )元素。<br/>
* Selector:一个选择器,指定要对齐到哪个元素。<br/>
*
* @property options.snap
* @type {Boolean | Selector}
* @default false
* @example
* //初始化带有指定 snap 选项的 draggable:
* $( ".selector" ).draggable({ snap: true });
*/
snap: false,
/**
* 决定 draggable 将对齐到对齐元素的哪个边缘。如果 snap 选项是 false 则忽略。
* 可能的值:"inner"、"outer"、"both"。
*
* @property options.snapMode
* @type {String}
* @default "both"
* @example
* //初始化带有指定 snapMode 选项的 draggable:
* $( ".selector" ).draggable({ snapMode: "inner" });
*/
snapMode: "both",
/**
* 从要发生对齐的对齐元素边缘起的距离,以像素计。
* 如果 snap 选项是 false 则忽略。
*
* @property options.snapTolerance
* @type {Number}
* @default 20
* @example
* //初始化带有指定 snapTolerance 选项的 draggable:
* $( ".selector" ).draggable({ snapTolerance: 30 });
*/
snapTolerance: 20,
/**
* 控制匹配选择器(selector)的元素集合的 z-index,总是在当前拖拽项的前面,在类似窗口管理器这样的事物中非常有用。
*
* @property options.stack
* @type {Selector}
* @default false
* @example
* //初始化带有指定 stack 选项的 draggable:
* $( ".selector" ).draggable({ stack: ".products" });
*/
stack: false,
/**
* 当被拖拽时,助手(helper)的 Z-index。
*
* @property options.zIndex
* @type {Number}
* @default false
* @example
* //初始化带有指定 zIndex 选项的 draggable:
* $( ".selector" ).draggable({ zIndex: 100 });
*/
zIndex: false,
// callbacks
drag: null,
start: null,
stop: null
/**
* 防止从指定的元素上开始拖拽。
*
* @property options.cancel
* @type {Selector}
* @default "input, textarea, button, select, option"
* @example
* //初始化带有指定 cancel 选项的 draggable:
* $( ".selector" ).draggable({ cancel: ".title" });
*/
/**
* 鼠标按下后直到拖拽开始为止的时间,以毫秒计。该选项可以防止点击在某个元素上时不必要的拖拽。
*
* @property options.delay
* @type {Boolean}
* @default false
* @example
* //初始化带有指定 delay 选项的 draggable:
* $( ".selector" ).draggable({ delay: 300 });
*/
/**
* 如果设置为 true,则禁用该 draggable。
*
* @property options.disabled
* @type {Boolean}
* @default false
* @example
* //初始化带有指定 disabled 选项的 draggable:
* $( ".selector" ).draggable({ disabled: true });
*/
/**
* 鼠标按下后拖拽开始前必须移动的距离,以像素计。该选项可以防止点击在某个元素上时不必要的拖拽。
*
* @property options.distance
* @type {Number}
* @default 1
* @example
* //初始化带有指定 distance 选项的 draggable:
* $( ".selector" ).draggable({ distance: 10 });
*/
},
_create: function() {
if ( this.options.helper === "original" ) {
this._setPositionRelative();
}
if (this.options.addClasses){
this.element.addClass("ui-draggable");
}
if (this.options.disabled){
this.element.addClass("ui-draggable-disabled");
}
this._setHandleClassName();
this._mouseInit();
},
_setOption: function( key, value ) {
this._super( key, value );
if ( key === "handle" ) {
this._removeHandleClassName();
this._setHandleClassName();
}
},
_destroy: function() {
if ( ( this.helper || this.element ).is( ".ui-draggable-dragging" ) ) {
this.destroyOnClear = true;
return;
}
this.element.removeClass( "ui-draggable ui-draggable-dragging ui-draggable-disabled" );
this._removeHandleClassName();
this._mouseDestroy();
},
_mouseCapture: function(event) {
var o = this.options;
this._blurActiveElement( event );
// among others, prevent a drag on a resizable-handle
if (this.helper || o.disabled || $(event.target).closest(".ui-resizable-handle").length > 0) {
return false;
}
//Quit if we're not on a valid handle
this.handle = this._getHandle(event);
if (!this.handle) {
return false;
}
$(o.iframeFix === true ? "iframe" : o.iframeFix).each(function() {
$("<div class='ui-draggable-iframeFix' style='background: #fff;'></div>")
.css({
width: this.offsetWidth + "px", height: this.offsetHeight + "px",
position: "absolute", opacity: "0.001", zIndex: 1000
})
.css($(this).offset())
.appendTo("body");
});
return true;
},
_blurActiveElement: function( event ) {
var document = this.document[ 0 ];
// Only need to blur if the event occurred on the draggable itself, see #10527
if ( !this.handleElement.is( event.target ) ) {
return;
}
// support: IE9
// IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe>
try {
// Support: IE9, IE10
// If the <body> is blurred, IE will switch windows, see #9520
if ( document.activeElement && document.activeElement.nodeName.toLowerCase() !== "body" ) {
// Blur any element that currently has focus, see #4261
$( document.activeElement ).blur();
}
} catch ( error ) {}
},
_mouseStart: function(event) {
var o = this.options;
//Create and append the visible helper
this.helper = this._createHelper(event);
this.helper.addClass("ui-draggable-dragging");
//Cache the helper size
this._cacheHelperProportions();
//If ddmanager is used for droppables, set the global draggable
if ($.ui.ddmanager) {
$.ui.ddmanager.current = this;
}
/*
* - Position generation -
* This block generates everything position related - it's the core of draggables.
*/
//Cache the margins of the original element
this._cacheMargins();
//Store the helper's css position
this.cssPosition = this.helper.css( "position" );
this.scrollParent = this.helper.scrollParent( true );
this.offsetParent = this.helper.offsetParent();
this.offsetParentCssPosition = this.offsetParent.css( "position" );
//The element's absolute position on the page minus margins
this.offset = this.positionAbs = this.element.offset();
this.offset = {
top: this.offset.top - this.margins.top,
left: this.offset.left - this.margins.left
};
//Reset scroll cache
this.offset.scroll = false;
$.extend(this.offset, {
click: { //Where the click happened, relative to the element
left: event.pageX - this.offset.left,
top: event.pageY - this.offset.top
},
parent: this._getParentOffset(),
relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
});
//Generate the original position
this.originalPosition = this.position = this._generatePosition( event, false );
this.originalPageX = event.pageX;
this.originalPageY = event.pageY;
//Adjust the mouse offset relative to the helper if "cursorAt" is supplied
(o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
//Set a containment if given in the options
this._setContainment();
//Trigger event + callbacks
if (this._trigger("start", event) === false) {
this._clear();
return false;
}
//Recache the helper size
this._cacheHelperProportions();
//Prepare the droppable offsets
if ($.ui.ddmanager && !o.dropBehaviour) {
$.ui.ddmanager.prepareOffsets(this, event);
}
// Reset helper's right/bottom css if they're set and set explicit width/height instead
// as this prevents resizing of elements with right/bottom set (see #7772)
this._normalizeRightBottom();
this._mouseDrag(event, true); //Execute the drag once - this causes the helper not to be visible before getting its correct position
//If the ddmanager is used for droppables, inform the manager that dragging has started (see #5003)
if ( $.ui.ddmanager ) {
$.ui.ddmanager.dragStart(this, event);
}
return true;
},
_mouseDrag: function(event, noPropagation) {
// reset any necessary cached properties (see #5009)
if ( this.offsetParentCssPosition === "fixed" ) {
this.offset.parent = this._getParentOffset();
}
//Compute the helpers position
this.position = this._generatePosition( event, true );
this.positionAbs = this._convertPositionTo("absolute");
//Call plugins and callbacks and use the resulting position if something is returned
if (!noPropagation) {
var ui = this._uiHash();
if (this._trigger("drag", event, ui) === false) {
this._mouseUp({});
return false;
}
this.position = ui.position;
}
this.helper[ 0 ].style.left = this.position.left + "px";
this.helper[ 0 ].style.top = this.position.top + "px";
if ($.ui.ddmanager) {
$.ui.ddmanager.drag(this, event);
}
return false;
},
_mouseStop: function(event) {
//If we are using droppables, inform the manager about the drop
var that = this,
dropped = false;
if ($.ui.ddmanager && !this.options.dropBehaviour) {
dropped = $.ui.ddmanager.drop(this, event);
}
//if a drop comes from outside (a sortable)
if (this.dropped) {
dropped = this.dropped;
this.dropped = false;
}
if ((this.options.revert === "invalid" && !dropped) || (this.options.revert === "valid" && dropped) || this.options.revert === true || ($.isFunction(this.options.revert) && this.options.revert.call(this.element, dropped))) {
$(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
if (that._trigger("stop", event) !== false) {
that._clear();
}
});
} else {
if (this._trigger("stop", event) !== false) {
this._clear();
}
}
return false;
},
_mouseUp: function(event) {
//Remove frame helpers
$("div.ui-draggable-iframeFix").each(function() {
this.parentNode.removeChild(this);
});
//If the ddmanager is used for droppables, inform the manager that dragging has stopped (see #5003)
if ( $.ui.ddmanager ) {
$.ui.ddmanager.dragStop(this, event);
}
// Only need to focus if the event occurred on the draggable itself, see #10527
if ( this.handleElement.is( event.target ) ) {
// The interaction is over; whether or not the click resulted in a drag, focus the element
this.element.focus();
}
return $.ui.mouse.prototype._mouseUp.call(this, event);
},
cancel: function() {
if (this.helper.is(".ui-draggable-dragging")) {
this._mouseUp({});
} else {
this._clear();
}
return this;
},
_getHandle: function(event) {
return this.options.handle ?
!!$( event.target ).closest( this.element.find( this.options.handle ) ).length :
true;
},
_setHandleClassName: function() {
this.handleElement = this.options.handle ?
this.element.find( this.options.handle ) : this.element;
this.handleElement.addClass( "ui-draggable-handle" );
},
_removeHandleClassName: function() {
this.handleElement.removeClass( "ui-draggable-handle" );
},
_createHelper: function(event) {
var o = this.options,
helperIsFunction = $.isFunction( o.helper ),
helper = helperIsFunction ?
$( o.helper.apply( this.element[ 0 ], [ event ] ) ) :
( o.helper === "clone" ?
this.element.clone().removeAttr( "id" ) :
this.element );
if (!helper.parents("body").length) {
helper.appendTo((o.appendTo === "parent" ? this.element[0].parentNode : o.appendTo));
}
// http://bugs.jqueryui.com/ticket/9446
// a helper function can return the original element
// which wouldn't have been set to relative in _create
if ( helperIsFunction && helper[ 0 ] === this.element[ 0 ] ) {
this._setPositionRelative();
}
if (helper[0] !== this.element[0] && !(/(fixed|absolute)/).test(helper.css("position"))) {
helper.css("position", "absolute");
}
return helper;
},
_setPositionRelative: function() {
if ( !( /^(?:r|a|f)/ ).test( this.element.css( "position" ) ) ) {
this.element[ 0 ].style.position = "relative";
}
},
_adjustOffsetFromHelper: function(obj) {
if (typeof obj === "string") {
obj = obj.split(" ");
}
if ($.isArray(obj)) {
obj = { left: +obj[0], top: +obj[1] || 0 };
}
if ("left" in obj) {
this.offset.click.left = obj.left + this.margins.left;
}
if ("right" in obj) {
this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
}
if ("top" in obj) {
this.offset.click.top = obj.top + this.margins.top;
}
if ("bottom" in obj) {
this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
}
},
_isRootNode: function( element ) {
return ( /(html|body)/i ).test( element.tagName ) || element === this.document[ 0 ];
},
_getParentOffset: function() {
//Get the offsetParent and cache its position
var po = this.offsetParent.offset(),
document = this.document[ 0 ];
// This is a special case where we need to modify a offset calculated on start, since the following happened:
// 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
// 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
// the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
if (this.cssPosition === "absolute" && this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) {
po.left += this.scrollParent.scrollLeft();
po.top += this.scrollParent.scrollTop();
}
if ( this._isRootNode( this.offsetParent[ 0 ] ) ) {
po = { top: 0, left: 0 };
}
return {
top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
};
},
_getRelativeOffset: function() {
if ( this.cssPosition !== "relative" ) {
return { top: 0, left: 0 };
}
var p = this.element.position(),
scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
return {
top: p.top - ( parseInt(this.helper.css( "top" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollTop() : 0 ),
left: p.left - ( parseInt(this.helper.css( "left" ), 10) || 0 ) + ( !scrollIsRootNode ? this.scrollParent.scrollLeft() : 0 )
};
},
_cacheMargins: function() {
this.margins = {
left: (parseInt(this.element.css("marginLeft"),10) || 0),
top: (parseInt(this.element.css("marginTop"),10) || 0),
right: (parseInt(this.element.css("marginRight"),10) || 0),
bottom: (parseInt(this.element.css("marginBottom"),10) || 0)
};
},
_cacheHelperProportions: function() {
this.helperProportions = {
width: this.helper.outerWidth(),
height: this.helper.outerHeight()
};
},
_setContainment: function() {
var isUserScrollable, c, ce,
o = this.options,
document = this.document[ 0 ];
this.relativeContainer = null;
if ( !o.containment ) {
this.containment = null;
return;
}
if ( o.containment === "window" ) {
this.containment = [
$( window ).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
$( window ).scrollTop() - this.offset.relative.top - this.offset.parent.top,
$( window ).scrollLeft() + $( window ).width() - this.helperProportions.width - this.margins.left,
$( window ).scrollTop() + ( $( window ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
];
return;
}
if ( o.containment === "document") {
this.containment = [
0,
0,
$( document ).width() - this.helperProportions.width - this.margins.left,
( $( document ).height() || document.body.parentNode.scrollHeight ) - this.helperProportions.height - this.margins.top
];
return;
}
if ( o.containment.constructor === Array ) {
this.containment = o.containment;
return;
}
if ( o.containment === "parent" ) {
o.containment = this.helper[ 0 ].parentNode;
}
c = $( o.containment );
ce = c[ 0 ];
if ( !ce ) {
return;
}
isUserScrollable = /(scroll|auto)/.test( c.css( "overflow" ) );
this.containment = [
( parseInt( c.css( "borderLeftWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingLeft" ), 10 ) || 0 ),
( parseInt( c.css( "borderTopWidth" ), 10 ) || 0 ) + ( parseInt( c.css( "paddingTop" ), 10 ) || 0 ),
( isUserScrollable ? Math.max( ce.scrollWidth, ce.offsetWidth ) : ce.offsetWidth ) -
( parseInt( c.css( "borderRightWidth" ), 10 ) || 0 ) -
( parseInt( c.css( "paddingRight" ), 10 ) || 0 ) -
this.helperProportions.width -
this.margins.left -
this.margins.right,
( isUserScrollable ? Math.max( ce.scrollHeight, ce.offsetHeight ) : ce.offsetHeight ) -
( parseInt( c.css( "borderBottomWidth" ), 10 ) || 0 ) -
( parseInt( c.css( "paddingBottom" ), 10 ) || 0 ) -
this.helperProportions.height -
this.margins.top -
this.margins.bottom
];
this.relativeContainer = c;
},
_convertPositionTo: function(d, pos) {
if (!pos) {
pos = this.position;
}
var mod = d === "absolute" ? 1 : -1,
scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] );
return {
top: (
pos.top + // The absolute mouse position
this.offset.relative.top * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
this.offset.parent.top * mod - // The offsetParent's offset without borders (offset + border)
( ( this.cssPosition === "fixed" ? -this.offset.scroll.top : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) ) * mod)
),
left: (
pos.left + // The absolute mouse position
this.offset.relative.left * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
this.offset.parent.left * mod - // The offsetParent's offset without borders (offset + border)
( ( this.cssPosition === "fixed" ? -this.offset.scroll.left : ( scrollIsRootNode ? 0 : this.offset.scroll.left ) ) * mod)
)
};
},
_generatePosition: function( event, constrainPosition ) {
var containment, co, top, left,
o = this.options,
scrollIsRootNode = this._isRootNode( this.scrollParent[ 0 ] ),
pageX = event.pageX,
pageY = event.pageY;
// Cache the scroll
if ( !scrollIsRootNode || !this.offset.scroll ) {
this.offset.scroll = {
top: this.scrollParent.scrollTop(),
left: this.scrollParent.scrollLeft()
};
}
/*
* - Position constraining -
* Constrain the position to a mix of grid, containment.
*/
// If we are not dragging yet, we won't check for options
if ( constrainPosition ) {
if ( this.containment ) {
if ( this.relativeContainer ){
co = this.relativeContainer.offset();
containment = [
this.containment[ 0 ] + co.left,
this.containment[ 1 ] + co.top,
this.containment[ 2 ] + co.left,
this.containment[ 3 ] + co.top
];
} else {
containment = this.containment;
}
if (event.pageX - this.offset.click.left < containment[0]) {
pageX = containment[0] + this.offset.click.left;
}
if (event.pageY - this.offset.click.top < containment[1]) {
pageY = containment[1] + this.offset.click.top;
}
if (event.pageX - this.offset.click.left > containment[2]) {
pageX = containment[2] + this.offset.click.left;
}
if (event.pageY - this.offset.click.top > containment[3]) {
pageY = containment[3] + this.offset.click.top;
}
}
if (o.grid) {
//Check for grid elements set to 0 to prevent divide by 0 error causing invalid argument errors in IE (see ticket #6950)
top = o.grid[1] ? this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1] : this.originalPageY;
pageY = containment ? ((top - this.offset.click.top >= containment[1] || top - this.offset.click.top > containment[3]) ? top : ((top - this.offset.click.top >= containment[1]) ? top - o.grid[1] : top + o.grid[1])) : top;
left = o.grid[0] ? this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0] : this.originalPageX;
pageX = containment ? ((left - this.offset.click.left >= containment[0] || left - this.offset.click.left > containment[2]) ? left : ((left - this.offset.click.left >= containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left;
}
if ( o.axis === "y" ) {
pageX = this.originalPageX;
}
if ( o.axis === "x" ) {
pageY = this.originalPageY;
}
}
return {
top: (
pageY - // The absolute mouse position
this.offset.click.top - // Click offset (relative to the element)
this.offset.relative.top - // Only for relative positioned nodes: Relative offset from element to offset parent
this.offset.parent.top + // The offsetParent's offset without borders (offset + border)
( this.cssPosition === "fixed" ? -this.offset.scroll.top : ( scrollIsRootNode ? 0 : this.offset.scroll.top ) )
),
left: (
pageX - // The absolute mouse position
this.offset.click.left - // Click offset (relative to the element)
this.offset.relative.left - // Only for relative positioned nodes: Relative offset from element to offset parent
this.offset.parent.left + // The offsetParent's offset without borders (offset + border)
( this.cssPosition === "fixed" ? -this.offset.scroll.left : ( scrollIsRootNode ? 0 : this.offset.scroll.left ) )
)
};
},
_clear: function() {
this.helper.removeClass("ui-draggable-dragging");
if (this.helper[0] !== this.element[0] && !this.cancelHelperRemoval) {
this.helper.remove();
}
this.helper = null;
this.cancelHelperRemoval = false;
if ( this.destroyOnClear ) {
this.destroy();
}
},
_normalizeRightBottom: function() {
if ( this.options.axis !== "y" && this.helper.css( "right" ) !== "auto" ) {
this.helper.width( this.helper.width() );
this.helper.css( "right", "auto" );
}
if ( this.options.axis !== "x" && this.helper.css( "bottom" ) !== "auto" ) {
this.helper.height( this.helper.height() );
this.helper.css( "bottom", "auto" );
}
},
// From now on bulk stuff - mainly helpers
_trigger: function(type, event, ui) {
ui = ui || this._uiHash();
$.ui.plugin.call( this, type, [ event, ui, this ], true );
// Absolute position and offset (see #6884 ) have to be recalculated after plugins
if ( /^(drag|start|stop)/.test( type ) ) {
this.positionAbs = this._convertPositionTo( "absolute" );
ui.offset = this.positionAbs;
}
return $.Widget.prototype._trigger.call(this, type, event, ui);
},
plugins: {},
_uiHash: function() {
return {
helper: this.helper,
position: this.position,
originalPosition: this.originalPosition,
offset: this.positionAbs
};
}
});
$.ui.plugin.add("draggable", "connectToSortable", {
start: function( event, ui, inst ) {
var o = inst.options,
uiSortable = $.extend({}, ui, { item: inst.element });
inst.sortables = [];
$(o.connectToSortable).each(function() {
var sortable = $( this ).sortable( "instance" );
if (sortable && !sortable.options.disabled) {
inst.sortables.push({
instance: sortable,
shouldRevert: sortable.options.revert
});
sortable.refreshPositions(); // Call the sortable's refreshPositions at drag start to refresh the containerCache since the sortable container cache is used in drag and needs to be up to date (this will ensure it's initialised as well as being kept in step with any changes that might have happened on the page).
sortable._trigger("activate", event, uiSortable);
}
});
},
stop: function( event, ui, inst ) {
//If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
var uiSortable = $.extend( {}, ui, {
item: inst.element
});
$.each(inst.sortables, function() {
if (this.instance.isOver) {
this.instance.isOver = 0;
inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
//The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: "valid/invalid"
if (this.shouldRevert) {
this.instance.options.revert = this.shouldRevert;
}
//Trigger the stop of the sortable
this.instance._mouseStop(event);
this.instance.options.helper = this.instance.options._helper;
//If the helper has been the original item, restore properties in the sortable
if (inst.options.helper === "original") {
this.instance.currentItem.css({ top: "auto", left: "auto" });
}
} else {
this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
this.instance._trigger("deactivate", event, uiSortable);
}
});
},
drag: function( event, ui, inst ) {
var that = this;
$.each(inst.sortables, function() {
var innermostIntersecting = false,
thisSortable = this;
//Copy over some variables to allow calling the sortable's native _intersectsWith
this.instance.positionAbs = inst.positionAbs;
this.instance.helperProportions = inst.helperProportions;
this.instance.offset.click = inst.offset.click;
if (this.instance._intersectsWith(this.instance.containerCache)) {
innermostIntersecting = true;
$.each(inst.sortables, function() {
this.instance.positionAbs = inst.positionAbs;
this.instance.helperProportions = inst.helperProportions;
this.instance.offset.click = inst.offset.click;
if (this !== thisSortable &&
this.instance._intersectsWith(this.instance.containerCache) &&
$.contains(thisSortable.instance.element[0], this.instance.element[0])
) {
innermostIntersecting = false;
}
return innermostIntersecting;
});
}
if (innermostIntersecting) {
//If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
if (!this.instance.isOver) {
this.instance.isOver = 1;
//Now we fake the start of dragging for the sortable instance,
//by cloning the list group item, appending it to the sortable and using it as inst.currentItem
//We can then fire the start event of the sortable with our passed browser event, and our own helper (so it doesn't create a new one)
this.instance.currentItem = $(that).clone().removeAttr("id").appendTo(this.instance.element).data("ui-sortable-item", true);
this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
this.instance.options.helper = function() { return ui.helper[0]; };
event.target = this.instance.currentItem[0];
this.instance._mouseCapture(event, true);
this.instance._mouseStart(event, true, true);
//Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
this.instance.offset.click.top = inst.offset.click.top;
this.instance.offset.click.left = inst.offset.click.left;
this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
inst._trigger("toSortable", event);
inst.dropped = this.instance.element; //draggable revert needs that
//hack so receive/update callbacks work (mostly)
inst.currentItem = inst.element;
this.instance.fromOutside = inst;
}
//Provided we did all the previous steps, we can fire the drag event of the sortable on every draggable drag, when it intersects with the sortable
if (this.instance.currentItem) {
this.instance._mouseDrag(event);
}
} else {
//If it doesn't intersect with the sortable, and it intersected before,
//we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
if (this.instance.isOver) {
this.instance.isOver = 0;
this.instance.cancelHelperRemoval = true;
//Prevent reverting on this forced stop
this.instance.options.revert = false;
// The out event needs to be triggered independently
this.instance._trigger("out", event, this.instance._uiHash(this.instance));
this.instance._mouseStop(event, true);
this.instance.options.helper = this.instance.options._helper;
//Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
this.instance.currentItem.remove();
if (this.instance.placeholder) {
this.instance.placeholder.remove();
}
inst._trigger("fromSortable", event);
inst.dropped = false; //draggable revert needs that
}
}
});
}
});
$.ui.plugin.add("draggable", "cursor", {
start: function( event, ui, instance ) {
var t = $( "body" ),
o = instance.options;
if (t.css("cursor")) {
o._cursor = t.css("cursor");
}
t.css("cursor", o.cursor);
},
stop: function( event, ui, instance ) {
var o = instance.options;
if (o._cursor) {
$("body").css("cursor", o._cursor);
}
}
});
$.ui.plugin.add("draggable", "opacity", {
start: function( event, ui, instance ) {
var t = $( ui.helper ),
o = instance.options;
if (t.css("opacity")) {
o._opacity = t.css("opacity");
}
t.css("opacity", o.opacity);
},
stop: function( event, ui, instance ) {
var o = instance.options;
if (o._opacity) {
$(ui.helper).css("opacity", o._opacity);
}
}
});
$.ui.plugin.add("draggable", "scroll", {
start: function( event, ui, i ) {
if ( !i.scrollParentNotHidden ) {
i.scrollParentNotHidden = i.helper.scrollParent( false );
}
if ( i.scrollParentNotHidden[ 0 ] !== i.document[ 0 ] && i.scrollParentNotHidden[ 0 ].tagName !== "HTML" ) {
i.overflowOffset = i.scrollParentNotHidden.offset();
}
},
drag: function( event, ui, i ) {
var o = i.options,
scrolled = false,
scrollParent = i.scrollParentNotHidden[ 0 ],
document = i.document[ 0 ];
if ( scrollParent !== document && scrollParent.tagName !== "HTML" ) {
if ( !o.axis || o.axis !== "x" ) {
if ( ( i.overflowOffset.top + scrollParent.offsetHeight ) - event.pageY < o.scrollSensitivity ) {
scrollParent.scrollTop = scrolled = scrollParent.scrollTop + o.scrollSpeed;
} else if ( event.pageY - i.overflowOffset.top < o.scrollSensitivity ) {
scrollParent.scrollTop = scrolled = scrollParent.scrollTop - o.scrollSpeed;
}
}
if ( !o.axis || o.axis !== "y" ) {
if ( ( i.overflowOffset.left + scrollParent.offsetWidth ) - event.pageX < o.scrollSensitivity ) {
scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft + o.scrollSpeed;
} else if ( event.pageX - i.overflowOffset.left < o.scrollSensitivity ) {
scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft - o.scrollSpeed;
}
}
} else {
if (!o.axis || o.axis !== "x") {
if (event.pageY - $(document).scrollTop() < o.scrollSensitivity) {
scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
} else if ($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) {
scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
}
}
if (!o.axis || o.axis !== "y") {
if (event.pageX - $(document).scrollLeft() < o.scrollSensitivity) {
scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
} else if ($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) {
scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
}
}
}
if (scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
$.ui.ddmanager.prepareOffsets(i, event);
}
}
});
$.ui.plugin.add("draggable", "snap", {
start: function( event, ui, i ) {
var o = i.options;
i.snapElements = [];
$(o.snap.constructor !== String ? ( o.snap.items || ":data(ui-draggable)" ) : o.snap).each(function() {
var $t = $(this),
$o = $t.offset();
if (this !== i.element[0]) {
i.snapElements.push({
item: this,
width: $t.outerWidth(), height: $t.outerHeight(),
top: $o.top, left: $o.left
});
}
});
},
drag: function( event, ui, inst ) {
var ts, bs, ls, rs, l, r, t, b, i, first,
o = inst.options,
d = o.snapTolerance,
x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
for (i = inst.snapElements.length - 1; i >= 0; i--){
l = inst.snapElements[i].left - inst.margins.left;
r = l + inst.snapElements[i].width;
t = inst.snapElements[i].top - inst.margins.top;
b = t + inst.snapElements[i].height;
if ( x2 < l - d || x1 > r + d || y2 < t - d || y1 > b + d || !$.contains( inst.snapElements[ i ].item.ownerDocument, inst.snapElements[ i ].item ) ) {
if (inst.snapElements[i].snapping) {
(inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
}
inst.snapElements[i].snapping = false;
continue;
}
if (o.snapMode !== "inner") {
ts = Math.abs(t - y2) <= d;
bs = Math.abs(b - y1) <= d;
ls = Math.abs(l - x2) <= d;
rs = Math.abs(r - x1) <= d;
if (ts) {
ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top;
}
if (bs) {
ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top;
}
if (ls) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left;
}
if (rs) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left;
}
}
first = (ts || bs || ls || rs);
if (o.snapMode !== "outer") {
ts = Math.abs(t - y1) <= d;
bs = Math.abs(b - y2) <= d;
ls = Math.abs(l - x1) <= d;
rs = Math.abs(r - x2) <= d;
if (ts) {
ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top;
}
if (bs) {
ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top;
}
if (ls) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left;
}
if (rs) {
ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left;
}
}
if (!inst.snapElements[i].snapping && (ts || bs || ls || rs || first)) {
(inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
}
inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
}
}
});
$.ui.plugin.add("draggable", "stack", {
start: function( event, ui, instance ) {
var min,
o = instance.options,
group = $.makeArray($(o.stack)).sort(function(a, b) {
return (parseInt($(a).css("zIndex"), 10) || 0) - (parseInt($(b).css("zIndex"), 10) || 0);
});
if (!group.length) { return; }
min = parseInt($(group[0]).css("zIndex"), 10) || 0;
$(group).each(function(i) {
$(this).css("zIndex", min + i);
});
this.css("zIndex", (min + group.length));
}
});
$.ui.plugin.add("draggable", "zIndex", {
start: function( event, ui, instance ) {
var t = $( ui.helper ),
o = instance.options;
if (t.css("zIndex")) {
o._zIndex = t.css("zIndex");
}
t.css("zIndex", o.zIndex);
},
stop: function( event, ui, instance ) {
var o = instance.options;
if (o._zIndex) {
$(ui.helper).css("zIndex", o._zIndex);
}
}
});
return $.ui.draggable;
}));