jquery.ui.sortable.js (39142B)
1 /* 2 * jQuery UI Sortable 1.8 3 * 4 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about) 5 * Dual licensed under the MIT (MIT-LICENSE.txt) 6 * and GPL (GPL-LICENSE.txt) licenses. 7 * 8 * http://docs.jquery.com/UI/Sortables 9 * 10 * Depends: 11 * jquery.ui.core.js 12 * jquery.ui.mouse.js 13 * jquery.ui.widget.js 14 */ 15 (function($) { 16 17 $.widget("ui.sortable", $.ui.mouse, { 18 widgetEventPrefix: "sort", 19 options: { 20 appendTo: "parent", 21 axis: false, 22 connectWith: false, 23 containment: false, 24 cursor: 'auto', 25 cursorAt: false, 26 dropOnEmpty: true, 27 forcePlaceholderSize: false, 28 forceHelperSize: false, 29 grid: false, 30 handle: false, 31 helper: "original", 32 items: '> *', 33 opacity: false, 34 placeholder: false, 35 revert: false, 36 scroll: true, 37 scrollSensitivity: 20, 38 scrollSpeed: 20, 39 scope: "default", 40 tolerance: "intersect", 41 zIndex: 1000 42 }, 43 _create: function() { 44 45 var o = this.options; 46 this.containerCache = {}; 47 this.element.addClass("ui-sortable"); 48 49 //Get the items 50 this.refresh(); 51 52 //Let's determine if the items are floating 53 this.floating = this.items.length ? (/left|right/).test(this.items[0].item.css('float')) : false; 54 55 //Let's determine the parent's offset 56 this.offset = this.element.offset(); 57 58 //Initialize mouse events for interaction 59 this._mouseInit(); 60 61 }, 62 63 destroy: function() { 64 this.element 65 .removeClass("ui-sortable ui-sortable-disabled") 66 .removeData("sortable") 67 .unbind(".sortable"); 68 this._mouseDestroy(); 69 70 for ( var i = this.items.length - 1; i >= 0; i-- ) 71 this.items[i].item.removeData("sortable-item"); 72 73 return this; 74 }, 75 76 _mouseCapture: function(event, overrideHandle) { 77 78 if (this.reverting) { 79 return false; 80 } 81 82 if(this.options.disabled || this.options.type == 'static') return false; 83 84 //We have to refresh the items data once first 85 this._refreshItems(event); 86 87 //Find out if the clicked node (or one of its parents) is a actual item in this.items 88 var currentItem = null, self = this, nodes = $(event.target).parents().each(function() { 89 if($.data(this, 'sortable-item') == self) { 90 currentItem = $(this); 91 return false; 92 } 93 }); 94 if($.data(event.target, 'sortable-item') == self) currentItem = $(event.target); 95 96 if(!currentItem) return false; 97 if(this.options.handle && !overrideHandle) { 98 var validHandle = false; 99 100 $(this.options.handle, currentItem).find("*").andSelf().each(function() { if(this == event.target) validHandle = true; }); 101 if(!validHandle) return false; 102 } 103 104 this.currentItem = currentItem; 105 this._removeCurrentsFromItems(); 106 return true; 107 108 }, 109 110 _mouseStart: function(event, overrideHandle, noActivation) { 111 112 var o = this.options, self = this; 113 this.currentContainer = this; 114 115 //We only need to call refreshPositions, because the refreshItems call has been moved to mouseCapture 116 this.refreshPositions(); 117 118 //Create and append the visible helper 119 this.helper = this._createHelper(event); 120 121 //Cache the helper size 122 this._cacheHelperProportions(); 123 124 /* 125 * - Position generation - 126 * This block generates everything position related - it's the core of draggables. 127 */ 128 129 //Cache the margins of the original element 130 this._cacheMargins(); 131 132 //Get the next scrolling parent 133 this.scrollParent = this.helper.scrollParent(); 134 135 //The element's absolute position on the page minus margins 136 this.offset = this.currentItem.offset(); 137 this.offset = { 138 top: this.offset.top - this.margins.top, 139 left: this.offset.left - this.margins.left 140 }; 141 142 // Only after we got the offset, we can change the helper's position to absolute 143 // TODO: Still need to figure out a way to make relative sorting possible 144 this.helper.css("position", "absolute"); 145 this.cssPosition = this.helper.css("position"); 146 147 $.extend(this.offset, { 148 click: { //Where the click happened, relative to the element 149 left: event.pageX - this.offset.left, 150 top: event.pageY - this.offset.top 151 }, 152 parent: this._getParentOffset(), 153 relative: this._getRelativeOffset() //This is a relative to absolute position minus the actual position calculation - only used for relative positioned helper 154 }); 155 156 //Generate the original position 157 this.originalPosition = this._generatePosition(event); 158 this.originalPageX = event.pageX; 159 this.originalPageY = event.pageY; 160 161 //Adjust the mouse offset relative to the helper if 'cursorAt' is supplied 162 (o.cursorAt && this._adjustOffsetFromHelper(o.cursorAt)); 163 164 //Cache the former DOM position 165 this.domPosition = { prev: this.currentItem.prev()[0], parent: this.currentItem.parent()[0] }; 166 167 //If the helper is not the original, hide the original so it's not playing any role during the drag, won't cause anything bad this way 168 if(this.helper[0] != this.currentItem[0]) { 169 this.currentItem.hide(); 170 } 171 172 //Create the placeholder 173 this._createPlaceholder(); 174 175 //Set a containment if given in the options 176 if(o.containment) 177 this._setContainment(); 178 179 if(o.cursor) { // cursor option 180 if ($('body').css("cursor")) this._storedCursor = $('body').css("cursor"); 181 $('body').css("cursor", o.cursor); 182 } 183 184 if(o.opacity) { // opacity option 185 if (this.helper.css("opacity")) this._storedOpacity = this.helper.css("opacity"); 186 this.helper.css("opacity", o.opacity); 187 } 188 189 if(o.zIndex) { // zIndex option 190 if (this.helper.css("zIndex")) this._storedZIndex = this.helper.css("zIndex"); 191 this.helper.css("zIndex", o.zIndex); 192 } 193 194 //Prepare scrolling 195 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') 196 this.overflowOffset = this.scrollParent.offset(); 197 198 //Call callbacks 199 this._trigger("start", event, this._uiHash()); 200 201 //Recache the helper size 202 if(!this._preserveHelperProportions) 203 this._cacheHelperProportions(); 204 205 206 //Post 'activate' events to possible containers 207 if(!noActivation) { 208 for (var i = this.containers.length - 1; i >= 0; i--) { this.containers[i]._trigger("activate", event, self._uiHash(this)); } 209 } 210 211 //Prepare possible droppables 212 if($.ui.ddmanager) 213 $.ui.ddmanager.current = this; 214 215 if ($.ui.ddmanager && !o.dropBehaviour) 216 $.ui.ddmanager.prepareOffsets(this, event); 217 218 this.dragging = true; 219 220 this.helper.addClass("ui-sortable-helper"); 221 this._mouseDrag(event); //Execute the drag once - this causes the helper not to be visible before getting its correct position 222 return true; 223 224 }, 225 226 _mouseDrag: function(event) { 227 228 //Compute the helpers position 229 this.position = this._generatePosition(event); 230 this.positionAbs = this._convertPositionTo("absolute"); 231 232 if (!this.lastPositionAbs) { 233 this.lastPositionAbs = this.positionAbs; 234 } 235 236 //Do scrolling 237 if(this.options.scroll) { 238 var o = this.options, scrolled = false; 239 if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') { 240 241 if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) 242 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed; 243 else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity) 244 this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed; 245 246 if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) 247 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed; 248 else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity) 249 this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed; 250 251 } else { 252 253 if(event.pageY - $(document).scrollTop() < o.scrollSensitivity) 254 scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed); 255 else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) 256 scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed); 257 258 if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity) 259 scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed); 260 else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) 261 scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed); 262 263 } 264 265 if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) 266 $.ui.ddmanager.prepareOffsets(this, event); 267 } 268 269 //Regenerate the absolute position used for position checks 270 this.positionAbs = this._convertPositionTo("absolute"); 271 272 //Set the helper position 273 if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px'; 274 if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px'; 275 276 //Rearrange 277 for (var i = this.items.length - 1; i >= 0; i--) { 278 279 //Cache variables and intersection, continue if no intersection 280 var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item); 281 if (!intersection) continue; 282 283 if(itemElement != this.currentItem[0] //cannot intersect with itself 284 && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before 285 && !$.ui.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked 286 && (this.options.type == 'semi-dynamic' ? !$.ui.contains(this.element[0], itemElement) : true) 287 //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container 288 ) { 289 290 this.direction = intersection == 1 ? "down" : "up"; 291 292 if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) { 293 this._rearrange(event, item); 294 } else { 295 break; 296 } 297 298 this._trigger("change", event, this._uiHash()); 299 break; 300 } 301 } 302 303 //Post events to containers 304 this._contactContainers(event); 305 306 //Interconnect with droppables 307 if($.ui.ddmanager) $.ui.ddmanager.drag(this, event); 308 309 //Call callbacks 310 this._trigger('sort', event, this._uiHash()); 311 312 this.lastPositionAbs = this.positionAbs; 313 return false; 314 315 }, 316 317 _mouseStop: function(event, noPropagation) { 318 319 if(!event) return; 320 321 //If we are using droppables, inform the manager about the drop 322 if ($.ui.ddmanager && !this.options.dropBehaviour) 323 $.ui.ddmanager.drop(this, event); 324 325 if(this.options.revert) { 326 var self = this; 327 var cur = self.placeholder.offset(); 328 329 self.reverting = true; 330 331 $(this.helper).animate({ 332 left: cur.left - this.offset.parent.left - self.margins.left + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollLeft), 333 top: cur.top - this.offset.parent.top - self.margins.top + (this.offsetParent[0] == document.body ? 0 : this.offsetParent[0].scrollTop) 334 }, parseInt(this.options.revert, 10) || 500, function() { 335 self._clear(event); 336 }); 337 } else { 338 this._clear(event, noPropagation); 339 } 340 341 return false; 342 343 }, 344 345 cancel: function() { 346 347 var self = this; 348 349 if(this.dragging) { 350 351 this._mouseUp(); 352 353 if(this.options.helper == "original") 354 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper"); 355 else 356 this.currentItem.show(); 357 358 //Post deactivating events to containers 359 for (var i = this.containers.length - 1; i >= 0; i--){ 360 this.containers[i]._trigger("deactivate", null, self._uiHash(this)); 361 if(this.containers[i].containerCache.over) { 362 this.containers[i]._trigger("out", null, self._uiHash(this)); 363 this.containers[i].containerCache.over = 0; 364 } 365 } 366 367 } 368 369 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node! 370 if(this.placeholder[0].parentNode) this.placeholder[0].parentNode.removeChild(this.placeholder[0]); 371 if(this.options.helper != "original" && this.helper && this.helper[0].parentNode) this.helper.remove(); 372 373 $.extend(this, { 374 helper: null, 375 dragging: false, 376 reverting: false, 377 _noFinalSort: null 378 }); 379 380 if(this.domPosition.prev) { 381 $(this.domPosition.prev).after(this.currentItem); 382 } else { 383 $(this.domPosition.parent).prepend(this.currentItem); 384 } 385 386 return this; 387 388 }, 389 390 serialize: function(o) { 391 392 var items = this._getItemsAsjQuery(o && o.connected); 393 var str = []; o = o || {}; 394 395 $(items).each(function() { 396 var res = ($(o.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); 397 if(res) str.push((o.key || res[1]+'[]')+'='+(o.key && o.expression ? res[1] : res[2])); 398 }); 399 400 return str.join('&'); 401 402 }, 403 404 toArray: function(o) { 405 406 var items = this._getItemsAsjQuery(o && o.connected); 407 var ret = []; o = o || {}; 408 409 items.each(function() { ret.push($(o.item || this).attr(o.attribute || 'id') || ''); }); 410 return ret; 411 412 }, 413 414 /* Be careful with the following core functions */ 415 _intersectsWith: function(item) { 416 417 var x1 = this.positionAbs.left, 418 x2 = x1 + this.helperProportions.width, 419 y1 = this.positionAbs.top, 420 y2 = y1 + this.helperProportions.height; 421 422 var l = item.left, 423 r = l + item.width, 424 t = item.top, 425 b = t + item.height; 426 427 var dyClick = this.offset.click.top, 428 dxClick = this.offset.click.left; 429 430 var isOverElement = (y1 + dyClick) > t && (y1 + dyClick) < b && (x1 + dxClick) > l && (x1 + dxClick) < r; 431 432 if( this.options.tolerance == "pointer" 433 || this.options.forcePointerForContainers 434 || (this.options.tolerance != "pointer" && this.helperProportions[this.floating ? 'width' : 'height'] > item[this.floating ? 'width' : 'height']) 435 ) { 436 return isOverElement; 437 } else { 438 439 return (l < x1 + (this.helperProportions.width / 2) // Right Half 440 && x2 - (this.helperProportions.width / 2) < r // Left Half 441 && t < y1 + (this.helperProportions.height / 2) // Bottom Half 442 && y2 - (this.helperProportions.height / 2) < b ); // Top Half 443 444 } 445 }, 446 447 _intersectsWithPointer: function(item) { 448 449 var isOverElementHeight = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top, item.height), 450 isOverElementWidth = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left, item.width), 451 isOverElement = isOverElementHeight && isOverElementWidth, 452 verticalDirection = this._getDragVerticalDirection(), 453 horizontalDirection = this._getDragHorizontalDirection(); 454 455 if (!isOverElement) 456 return false; 457 458 return this.floating ? 459 ( ((horizontalDirection && horizontalDirection == "right") || verticalDirection == "down") ? 2 : 1 ) 460 : ( verticalDirection && (verticalDirection == "down" ? 2 : 1) ); 461 462 }, 463 464 _intersectsWithSides: function(item) { 465 466 var isOverBottomHalf = $.ui.isOverAxis(this.positionAbs.top + this.offset.click.top, item.top + (item.height/2), item.height), 467 isOverRightHalf = $.ui.isOverAxis(this.positionAbs.left + this.offset.click.left, item.left + (item.width/2), item.width), 468 verticalDirection = this._getDragVerticalDirection(), 469 horizontalDirection = this._getDragHorizontalDirection(); 470 471 if (this.floating && horizontalDirection) { 472 return ((horizontalDirection == "right" && isOverRightHalf) || (horizontalDirection == "left" && !isOverRightHalf)); 473 } else { 474 return verticalDirection && ((verticalDirection == "down" && isOverBottomHalf) || (verticalDirection == "up" && !isOverBottomHalf)); 475 } 476 477 }, 478 479 _getDragVerticalDirection: function() { 480 var delta = this.positionAbs.top - this.lastPositionAbs.top; 481 return delta != 0 && (delta > 0 ? "down" : "up"); 482 }, 483 484 _getDragHorizontalDirection: function() { 485 var delta = this.positionAbs.left - this.lastPositionAbs.left; 486 return delta != 0 && (delta > 0 ? "right" : "left"); 487 }, 488 489 refresh: function(event) { 490 this._refreshItems(event); 491 this.refreshPositions(); 492 return this; 493 }, 494 495 _connectWith: function() { 496 var options = this.options; 497 return options.connectWith.constructor == String 498 ? [options.connectWith] 499 : options.connectWith; 500 }, 501 502 _getItemsAsjQuery: function(connected) { 503 504 var self = this; 505 var items = []; 506 var queries = []; 507 var connectWith = this._connectWith(); 508 509 if(connectWith && connected) { 510 for (var i = connectWith.length - 1; i >= 0; i--){ 511 var cur = $(connectWith[i]); 512 for (var j = cur.length - 1; j >= 0; j--){ 513 var inst = $.data(cur[j], 'sortable'); 514 if(inst && inst != this && !inst.options.disabled) { 515 queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element) : $(inst.options.items, inst.element).not(".ui-sortable-helper").not('.ui-sortable-placeholder'), inst]); 516 } 517 }; 518 }; 519 } 520 521 queries.push([$.isFunction(this.options.items) ? this.options.items.call(this.element, null, { options: this.options, item: this.currentItem }) : $(this.options.items, this.element).not(".ui-sortable-helper").not('.ui-sortable-placeholder'), this]); 522 523 for (var i = queries.length - 1; i >= 0; i--){ 524 queries[i][0].each(function() { 525 items.push(this); 526 }); 527 }; 528 529 return $(items); 530 531 }, 532 533 _removeCurrentsFromItems: function() { 534 535 var list = this.currentItem.find(":data(sortable-item)"); 536 537 for (var i=0; i < this.items.length; i++) { 538 539 for (var j=0; j < list.length; j++) { 540 if(list[j] == this.items[i].item[0]) 541 this.items.splice(i,1); 542 }; 543 544 }; 545 546 }, 547 548 _refreshItems: function(event) { 549 550 this.items = []; 551 this.containers = [this]; 552 var items = this.items; 553 var self = this; 554 var queries = [[$.isFunction(this.options.items) ? this.options.items.call(this.element[0], event, { item: this.currentItem }) : $(this.options.items, this.element), this]]; 555 var connectWith = this._connectWith(); 556 557 if(connectWith) { 558 for (var i = connectWith.length - 1; i >= 0; i--){ 559 var cur = $(connectWith[i]); 560 for (var j = cur.length - 1; j >= 0; j--){ 561 var inst = $.data(cur[j], 'sortable'); 562 if(inst && inst != this && !inst.options.disabled) { 563 queries.push([$.isFunction(inst.options.items) ? inst.options.items.call(inst.element[0], event, { item: this.currentItem }) : $(inst.options.items, inst.element), inst]); 564 this.containers.push(inst); 565 } 566 }; 567 }; 568 } 569 570 for (var i = queries.length - 1; i >= 0; i--) { 571 var targetData = queries[i][1]; 572 var _queries = queries[i][0]; 573 574 for (var j=0, queriesLength = _queries.length; j < queriesLength; j++) { 575 var item = $(_queries[j]); 576 577 item.data('sortable-item', targetData); // Data for target checking (mouse manager) 578 579 items.push({ 580 item: item, 581 instance: targetData, 582 width: 0, height: 0, 583 left: 0, top: 0 584 }); 585 }; 586 }; 587 588 }, 589 590 refreshPositions: function(fast) { 591 592 //This has to be redone because due to the item being moved out/into the offsetParent, the offsetParent's position will change 593 if(this.offsetParent && this.helper) { 594 this.offset.parent = this._getParentOffset(); 595 } 596 597 for (var i = this.items.length - 1; i >= 0; i--){ 598 var item = this.items[i]; 599 600 var t = this.options.toleranceElement ? $(this.options.toleranceElement, item.item) : item.item; 601 602 if (!fast) { 603 item.width = t.outerWidth(); 604 item.height = t.outerHeight(); 605 } 606 607 var p = t.offset(); 608 item.left = p.left; 609 item.top = p.top; 610 }; 611 612 if(this.options.custom && this.options.custom.refreshContainers) { 613 this.options.custom.refreshContainers.call(this); 614 } else { 615 for (var i = this.containers.length - 1; i >= 0; i--){ 616 var p = this.containers[i].element.offset(); 617 this.containers[i].containerCache.left = p.left; 618 this.containers[i].containerCache.top = p.top; 619 this.containers[i].containerCache.width = this.containers[i].element.outerWidth(); 620 this.containers[i].containerCache.height = this.containers[i].element.outerHeight(); 621 }; 622 } 623 624 return this; 625 }, 626 627 _createPlaceholder: function(that) { 628 629 var self = that || this, o = self.options; 630 631 if(!o.placeholder || o.placeholder.constructor == String) { 632 var className = o.placeholder; 633 o.placeholder = { 634 element: function() { 635 636 var el = $(document.createElement(self.currentItem[0].nodeName)) 637 .addClass(className || self.currentItem[0].className+" ui-sortable-placeholder") 638 .removeClass("ui-sortable-helper")[0]; 639 640 if(!className) 641 el.style.visibility = "hidden"; 642 643 return el; 644 }, 645 update: function(container, p) { 646 647 // 1. If a className is set as 'placeholder option, we don't force sizes - the class is responsible for that 648 // 2. The option 'forcePlaceholderSize can be enabled to force it even if a class name is specified 649 if(className && !o.forcePlaceholderSize) return; 650 651 //If the element doesn't have a actual height by itself (without styles coming from a stylesheet), it receives the inline height from the dragged item 652 if(!p.height()) { p.height(self.currentItem.innerHeight() - parseInt(self.currentItem.css('paddingTop')||0, 10) - parseInt(self.currentItem.css('paddingBottom')||0, 10)); }; 653 if(!p.width()) { p.width(self.currentItem.innerWidth() - parseInt(self.currentItem.css('paddingLeft')||0, 10) - parseInt(self.currentItem.css('paddingRight')||0, 10)); }; 654 } 655 }; 656 } 657 658 //Create the placeholder 659 self.placeholder = $(o.placeholder.element.call(self.element, self.currentItem)); 660 661 //Append it after the actual current item 662 self.currentItem.after(self.placeholder); 663 664 //Update the size of the placeholder (TODO: Logic to fuzzy, see line 316/317) 665 o.placeholder.update(self, self.placeholder); 666 667 }, 668 669 _contactContainers: function(event) { 670 671 // get innermost container that intersects with item 672 var innermostContainer = null, innermostIndex = null; 673 674 675 for (var i = this.containers.length - 1; i >= 0; i--){ 676 677 // never consider a container that's located within the item itself 678 if($.ui.contains(this.currentItem[0], this.containers[i].element[0])) 679 continue; 680 681 if(this._intersectsWith(this.containers[i].containerCache)) { 682 683 // if we've already found a container and it's more "inner" than this, then continue 684 if(innermostContainer && $.ui.contains(this.containers[i].element[0], innermostContainer.element[0])) 685 continue; 686 687 innermostContainer = this.containers[i]; 688 innermostIndex = i; 689 690 } else { 691 // container doesn't intersect. trigger "out" event if necessary 692 if(this.containers[i].containerCache.over) { 693 this.containers[i]._trigger("out", event, this._uiHash(this)); 694 this.containers[i].containerCache.over = 0; 695 } 696 } 697 698 } 699 700 // if no intersecting containers found, return 701 if(!innermostContainer) return; 702 703 // move the item into the container if it's not there already 704 if(this.containers.length === 1) { 705 this.containers[innermostIndex]._trigger("over", event, this._uiHash(this)); 706 this.containers[innermostIndex].containerCache.over = 1; 707 } else if(this.currentContainer != this.containers[innermostIndex]) { 708 709 //When entering a new container, we will find the item with the least distance and append our item near it 710 var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[innermostIndex].floating ? 'left' : 'top']; 711 for (var j = this.items.length - 1; j >= 0; j--) { 712 if(!$.ui.contains(this.containers[innermostIndex].element[0], this.items[j].item[0])) continue; 713 var cur = this.items[j][this.containers[innermostIndex].floating ? 'left' : 'top']; 714 if(Math.abs(cur - base) < dist) { 715 dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j]; 716 } 717 } 718 719 if(!itemWithLeastDistance && !this.options.dropOnEmpty) //Check if dropOnEmpty is enabled 720 return; 721 722 this.currentContainer = this.containers[innermostIndex]; 723 itemWithLeastDistance ? this._rearrange(event, itemWithLeastDistance, null, true) : this._rearrange(event, null, this.containers[innermostIndex].element, true); 724 this._trigger("change", event, this._uiHash()); 725 this.containers[innermostIndex]._trigger("change", event, this._uiHash(this)); 726 727 //Update the placeholder 728 this.options.placeholder.update(this.currentContainer, this.placeholder); 729 730 this.containers[innermostIndex]._trigger("over", event, this._uiHash(this)); 731 this.containers[innermostIndex].containerCache.over = 1; 732 } 733 734 735 }, 736 737 _createHelper: function(event) { 738 739 var o = this.options; 740 var helper = $.isFunction(o.helper) ? $(o.helper.apply(this.element[0], [event, this.currentItem])) : (o.helper == 'clone' ? this.currentItem.clone() : this.currentItem); 741 742 if(!helper.parents('body').length) //Add the helper to the DOM if that didn't happen already 743 $(o.appendTo != 'parent' ? o.appendTo : this.currentItem[0].parentNode)[0].appendChild(helper[0]); 744 745 if(helper[0] == this.currentItem[0]) 746 this._storedCSS = { width: this.currentItem[0].style.width, height: this.currentItem[0].style.height, position: this.currentItem.css("position"), top: this.currentItem.css("top"), left: this.currentItem.css("left") }; 747 748 if(helper[0].style.width == '' || o.forceHelperSize) helper.width(this.currentItem.width()); 749 if(helper[0].style.height == '' || o.forceHelperSize) helper.height(this.currentItem.height()); 750 751 return helper; 752 753 }, 754 755 _adjustOffsetFromHelper: function(obj) { 756 if (typeof obj == 'string') { 757 obj = obj.split(' '); 758 } 759 if ($.isArray(obj)) { 760 obj = {left: +obj[0], top: +obj[1] || 0}; 761 } 762 if ('left' in obj) { 763 this.offset.click.left = obj.left + this.margins.left; 764 } 765 if ('right' in obj) { 766 this.offset.click.left = this.helperProportions.width - obj.right + this.margins.left; 767 } 768 if ('top' in obj) { 769 this.offset.click.top = obj.top + this.margins.top; 770 } 771 if ('bottom' in obj) { 772 this.offset.click.top = this.helperProportions.height - obj.bottom + this.margins.top; 773 } 774 }, 775 776 _getParentOffset: function() { 777 778 779 //Get the offsetParent and cache its position 780 this.offsetParent = this.helper.offsetParent(); 781 var po = this.offsetParent.offset(); 782 783 // This is a special case where we need to modify a offset calculated on start, since the following happened: 784 // 1. The position of the helper is absolute, so it's position is calculated based on the next positioned parent 785 // 2. The actual offset parent is a child of the scroll parent, and the scroll parent isn't the document, which means that 786 // the scroll is included in the initial calculation of the offset of the parent, and never recalculated upon drag 787 if(this.cssPosition == 'absolute' && this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) { 788 po.left += this.scrollParent.scrollLeft(); 789 po.top += this.scrollParent.scrollTop(); 790 } 791 792 if((this.offsetParent[0] == document.body) //This needs to be actually done for all browsers, since pageX/pageY includes this information 793 || (this.offsetParent[0].tagName && this.offsetParent[0].tagName.toLowerCase() == 'html' && $.browser.msie)) //Ugly IE fix 794 po = { top: 0, left: 0 }; 795 796 return { 797 top: po.top + (parseInt(this.offsetParent.css("borderTopWidth"),10) || 0), 798 left: po.left + (parseInt(this.offsetParent.css("borderLeftWidth"),10) || 0) 799 }; 800 801 }, 802 803 _getRelativeOffset: function() { 804 805 if(this.cssPosition == "relative") { 806 var p = this.currentItem.position(); 807 return { 808 top: p.top - (parseInt(this.helper.css("top"),10) || 0) + this.scrollParent.scrollTop(), 809 left: p.left - (parseInt(this.helper.css("left"),10) || 0) + this.scrollParent.scrollLeft() 810 }; 811 } else { 812 return { top: 0, left: 0 }; 813 } 814 815 }, 816 817 _cacheMargins: function() { 818 this.margins = { 819 left: (parseInt(this.currentItem.css("marginLeft"),10) || 0), 820 top: (parseInt(this.currentItem.css("marginTop"),10) || 0) 821 }; 822 }, 823 824 _cacheHelperProportions: function() { 825 this.helperProportions = { 826 width: this.helper.outerWidth(), 827 height: this.helper.outerHeight() 828 }; 829 }, 830 831 _setContainment: function() { 832 833 var o = this.options; 834 if(o.containment == 'parent') o.containment = this.helper[0].parentNode; 835 if(o.containment == 'document' || o.containment == 'window') this.containment = [ 836 0 - this.offset.relative.left - this.offset.parent.left, 837 0 - this.offset.relative.top - this.offset.parent.top, 838 $(o.containment == 'document' ? document : window).width() - this.helperProportions.width - this.margins.left, 839 ($(o.containment == 'document' ? document : window).height() || document.body.parentNode.scrollHeight) - this.helperProportions.height - this.margins.top 840 ]; 841 842 if(!(/^(document|window|parent)$/).test(o.containment)) { 843 var ce = $(o.containment)[0]; 844 var co = $(o.containment).offset(); 845 var over = ($(ce).css("overflow") != 'hidden'); 846 847 this.containment = [ 848 co.left + (parseInt($(ce).css("borderLeftWidth"),10) || 0) + (parseInt($(ce).css("paddingLeft"),10) || 0) - this.margins.left, 849 co.top + (parseInt($(ce).css("borderTopWidth"),10) || 0) + (parseInt($(ce).css("paddingTop"),10) || 0) - this.margins.top, 850 co.left+(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, 851 co.top+(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 852 ]; 853 } 854 855 }, 856 857 _convertPositionTo: function(d, pos) { 858 859 if(!pos) pos = this.position; 860 var mod = d == "absolute" ? 1 : -1; 861 var o = this.options, scroll = this.cssPosition == 'absolute' && !(this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName); 862 863 return { 864 top: ( 865 pos.top // The absolute mouse position 866 + this.offset.relative.top * mod // Only for relative positioned nodes: Relative offset from element to offset parent 867 + this.offset.parent.top * mod // The offsetParent's offset without borders (offset + border) 868 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) ) * mod) 869 ), 870 left: ( 871 pos.left // The absolute mouse position 872 + this.offset.relative.left * mod // Only for relative positioned nodes: Relative offset from element to offset parent 873 + this.offset.parent.left * mod // The offsetParent's offset without borders (offset + border) 874 - ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() ) * mod) 875 ) 876 }; 877 878 }, 879 880 _generatePosition: function(event) { 881 882 var o = this.options, scroll = this.cssPosition == 'absolute' && !(this.scrollParent[0] != document && $.ui.contains(this.scrollParent[0], this.offsetParent[0])) ? this.offsetParent : this.scrollParent, scrollIsRootNode = (/(html|body)/i).test(scroll[0].tagName); 883 884 // This is another very weird special case that only happens for relative elements: 885 // 1. If the css position is relative 886 // 2. and the scroll parent is the document or similar to the offset parent 887 // we have to refresh the relative offset during the scroll so there are no jumps 888 if(this.cssPosition == 'relative' && !(this.scrollParent[0] != document && this.scrollParent[0] != this.offsetParent[0])) { 889 this.offset.relative = this._getRelativeOffset(); 890 } 891 892 var pageX = event.pageX; 893 var pageY = event.pageY; 894 895 /* 896 * - Position constraining - 897 * Constrain the position to a mix of grid, containment. 898 */ 899 900 if(this.originalPosition) { //If we are not dragging yet, we won't check for options 901 902 if(this.containment) { 903 if(event.pageX - this.offset.click.left < this.containment[0]) pageX = this.containment[0] + this.offset.click.left; 904 if(event.pageY - this.offset.click.top < this.containment[1]) pageY = this.containment[1] + this.offset.click.top; 905 if(event.pageX - this.offset.click.left > this.containment[2]) pageX = this.containment[2] + this.offset.click.left; 906 if(event.pageY - this.offset.click.top > this.containment[3]) pageY = this.containment[3] + this.offset.click.top; 907 } 908 909 if(o.grid) { 910 var top = this.originalPageY + Math.round((pageY - this.originalPageY) / o.grid[1]) * o.grid[1]; 911 pageY = this.containment ? (!(top - this.offset.click.top < this.containment[1] || top - this.offset.click.top > this.containment[3]) ? top : (!(top - this.offset.click.top < this.containment[1]) ? top - o.grid[1] : top + o.grid[1])) : top; 912 913 var left = this.originalPageX + Math.round((pageX - this.originalPageX) / o.grid[0]) * o.grid[0]; 914 pageX = this.containment ? (!(left - this.offset.click.left < this.containment[0] || left - this.offset.click.left > this.containment[2]) ? left : (!(left - this.offset.click.left < this.containment[0]) ? left - o.grid[0] : left + o.grid[0])) : left; 915 } 916 917 } 918 919 return { 920 top: ( 921 pageY // The absolute mouse position 922 - this.offset.click.top // Click offset (relative to the element) 923 - this.offset.relative.top // Only for relative positioned nodes: Relative offset from element to offset parent 924 - this.offset.parent.top // The offsetParent's offset without borders (offset + border) 925 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollTop() : ( scrollIsRootNode ? 0 : scroll.scrollTop() ) )) 926 ), 927 left: ( 928 pageX // The absolute mouse position 929 - this.offset.click.left // Click offset (relative to the element) 930 - this.offset.relative.left // Only for relative positioned nodes: Relative offset from element to offset parent 931 - this.offset.parent.left // The offsetParent's offset without borders (offset + border) 932 + ($.browser.safari && this.cssPosition == 'fixed' ? 0 : ( this.cssPosition == 'fixed' ? -this.scrollParent.scrollLeft() : scrollIsRootNode ? 0 : scroll.scrollLeft() )) 933 ) 934 }; 935 936 }, 937 938 _rearrange: function(event, i, a, hardRefresh) { 939 940 a ? a[0].appendChild(this.placeholder[0]) : i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction == 'down' ? i.item[0] : i.item[0].nextSibling)); 941 942 //Various things done here to improve the performance: 943 // 1. we create a setTimeout, that calls refreshPositions 944 // 2. on the instance, we have a counter variable, that get's higher after every append 945 // 3. on the local scope, we copy the counter variable, and check in the timeout, if it's still the same 946 // 4. this lets only the last addition to the timeout stack through 947 this.counter = this.counter ? ++this.counter : 1; 948 var self = this, counter = this.counter; 949 950 window.setTimeout(function() { 951 if(counter == self.counter) self.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove 952 },0); 953 954 }, 955 956 _clear: function(event, noPropagation) { 957 958 this.reverting = false; 959 // We delay all events that have to be triggered to after the point where the placeholder has been removed and 960 // everything else normalized again 961 var delayedTriggers = [], self = this; 962 963 // We first have to update the dom position of the actual currentItem 964 // Note: don't do it if the current item is already removed (by a user), or it gets reappended (see #4088) 965 if(!this._noFinalSort && this.currentItem[0].parentNode) this.placeholder.before(this.currentItem); 966 this._noFinalSort = null; 967 968 if(this.helper[0] == this.currentItem[0]) { 969 for(var i in this._storedCSS) { 970 if(this._storedCSS[i] == 'auto' || this._storedCSS[i] == 'static') this._storedCSS[i] = ''; 971 } 972 this.currentItem.css(this._storedCSS).removeClass("ui-sortable-helper"); 973 } else { 974 this.currentItem.show(); 975 } 976 977 if(this.fromOutside && !noPropagation) delayedTriggers.push(function(event) { this._trigger("receive", event, this._uiHash(this.fromOutside)); }); 978 if((this.fromOutside || this.domPosition.prev != this.currentItem.prev().not(".ui-sortable-helper")[0] || this.domPosition.parent != this.currentItem.parent()[0]) && !noPropagation) delayedTriggers.push(function(event) { this._trigger("update", event, this._uiHash()); }); //Trigger update callback if the DOM position has changed 979 if(!$.ui.contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element 980 if(!noPropagation) delayedTriggers.push(function(event) { this._trigger("remove", event, this._uiHash()); }); 981 for (var i = this.containers.length - 1; i >= 0; i--){ 982 if($.ui.contains(this.containers[i].element[0], this.currentItem[0]) && !noPropagation) { 983 delayedTriggers.push((function(c) { return function(event) { c._trigger("receive", event, this._uiHash(this)); }; }).call(this, this.containers[i])); 984 delayedTriggers.push((function(c) { return function(event) { c._trigger("update", event, this._uiHash(this)); }; }).call(this, this.containers[i])); 985 } 986 }; 987 }; 988 989 //Post events to containers 990 for (var i = this.containers.length - 1; i >= 0; i--){ 991 if(!noPropagation) delayedTriggers.push((function(c) { return function(event) { c._trigger("deactivate", event, this._uiHash(this)); }; }).call(this, this.containers[i])); 992 if(this.containers[i].containerCache.over) { 993 delayedTriggers.push((function(c) { return function(event) { c._trigger("out", event, this._uiHash(this)); }; }).call(this, this.containers[i])); 994 this.containers[i].containerCache.over = 0; 995 } 996 } 997 998 //Do what was originally in plugins 999 if(this._storedCursor) $('body').css("cursor", this._storedCursor); //Reset cursor 1000 if(this._storedOpacity) this.helper.css("opacity", this._storedOpacity); //Reset opacity 1001 if(this._storedZIndex) this.helper.css("zIndex", this._storedZIndex == 'auto' ? '' : this._storedZIndex); //Reset z-index 1002 1003 this.dragging = false; 1004 if(this.cancelHelperRemoval) { 1005 if(!noPropagation) { 1006 this._trigger("beforeStop", event, this._uiHash()); 1007 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events 1008 this._trigger("stop", event, this._uiHash()); 1009 } 1010 return false; 1011 } 1012 1013 if(!noPropagation) this._trigger("beforeStop", event, this._uiHash()); 1014 1015 //$(this.placeholder[0]).remove(); would have been the jQuery way - unfortunately, it unbinds ALL events from the original node! 1016 this.placeholder[0].parentNode.removeChild(this.placeholder[0]); 1017 1018 if(this.helper[0] != this.currentItem[0]) this.helper.remove(); this.helper = null; 1019 1020 if(!noPropagation) { 1021 for (var i=0; i < delayedTriggers.length; i++) { delayedTriggers[i].call(this, event); }; //Trigger all delayed events 1022 this._trigger("stop", event, this._uiHash()); 1023 } 1024 1025 this.fromOutside = false; 1026 return true; 1027 1028 }, 1029 1030 _trigger: function() { 1031 if ($.Widget.prototype._trigger.apply(this, arguments) === false) { 1032 this.cancel(); 1033 } 1034 }, 1035 1036 _uiHash: function(inst) { 1037 var self = inst || this; 1038 return { 1039 helper: self.helper, 1040 placeholder: self.placeholder || $([]), 1041 position: self.position, 1042 originalPosition: self.originalPosition, 1043 offset: self.positionAbs, 1044 item: self.currentItem, 1045 sender: inst ? inst.element : null 1046 }; 1047 } 1048 1049 }); 1050 1051 $.extend($.ui.sortable, { 1052 version: "1.8" 1053 }); 1054 1055 })(jQuery);