Browse Source

Merge pull request #607 from dlamotte/master

fix for sorting elements within a non-homogenus list of elements
pull/670/head
Lebedev Konstantin 9 years ago
parent
commit
6952f3b525
  1. 41
      Sortable.js

41
Sortable.js

@ -271,7 +271,7 @@
} }
// get the index of the dragged element within its parent // get the index of the dragged element within its parent
oldIndex = _index(target); oldIndex = _index(target, options.draggable);
// Check filter // Check filter
if (typeof filter === 'function') { if (typeof filter === 'function') {
@ -766,7 +766,7 @@
_toggleClass(dragEl, this.options.chosenClass, false); _toggleClass(dragEl, this.options.chosenClass, false);
if (rootEl !== parentEl) { if (rootEl !== parentEl) {
newIndex = _index(dragEl); newIndex = _index(dragEl, options.draggable);
if (newIndex >= 0) { if (newIndex >= 0) {
// drag from one list and drop into another // drag from one list and drop into another
@ -786,7 +786,7 @@
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, options.draggable);
if (newIndex >= 0) { if (newIndex >= 0) {
// drag & drop within the same list // drag & drop within the same list
@ -979,17 +979,11 @@
function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) { function _closest(/**HTMLElement*/el, /**String*/selector, /**HTMLElement*/ctx) {
if (el) { if (el) {
ctx = ctx || document; ctx = ctx || document;
selector = selector.split('.');
var tag = selector.shift().toUpperCase(),
re = new RegExp('\\s(' + selector.join('|') + ')(?=\\s)', 'g');
do { do {
if ( if (
(tag === '>*' && el.parentNode === ctx) || ( (selector === '>*' && el.parentNode === ctx)
(tag === '' || el.nodeName.toUpperCase() == tag) && || _matches(el, selector)
(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
)
) { ) {
return el; return el;
} }
@ -1162,11 +1156,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 {HTMLElement} el
* @param {selector} selector
* @return {number} * @return {number}
*/ */
function _index(el) { function _index(el, selector) {
var index = 0; var index = 0;
if (!el || !el.parentNode) { if (!el || !el.parentNode) {
@ -1174,7 +1170,8 @@
} }
while (el && (el = el.previousElementSibling)) { while (el && (el = el.previousElementSibling)) {
if (el.nodeName.toUpperCase() !== 'TEMPLATE') { if (el.nodeName.toUpperCase() !== 'TEMPLATE'
&& _matches(el, selector)) {
index++; index++;
} }
} }
@ -1182,6 +1179,22 @@
return index; return index;
} }
function _matches(/**HTMLElement*/el, /**String*/selector) {
if (el) {
selector = selector.split('.');
var tag = selector.shift().toUpperCase(),
re = new RegExp('\\s(' + selector.join('|') + ')(?=\\s)', 'g');
return (
(tag === '' || el.nodeName.toUpperCase() == tag) &&
(!selector.length || ((' ' + el.className + ' ').match(re) || []).length == selector.length)
);
}
return false;
}
function _throttle(callback, ms) { function _throttle(callback, ms) {
var args, _this; var args, _this;

Loading…
Cancel
Save