From 178908169f3e55ac6794735fa7791acfbb455132 Mon Sep 17 00:00:00 2001 From: qiuling Date: Tue, 4 Jul 2017 16:45:46 +0800 Subject: [PATCH 1/2] add amd && cmd && commonjs support --- jquery.sortable.js | 161 ++++++++++++++++++++++------------------- jquery.sortable.min.js | 9 +-- 2 files changed, 87 insertions(+), 83 deletions(-) diff --git a/jquery.sortable.js b/jquery.sortable.js index 2ebca6f..cf9de34 100644 --- a/jquery.sortable.js +++ b/jquery.sortable.js @@ -5,81 +5,92 @@ * Copyright 2012, Ali Farhadi * Released under the MIT license. */ -(function($) { +(function( factory ) { + if ( typeof define === "function" && define.amd ) { + define( ["jquery"], factory ); + } else if (typeof module === "object" && module.exports) { + module.exports = factory( require( "jquery" ) ); + } else { + factory( jQuery ); + } +}(function( $ ) { +// (function($) { var dragging, placeholders = $(); $.fn.sortable = function(options) { - var method = String(options); - options = $.extend({ - connectWith: false - }, options); - return this.each(function() { - if (/^(enable|disable|destroy)$/.test(method)) { - var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable'); - if (method == 'destroy') { - items.add(this).removeData('connectWith items') - .off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s'); - } - return; - } - var isHandle, index, 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() { - isHandle = false; - }); - $(this).data('items', options.items) - placeholders = placeholders.add(placeholder); - if (options.connectWith) { - $(options.connectWith).add(this).data('connectWith', options.connectWith); - } - items.attr('draggable', 'true').on('dragstart.h5s', function(e) { - if (options.handle && !isHandle) { - return false; - } - isHandle = false; - var dt = e.originalEvent.dataTransfer; - dt.effectAllowed = 'move'; - dt.setData('Text', 'dummy'); - index = (dragging = $(this)).addClass('sortable-dragging').index(); - }).on('dragend.h5s', function() { - if (!dragging) { - return; - } - dragging.removeClass('sortable-dragging').show(); - placeholders.detach(); - if (index != dragging.index()) { - dragging.parent().trigger('sortupdate', {item: dragging}); - } - dragging = null; - }).not('a[href], img').on('selectstart.h5s', function() { - 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') { - e.stopPropagation(); - placeholders.filter(':visible').after(dragging); - dragging.trigger('dragend.h5s'); - return false; - } - e.preventDefault(); - e.originalEvent.dataTransfer.dropEffect = 'move'; - if (items.is(this)) { - if (options.forcePlaceholderSize) { - placeholder.height(dragging.outerHeight()); - } - dragging.hide(); - $(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder); - placeholders.not(placeholder).detach(); - } else if (!placeholders.is(this) && !$(this).children(options.items).length) { - placeholders.detach(); - $(this).append(placeholder); - } - return false; - }); - }); + var method = String(options); + options = $.extend({ + connectWith: false + }, options); + return this.each(function() { + if (/^(enable|disable|destroy)$/.test(method)) { + var items = $(this).children($(this).data('items')).attr('draggable', method == 'enable'); + if (method == 'destroy') { + items.add(this).removeData('connectWith items') + .off('dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s'); + } + return; + } + var isHandle, index, 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() { + isHandle = false; + }); + $(this).data('items', options.items) + placeholders = placeholders.add(placeholder); + if (options.connectWith) { + $(options.connectWith).add(this).data('connectWith', options.connectWith); + } + items.attr('draggable', 'true').on('dragstart.h5s', function(e) { + if (options.handle && !isHandle) { + return false; + } + isHandle = false; + var dt = e.originalEvent.dataTransfer; + dt.effectAllowed = 'move'; + dt.setData('Text', 'dummy'); + index = (dragging = $(this)).addClass('sortable-dragging').index(); + }).on('dragend.h5s', function() { + if (!dragging) { + return; + } + dragging.removeClass('sortable-dragging').show(); + placeholders.detach(); + if (index != dragging.index()) { + dragging.parent().trigger('sortupdate', {item: dragging}); + } + dragging = null; + }).not('a[href], img').on('selectstart.h5s', function() { + 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') { + e.stopPropagation(); + placeholders.filter(':visible').after(dragging); + dragging.trigger('dragend.h5s'); + return false; + } + e.preventDefault(); + e.originalEvent.dataTransfer.dropEffect = 'move'; + if (items.is(this)) { + if (options.forcePlaceholderSize) { + placeholder.height(dragging.outerHeight()); + } + dragging.hide(); + $(this)[placeholder.index() < $(this).index() ? 'after' : 'before'](placeholder); + placeholders.not(placeholder).detach(); + } else if (!placeholders.is(this) && !$(this).children(options.items).length) { + placeholders.detach(); + $(this).append(placeholder); + } + return false; + }); + }); }; -})(jQuery); +// }(jQuery)); +return $; +})); diff --git a/jquery.sortable.min.js b/jquery.sortable.min.js index 7e90810..1c28124 100644 --- a/jquery.sortable.min.js +++ b/jquery.sortable.min.js @@ -1,8 +1 @@ -/* - * HTML5 Sortable jQuery Plugin - * http://farhadi.ir/projects/html5sortable - * - * Copyright 2012, Ali Farhadi - * Released under the MIT license. - */ -(function(e){var t,n=e();e.fn.sortable=function(r){var i=String(r);r=e.extend({connectWith:false},r);return this.each(function(){if(/^enable|disable|destroy$/.test(i)){var s=e(this).children(e(this).data("items")).attr("draggable",i=="enable");if(i=="destroy"){s.add(this).removeData("connectWith items").off("dragstart.h5s dragend.h5s selectstart.h5s dragover.h5s dragenter.h5s drop.h5s")}return}var o,u,s=e(this).children(r.items);var a=e("<"+(/^ul|ol$/i.test(this.tagName)?"li":"div")+' class="sortable-placeholder">');s.find(r.handle).mousedown(function(){o=true}).mouseup(function(){o=false});e(this).data("items",r.items);n=n.add(a);if(r.connectWith){e(r.connectWith).add(this).data("connectWith",r.connectWith)}s.attr("draggable","true").on("dragstart.h5s",function(n){if(r.handle&&!o){return false}o=false;var i=n.originalEvent.dataTransfer;i.effectAllowed="move";i.setData("Text","dummy");u=(t=e(this)).addClass("sortable-dragging").index()}).on("dragend.h5s",function(){if(!t){return}t.removeClass("sortable-dragging").show();n.detach();if(u!=t.index()){t.parent().trigger("sortupdate",{item:t})}t=null}).not("a[href], img").on("selectstart.h5s",function(){this.dragDrop&&this.dragDrop();return false}).end().add([this,a]).on("dragover.h5s dragenter.h5s drop.h5s",function(i){if(!s.is(t)&&r.connectWith!==e(t).parent().data("connectWith")){return true}if(i.type=="drop"){i.stopPropagation();n.filter(":visible").after(t);t.trigger("dragend.h5s");return false}i.preventDefault();i.originalEvent.dataTransfer.dropEffect="move";if(s.is(this)){if(r.forcePlaceholderSize){a.height(t.outerHeight())}t.hide();e(this)[a.index()');f.find(d.handle).mousedown(function(){h=true;}).mouseup(function(){h=false;});c(this).data("items",d.items);a=a.add(i);if(d.connectWith){c(d.connectWith).add(this).data("connectWith",d.connectWith);}f.attr("draggable","true").on("dragstart.h5s",function(k){if(d.handle&&!h){return false;}h=false;var j=k.originalEvent.dataTransfer;j.effectAllowed="move";j.setData("Text","dummy");g=(b=c(this)).addClass("sortable-dragging").index();}).on("dragend.h5s",function(){if(!b){return;}b.removeClass("sortable-dragging").show();a.detach();if(g!=b.index()){b.parent().trigger("sortupdate",{item:b});}b=null;}).not("a[href], img").on("selectstart.h5s",function(){this.dragDrop&&this.dragDrop();return false;}).end().add([this,i]).on("dragover.h5s dragenter.h5s drop.h5s",function(j){if(!f.is(b)&&d.connectWith!==c(b).parent().data("connectWith")){return true;}if(j.type=="drop"){j.stopPropagation();a.filter(":visible").after(b);b.trigger("dragend.h5s");return false;}j.preventDefault();j.originalEvent.dataTransfer.dropEffect="move";if(f.is(this)){if(d.forcePlaceholderSize){i.height(b.outerHeight());}b.hide();c(this)[i.index() Date: Tue, 4 Jul 2017 17:02:43 +0800 Subject: [PATCH 2/2] add package.json --- package.json | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..85928f3 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "html5sortable", + "version": "0.0.2", + "description": "ightweight jQuery plugin to create sortable lists and grids using native HTML5 drag and drop API.", + "main": "./jquery.sortable.js ", + "repository": { + "type": "git", + "url": "git+https://github.com/ql2005/html5sortable.git" + }, + "keywords": [ + "html5", + "sortable", + "jquery" + ], + "dependencies": { + "jquery": ">= 1.9.1" + }, + "author": "Ali Farhadi ", + "license": "MIT", + "bugs": { + "url": "https://github.com/farhadi/html5sortable/issues" + }, + "homepage": "http://farhadi.ir/projects/html5sortable/" +}