jquery.sortable.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * HTML5 Sortable jQuery Plugin
  3. * http://farhadi.ir/projects/html5sortable
  4. *
  5. * Copyright 2012, Ali Farhadi
  6. * Released under the MIT license.
  7. */
  8. (function($) {
  9. var dragging, placeholders = $();
  10. $.fn.sortable = function(options) {
  11. var method = String(options);
  12. options = $.extend({
  13. connectWith: false
  14. }, options);
  15. return this.each(function() {
  16. if (/^enable|disable|destroy$/.test(method)) {
  17. var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable');
  18. if (method == 'destroy') {
  19. items.add(this).removeData('connectWith items')
  20. .off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s');
  21. }
  22. return;
  23. }
  24. var isHandle, index, items = $(this).children(options.items);
  25. var placeholder = $('<' + (/^ul|ol$/i.test(this.tagName) ? 'li' : 'div') + ' class="sortable-placeholder">');
  26. items.find(options.handle).mousedown(function() {
  27. isHandle = true;
  28. }).mouseup(function() {
  29. isHandle = false;
  30. });
  31. $(this).data('items', options.items)
  32. placeholders = placeholders.add(placeholder);
  33. if (options.connectWith) {
  34. $(options.connectWith).add(this).data('connectWith', options.connectWith);
  35. }
  36. items.attr('draggable', 'true').on('dragstart.h5s', function(e) {
  37. if (options.handle && !isHandle) {
  38. return false;
  39. }
  40. isHandle = false;
  41. var dt = e.originalEvent.dataTransfer;
  42. dt.effectAllowed = 'move';
  43. dt.setData('Text', 'dummy');
  44. index = (dragging = $(this)).addClass('sortable-dragging').index();
  45. }).on('dragend.h5s', function() {
  46. dragging.removeClass('sortable-dragging').show();
  47. placeholders.detach();
  48. if (index != dragging.index()) {
  49. items.parent().trigger('sortupdate', {item: dragging});
  50. }
  51. dragging = null;
  52. }).not('a[href], img').on('selectstart.h5s', function() {
  53. this.dragDrop && this.dragDrop();
  54. return false;
  55. }).end().add([this, placeholder]).on('dragover.h5s dragenter.h5s drop.h5s', function(e) {
  56. if (!items.is(dragging) && options.connectWith !== $(dragging).parent().data('connectWith')) {
  57. return true;
  58. }
  59. if (e.type == 'drop') {
  60. e.stopPropagation();
  61. placeholders.filter(':visible').after(dragging);
  62. return false;
  63. }
  64. e.preventDefault();
  65. e.originalEvent.dataTransfer.dropEffect = 'move';
  66. if (items.is(this)) {
  67. if (options.forcePlaceholderSize) {
  68. placeholder.height(dragging.outerHeight());
  69. }
  70. dragging.hide();
  71. $(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder);
  72. placeholders.not(placeholder).detach();
  73. } else if (!placeholders.is(this) && !$(this).children(options.items).length) {
  74. placeholders.detach();
  75. $(this).append(placeholder);
  76. }
  77. return false;
  78. });
  79. });
  80. };
  81. })(jQuery);