|
41 | 41 |
|
42 | 42 | oldIndex, |
43 | 43 | newIndex, |
| 44 | + oldDraggableIndex, |
| 45 | + newDraggableIndex, |
44 | 46 |
|
45 | 47 | activeGroup, |
46 | 48 | putSortable, |
|
633 | 635 | target = (touch || evt).target, |
634 | 636 | originalTarget = evt.target.shadowRoot && ((evt.path && evt.path[0]) || (evt.composedPath && evt.composedPath()[0])) || target, |
635 | 637 | filter = options.filter, |
636 | | - startIndex; |
| 638 | + startIndex, |
| 639 | + startDraggableIndex; |
637 | 640 |
|
638 | 641 | _saveInputCheckedState(el); |
639 | 642 |
|
|
660 | 663 | } |
661 | 664 |
|
662 | 665 | // Get the index of the dragged element within its parent |
663 | | - startIndex = _index(target, options.draggable); |
| 666 | + startIndex = _index(target); |
| 667 | + startDraggableIndex = _index(target, options.draggable); |
664 | 668 |
|
665 | 669 | // Check filter |
666 | 670 | if (typeof filter === 'function') { |
667 | 671 | if (filter.call(this, evt, target, this)) { |
668 | | - _dispatchEvent(_this, originalTarget, 'filter', target, el, el, startIndex); |
| 672 | + _dispatchEvent(_this, originalTarget, 'filter', target, el, el, startIndex, undefined, startDraggableIndex); |
669 | 673 | preventOnFilter && evt.cancelable && evt.preventDefault(); |
670 | 674 | return; // cancel dnd |
671 | 675 | } |
|
675 | 679 | criteria = _closest(originalTarget, criteria.trim(), el, false); |
676 | 680 |
|
677 | 681 | if (criteria) { |
678 | | - _dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex); |
| 682 | + _dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex, undefined, startDraggableIndex); |
679 | 683 | return true; |
680 | 684 | } |
681 | 685 | }); |
|
691 | 695 | } |
692 | 696 |
|
693 | 697 | // Prepare `dragstart` |
694 | | - this._prepareDragStart(evt, touch, target, startIndex); |
| 698 | + this._prepareDragStart(evt, touch, target, startIndex, startDraggableIndex); |
695 | 699 | }, |
696 | 700 |
|
697 | 701 |
|
|
747 | 751 | } |
748 | 752 | }, |
749 | 753 |
|
750 | | - _prepareDragStart: function (/** Event */evt, /** Touch */touch, /** HTMLElement */target, /** Number */startIndex) { |
| 754 | + _prepareDragStart: function (/** Event */evt, /** Touch */touch, /** HTMLElement */target, /** Number */startIndex, /** Number */startDraggableIndex) { |
751 | 755 | var _this = this, |
752 | 756 | el = _this.el, |
753 | 757 | options = _this.options, |
|
762 | 766 | lastDownEl = target; |
763 | 767 | activeGroup = options.group; |
764 | 768 | oldIndex = startIndex; |
| 769 | + oldDraggableIndex = startDraggableIndex; |
765 | 770 |
|
766 | 771 | tapEvt = { |
767 | 772 | target: dragEl, |
|
790 | 795 | _this._triggerDragStart(evt, touch); |
791 | 796 |
|
792 | 797 | // Drag start event |
793 | | - _dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex); |
| 798 | + _dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex, undefined, oldDraggableIndex); |
794 | 799 |
|
795 | 800 | // Chosen item |
796 | 801 | _toggleClass(dragEl, options.chosenClass, true); |
|
914 | 919 | fallback && this._appendGhost(); |
915 | 920 |
|
916 | 921 | // Drag start event |
917 | | - _dispatchEvent(this, rootEl, 'start', dragEl, rootEl, rootEl, oldIndex, undefined, evt); |
| 922 | + _dispatchEvent(this, rootEl, 'start', dragEl, rootEl, rootEl, oldIndex, undefined, oldDraggableIndex, undefined, evt); |
918 | 923 | } else { |
919 | 924 | this._nulling(); |
920 | 925 | } |
|
1193 | 1198 |
|
1194 | 1199 | // Call when dragEl has been inserted |
1195 | 1200 | function changed() { |
1196 | | - _dispatchEvent(_this, rootEl, 'change', target, el, rootEl, oldIndex, _index(dragEl, options.draggable), evt); |
| 1201 | + _dispatchEvent(_this, rootEl, 'change', target, el, rootEl, oldIndex, _index(dragEl), oldDraggableIndex, _index(dragEl, options.draggable), evt); |
1197 | 1202 | } |
1198 | 1203 |
|
1199 | 1204 |
|
|
1207 | 1212 | target = _closest(target, options.draggable, el, true); |
1208 | 1213 |
|
1209 | 1214 | // target is dragEl or target is animated |
1210 | | - if (!!_closest(evt.target, null, dragEl, true) || target.animated) { |
| 1215 | + if (dragEl.contains(evt.target) || target.animated) { |
1211 | 1216 | return completed(false); |
1212 | 1217 | } |
1213 | 1218 |
|
|
1497 | 1502 | _toggleClass(dragEl, this.options.chosenClass, false); |
1498 | 1503 |
|
1499 | 1504 | // Drag stop event |
1500 | | - _dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, evt); |
| 1505 | + _dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, oldDraggableIndex, null, evt); |
1501 | 1506 |
|
1502 | 1507 | if (rootEl !== parentEl) { |
1503 | | - newIndex = _index(dragEl, options.draggable); |
| 1508 | + newIndex = _index(dragEl); |
| 1509 | + newDraggableIndex = _index(dragEl, options.draggable); |
1504 | 1510 |
|
1505 | 1511 | if (newIndex >= 0) { |
1506 | 1512 | // Add event |
1507 | | - _dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1513 | + _dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1508 | 1514 |
|
1509 | 1515 | // Remove event |
1510 | | - _dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1516 | + _dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1511 | 1517 |
|
1512 | 1518 | // drag from one list and drop into another |
1513 | | - _dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
1514 | | - _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1519 | + _dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
| 1520 | + _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1515 | 1521 | } |
1516 | 1522 |
|
1517 | 1523 | putSortable && putSortable.save(); |
1518 | 1524 | } |
1519 | 1525 | else { |
1520 | 1526 | if (dragEl.nextSibling !== nextEl) { |
1521 | 1527 | // Get the index of the dragged element within its parent |
1522 | | - newIndex = _index(dragEl, options.draggable); |
| 1528 | + newIndex = _index(dragEl); |
| 1529 | + newDraggableIndex = _index(dragEl, options.draggable); |
1523 | 1530 |
|
1524 | 1531 | if (newIndex >= 0) { |
1525 | 1532 | // drag & drop within the same list |
1526 | | - _dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
1527 | | - _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1533 | + _dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
| 1534 | + _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1528 | 1535 | } |
1529 | 1536 | } |
1530 | 1537 | } |
|
1533 | 1540 | /* jshint eqnull:true */ |
1534 | 1541 | if (newIndex == null || newIndex === -1) { |
1535 | 1542 | newIndex = oldIndex; |
| 1543 | + newDraggableIndex = oldDraggableIndex; |
1536 | 1544 | } |
1537 | | - _dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1545 | + _dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1538 | 1546 |
|
1539 | 1547 | // Save sorting |
1540 | 1548 | this.save(); |
|
1767 | 1775 | if ( |
1768 | 1776 | selector != null && |
1769 | 1777 | ( |
1770 | | - selector[0] === '>' && el.parentNode === ctx && _matches(el, selector.substring(1)) || |
| 1778 | + selector[0] === '>' ? |
| 1779 | + el.parentNode === ctx && _matches(el, selector) : |
1771 | 1780 | _matches(el, selector) |
1772 | 1781 | ) || |
1773 | 1782 | includeCTX && el === ctx |
|
1885 | 1894 |
|
1886 | 1895 |
|
1887 | 1896 |
|
1888 | | - function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt) { |
| 1897 | + function _dispatchEvent( |
| 1898 | + sortable, rootEl, name, |
| 1899 | + targetEl, toEl, fromEl, |
| 1900 | + startIndex, newIndex, |
| 1901 | + startDraggableIndex, newDraggableIndex, |
| 1902 | + originalEvt |
| 1903 | + ) { |
1889 | 1904 | sortable = (sortable || rootEl[expando]); |
1890 | 1905 | var evt, |
1891 | 1906 | options = sortable.options, |
|
1909 | 1924 | evt.oldIndex = startIndex; |
1910 | 1925 | evt.newIndex = newIndex; |
1911 | 1926 |
|
| 1927 | + evt.oldDraggableIndex = startDraggableIndex; |
| 1928 | + evt.newDraggableIndex = newDraggableIndex; |
| 1929 | + |
1912 | 1930 | evt.originalEvent = originalEvt; |
1913 | 1931 | evt.pullMode = putSortable ? putSortable.lastPutMode : undefined; |
1914 | 1932 |
|
|
2153 | 2171 | } |
2154 | 2172 |
|
2155 | 2173 | while (el && (el = el.previousElementSibling)) { |
2156 | | - if ((el.nodeName.toUpperCase() !== 'TEMPLATE') && el !== cloneEl) { |
| 2174 | + if ((el.nodeName.toUpperCase() !== 'TEMPLATE') && el !== cloneEl && (!selector || _matches(el, selector))) { |
2157 | 2175 | index++; |
2158 | 2176 | } |
2159 | 2177 | } |
|
2162 | 2180 | } |
2163 | 2181 |
|
2164 | 2182 | function _matches(/**HTMLElement*/el, /**String*/selector) { |
| 2183 | + if (!selector) return; |
| 2184 | + |
| 2185 | + selector[0] === '>' && (selector = selector.substring(1)); |
| 2186 | + |
2165 | 2187 | if (el) { |
2166 | 2188 | try { |
2167 | 2189 | if (el.matches) { |
|
0 commit comments