diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/README.md b/README.md index 27d6c0b..d83d50e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ HTML5 Sortable jQuery Plugin **[Demos & Documentation](http://farhadi.ir/projects/html5sortable)** +Changes From the Original Version +-------- +* `sortupdate` event now provides `from` and `to` indexes of dragged item + Features -------- * Less than 1KB (minified and gzipped). diff --git a/jquery.sortable.js b/jquery.sortable.js index 2ebca6f..2e30f2b 100644 --- a/jquery.sortable.js +++ b/jquery.sortable.js @@ -21,8 +21,11 @@ $.fn.sortable = function(options) { } return; } - var isHandle, index, items = $(this).children(options.items); + + var isHandle, index, parent, items = $(this).children(options.items); var placeholder = $('<' + (/^(ul|ol)$/i.test(this.tagName) ? 'li' : 'div') + ' class="sortable-placeholder">'); + + items.find(options.handle).mousedown(function() { isHandle = true; }).mouseup(function() { @@ -42,24 +45,34 @@ $.fn.sortable = function(options) { dt.effectAllowed = 'move'; dt.setData('Text', 'dummy'); index = (dragging = $(this)).addClass('sortable-dragging').index(); - }).on('dragend.h5s', function() { + parent = dragging.parent()[0]; + }).on('dragend.h5s', function() { if (!dragging) { return; } dragging.removeClass('sortable-dragging').show(); placeholders.detach(); - if (index != dragging.index()) { - dragging.parent().trigger('sortupdate', {item: dragging}); + if (index != dragging.index() || dragging.parent()[0] != parent) { + dragging.parent().trigger('sortupdate', { item : dragging, from : index, to : dragging.index() }); } dragging = null; - }).not('a[href], img').on('selectstart.h5s', function() { - this.dragDrop && this.dragDrop(); - return false; + }).not('a[href], img').on('selectstart.h5s', function(event) { + // if event originates from input field, let it be + if ($(event.target).is('input, textarea, select')) { + return true; + } else { + this.dragDrop && this.dragDrop(); + return false; + } + }).end().add([this, placeholder]).on('dragover.h5s dragenter.h5s drop.h5s', function(e) { if (!items.is(dragging) && options.connectWith !== $(dragging).parent().data('connectWith')) { return true; } if (e.type == 'drop') { + if (options.forcePlaceholderSize) { + placeholder.removeClass('forcedPlaceholderSize'); + } // if e.stopPropagation(); placeholders.filter(':visible').after(dragging); dragging.trigger('dragend.h5s'); @@ -68,9 +81,10 @@ $.fn.sortable = function(options) { e.preventDefault(); e.originalEvent.dataTransfer.dropEffect = 'move'; if (items.is(this)) { - if (options.forcePlaceholderSize) { - placeholder.height(dragging.outerHeight()); - } + if (options.forcePlaceholderSize && !placeholder.is('.forcedPlaceholderSize')) { + placeholder.addClass('forcedPlaceholderSize'); + placeholder.height(dragging.outerHeight()); + } dragging.hide(); $(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder); placeholders.not(placeholder).detach();