|
| 1 | +/*! |
| 2 | + * jQuery UI Sortable |
| 3 | + * http://jqueryui.com |
| 4 | + * |
| 5 | + * Copyright jQuery Foundation and other contributors |
| 6 | + * Released under the MIT license. |
| 7 | + * http://jquery.org/license |
| 8 | + */ |
| 9 | + |
| 10 | +define([ |
| 11 | + 'jquery' |
| 12 | +], function ($) { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + /** |
| 16 | + * Patch for sortable widget. |
| 17 | + * Can safely remove only when jQuery UI is upgraded to >= 1.13.1. |
| 18 | + * Fixes: |
| 19 | + * https://github.com/jquery/jquery-ui/pull/2008 |
| 20 | + * https://github.com/jquery/jquery-ui/pull/2009 |
| 21 | + */ |
| 22 | + var sortablePatch = { |
| 23 | + /** @inheritdoc */ |
| 24 | + _mouseDrag: function (event) { |
| 25 | + var i, item, itemElement, intersection, |
| 26 | + o = this.options; |
| 27 | + |
| 28 | + //Compute the helpers position |
| 29 | + this.position = this._generatePosition(event); |
| 30 | + this.positionAbs = this._convertPositionTo("absolute"); |
| 31 | + |
| 32 | + //Set the helper position |
| 33 | + if (!this.options.axis || this.options.axis !== "y") { |
| 34 | + this.helper[0].style.left = this.position.left + "px"; |
| 35 | + } |
| 36 | + if (!this.options.axis || this.options.axis !== "x") { |
| 37 | + this.helper[0].style.top = this.position.top + "px"; |
| 38 | + } |
| 39 | + |
| 40 | + //Do scrolling |
| 41 | + if (o.scroll) { |
| 42 | + if (this._scroll(event) !== false) { |
| 43 | + |
| 44 | + //Update item positions used in position checks |
| 45 | + this._refreshItemPositions(true); |
| 46 | + |
| 47 | + if ($.ui.ddmanager && !o.dropBehaviour) { |
| 48 | + $.ui.ddmanager.prepareOffsets(this, event); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + this.dragDirection = { |
| 54 | + vertical: this._getDragVerticalDirection(), |
| 55 | + horizontal: this._getDragHorizontalDirection() |
| 56 | + }; |
| 57 | + |
| 58 | + //Rearrange |
| 59 | + for (i = this.items.length - 1; i >= 0; i--) { |
| 60 | + |
| 61 | + //Cache variables and intersection, continue if no intersection |
| 62 | + item = this.items[i]; |
| 63 | + itemElement = item.item[0]; |
| 64 | + intersection = this._intersectsWithPointer(item); |
| 65 | + if (!intersection) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // Only put the placeholder inside the current Container, skip all |
| 70 | + // items from other containers. This works because when moving |
| 71 | + // an item from one container to another the |
| 72 | + // currentContainer is switched before the placeholder is moved. |
| 73 | + // |
| 74 | + // Without this, moving items in "sub-sortables" can cause |
| 75 | + // the placeholder to jitter between the outer and inner container. |
| 76 | + if (item.instance !== this.currentContainer) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Cannot intersect with itself |
| 81 | + // no useless actions that have been done before |
| 82 | + // no action if the item moved is the parent of the item checked |
| 83 | + if (itemElement !== this.currentItem[0] && |
| 84 | + this.placeholder[intersection === 1 ? |
| 85 | + "next" : "prev"]()[0] !== itemElement && |
| 86 | + !$.contains(this.placeholder[0], itemElement) && |
| 87 | + (this.options.type === "semi-dynamic" ? |
| 88 | + !$.contains(this.element[0], itemElement) : |
| 89 | + true |
| 90 | + ) |
| 91 | + ) { |
| 92 | + |
| 93 | + this.direction = intersection === 1 ? "down" : "up"; |
| 94 | + |
| 95 | + if (this.options.tolerance === "pointer" || |
| 96 | + this._intersectsWithSides(item)) { |
| 97 | + this._rearrange(event, item); |
| 98 | + } else { |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + this._trigger("change", event, this._uiHash()); |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + //Post events to containers |
| 108 | + this._contactContainers(event); |
| 109 | + |
| 110 | + //Interconnect with droppables |
| 111 | + if ($.ui.ddmanager) { |
| 112 | + $.ui.ddmanager.drag(this, event); |
| 113 | + } |
| 114 | + |
| 115 | + //Call callbacks |
| 116 | + this._trigger("sort", event, this._uiHash()); |
| 117 | + |
| 118 | + this.lastPositionAbs = this.positionAbs; |
| 119 | + return false; |
| 120 | + |
| 121 | + }, |
| 122 | + |
| 123 | + /** @inheritdoc */ |
| 124 | + refreshPositions: function (fast) { |
| 125 | + |
| 126 | + // Determine whether items are being displayed horizontally |
| 127 | + this.floating = this.items.length ? |
| 128 | + this.options.axis === "x" || this._isFloating(this.items[0].item) : |
| 129 | + false; |
| 130 | + |
| 131 | + // This has to be redone because due to the item being moved out/into the offsetParent, |
| 132 | + // the offsetParent's position will change |
| 133 | + if (this.offsetParent && this.helper) { |
| 134 | + this.offset.parent = this._getParentOffset(); |
| 135 | + } |
| 136 | + |
| 137 | + this._refreshItemPositions(fast); |
| 138 | + |
| 139 | + var i, p; |
| 140 | + |
| 141 | + if (this.options.custom && this.options.custom.refreshContainers) { |
| 142 | + this.options.custom.refreshContainers.call(this); |
| 143 | + } else { |
| 144 | + for (i = this.containers.length - 1; i >= 0; i--) { |
| 145 | + p = this.containers[i].element.offset(); |
| 146 | + this.containers[i].containerCache.left = p.left; |
| 147 | + this.containers[i].containerCache.top = p.top; |
| 148 | + this.containers[i].containerCache.width = |
| 149 | + this.containers[i].element.outerWidth(); |
| 150 | + this.containers[i].containerCache.height = |
| 151 | + this.containers[i].element.outerHeight(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return this; |
| 156 | + }, |
| 157 | + |
| 158 | + /** @inheritdoc */ |
| 159 | + _contactContainers: function (event) { |
| 160 | + var i, j, dist, itemWithLeastDistance, posProperty, sizeProperty, cur, nearBottom, |
| 161 | + floating, axis, |
| 162 | + innermostContainer = null, |
| 163 | + innermostIndex = null; |
| 164 | + |
| 165 | + // Get innermost container that intersects with item |
| 166 | + for (i = this.containers.length - 1; i >= 0; i--) { |
| 167 | + |
| 168 | + // Never consider a container that's located within the item itself |
| 169 | + if ($.contains(this.currentItem[0], this.containers[i].element[0])) { |
| 170 | + continue; |
| 171 | + } |
| 172 | + |
| 173 | + if (this._intersectsWith(this.containers[i].containerCache)) { |
| 174 | + |
| 175 | + // If we've already found a container and it's more "inner" than this, then continue |
| 176 | + if (innermostContainer && |
| 177 | + $.contains( |
| 178 | + this.containers[i].element[0], |
| 179 | + innermostContainer.element[0])) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + innermostContainer = this.containers[i]; |
| 184 | + innermostIndex = i; |
| 185 | + |
| 186 | + } else { |
| 187 | + |
| 188 | + // container doesn't intersect. trigger "out" event if necessary |
| 189 | + if (this.containers[i].containerCache.over) { |
| 190 | + this.containers[i]._trigger("out", event, this._uiHash(this)); |
| 191 | + this.containers[i].containerCache.over = 0; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + // If no intersecting containers found, return |
| 198 | + if (!innermostContainer) { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + // Move the item into the container if it's not there already |
| 203 | + if (this.containers.length === 1) { |
| 204 | + if (!this.containers[innermostIndex].containerCache.over) { |
| 205 | + this.containers[innermostIndex]._trigger("over", event, this._uiHash(this)); |
| 206 | + this.containers[innermostIndex].containerCache.over = 1; |
| 207 | + } |
| 208 | + } else { |
| 209 | + |
| 210 | + // When entering a new container, we will find the item with the least distance and |
| 211 | + // append our item near it |
| 212 | + dist = 10000; |
| 213 | + itemWithLeastDistance = null; |
| 214 | + floating = innermostContainer.floating || this._isFloating(this.currentItem); |
| 215 | + posProperty = floating ? "left" : "top"; |
| 216 | + sizeProperty = floating ? "width" : "height"; |
| 217 | + axis = floating ? "pageX" : "pageY"; |
| 218 | + |
| 219 | + for (j = this.items.length - 1; j >= 0; j--) { |
| 220 | + if (!$.contains( |
| 221 | + this.containers[innermostIndex].element[0], this.items[j].item[0]) |
| 222 | + ) { |
| 223 | + continue; |
| 224 | + } |
| 225 | + if (this.items[j].item[0] === this.currentItem[0]) { |
| 226 | + continue; |
| 227 | + } |
| 228 | + |
| 229 | + cur = this.items[j].item.offset()[posProperty]; |
| 230 | + nearBottom = false; |
| 231 | + if (event[axis] - cur > this.items[j][sizeProperty] / 2) { |
| 232 | + nearBottom = true; |
| 233 | + } |
| 234 | + |
| 235 | + if (Math.abs(event[axis] - cur) < dist) { |
| 236 | + dist = Math.abs(event[axis] - cur); |
| 237 | + itemWithLeastDistance = this.items[j]; |
| 238 | + this.direction = nearBottom ? "up" : "down"; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + //Check if dropOnEmpty is enabled |
| 243 | + if (!itemWithLeastDistance && !this.options.dropOnEmpty) { |
| 244 | + return; |
| 245 | + } |
| 246 | + |
| 247 | + if (this.currentContainer === this.containers[innermostIndex]) { |
| 248 | + if (!this.currentContainer.containerCache.over) { |
| 249 | + this.containers[innermostIndex]._trigger("over", event, this._uiHash()); |
| 250 | + this.currentContainer.containerCache.over = 1; |
| 251 | + } |
| 252 | + return; |
| 253 | + } |
| 254 | + |
| 255 | + if (itemWithLeastDistance) { |
| 256 | + this._rearrange(event, itemWithLeastDistance, null, true); |
| 257 | + } else { |
| 258 | + this._rearrange(event, null, this.containers[innermostIndex].element, true); |
| 259 | + } |
| 260 | + this._trigger("change", event, this._uiHash()); |
| 261 | + this.containers[innermostIndex]._trigger("change", event, this._uiHash(this)); |
| 262 | + this.currentContainer = this.containers[innermostIndex]; |
| 263 | + |
| 264 | + //Update the placeholder |
| 265 | + this.options.placeholder.update(this.currentContainer, this.placeholder); |
| 266 | + |
| 267 | + //Update scrollParent |
| 268 | + this.scrollParent = this.placeholder.scrollParent(); |
| 269 | + |
| 270 | + //Update overflowOffset |
| 271 | + if (this.scrollParent[0] !== this.document[0] && |
| 272 | + this.scrollParent[0].tagName !== "HTML") { |
| 273 | + this.overflowOffset = this.scrollParent.offset(); |
| 274 | + } |
| 275 | + |
| 276 | + this.containers[innermostIndex]._trigger("over", event, this._uiHash(this)); |
| 277 | + this.containers[innermostIndex].containerCache.over = 1; |
| 278 | + } |
| 279 | + |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return function () { |
| 284 | + var majorVersion = parseInt($.ui.version.split('.')[0]), |
| 285 | + minorVersion = parseInt($.ui.version.split('.')[1]), |
| 286 | + patchVersion = parseInt($.ui.version.split('.')[2]) |
| 287 | + |
| 288 | + if (majorVersion === 1 && minorVersion === 13 && patchVersion > 0 || |
| 289 | + majorVersion === 1 && minorVersion >= 14 || |
| 290 | + majorVersion >= 2 |
| 291 | + ) { |
| 292 | + console.warn('jQuery ui sortable patch is no longer necessary, and should be removed'); |
| 293 | + } |
| 294 | + |
| 295 | + $.widget('ui.sortable', $.ui.sortable, sortablePatch); |
| 296 | + }; |
| 297 | + |
| 298 | +}); |
0 commit comments