diff --git a/Sortable.js b/Sortable.js index 8699317..1c7b2b6 100644 --- a/Sortable.js +++ b/Sortable.js @@ -271,7 +271,7 @@ } // get the index of the dragged element within its parent - oldIndex = _index(target); + oldIndex = _index(target, options.draggable); // Check filter if (typeof filter === 'function') { @@ -766,7 +766,7 @@ _toggleClass(dragEl, this.options.chosenClass, false); if (rootEl !== parentEl) { - newIndex = _index(dragEl); + newIndex = _index(dragEl, options.draggable); if (newIndex >= 0) { // drag from one list and drop into another @@ -786,7 +786,7 @@ if (dragEl.nextSibling !== nextEl) { // Get the index of the dragged element within its parent - newIndex = _index(dragEl); + newIndex = _index(dragEl, options.draggable); if (newIndex >= 0) { // drag & drop within the same list @@ -1162,11 +1162,13 @@ } /** - * Returns the index of an element within its parent + * Returns the index of an element within its parent for a selected set of + * elements * @param {HTMLElement} el + * @param {selector} selector * @return {number} */ - function _index(el) { + function _index(el, selector) { var index = 0; if (!el || !el.parentNode) { @@ -1174,7 +1176,8 @@ } while (el && (el = el.previousElementSibling)) { - if (el.nodeName.toUpperCase() !== 'TEMPLATE') { + if (el.nodeName.toUpperCase() !== 'TEMPLATE' + && _matches(el, selector)) { index++; } } @@ -1182,6 +1185,23 @@ return index; } + function _matches(/**HTMLElement*/el, /**String*/selector) { + var matches, + i = 0; + + if (el.matches) { + return el.matches(selector); + } else if (el.matchesSelector) { + return el.matchesSelector(selector); + } else { + matches = (el.document || el.ownerDocument).querySelectorAll(selector); + while (matches[i] && matches[i] !== el) { + i++; + } + return matches[i] ? true : false; + } + } + function _throttle(callback, ms) { var args, _this;