* To be called when an item is being sorted. * @param item Item to be sorted. * @param pointerX Position of the item along the X axis. * @param pointerY Position of the item along the Y axis. * @param pointerDelta Direction in which the pointer is moving along each axis.
(
item: DragRef,
pointerX: number,
pointerY: number,
pointerDelta: {x: number; y: number},
)
| 81 | * @param pointerDelta Direction in which the pointer is moving along each axis. |
| 82 | */ |
| 83 | sort( |
| 84 | item: DragRef, |
| 85 | pointerX: number, |
| 86 | pointerY: number, |
| 87 | pointerDelta: {x: number; y: number}, |
| 88 | ): {previousIndex: number; currentIndex: number} | null { |
| 89 | const newIndex = this._getItemIndexFromPointerPosition(item, pointerX, pointerY); |
| 90 | const previousSwap = this._previousSwap; |
| 91 | |
| 92 | if (newIndex === -1 || this._activeItems[newIndex] === item) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | const toSwapWith = this._activeItems[newIndex]; |
| 97 | |
| 98 | // Prevent too many swaps over the same item. |
| 99 | if ( |
| 100 | previousSwap.drag === toSwapWith && |
| 101 | previousSwap.overlaps && |
| 102 | previousSwap.deltaX === pointerDelta.x && |
| 103 | previousSwap.deltaY === pointerDelta.y |
| 104 | ) { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | const previousIndex = this.getItemIndex(item); |
| 109 | const current = item.getPlaceholderElement(); |
| 110 | const overlapElement = toSwapWith.getRootElement(); |
| 111 | |
| 112 | if (newIndex > previousIndex) { |
| 113 | overlapElement.after(current); |
| 114 | } else { |
| 115 | overlapElement.before(current); |
| 116 | } |
| 117 | |
| 118 | moveItemInArray(this._activeItems, previousIndex, newIndex); |
| 119 | |
| 120 | const newOverlapElement = this._getRootNode().elementFromPoint(pointerX, pointerY); |
| 121 | // Note: it's tempting to save the entire `pointerDelta` object here, however that'll |
| 122 | // break this functionality, because the same object is passed for all `sort` calls. |
| 123 | previousSwap.deltaX = pointerDelta.x; |
| 124 | previousSwap.deltaY = pointerDelta.y; |
| 125 | previousSwap.drag = toSwapWith; |
| 126 | previousSwap.overlaps = |
| 127 | overlapElement === newOverlapElement || overlapElement.contains(newOverlapElement); |
| 128 | |
| 129 | return { |
| 130 | previousIndex, |
| 131 | currentIndex: newIndex, |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Called when an item is being moved into the container. |
nothing calls this directly
no test coverage detected