Merge "Use statslib for metrics emitted by HtmlInputTransformHelper, HtmlToContentTra...
[mediawiki.git] / resources / lib / jquery.ui / jquery.ui.draggable.js
blob413b831398b90c30f9a90bb7a7b7eb5d7cbd27c7
1 /*!
2  * jQuery UI Draggable 1.9.2
3  * http://jqueryui.com
4  *
5  * Copyright 2012 jQuery Foundation and other contributors
6  * Released under the MIT license.
7  * http://jquery.org/license
8  *
9  * http://api.jqueryui.com/draggable/
10  *
11  * Depends:
12  *      jquery.ui.core.js
13  *      jquery.ui.mouse.js
14  *      jquery.ui.widget.js
15  */
16 (function( $, undefined ) {
18 $.widget("ui.draggable", $.ui.mouse, {
19         version: "1.9.2",
20         widgetEventPrefix: "drag",
21         options: {
22                 addClasses: true,
23                 appendTo: "parent",
24                 axis: false,
25                 connectToSortable: false,
26                 containment: false,
27                 cursor: "auto",
28                 cursorAt: false,
29                 grid: false,
30                 handle: false,
31                 helper: "original",
32                 iframeFix: false,
33                 opacity: false,
34                 refreshPositions: false,
35                 revert: false,
36                 revertDuration: 500,
37                 scope: "default",
38                 scroll: true,
39                 scrollSensitivity: 20,
40                 scrollSpeed: 20,
41                 snap: false,
42                 snapMode: "both",
43                 snapTolerance: 20,
44                 stack: false,
45                 zIndex: false
46         },
47         _create: function() {
49                 if (this.options.helper == 'original' && !(/^(?:r|a|f)/).test(this.element.css("position")))
50                         this.element[0].style.position = 'relative';
52                 (this.options.addClasses && this.element.addClass("ui-draggable"));
53                 (this.options.disabled && this.element.addClass("ui-draggable-disabled"));
55                 this._mouseInit();
57         },
59         _destroy: function() {
60                 this.element.removeClass( "ui-draggable ui-draggable-dragging ui-draggable-disabled" );
61                 this._mouseDestroy();
62         },
64         _mouseCapture: function(event) {
66                 var o = this.options;
68                 // among others, prevent a drag on a resizable-handle
69                 if (this.helper || o.disabled || $(event.target).is('.ui-resizable-handle'))
70                         return false;
72                 //Quit if we're not on a valid handle
73                 this.handle = this._getHandle(event);
74                 if (!this.handle)
75                         return false;
77                 $(o.iframeFix === true ? "iframe" : o.iframeFix).each(function() {
78                         $('<div class="ui-draggable-iframeFix" style="background: #fff;"></div>')
79                         .css({
80                                 width: this.offsetWidth+"px", height: this.offsetHeight+"px",
81                                 position: "absolute", opacity: "0.001", zIndex: 1000
82                         })
83                         .css($(this).offset())
84                         .appendTo("body");
85                 });
87                 return true;
89         },
91         _mouseStart: function(event) {
93                 var o = this.options;
95                 //Create and append the visible helper
96                 this.helper = this._createHelper(event);
98                 this.helper.addClass("ui-draggable-dragging");
100                 //Cache the helper size
101                 this._cacheHelperProportions();
103                 //If ddmanager is used for droppables, set the global draggable
104                 if($.ui.ddmanager)
105                         $.ui.ddmanager.current = this;
107                 /*
108                  * - Position generation -
109                  * This block generates everything position related - it's the core of draggables.
110                  */
112                 //Cache the margins of the original element
113                 this._cacheMargins();
115                 //Store the helper's css position
116                 this.cssPosition = this.helper.css("position");
117                 this.scrollParent = this.helper.scrollParent();
119                 //The element's absolute position on the page minus margins
120                 this.offset = this.positionAbs = this.element.offset();
121                 this.offset = {
122                         top: this.offset.top - this.margins.top,
123                         left: this.offset.left - this.margins.left
124                 };
126                 $.extend(this.offset, {
127                         click: { //Where the click happened, relative to the element
128                                 left: event.pageX - this.offset.left,
129                                 top: event.pageY - this.offset.top
130                         },
131                         parent: this._getParentOffset(),
132                         relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper
133                 });
135                 //Generate the original position
136                 this.originalPageX = event.pageX;
137                 this.originalPageY = event.pageY;
138                 this.originalPosition = this.position = this._generatePosition(event);
139                 // These lines where moved up to fix an issue with with draggable, revert and grid
140                 // See: https://bugs.jqueryui.com/ticket/4696 and https://gerrit.wikimedia.org/r/#/c/333224
141                 // this.originalPageX = event.pageX;
142                 // this.originalPageY = event.pageY;
144                 //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied
145                 (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt));
147                 //Set a containment if given in the options
148                 if(o.containment)
149                         this._setContainment();
151                 //Trigger event + callbacks
152                 if(this._trigger("start", event) === false) {
153                         this._clear();
154                         return false;
155                 }
157                 //Recache the helper size
158                 this._cacheHelperProportions();
160                 //Prepare the droppable offsets
161                 if ($.ui.ddmanager && !o.dropBehaviour)
162                         $.ui.ddmanager.prepareOffsets(this, event);
165                 this._mouseDrag(event, true); //Execute the drag once - this causes the helper not to be visible before getting its correct position
167                 //If the ddmanager is used for droppables, inform the manager that dragging has started (see #5003)
168                 if ( $.ui.ddmanager ) $.ui.ddmanager.dragStart(this, event);
170                 return true;
171         },
173         _mouseDrag: function(event, noPropagation) {
175                 //Compute the helpers position
176                 this.position = this._generatePosition(event);
177                 this.positionAbs = this._convertPositionTo("absolute");
179                 //Call plugins and callbacks and use the resulting position if something is returned
180                 if (!noPropagation) {
181                         var ui = this._uiHash();
182                         if(this._trigger('drag', event, ui) === false) {
183                                 this._mouseUp({});
184                                 return false;
185                         }
186                         this.position = ui.position;
187                 }
189                 if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px';
190                 if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px';
191                 if($.ui.ddmanager) $.ui.ddmanager.drag(this, event);
193                 return false;
194         },
196         _mouseStop: function(event) {
198                 //If we are using droppables, inform the manager about the drop
199                 var dropped = false;
200                 if ($.ui.ddmanager && !this.options.dropBehaviour)
201                         dropped = $.ui.ddmanager.drop(this, event);
203                 //if a drop comes from outside (a sortable)
204                 if(this.dropped) {
205                         dropped = this.dropped;
206                         this.dropped = false;
207                 }
209                 //if the original element is no longer in the DOM don't bother to continue (see #8269)
210                 var element = this.element[0], elementInDom = false;
211                 while ( element && (element = element.parentNode) ) {
212                         if (element == document ) {
213                                 elementInDom = true;
214                         }
215                 }
216                 if ( !elementInDom && this.options.helper === "original" )
217                         return false;
219                 if((this.options.revert == "invalid" && !dropped) || (this.options.revert == "valid" && dropped) || this.options.revert === true || (typeof this.options.revert === 'function' && this.options.revert.call(this.element, dropped))) {
220                         var that = this;
221                         $(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
222                                 if(that._trigger("stop", event) !== false) {
223                                         that._clear();
224                                 }
225                         });
226                 } else {
227                         if(this._trigger("stop", event) !== false) {
228                                 this._clear();
229                         }
230                 }
232                 return false;
233         },
235         _mouseUp: function(event) {
236                 //Remove frame helpers
237                 $("div.ui-draggable-iframeFix").each(function() {
238                         this.parentNode.removeChild(this);
239                 });
241                 //If the ddmanager is used for droppables, inform the manager that dragging has stopped (see #5003)
242                 if( $.ui.ddmanager ) $.ui.ddmanager.dragStop(this, event);
244                 return $.ui.mouse.prototype._mouseUp.call(this, event);
245         },
247         cancel: function() {
249                 if(this.helper.is(".ui-draggable-dragging")) {
250                         this._mouseUp({});
251                 } else {
252                         this._clear();
253                 }
255                 return this;
257         },
259         _getHandle: function(event) {
261                 var handle = !this.options.handle || !$(this.options.handle, this.element).length ? true : false;
262                 $(this.options.handle, this.element)
263                         .find("*")
264                         .addBack()
265                         .each(function() {
266                                 if(this == event.target) handle = true;
267                         });
269                 return handle;
271         },
273         _createHelper: function(event) {
275                 var o = this.options;
276                 var helper = typeof o.helper === 'function' ? $(o.helper.apply(this.element[0], [event])) : (o.helper == 'clone' ? this.element.clone().removeAttr('id') : this.element);
278                 if(!helper.parents('body').length)
279                         helper.appendTo((o.appendTo == 'parent' ? this.element[0].parentNode : o.appendTo));
281                 if(helper[0] != this.element[0] && !(/(fixed|absolute)/).test(helper.css("position")))
282                         helper.css("position", "absolute");
284                 return helper;
286         },
288         _adjustOffsetFromHelper: function(obj) {
289                 if (typeof obj == 'string') {
290                         obj = obj.split(' ');
291                 }
292                 if (Array.isArray(obj)) {
293                         obj = {left: +obj[0], top: +obj[1] || 0};
294                 }
295                 if ('left' in obj) {
296                         this.offset.click.left = obj.left + this.margins.left;
297                 }
298                 if ('right' in obj) {
299                         this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left;
300                 }
301                 if ('top' in obj) {
302                         this.offset.click.top = obj.top + this.margins.top;
303                 }
304                 if ('bottom' in obj) {
305                         this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top;
306                 }
307         },
309         _getParentOffset: function() {
311                 //Get the offsetParent and cache its position
312                 this.offsetParent = this.helper.offsetParent();
313                 var po = this.offsetParent.offset();
315                 // This is a special case where we need to modify a offset calculated on start, since the following happened:
316                 // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent
317                 // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that
318                 //    the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag
319                 if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.contains(this.scrollParent[0], this.offsetParent[0])) {
320                         po.left += this.scrollParent.scrollLeft();
321                         po.top += this.scrollParent.scrollTop();
322                 }
324                 if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information
325                 || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.ui.ie)) //Ugly IE fix
326                         po = { top: 0, left: 0 };
328                 return {
329                         top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0),
330                         left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0)
331                 };
333         },
335         _getRelativeOffset: function() {
337                 if(this.cssPosition == "relative") {
338                         var p = this.element.position();
339                         return {
340                                 top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(),
341                                 left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft()
342                         };
343                 } else {
344                         return { top: 0, left: 0 };
345                 }
347         },
349         _cacheMargins: function() {
350                 this.margins = {
351                         left: (parseInt(this.element.css("marginLeft"),10) || 0),
352                         top: (parseInt(this.element.css("marginTop"),10) || 0),
353                         right: (parseInt(this.element.css("marginRight"),10) || 0),
354                         bottom: (parseInt(this.element.css("marginBottom"),10) || 0)
355                 };
356         },
358         _cacheHelperProportions: function() {
359                 this.helperProportions = {
360                         width: this.helper.outerWidth(),
361                         height: this.helper.outerHeight()
362                 };
363         },
365         _setContainment: function() {
367                 var o = this.options;
368                 if(o.containment == 'parent') o.containment = this.helper[0].parentNode;
369                 if(o.containment == 'document' || o.containment == 'window') this.containment = [
370                         o.containment == 'document' ? 0 : $(window).scrollLeft() - this.offset.relative.left - this.offset.parent.left,
371                         o.containment == 'document' ? 0 : $(window).scrollTop() - this.offset.relative.top - this.offset.parent.top,
372                         (o.containment == 'document' ? 0 : $(window).scrollLeft()) + $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left,
373                         (o.containment == 'document' ? 0 : $(window).scrollTop()) + ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top
374                 ];
376                 if(!(/^(document|window|parent)$/).test(o.containment) && o.containment.constructor != Array) {
377                         var c = $(o.containment);
378                         var ce = c[0]; if(!ce) return;
379                         var co = c.offset();
380                         var over = ($(ce).css("overflow") != 'hidden');
382                         this.containment = [
383                                 (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0),
384                                 (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0),
385                                 (over ? Math.max(ce.scrollWidth,ce.offsetWidth) : ce.offsetWidth) - (parseInt($(ce).css("borderLeftWidth"),10) || 0) - (parseInt($(ce).css("paddingRight"),10) || 0) - this.helperProportions.width - this.margins.left - this.margins.right,
386                                 (over ? Math.max(ce.scrollHeight,ce.offsetHeight) : ce.offsetHeight) - (parseInt($(ce).css("borderTopWidth"),10) || 0) - (parseInt($(ce).css("paddingBottom"),10) || 0) - this.helperProportions.height - this.margins.top  - this.margins.bottom
387                         ];
388                         this.relative_container = c;
390                 } else if(o.containment.constructor == Array) {
391                         this.containment = o.containment;
392                 }
394         },
396         _convertPositionTo: function(d, pos) {
398                 if(!pos) pos = this.position;
399                 var mod = d == "absolute" ? 1 : -1;
400                 var o = this.options, 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);
402                 return {
403                         top: (
404                                 pos.top                                                                                                                                 // The absolute mouse position
405                                 + this.offset.relative.top * mod                                                                                // Only for relative positioned nodes: Relative offset from element to offset parent
406                                 + this.offset.parent.top * mod                                                                                  // The offsetParent's offset without borders (offset + border)
407                                 - ( ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod)
408                         ),
409                         left: (
410                                 pos.left                                                                                                                                // The absolute mouse position
411                                 + this.offset.relative.left * mod                                                                               // Only for relative positioned nodes: Relative offset from element to offset parent
412                                 + this.offset.parent.left * mod                                                                                 // The offsetParent's offset without borders (offset + border)
413                                 - ( ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod)
414                         )
415                 };
417         },
419         _generatePosition: function(event) {
421                 var o = this.options, 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);
422                 var pageX = event.pageX;
423                 var pageY = event.pageY;
425                 /*
426                  * - Position constraining -
427                  * Constrain the position to a mix of grid, containment.
428                  */
430                 if(this.originalPosition) { //If we are not dragging yet, we won't check for options
431                         var containment;
432                         if(this.containment) {
433                         if (this.relative_container){
434                                 var co = this.relative_container.offset();
435                                 containment = [ this.containment[0] + co.left,
436                                         this.containment[1] + co.top,
437                                         this.containment[2] + co.left,
438                                         this.containment[3] + co.top ];
439                         }
440                         else {
441                                 containment = this.containment;
442                         }
444                                 if(event.pageX - this.offset.click.left < containment[0]) pageX = containment[0] + this.offset.click.left;
445                                 if(event.pageY - this.offset.click.top < containment[1]) pageY = containment[1] + this.offset.click.top;
446                                 if(event.pageX - this.offset.click.left > containment[2]) pageX = containment[2] + this.offset.click.left;
447                                 if(event.pageY - this.offset.click.top > containment[3]) pageY = containment[3] + this.offset.click.top;
448                         }
450                         if(o.grid) {
451                                 //Check for grid elements set to 0 to prevent divide by 0 error causing invalid argument errors in IE (see ticket #6950)
452                                 var top = o.grid[1] ? this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1] : this.originalPageY;
453                                 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;
455                                 var left = o.grid[0] ? this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0] : this.originalPageX;
456                                 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;
457                         }
459                 }
461                 return {
462                         top: (
463                                 pageY                                                                                                                           // The absolute mouse position
464                                 - this.offset.click.top                                                                                                 // Click offset (relative to the element)
465                                 - this.offset.relative.top                                                                                              // Only for relative positioned nodes: Relative offset from element to offset parent
466                                 - this.offset.parent.top                                                                                                // The offsetParent's offset without borders (offset + border)
467                                 + ( ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ))
468                         ),
469                         left: (
470                                 pageX                                                                                                                           // The absolute mouse position
471                                 - this.offset.click.left                                                                                                // Click offset (relative to the element)
472                                 - this.offset.relative.left                                                                                             // Only for relative positioned nodes: Relative offset from element to offset parent
473                                 - this.offset.parent.left                                                                                               // The offsetParent's offset without borders (offset + border)
474                                 + ( ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ))
475                         )
476                 };
478         },
480         _clear: function() {
481                 this.helper.removeClass("ui-draggable-dragging");
482                 if(this.helper[0] != this.element[0] && !this.cancelHelperRemoval) this.helper.remove();
483                 //if($.ui.ddmanager) $.ui.ddmanager.current = null;
484                 this.helper = null;
485                 this.cancelHelperRemoval = false;
486         },
488         // From now on bulk stuff - mainly helpers
490         _trigger: function(type, event, ui) {
491                 ui = ui || this._uiHash();
492                 $.ui.plugin.call(this, type, [event, ui]);
493                 if(type == "drag") this.positionAbs = this._convertPositionTo("absolute"); //The absolute position has to be recalculated after plugins
494                 return $.Widget.prototype._trigger.call(this, type, event, ui);
495         },
497         plugins: {},
499         _uiHash: function(event) {
500                 return {
501                         helper: this.helper,
502                         position: this.position,
503                         originalPosition: this.originalPosition,
504                         offset: this.positionAbs
505                 };
506         }
510 $.ui.plugin.add("draggable", "connectToSortable", {
511         start: function(event, ui) {
513                 var inst = $(this).data("draggable"), o = inst.options,
514                         uiSortable = $.extend({}, ui, { item: inst.element });
515                 inst.sortables = [];
516                 $(o.connectToSortable).each(function() {
517                         var sortable = $.data(this, 'sortable');
518                         if (sortable && !sortable.options.disabled) {
519                                 inst.sortables.push({
520                                         instance: sortable,
521                                         shouldRevert: sortable.options.revert
522                                 });
523                                 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).
524                                 sortable._trigger("activate", event, uiSortable);
525                         }
526                 });
528         },
529         stop: function(event, ui) {
531                 //If we are still over the sortable, we fake the stop event of the sortable, but also remove helper
532                 var inst = $(this).data("draggable"),
533                         uiSortable = $.extend({}, ui, { item: inst.element });
535                 $.each(inst.sortables, function() {
536                         if(this.instance.isOver) {
538                                 this.instance.isOver = 0;
540                                 inst.cancelHelperRemoval = true; //Don't remove the helper in the draggable instance
541                                 this.instance.cancelHelperRemoval = false; //Remove it in the sortable instance (so sortable plugins like revert still work)
543                                 //The sortable revert is supported, and we have to set a temporary dropped variable on the draggable to support revert: 'valid/invalid'
544                                 if(this.shouldRevert) this.instance.options.revert = true;
546                                 //Trigger the stop of the sortable
547                                 this.instance._mouseStop(event);
549                                 this.instance.options.helper = this.instance.options._helper;
551                                 //If the helper has been the original item, restore properties in the sortable
552                                 if(inst.options.helper == 'original')
553                                         this.instance.currentItem.css({ top: 'auto', left: 'auto' });
555                         } else {
556                                 this.instance.cancelHelperRemoval = false; //Remove the helper in the sortable instance
557                                 this.instance._trigger("deactivate", event, uiSortable);
558                         }
560                 });
562         },
563         drag: function(event, ui) {
565                 var inst = $(this).data("draggable"), that = this;
567                 var checkPos = function(o) {
568                         var dyClick = this.offset.click.top, dxClick = this.offset.click.left;
569                         var helperTop = this.positionAbs.top, helperLeft = this.positionAbs.left;
570                         var itemHeight = o.height, itemWidth = o.width;
571                         var itemTop = o.top, itemLeft = o.left;
573                         return $.ui.isOver(helperTop + dyClick, helperLeft + dxClick, itemTop, itemLeft, itemHeight, itemWidth);
574                 };
576                 $.each(inst.sortables, function(i) {
578                         var innermostIntersecting = false;
579                         var thisSortable = this;
580                         //Copy over some variables to allow calling the sortable's native _intersectsWith
581                         this.instance.positionAbs = inst.positionAbs;
582                         this.instance.helperProportions = inst.helperProportions;
583                         this.instance.offset.click = inst.offset.click;
585                         if(this.instance._intersectsWith(this.instance.containerCache)) {
586                                 innermostIntersecting = true;
587                                 $.each(inst.sortables, function () {
588                                         this.instance.positionAbs = inst.positionAbs;
589                                         this.instance.helperProportions = inst.helperProportions;
590                                         this.instance.offset.click = inst.offset.click;
591                                         if  (this != thisSortable
592                                                 && this.instance._intersectsWith(this.instance.containerCache)
593                                                 && $.ui.contains(thisSortable.instance.element[0], this.instance.element[0]))
594                                                 innermostIntersecting = false;
595                                                 return innermostIntersecting;
596                                 });
597                         }
600                         if(innermostIntersecting) {
601                                 //If it intersects, we use a little isOver variable and set it once, so our move-in stuff gets fired only once
602                                 if(!this.instance.isOver) {
604                                         this.instance.isOver = 1;
605                                         //Now we fake the start of dragging for the sortable instance,
606                                         //by cloning the list group item, appending it to the sortable and using it as inst.currentItem
607                                         //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)
608                                         this.instance.currentItem = $(that).clone().removeAttr('id').appendTo(this.instance.element).data("sortable-item", true);
609                                         this.instance.options._helper = this.instance.options.helper; //Store helper option to later restore it
610                                         this.instance.options.helper = function() { return ui.helper[0]; };
612                                         event.target = this.instance.currentItem[0];
613                                         this.instance._mouseCapture(event, true);
614                                         this.instance._mouseStart(event, true, true);
616                                         //Because the browser event is way off the new appended portlet, we modify a couple of variables to reflect the changes
617                                         this.instance.offset.click.top = inst.offset.click.top;
618                                         this.instance.offset.click.left = inst.offset.click.left;
619                                         this.instance.offset.parent.left -= inst.offset.parent.left - this.instance.offset.parent.left;
620                                         this.instance.offset.parent.top -= inst.offset.parent.top - this.instance.offset.parent.top;
622                                         inst._trigger("toSortable", event);
623                                         inst.dropped = this.instance.element; //draggable revert needs that
624                                         //hack so receive/update callbacks work (mostly)
625                                         inst.currentItem = inst.element;
626                                         this.instance.fromOutside = inst;
628                                 }
630                                 //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
631                                 if(this.instance.currentItem) this.instance._mouseDrag(event);
633                         } else {
635                                 //If it doesn't intersect with the sortable, and it intersected before,
636                                 //we fake the drag stop of the sortable, but make sure it doesn't remove the helper by using cancelHelperRemoval
637                                 if(this.instance.isOver) {
639                                         this.instance.isOver = 0;
640                                         this.instance.cancelHelperRemoval = true;
642                                         //Prevent reverting on this forced stop
643                                         this.instance.options.revert = false;
645                                         // The out event needs to be triggered independently
646                                         this.instance._trigger('out', event, this.instance._uiHash(this.instance));
648                                         this.instance._mouseStop(event, true);
649                                         this.instance.options.helper = this.instance.options._helper;
651                                         //Now we remove our currentItem, the list group clone again, and the placeholder, and animate the helper back to it's original size
652                                         this.instance.currentItem.remove();
653                                         if(this.instance.placeholder) this.instance.placeholder.remove();
655                                         inst._trigger("fromSortable", event);
656                                         inst.dropped = false; //draggable revert needs that
657                                 }
659                         };
661                 });
663         }
666 $.ui.plugin.add("draggable", "cursor", {
667         start: function(event, ui) {
668                 var t = $('body'), o = $(this).data('draggable').options;
669                 if (t.css("cursor")) o._cursor = t.css("cursor");
670                 t.css("cursor", o.cursor);
671         },
672         stop: function(event, ui) {
673                 var o = $(this).data('draggable').options;
674                 if (o._cursor) $('body').css("cursor", o._cursor);
675         }
678 $.ui.plugin.add("draggable", "opacity", {
679         start: function(event, ui) {
680                 var t = $(ui.helper), o = $(this).data('draggable').options;
681                 if(t.css("opacity")) o._opacity = t.css("opacity");
682                 t.css('opacity', o.opacity);
683         },
684         stop: function(event, ui) {
685                 var o = $(this).data('draggable').options;
686                 if(o._opacity) $(ui.helper).css('opacity', o._opacity);
687         }
690 $.ui.plugin.add("draggable", "scroll", {
691         start: function(event, ui) {
692                 var i = $(this).data("draggable");
693                 if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') i.overflowOffset = i.scrollParent.offset();
694         },
695         drag: function(event, ui) {
697                 var i = $(this).data("draggable"), o = i.options, scrolled = false;
699                 if(i.scrollParent[0] != document && i.scrollParent[0].tagName != 'HTML') {
701                         if(!o.axis || o.axis != 'x') {
702                                 if((i.overflowOffset.top + i.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity)
703                                         i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop + o.scrollSpeed;
704                                 else if(event.pageY - i.overflowOffset.top < o.scrollSensitivity)
705                                         i.scrollParent[0].scrollTop = scrolled = i.scrollParent[0].scrollTop - o.scrollSpeed;
706                         }
708                         if(!o.axis || o.axis != 'y') {
709                                 if((i.overflowOffset.left + i.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity)
710                                         i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft + o.scrollSpeed;
711                                 else if(event.pageX - i.overflowOffset.left < o.scrollSensitivity)
712                                         i.scrollParent[0].scrollLeft = scrolled = i.scrollParent[0].scrollLeft - o.scrollSpeed;
713                         }
715                 } else {
717                         if(!o.axis || o.axis != 'x') {
718                                 if(event.pageY - $(document).scrollTop() < o.scrollSensitivity)
719                                         scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed);
720                                 else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity)
721                                         scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed);
722                         }
724                         if(!o.axis || o.axis != 'y') {
725                                 if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity)
726                                         scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed);
727                                 else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity)
728                                         scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed);
729                         }
731                 }
733                 if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour)
734                         $.ui.ddmanager.prepareOffsets(i, event);
736         }
739 $.ui.plugin.add("draggable", "snap", {
740         start: function(event, ui) {
742                 var i = $(this).data("draggable"), o = i.options;
743                 i.snapElements = [];
745                 $(o.snap.constructor != String ? ( o.snap.items || ':data(draggable)' ) : o.snap).each(function() {
746                         var $t = $(this); var $o = $t.offset();
747                         if(this != i.element[0]) i.snapElements.push({
748                                 item: this,
749                                 width: $t.outerWidth(), height: $t.outerHeight(),
750                                 top: $o.top, left: $o.left
751                         });
752                 });
754         },
755         drag: function(event, ui) {
757                 var inst = $(this).data("draggable"), o = inst.options;
758                 var d = o.snapTolerance;
760                 var x1 = ui.offset.left, x2 = x1 + inst.helperProportions.width,
761                         y1 = ui.offset.top, y2 = y1 + inst.helperProportions.height;
763                 for (var i = inst.snapElements.length - 1; i >= 0; i--){
765                         var l = inst.snapElements[i].left, r = l + inst.snapElements[i].width,
766                                 t = inst.snapElements[i].top, b = t + inst.snapElements[i].height;
768                         //Yes, I know, this is insane ;)
769                         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))) {
770                                 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 })));
771                                 inst.snapElements[i].snapping = false;
772                                 continue;
773                         }
775                         if(o.snapMode != 'inner') {
776                                 var ts = Math.abs(t - y2) <= d;
777                                 var bs = Math.abs(b - y1) <= d;
778                                 var ls = Math.abs(l - x2) <= d;
779                                 var rs = Math.abs(r - x1) <= d;
780                                 if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
781                                 if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b, left: 0 }).top - inst.margins.top;
782                                 if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l - inst.helperProportions.width }).left - inst.margins.left;
783                                 if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r }).left - inst.margins.left;
784                         }
786                         var first = (ts || bs || ls || rs);
788                         if(o.snapMode != 'outer') {
789                                 var ts = Math.abs(t - y1) <= d;
790                                 var bs = Math.abs(b - y2) <= d;
791                                 var ls = Math.abs(l - x1) <= d;
792                                 var rs = Math.abs(r - x2) <= d;
793                                 if(ts) ui.position.top = inst._convertPositionTo("relative", { top: t, left: 0 }).top - inst.margins.top;
794                                 if(bs) ui.position.top = inst._convertPositionTo("relative", { top: b - inst.helperProportions.height, left: 0 }).top - inst.margins.top;
795                                 if(ls) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: l }).left - inst.margins.left;
796                                 if(rs) ui.position.left = inst._convertPositionTo("relative", { top: 0, left: r - inst.helperProportions.width }).left - inst.margins.left;
797                         }
799                         if(!inst.snapElements[i].snapping && (ts || bs || ls || rs || first))
800                                 (inst.options.snap.snap && inst.options.snap.snap.call(inst.element, event, $.extend(inst._uiHash(), { snapItem: inst.snapElements[i].item })));
801                         inst.snapElements[i].snapping = (ts || bs || ls || rs || first);
803                 };
805         }
808 $.ui.plugin.add("draggable", "stack", {
809         start: function(event, ui) {
811                 var o = $(this).data("draggable").options;
813                 var group = $.makeArray($(o.stack)).sort(function(a,b) {
814                         return (parseInt($(a).css("zIndex"),10) || 0) - (parseInt($(b).css("zIndex"),10) || 0);
815                 });
816                 if (!group.length) { return; }
818                 var min = parseInt(group[0].style.zIndex) || 0;
819                 $(group).each(function(i) {
820                         this.style.zIndex = min + i;
821                 });
823                 this[0].style.zIndex = min + group.length;
825         }
828 $.ui.plugin.add("draggable", "zIndex", {
829         start: function(event, ui) {
830                 var t = $(ui.helper), o = $(this).data("draggable").options;
831                 if(t.css("zIndex")) o._zIndex = t.css("zIndex");
832                 t.css('zIndex', o.zIndex);
833         },
834         stop: function(event, ui) {
835                 var o = $(this).data("draggable").options;
836                 if(o._zIndex) $(ui.helper).css('zIndex', o._zIndex);
837         }
840 })(jQuery);