2 * jQuery UI Draggable 1.10.2
5 * Copyright 2013 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/draggable/
16 (function( $, undefined ) {
18 $.widget("ui.draggable", $.ui.mouse, {
20 widgetEventPrefix: "drag",
25 connectToSortable: false,
34 refreshPositions: false,
39 scrollSensitivity: 20,
54 if (this.options.helper === "original" && !(/^(?:r|a|f)/).test(this.element.css("position"))) {
55 this.element[0].style.position = "relative";
57 if (this.options.addClasses){
58 this.element.addClass("ui-draggable");
60 if (this.options.disabled){
61 this.element.addClass("ui-draggable-disabled");
68 _destroy: function() {
69 this.element.removeClass( "ui-draggable ui-draggable-dragging ui-draggable-disabled" );
73 _mouseCapture: function(event) {
77 // among others, prevent a drag on a resizable-handle
78 if (this.helper || o.disabled || $(event.target).closest(".ui-resizable-handle").length > 0) {
82 //Quit if we're not on a valid handle
83 this.handle = this._getHandle(event);
88 $(o.iframeFix === true ? "iframe" : o.iframeFix).each(function() {
89 $("<div class='ui-draggable-iframeFix' style='background: #fff;'></div>")
91 width: this.offsetWidth+"px", height: this.offsetHeight+"px",
92 position: "absolute", opacity: "0.001", zIndex: 1000
94 .css($(this).offset())
102 _mouseStart: function(event) {
104 var o = this.options;
106 //Create and append the visible helper
107 this.helper = this._createHelper(event);
109 this.helper.addClass("ui-draggable-dragging");
111 //Cache the helper size
112 this._cacheHelperProportions();
114 //If ddmanager is used for droppables, set the global draggable
116 $.ui.ddmanager.current = this;
120 * - Position generation -
121 * This block generates everything position related - it's the core of draggables.
124 //Cache the margins of the original element
125 this._cacheMargins();
127 //Store the helper's css position
128 this.cssPosition = this.helper.css("position");
129 this.scrollParent = this.helper.scrollParent();
131 //The element's absolute position on the page minus margins
132 this.offset = this.positionAbs = this.element.offset();
134 top: this.offset.top - this.margins.top,
135 left: this.offset.left - this.margins.left
138 $.extend(this.offset, {
139 click: { //Where the click happened, relative to the element
140 left: event.pageX - this.offset.left,
141 top: event.pageY - this.offset.top
143 parent: this._getParentOffset(),
144 relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
147 //Generate the original position
148 this.originalPosition = this.position = this._generatePosition(event);
149 this.originalPageX = event.pageX;
150 this.originalPageY = event.pageY;
152 //Adjust the mouse offset relative to the helper if "cursorAt" is supplied
153 (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
155 //Set a containment if given in the options
157 this._setContainment();
160 //Trigger event + callbacks
161 if(this._trigger("start", event) === false) {
166 //Recache the helper size
167 this._cacheHelperProportions();
169 //Prepare the droppable offsets
170 if ($.ui.ddmanager && !o.dropBehaviour) {
171 $.ui.ddmanager.prepareOffsets(this, event);
175 this._mouseDrag(event, true); //Execute the drag once - this causes the helper not to be visible before getting its correct position
177 //If the ddmanager is used for droppables, inform the manager that dragging has started (see #5003)
178 if ( $.ui.ddmanager ) {
179 $.ui.ddmanager.dragStart(this, event);
185 _mouseDrag: function(event, noPropagation) {
187 //Compute the helpers position
188 this.position = this._generatePosition(event);
189 this.positionAbs = this._convertPositionTo("absolute");
191 //Call plugins and callbacks and use the resulting position if something is returned
192 if (!noPropagation) {
193 var ui = this._uiHash();
194 if(this._trigger("drag", event, ui) === false) {
198 this.position = ui.position;
201 if(!this.options.axis || this.options.axis !== "y") {
202 this.helper[0].style.left = this.position.left+"px";
204 if(!this.options.axis || this.options.axis !== "x") {
205 this.helper[0].style.top = this.position.top+"px";
208 $.ui.ddmanager.drag(this, event);
214 _mouseStop: function(event) {
216 //If we are using droppables, inform the manager about the drop
219 elementInDom = false,
221 if ($.ui.ddmanager && !this.options.dropBehaviour) {
222 dropped = $.ui.ddmanager.drop(this, event);
225 //if a drop comes from outside (a sortable)
227 dropped = this.dropped;
228 this.dropped = false;
231 //if the original element is no longer in the DOM don't bother to continue (see #8269)
232 element = this.element[0];
233 while ( element && (element = element.parentNode) ) {
234 if (element === document ) {
238 if ( !elementInDom && this.options.helper === "original" ) {
242 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))) {
243 $(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
244 if(that._trigger("stop", event) !== false) {
249 if(this._trigger("stop", event) !== false) {
257 _mouseUp: function(event) {
258 //Remove frame helpers
259 $("div.ui-draggable-iframeFix").each(function() {
260 this.parentNode.removeChild(this);
263 //If the ddmanager is used for droppables, inform the manager that dragging has stopped (see #5003)
264 if( $.ui.ddmanager ) {
265 $.ui.ddmanager.dragStop(this, event);
268 return $.ui.mouse.prototype._mouseUp.call(this, event);
273 if(this.helper.is(".ui-draggable-dragging")) {
283 _getHandle: function(event) {
284 return this.options.handle ?
285 !!$( event.target ).closest( this.element.find( this.options.handle ) ).length :
289 _createHelper: function(event) {
291 var o = this.options,
292 helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event])) : (o.helper === "clone" ? this.element.clone().removeAttr("id") : this.element);
294 if(!helper.parents("body").length) {
295 helper.appendTo((o.appendTo === "parent" ? this.element[0].parentNode : o.appendTo));
298 if(helper[0] !== this.element[0] && !(/(fixed|absolute)/).test(helper.css("position"))) {
299 helper.css("position", "absolute");
306 _adjustOffsetFromHelper: function(obj) {
307 if (typeof obj === "string") {
308 obj = obj.split(" ");
310 if ($.isArray(obj)) {
311 obj = {left: +obj[0], top: +obj[1] || 0};
314 this.offset.click.left = obj.left + this.margins.left;
316 if ("right" in obj) {
317 this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
320 this.offset.click.top = obj.top + this.margins.top;
322 if ("bottom" in obj) {
323 this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
327 _getParentOffset: function() {
329 //Get the offsetParent and cache its position
330 this.offsetParent = this.helper.offsetParent();
331 var po = this.offsetParent.offset();
333 // This is a special case where we need to modify a offset calculated on start, since the following happened:
334 // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
335 // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
336 // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
337 if(this.cssPosition === "absolute" && this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) {
338 po.left += this.scrollParent.scrollLeft();
339 po.top += this.scrollParent.scrollTop();
342 //This needs to be actually done for all browsers, since pageX/pageY includes this information
344 if((this.offsetParent[0] === document.body) ||
345 (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() === "html" && $.ui.ie)) {
346 po = { top: 0, left: 0 };
350 top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
351 left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
356 _getRelativeOffset: function() {
358 if(this.cssPosition === "relative") {
359 var p = this.element.position();
361 top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
362 left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
365 return { top: 0, left: 0 };
370 _cacheMargins: function() {
372 left: (parseInt(this.element.css("marginLeft"),10) || 0),
373 top: (parseInt(this.element.css("marginTop"),10) || 0),
374 right: (parseInt(this.element.css("marginRight"),10) || 0),
375 bottom: (parseInt(this.element.css("marginBottom"),10) || 0)
379 _cacheHelperProportions: function() {
380 this.helperProportions = {
381 width: this.helper.outerWidth(),
382 height: this.helper.outerHeight()
386 _setContainment: function() {
391 if(o.containment === "parent") {
392 o.containment = this.helper[0].parentNode;
394 if(o.containment === "document" || o.containment === "window") {
396 o.containment === "document" ? 0 : $(window).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
397 o.containment === "document" ? 0 : $(window).scrollTop() - this.offset.relative.top - this.offset.parent.top,
398 (o.containment === "document" ? 0 : $(window).scrollLeft()) + $(o.containment === "document" ? document : window).width() - this.helperProportions.width - this.margins.left,
399 (o.containment === "document" ? 0 : $(window).scrollTop()) + ($(o.containment === "document" ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
403 if(!(/^(document|window|parent)$/).test(o.containment) && o.containment.constructor !== Array) {
404 c = $(o.containment);
411 over = ($(ce).css("overflow") !== "hidden");
414 (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0),
415 (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0),
416 (over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderRightWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left - this.margins.right,
417 (over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderBottomWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top - this.margins.bottom
419 this.relative_container = c;
421 } else if(o.containment.constructor === Array) {
422 this.containment = o.containment;
427 _convertPositionTo: function(d, pos) {
433 var mod = d === "absolute" ? 1 : -1,
434 scroll = this.cssPosition === "absolute" && !(this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName);
438 pos.top + // The absolute mouse position
439 this.offset.relative.top * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
440 this.offset.parent.top * mod - // The offsetParent's offset without borders (offset + border)
441 ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
444 pos.left + // The absolute mouse position
445 this.offset.relative.left * mod + // Only for relative positioned nodes: Relative offset from element to offset parent
446 this.offset.parent.left * mod - // The offsetParent's offset without borders (offset + border)
447 ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
453 _generatePosition: function(event) {
455 var containment, co, top, left,
457 scroll = this.cssPosition === "absolute" && !(this.scrollParent[0] !== document && $.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent,
458 scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName),
463 * - Position constraining -
464 * Constrain the position to a mix of grid, containment.
467 if(this.originalPosition) { //If we are not dragging yet, we won't check for options
468 if(this.containment) {
469 if (this.relative_container){
470 co = this.relative_container.offset();
471 containment = [ this.containment[0] + co.left,
472 this.containment[1] + co.top,
473 this.containment[2] + co.left,
474 this.containment[3] + co.top ];
477 containment = this.containment;
480 if(event.pageX - this.offset.click.left < containment[0]) {
481 pageX = containment[0] + this.offset.click.left;
483 if(event.pageY - this.offset.click.top < containment[1]) {
484 pageY = containment[1] + this.offset.click.top;
486 if(event.pageX - this.offset.click.left > containment[2]) {
487 pageX = containment[2] + this.offset.click.left;
489 if(event.pageY - this.offset.click.top > containment[3]) {
490 pageY = containment[3] + this.offset.click.top;
495 //Check for grid elements set to 0 to prevent divide by 0 error causing invalid argument errors in IE (see ticket #6950)
496 top = o.grid[1] ? this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1] : this.originalPageY;
497 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;
499 left = o.grid[0] ? this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0] : this.originalPageX;
500 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;
507 pageY - // The absolute mouse position
508 this.offset.click.top - // Click offset (relative to the element)
509 this.offset.relative.top - // Only for relative positioned nodes: Relative offset from element to offset parent
510 this.offset.parent.top + // The offsetParent's offset without borders (offset + border)
511 ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
514 pageX - // The absolute mouse position
515 this.offset.click.left - // Click offset (relative to the element)
516 this.offset.relative.left - // Only for relative positioned nodes: Relative offset from element to offset parent
517 this.offset.parent.left + // The offsetParent's offset without borders (offset + border)
518 ( ( this.cssPosition === "fixed" ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
525 this.helper.removeClass("ui-draggable-dragging");
526 if(this.helper[0] !== this.element[0] && !this.cancelHelperRemoval) {
527 this.helper.remove();
530 this.cancelHelperRemoval = false;
533 // From now on bulk stuff - mainly helpers
535 _trigger: function(type, event, ui) {
536 ui = ui || this._uiHash();
537 $.ui.plugin.call(this, type, [event, ui]);
538 //The absolute position has to be recalculated after plugins
539 if(type === "drag") {
540 this.positionAbs = this._convertPositionTo("absolute");
542 return $.Widget.prototype._trigger.call(this, type, event, ui);
547 _uiHash: function() {
550 position: this.position,
551 originalPosition: this.originalPosition,
552 offset: this.positionAbs
558 $.ui.plugin.add("draggable", "connectToSortable", {
559 start: function(event, ui) {
561 var inst = $(this).data("ui-draggable"), o = inst.options,
562 uiSortable = $.extend({}, ui, { item: inst.element });
564 $(o.connectToSortable).each(function() {
565 var sortable = $.data(this, "ui-sortable");
566 if (sortable && !sortable.options.disabled) {
567 inst.sortables.push({
569 shouldRevert: sortable.options.revert
571 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).
572 sortable._trigger("activate", event, uiSortable);
577 stop: function(event, ui) {
579 //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
580 var inst = $(this).data("ui-draggable"),
581 uiSortable = $.extend({}, ui, { item: inst.element });
583 $.each(inst.sortables, function() {
584 if(this.instance.isOver) {
586 this.instance.isOver = 0;
588 inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
589 this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
591 //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: "valid/invalid"
592 if(this.shouldRevert) {
593 this.instance.options.revert = this.shouldRevert;
596 //Trigger the stop of the sortable
597 this.instance._mouseStop(event);
599 this.instance.options.helper = this.instance.options._helper;
601 //If the helper has been the original item, restore properties in the sortable
602 if(inst.options.helper === "original") {
603 this.instance.currentItem.css({ top: "auto", left: "auto" });
607 this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
608 this.instance._trigger("deactivate", event, uiSortable);
614 drag: function(event, ui) {
616 var inst = $(this).data("ui-draggable"), that = this;
618 $.each(inst.sortables, function() {
620 var innermostIntersecting = false,
623 //Copy over some variables to allow calling the sortable's native _intersectsWith
624 this.instance.positionAbs = inst.positionAbs;
625 this.instance.helperProportions = inst.helperProportions;
626 this.instance.offset.click = inst.offset.click;
628 if(this.instance._intersectsWith(this.instance.containerCache)) {
629 innermostIntersecting = true;
630 $.each(inst.sortables, function () {
631 this.instance.positionAbs = inst.positionAbs;
632 this.instance.helperProportions = inst.helperProportions;
633 this.instance.offset.click = inst.offset.click;
634 if (this !== thisSortable &&
635 this.instance._intersectsWith(this.instance.containerCache) &&
636 $.contains(thisSortable.instance.element[0], this.instance.element[0])
638 innermostIntersecting = false;
640 return innermostIntersecting;
645 if(innermostIntersecting) {
646 //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
647 if(!this.instance.isOver) {
649 this.instance.isOver = 1;
650 //Now we fake the start of dragging for the sortable instance,
651 //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
652 //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)
653 this.instance.currentItem = $(that).clone().removeAttr("id").appendTo(this.instance.element).data("ui-sortable-item", true);
654 this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
655 this.instance.options.helper = function() { return ui.helper[0]; };
657 event.target = this.instance.currentItem[0];
658 this.instance._mouseCapture(event, true);
659 this.instance._mouseStart(event, true, true);
661 //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
662 this.instance.offset.click.top = inst.offset.click.top;
663 this.instance.offset.click.left = inst.offset.click.left;
664 this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
665 this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
667 inst._trigger("toSortable", event);
668 inst.dropped = this.instance.element; //draggable revert needs that
669 //hack so receive/update callbacks work (mostly)
670 inst.currentItem = inst.element;
671 this.instance.fromOutside = inst;
675 //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
676 if(this.instance.currentItem) {
677 this.instance._mouseDrag(event);
682 //If it doesn't intersect with the sortable, and it intersected before,
683 //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
684 if(this.instance.isOver) {
686 this.instance.isOver = 0;
687 this.instance.cancelHelperRemoval = true;
689 //Prevent reverting on this forced stop
690 this.instance.options.revert = false;
692 // The out event needs to be triggered independently
693 this.instance._trigger("out", event, this.instance._uiHash(this.instance));
695 this.instance._mouseStop(event, true);
696 this.instance.options.helper = this.instance.options._helper;
698 //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
699 this.instance.currentItem.remove();
700 if(this.instance.placeholder) {
701 this.instance.placeholder.remove();
704 inst._trigger("fromSortable", event);
705 inst.dropped = false; //draggable revert needs that
715 $.ui.plugin.add("draggable", "cursor", {
717 var t = $("body"), o = $(this).data("ui-draggable").options;
718 if (t.css("cursor")) {
719 o._cursor = t.css("cursor");
721 t.css("cursor", o.cursor);
724 var o = $(this).data("ui-draggable").options;
726 $("body").css("cursor", o._cursor);
731 $.ui.plugin.add("draggable", "opacity", {
732 start: function(event, ui) {
733 var t = $(ui.helper), o = $(this).data("ui-draggable").options;
734 if(t.css("opacity")) {
735 o._opacity = t.css("opacity");
737 t.css("opacity", o.opacity);
739 stop: function(event, ui) {
740 var o = $(this).data("ui-draggable").options;
742 $(ui.helper).css("opacity", o._opacity);
747 $.ui.plugin.add("draggable", "scroll", {
749 var i = $(this).data("ui-draggable");
750 if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") {
751 i.overflowOffset = i.scrollParent.offset();
754 drag: function( event ) {
756 var i = $(this).data("ui-draggable"), o = i.options, scrolled = false;
758 if(i.scrollParent[0] !== document && i.scrollParent[0].tagName !== "HTML") {
760 if(!o.axis || o.axis !== "x") {
761 if((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) {
762 i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed;
763 } else if(event.pageY - i.overflowOffset.top < o.scrollSensitivity) {
764 i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop - o.scrollSpeed;
768 if(!o.axis || o.axis !== "y") {
769 if((i.overflowOffset.left + i.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) {
770 i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft + o.scrollSpeed;
771 } else if(event.pageX - i.overflowOffset.left < o.scrollSensitivity) {
772 i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft - o.scrollSpeed;
778 if(!o.axis || o.axis !== "x") {
779 if(event.pageY - $(document).scrollTop() < o.scrollSensitivity) {
780 scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
781 } else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) {
782 scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
786 if(!o.axis || o.axis !== "y") {
787 if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity) {
788 scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
789 } else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) {
790 scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
796 if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
797 $.ui.ddmanager.prepareOffsets(i, event);
803 $.ui.plugin.add("draggable", "snap", {
806 var i = $(this).data("ui-draggable"),
811 $(o.snap.constructor !== String ? ( o.snap.items || ":data(ui-draggable)" ) : o.snap).each(function() {
814 if(this !== i.element[0]) {
815 i.snapElements.push({
817 width: $t.outerWidth(), height: $t.outerHeight(),
818 top: $o.top, left: $o.left
824 drag: function(event, ui) {
826 var ts, bs, ls, rs, l, r, t, b, i, first,
827 inst = $(this).data("ui-draggable"),
830 x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
831 y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
833 for (i = inst.snapElements.length - 1; i >= 0; i--){
835 l = inst.snapElements[i].left;
836 r = l + inst.snapElements[i].width;
837 t = inst.snapElements[i].top;
838 b = t + inst.snapElements[i].height;
840 //Yes, I know, this is insane ;)
841 if(!((l-d < x1 && x1 < r+d && t-d < y1 && y1 < b+d) || (l-d < x1 && x1 < r+d && t-d < y2 && y2 < b+d) || (l-d < x2 && x2 < r+d && t-d < y1 && y1 < b+d) || (l-d < x2 && x2 < r+d && t-d < y2 && y2 < b+d))) {
842 if(inst.snapElements[i].snapping) {
843 (inst.options.snap.release && inst.options.snap.release.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
845 inst.snapElements[i].snapping = false;
849 if(o.snapMode !== "inner") {
850 ts = Math.abs(t - y2) <= d;
851 bs = Math.abs(b - y1) <= d;
852 ls = Math.abs(l - x2) <= d;
853 rs = Math.abs(r - x1) <= d;
855 ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
858 ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
861 ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
864 ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
868 first = (ts || bs || ls || rs);
870 if(o.snapMode !== "outer") {
871 ts = Math.abs(t - y1) <= d;
872 bs = Math.abs(b - y2) <= d;
873 ls = Math.abs(l - x1) <= d;
874 rs = Math.abs(r - x2) <= d;
876 ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
879 ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
882 ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
885 ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
889 if(!inst.snapElements[i].snapping && (ts || bs || ls || rs || first)) {
890 (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
892 inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
899 $.ui.plugin.add("draggable", "stack", {
902 o = this.data("ui-draggable").options,
903 group = $.makeArray($(o.stack)).sort(function(a,b) {
904 return (parseInt($(a).css("zIndex"),10) || 0) - (parseInt($(b).css("zIndex"),10) || 0);
907 if (!group.length) { return; }
909 min = parseInt($(group[0]).css("zIndex"), 10) || 0;
910 $(group).each(function(i) {
911 $(this).css("zIndex", min + i);
913 this.css("zIndex", (min + group.length));
917 $.ui.plugin.add("draggable", "zIndex", {
918 start: function(event, ui) {
919 var t = $(ui.helper), o = $(this).data("ui-draggable").options;
920 if(t.css("zIndex")) {
921 o._zIndex = t.css("zIndex");
923 t.css("zIndex", o.zIndex);
925 stop: function(event, ui) {
926 var o = $(this).data("ui-draggable").options;
928 $(ui.helper).css("zIndex", o._zIndex);