Browse Source

#207: * newIndex

issue-210
RubaXa 10 years ago
parent
commit
3f909d84df
  1. 39
      Sortable.js

39
Sortable.js

@ -25,7 +25,6 @@
"use strict"; "use strict";
var dragEl, var dragEl,
startIndex,
ghostEl, ghostEl,
cloneEl, cloneEl,
rootEl, rootEl,
@ -35,6 +34,9 @@
lastEl, lastEl,
lastCSS, lastCSS,
oldIndex,
newIndex,
activeGroup, activeGroup,
autoScroll = {}, autoScroll = {},
@ -173,7 +175,7 @@
Sortable.active = this; Sortable.active = this;
// Drag start event // Drag start event
_dispatchEvent(rootEl, 'start', dragEl, rootEl, startIndex); _dispatchEvent(rootEl, 'start', dragEl, rootEl, oldIndex);
}, },
@ -197,12 +199,12 @@
target = _closest(target, options.draggable, el); target = _closest(target, options.draggable, el);
// get the index of the dragged element within its parent // get the index of the dragged element within its parent
startIndex = _index(target); oldIndex = _index(target);
// Check filter // Check filter
if (typeof filter === 'function') { if (typeof filter === 'function') {
if (filter.call(this, evt, target, this)) { if (filter.call(this, evt, target, this)) {
_dispatchEvent(originalTarget, 'filter', target, el, startIndex); _dispatchEvent(originalTarget, 'filter', target, el, oldIndex);
evt.preventDefault(); evt.preventDefault();
return; // cancel dnd return; // cancel dnd
} }
@ -212,7 +214,7 @@
criteria = _closest(originalTarget, criteria.trim(), el); criteria = _closest(originalTarget, criteria.trim(), el);
if (criteria) { if (criteria) {
_dispatchEvent(criteria, 'filter', target, el, startIndex); _dispatchEvent(criteria, 'filter', target, el, oldIndex);
return true; return true;
} }
}); });
@ -606,33 +608,36 @@
if (dragEl) { if (dragEl) {
_off(dragEl, 'dragend', this); _off(dragEl, 'dragend', this);
// get the index of the dragged element within its parent
var newIndex = _index(dragEl);
_disableDraggable(dragEl); _disableDraggable(dragEl);
_toggleClass(dragEl, this.options.ghostClass, false); _toggleClass(dragEl, this.options.ghostClass, false);
if (rootEl !== dragEl.parentNode) { if (rootEl !== dragEl.parentNode) {
newIndex = _index(dragEl);
// drag from one list and drop into another // drag from one list and drop into another
_dispatchEvent(dragEl.parentNode, 'sort', dragEl, rootEl, startIndex, newIndex); _dispatchEvent(dragEl.parentNode, 'sort', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(rootEl, 'sort', dragEl, rootEl, startIndex, newIndex); _dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
// Add event // Add event
_dispatchEvent(dragEl, 'add', dragEl, rootEl, startIndex, newIndex); _dispatchEvent(dragEl, 'add', dragEl, rootEl, oldIndex, newIndex);
// Remove event // Remove event
_dispatchEvent(rootEl, 'remove', dragEl, rootEl, startIndex, newIndex); _dispatchEvent(rootEl, 'remove', dragEl, rootEl, oldIndex, newIndex);
} }
else if (dragEl.nextSibling !== nextEl) { else if (dragEl.nextSibling !== nextEl) {
// drag & drop within the same list // (1) Remove clone
_dispatchEvent(rootEl, 'update', dragEl, rootEl, startIndex, newIndex);
_dispatchEvent(rootEl, 'sort', dragEl, rootEl, startIndex, newIndex);
cloneEl && cloneEl.parentNode.removeChild(cloneEl); cloneEl && cloneEl.parentNode.removeChild(cloneEl);
// (2) Get the index of the dragged element within its parent
newIndex = _index(dragEl);
// drag & drop within the same list
_dispatchEvent(rootEl, 'update', dragEl, rootEl, oldIndex, newIndex);
_dispatchEvent(rootEl, 'sort', dragEl, rootEl, oldIndex, newIndex);
} }
// Drag end event // Drag end event
Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, startIndex, newIndex); Sortable.active && _dispatchEvent(rootEl, 'end', dragEl, rootEl, oldIndex, newIndex);
} }
// Set NULL // Set NULL

Loading…
Cancel
Save