|
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 | } |
|
1189 | 1194 |
|
1190 | 1195 | // Call when dragEl has been inserted |
1191 | 1196 | function changed() { |
1192 | | - _dispatchEvent(_this, rootEl, 'change', target, el, rootEl, oldIndex, _index(dragEl, options.draggable), evt); |
| 1197 | + _dispatchEvent(_this, rootEl, 'change', target, el, rootEl, oldIndex, _index(dragEl), oldDraggableIndex, _index(dragEl, options.draggable), evt); |
1193 | 1198 | } |
1194 | 1199 |
|
1195 | 1200 |
|
|
1203 | 1208 | target = _closest(target, options.draggable, el, true); |
1204 | 1209 |
|
1205 | 1210 | // target is dragEl or target is animated |
1206 | | - if (!!_closest(evt.target, null, dragEl, true) || target.animated) { |
| 1211 | + if (dragEl.contains(evt.target) || target.animated) { |
1207 | 1212 | return completed(false); |
1208 | 1213 | } |
1209 | 1214 |
|
|
1493 | 1498 | _toggleClass(dragEl, this.options.chosenClass, false); |
1494 | 1499 |
|
1495 | 1500 | // Drag stop event |
1496 | | - _dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, evt); |
| 1501 | + _dispatchEvent(this, rootEl, 'unchoose', dragEl, parentEl, rootEl, oldIndex, null, oldDraggableIndex, null, evt); |
1497 | 1502 |
|
1498 | 1503 | if (rootEl !== parentEl) { |
1499 | | - newIndex = _index(dragEl, options.draggable); |
| 1504 | + newIndex = _index(dragEl); |
| 1505 | + newDraggableIndex = _index(dragEl, options.draggable); |
1500 | 1506 |
|
1501 | 1507 | if (newIndex >= 0) { |
1502 | 1508 | // Add event |
1503 | | - _dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1509 | + _dispatchEvent(null, parentEl, 'add', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1504 | 1510 |
|
1505 | 1511 | // Remove event |
1506 | | - _dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1512 | + _dispatchEvent(this, rootEl, 'remove', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1507 | 1513 |
|
1508 | 1514 | // drag from one list and drop into another |
1509 | | - _dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
1510 | | - _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1515 | + _dispatchEvent(null, parentEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
| 1516 | + _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1511 | 1517 | } |
1512 | 1518 |
|
1513 | 1519 | putSortable && putSortable.save(); |
1514 | 1520 | } |
1515 | 1521 | else { |
1516 | 1522 | if (dragEl.nextSibling !== nextEl) { |
1517 | 1523 | // Get the index of the dragged element within its parent |
1518 | | - newIndex = _index(dragEl, options.draggable); |
| 1524 | + newIndex = _index(dragEl); |
| 1525 | + newDraggableIndex = _index(dragEl, options.draggable); |
1519 | 1526 |
|
1520 | 1527 | if (newIndex >= 0) { |
1521 | 1528 | // drag & drop within the same list |
1522 | | - _dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
1523 | | - _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1529 | + _dispatchEvent(this, rootEl, 'update', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
| 1530 | + _dispatchEvent(this, rootEl, 'sort', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1524 | 1531 | } |
1525 | 1532 | } |
1526 | 1533 | } |
|
1529 | 1536 | /* jshint eqnull:true */ |
1530 | 1537 | if (newIndex == null || newIndex === -1) { |
1531 | 1538 | newIndex = oldIndex; |
| 1539 | + newDraggableIndex = oldDraggableIndex; |
1532 | 1540 | } |
1533 | | - _dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt); |
| 1541 | + _dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt); |
1534 | 1542 |
|
1535 | 1543 | // Save sorting |
1536 | 1544 | this.save(); |
|
1763 | 1771 | if ( |
1764 | 1772 | selector != null && |
1765 | 1773 | ( |
1766 | | - selector[0] === '>' && el.parentNode === ctx && _matches(el, selector.substring(1)) || |
| 1774 | + selector[0] === '>' ? |
| 1775 | + el.parentNode === ctx && _matches(el, selector) : |
1767 | 1776 | _matches(el, selector) |
1768 | 1777 | ) || |
1769 | 1778 | includeCTX && el === ctx |
|
1881 | 1890 |
|
1882 | 1891 |
|
1883 | 1892 |
|
1884 | | - function _dispatchEvent(sortable, rootEl, name, targetEl, toEl, fromEl, startIndex, newIndex, originalEvt) { |
| 1893 | + function _dispatchEvent( |
| 1894 | + sortable, rootEl, name, |
| 1895 | + targetEl, toEl, fromEl, |
| 1896 | + startIndex, newIndex, |
| 1897 | + startDraggableIndex, newDraggableIndex, |
| 1898 | + originalEvt |
| 1899 | + ) { |
1885 | 1900 | sortable = (sortable || rootEl[expando]); |
1886 | 1901 | var evt, |
1887 | 1902 | options = sortable.options, |
|
1905 | 1920 | evt.oldIndex = startIndex; |
1906 | 1921 | evt.newIndex = newIndex; |
1907 | 1922 |
|
| 1923 | + evt.oldDraggableIndex = startDraggableIndex; |
| 1924 | + evt.newDraggableIndex = newDraggableIndex; |
| 1925 | + |
1908 | 1926 | evt.originalEvent = originalEvt; |
1909 | 1927 | evt.pullMode = putSortable ? putSortable.lastPutMode : undefined; |
1910 | 1928 |
|
|
2149 | 2167 | } |
2150 | 2168 |
|
2151 | 2169 | while (el && (el = el.previousElementSibling)) { |
2152 | | - if ((el.nodeName.toUpperCase() !== 'TEMPLATE') && el !== cloneEl) { |
| 2170 | + if ((el.nodeName.toUpperCase() !== 'TEMPLATE') && el !== cloneEl && (!selector || _matches(el, selector))) { |
2153 | 2171 | index++; |
2154 | 2172 | } |
2155 | 2173 | } |
|
2158 | 2176 | } |
2159 | 2177 |
|
2160 | 2178 | function _matches(/**HTMLElement*/el, /**String*/selector) { |
| 2179 | + if (!selector) return; |
| 2180 | + |
| 2181 | + selector[0] === '>' && (selector = selector.substring(1)); |
| 2182 | + |
2161 | 2183 | if (el) { |
2162 | 2184 | try { |
2163 | 2185 | if (el.matches) { |
|
0 commit comments