forked from Barrior/DDSort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddsort.min.js
5 lines (5 loc) · 3.22 KB
/
ddsort.min.js
1
2
3
4
5
/**
* DDSort: drag and drop sorting.
* Documentation: https://github.com/Barrior/DDSort
*/
+function(e){var b={down:e.noop,move:e.noop,up:e.noop,target:"li",delay:100,cloneStyle:{"background-color":"#eee"},floatStyle:{"position":"fixed","box-shadow":"10px 10px 20px 0 #eee","webkitTransform":"rotate(4deg)","mozTransform":"rotate(4deg)","msTransform":"rotate(4deg)","transform":"rotate(4deg)"}};e.fn.DDSort=function(h){var j=e(document);var i=e.extend(true,{},b,h);return this.each(function(){var m=e(this);var k="height";var l="width";if(m.css("box-sizing")=="border-box"){k="outerHeight";l="outerWidth"}m.on("mousedown.DDSort touchstart.DDSort",i.target,function(D){var z=new Date().getTime();if(D.type=="mousedown"&&D.which!=1){return}var s=D.target.tagName.toLowerCase();if(s=="input"||s=="textarea"||s=="select"||s=="a"||e(D.target).prop("contenteditable")=="true"){return}var A=this;var v=e(A);var u=v.offset();var p=D.pageX;var o=D.pageY;var F=D.originalEvent.targetTouches;if(D.type=="touchstart"&&F){p=F[0].pageX;o=F[0].pageY}var E=v.clone().css(i.cloneStyle).css("height",v[k]()).empty();var x=1;var y=v.outerHeight(),B=v.outerWidth(),C=m.outerHeight(),n=m.outerWidth();var t=y,w=y,q=B,r=B,G=y*3;i.down.call(A);j.on("mousemove.DDSort touchmove.DDSort",function(O){var N=O.pageX;var M=O.pageY;var Q=O.originalEvent.targetTouches;if(O.type=="touchmove"&&Q){N=Q[0].pageX;M=Q[0].pageY}if(new Date().getTime()-z<i.delay){return}if(x){v.before(E).css("width",v[l]()).css(i.floatStyle).appendTo(v.parent());x=0}var X=p-N;var W=o-M;var J=u.left-X;var S=u.top-W;v.offset({left:J,top:S});var H=a(E),T=g(E,v),L=d(E),R=f(E,v);if(L&&L.length&&S<L.offset().top+L.outerHeight(true)/2){L.before(E)}else{if(R&&R.length&&S+y>R.offset().top+R.outerHeight(true)/2){R.after(E)}else{if(H&&H.length&&J<H.offset().left+H.outerWidth(true)/2){H.before(E)}else{if(T&&T.length&&J+B>T.offset().left+T.outerWidth(true)/2){T.after(E)}}}}var V=m.scrollTop();var U=m.offset().top;if(S<U){w=y;t=++t>G?G:t;var P=V-t;m.scrollTop(P)}else{if(S+y-U>C){t=y;w=++w>G?G:w;var P=V+w;m.scrollTop(P)}}var K=m.scrollLeft();var I=m.offset().left;if(J<m.offset().left){r=B;q=++q>G?G:q;var P=K-q;m.scrollLeft(P)}else{if(J+B-I>n){q=B;r=++r>G?G:r;var P=K+r;m.scrollLeft(P)}}i.move.call(A,J,S)}).on("mouseup.DDSort touchend.DDSort",function(){j.off("mousemove.DDSort mouseup.DDSort touchmove.DDSort touchend.DDSort");if(!x){E.before(v.removeAttr("style")).remove();i.up.call(A)}});return false})})};var c=5;var a=function(j){var i=j.prev();if(i.length&&j.offset().top==i.offset().top){var h=Math.abs(j.offset().left-(i.offset().left+i.outerWidth(true)));if(h<=c){return i}}return undefined};var d=function(j,i){if(!i){i=j.prev()}if(!i.length){return undefined}if(j.offset().left==i.offset().left){var h=Math.abs(j.offset().top-(i.offset().top+i.outerHeight(true)));if(h<=c){return i}}return d(j,i.prev())};var g=function(k,j){var i=k.next().not(j);if(i.length&&k.offset().top==i.offset().top){var h=Math.abs(k.offset().left-(i.offset().left-k.outerWidth(true)));if(h<=c){return i}}return undefined};var f=function(k,j,i){if(!i){i=k.next().not(j)}if(!i.length){return undefined}if(k.offset().left==i.offset().left){var h=Math.abs(k.offset().top-(i.offset().top-k.outerHeight(true)));if(h<=c){return i}}return f(k,j,i.next().not(j))}}(jQuery);