Skip to content

Commit

Permalink
#447: emit 'onEnd' Only if we really move the item
Browse files Browse the repository at this point in the history
  • Loading branch information
RubaXa committed Sep 15, 2015
1 parent 42fa792 commit 4a67b43
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@
if (rootEl !== parentEl) {
newIndex = _index(dragEl);

if (newIndex != -1) {
if (newIndex >= 0) {
// drag from one list and drop into another
_dispatchEvent(null, parentEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
Expand All @@ -782,16 +782,17 @@
if (dragEl.nextSibling !== nextEl) {
// Get the index of the dragged element within its parent
newIndex = _index(dragEl);
if (newIndex != -1) {

if (newIndex >= 0) {
// drag & drop within the same list
_dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
}
}
}

if (Sortable.active) {
// Drag end event
if (newIndex >= 0) {
// Only if we really move the item.
_dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);

// Save sorting
Expand Down Expand Up @@ -1154,20 +1155,22 @@

/**
* Returns the index of an element within its parent
* @param el
* @returns {number}
* @private
* @param {HTMLElement} el
* @return {number}
*/
function _index(/**HTMLElement*/el) {
function _index(el) {
var index = 0;

if (!el || !el.parentNode) {
return -1;
}
var index = 0;

while (el && (el = el.previousElementSibling)) {
if (el.nodeName.toUpperCase() !== 'TEMPLATE') {
index++;
}
}

return index;
}

Expand Down

0 comments on commit 4a67b43

Please sign in to comment.