Browse Source

#447: emit 'onEnd' Only if we really move the item

pull/560/merge
RubaXa 9 years ago
parent
commit
4a67b43188
  1. 21
      Sortable.js

21
Sortable.js

@ -763,7 +763,7 @@
if (rootEl !== parentEl) { if (rootEl !== parentEl) {
newIndex = _index(dragEl); newIndex = _index(dragEl);
if (newIndex != -1) { if (newIndex >= 0) {
// drag from one list and drop into another // drag from one list and drop into another
_dispatchEvent(null, parentEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(null, parentEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
@ -782,7 +782,8 @@
if (dragEl.nextSibling !== nextEl) { if (dragEl.nextSibling !== nextEl) {
// Get the index of the dragged element within its parent // Get the index of the dragged element within its parent
newIndex = _index(dragEl); newIndex = _index(dragEl);
if (newIndex != -1) {
if (newIndex >= 0) {
// drag & drop within the same list // drag & drop within the same list
_dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'update', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
@ -790,8 +791,8 @@
} }
} }
if (Sortable.active) { if (newIndex >= 0) {
// Drag end event // Only if we really move the item.
_dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex); _dispatchEvent(this, rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
// Save sorting // Save sorting
@ -1154,20 +1155,22 @@
/** /**
* Returns the index of an element within its parent * Returns the index of an element within its parent
* @param el * @param {HTMLElement} el
* @returns {number} * @return {number}
* @private
*/ */
function _index(/**HTMLElement*/el) { function _index(el) {
var index = 0;
if (!el || !el.parentNode) { if (!el || !el.parentNode) {
return -1; return -1;
} }
var index = 0;
while (el && (el = el.previousElementSibling)) { while (el && (el = el.previousElementSibling)) {
if (el.nodeName.toUpperCase() !== 'TEMPLATE') { if (el.nodeName.toUpperCase() !== 'TEMPLATE') {
index++; index++;
} }
} }
return index; return index;
} }

Loading…
Cancel
Save