Skip to content

Commit 000d59e

Browse files
committed
Merge branch 'master' of https://github.com/SortableJS/Sortable
2 parents 6f7fc27 + 4ca519e commit 000d59e

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ var sortable = new Sortable(el, {
146146
evt.from; // previous list
147147
evt.oldIndex; // element's old index within old parent
148148
evt.newIndex; // element's new index within new parent
149+
evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
150+
evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
149151
evt.clone // the clone element
150152
evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
151153
},
@@ -531,6 +533,8 @@ Demo: https://jsbin.com/becavoj/edit?js,output
531533
- clone:`HTMLElement`
532534
- oldIndex:`Number|undefined` — old index within parent
533535
- newIndex:`Number|undefined` — new index within parent
536+
- oldDraggableIndex: `Number|undefined` — old index within parent, only counting draggable elements
537+
- newDraggableIndex: `Number|undefined` — new index within parent, only counting draggable elements
534538
- pullMode:`String|Boolean|undefined` — Pull mode if dragging into another sortable (`"clone"`, `true`, or `false`), otherwise undefined
535539

536540

Sortable.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
oldIndex,
4343
newIndex,
44+
oldDraggableIndex,
45+
newDraggableIndex,
4446

4547
activeGroup,
4648
putSortable,
@@ -633,7 +635,8 @@
633635
target = (touch || evt).target,
634636
originalTarget = evt.target.shadowRoot && ((evt.path && evt.path[0]) || (evt.composedPath && evt.composedPath()[0])) || target,
635637
filter = options.filter,
636-
startIndex;
638+
startIndex,
639+
startDraggableIndex;
637640

638641
_saveInputCheckedState(el);
639642

@@ -660,12 +663,13 @@
660663
}
661664

662665
// 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);
664668

665669
// Check filter
666670
if (typeof filter === 'function') {
667671
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);
669673
preventOnFilter && evt.cancelable && evt.preventDefault();
670674
return; // cancel dnd
671675
}
@@ -675,7 +679,7 @@
675679
criteria = _closest(originalTarget, criteria.trim(), el, false);
676680

677681
if (criteria) {
678-
_dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex);
682+
_dispatchEvent(_this, criteria, 'filter', target, el, el, startIndex, undefined, startDraggableIndex);
679683
return true;
680684
}
681685
});
@@ -691,7 +695,7 @@
691695
}
692696

693697
// Prepare `dragstart`
694-
this._prepareDragStart(evt, touch, target, startIndex);
698+
this._prepareDragStart(evt, touch, target, startIndex, startDraggableIndex);
695699
},
696700

697701

@@ -747,7 +751,7 @@
747751
}
748752
},
749753

750-
_prepareDragStart: function (/** Event */evt, /** Touch */touch, /** HTMLElement */target, /** Number */startIndex) {
754+
_prepareDragStart: function (/** Event */evt, /** Touch */touch, /** HTMLElement */target, /** Number */startIndex, /** Number */startDraggableIndex) {
751755
var _this = this,
752756
el = _this.el,
753757
options = _this.options,
@@ -762,6 +766,7 @@
762766
lastDownEl = target;
763767
activeGroup = options.group;
764768
oldIndex = startIndex;
769+
oldDraggableIndex = startDraggableIndex;
765770

766771
tapEvt = {
767772
target: dragEl,
@@ -790,7 +795,7 @@
790795
_this._triggerDragStart(evt, touch);
791796

792797
// Drag start event
793-
_dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex);
798+
_dispatchEvent(_this, rootEl, 'choose', dragEl, rootEl, rootEl, oldIndex, undefined, oldDraggableIndex);
794799

795800
// Chosen item
796801
_toggleClass(dragEl, options.chosenClass, true);
@@ -914,7 +919,7 @@
914919
fallback && this._appendGhost();
915920

916921
// 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);
918923
} else {
919924
this._nulling();
920925
}
@@ -1193,7 +1198,7 @@
11931198

11941199
// Call when dragEl has been inserted
11951200
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);
11971202
}
11981203

11991204

@@ -1207,7 +1212,7 @@
12071212
target = _closest(target, options.draggable, el, true);
12081213

12091214
// 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) {
12111216
return completed(false);
12121217
}
12131218

@@ -1497,34 +1502,36 @@
14971502
_toggleClass(dragEl, this.options.chosenClass, false);
14981503

14991504
// 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);
15011506

15021507
if (rootEl !== parentEl) {
1503-
newIndex = _index(dragEl, options.draggable);
1508+
newIndex = _index(dragEl);
1509+
newDraggableIndex = _index(dragEl, options.draggable);
15041510

15051511
if (newIndex >= 0) {
15061512
// 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);
15081514

15091515
// 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);
15111517

15121518
// 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);
15151521
}
15161522

15171523
putSortable && putSortable.save();
15181524
}
15191525
else {
15201526
if (dragEl.nextSibling !== nextEl) {
15211527
// 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);
15231530

15241531
if (newIndex >= 0) {
15251532
// 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);
15281535
}
15291536
}
15301537
}
@@ -1533,8 +1540,9 @@
15331540
/* jshint eqnull:true */
15341541
if (newIndex == null || newIndex === -1) {
15351542
newIndex = oldIndex;
1543+
newDraggableIndex = oldDraggableIndex;
15361544
}
1537-
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, evt);
1545+
_dispatchEvent(this, rootEl, 'end', dragEl, parentEl, rootEl, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, evt);
15381546

15391547
// Save sorting
15401548
this.save();
@@ -1767,7 +1775,8 @@
17671775
if (
17681776
selector != null &&
17691777
(
1770-
selector[0] === '>' && el.parentNode === ctx && _matches(el, selector.substring(1)) ||
1778+
selector[0] === '>' ?
1779+
el.parentNode === ctx && _matches(el, selector) :
17711780
_matches(el, selector)
17721781
) ||
17731782
includeCTX && el === ctx
@@ -1885,7 +1894,13 @@
18851894

18861895

18871896

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+
) {
18891904
sortable = (sortable || rootEl[expando]);
18901905
var evt,
18911906
options = sortable.options,
@@ -1909,6 +1924,9 @@
19091924
evt.oldIndex = startIndex;
19101925
evt.newIndex = newIndex;
19111926

1927+
evt.oldDraggableIndex = startDraggableIndex;
1928+
evt.newDraggableIndex = newDraggableIndex;
1929+
19121930
evt.originalEvent = originalEvt;
19131931
evt.pullMode = putSortable ? putSortable.lastPutMode : undefined;
19141932

@@ -2153,7 +2171,7 @@
21532171
}
21542172

21552173
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))) {
21572175
index++;
21582176
}
21592177
}
@@ -2162,6 +2180,10 @@
21622180
}
21632181

21642182
function _matches(/**HTMLElement*/el, /**String*/selector) {
2183+
if (!selector) return;
2184+
2185+
selector[0] === '>' && (selector = selector.substring(1));
2186+
21652187
if (el) {
21662188
try {
21672189
if (el.matches) {

0 commit comments

Comments
 (0)