|
|
|
(function(factory){"use strict";if(typeof define==="function"&&define.amd){define(factory)}else if(typeof module!="undefined"&&typeof module.exports!="undefined"){module.exports=factory()}else{window["Sortable"]=factory()}})(function(){"use strict";var dragEl,ghostEl,rootEl,nextEl,lastEl,lastCSS,lastRect,activeGroup,tapEvt,touchEvt,expando="Sortable"+(new Date).getTime(),win=window,document=win.document,parseInt=win.parseInt,supportIEdnd=!!document.createElement("div").dragDrop,_silent=false,_createEvent=function(event,item){var evt=document.createEvent("Event");evt.initEvent(event,true,true);evt.item=item;return evt},noop=function(){},slice=[].slice,touchDragOverListeners=[];function Sortable(el,options){this.el=el;this.options=options=options||{};options.group=options.group||Math.random();options.handle=options.handle||null;options.draggable=options.draggable||el.children[0]&&el.children[0].nodeName||(/[uo]l/i.test(el.nodeName)?"li":"*");options.ghostClass=options.ghostClass||"sortable-ghost";options.ignore=options.ignore||"a, img";options.onAdd=_bind(this,options.onAdd||noop);options.onUpdate=_bind(this,options.onUpdate||noop);options.onRemove=_bind(this,options.onRemove||noop);el[expando]=options.group;for(var fn in this){if(fn.charAt(0)==="_"){this[fn]=_bind(this,this[fn])}}_on(el,"add",options.onAdd);_on(el,"update",options.onUpdate);_on(el,"remove",options.onRemove);_on(el,"mousedown",this._onTapStart);_on(el,"touchstart",this._onTapStart);supportIEdnd&&_on(el,"selectstart",this._onTapStart);_on(el,"dragover",this._onDragOver);_on(el,"dragenter",this._onDragOver);touchDragOverListeners.push(this._onDragOver)}Sortable.prototype={constructor:Sortable,_applyEffects:function(){_toggleClass(dragEl,this.options.ghostClass,true)},_onTapStart:function(evt){var touch=evt.touches&&evt.touches[0],target=(touch||evt).target,options=this.options,el=this.el;if(options.handle){target=_closest(target,options.handle,el)}target=_closest(target,options.draggable,el);if(target&&evt.type=="selectstart"){if(target.tagName!="A"&&target.tagName!="IMG"){target.dragDrop()}}if(target&&!dragEl&&target.parentNode===el){tapEvt=evt;target.draggable=true;Array.prototype.forEach.call(options.ignore.split(","),function(criteria){_find(target,criteria.trim(),_disableDraggable)});if(touch){tapEvt={target:target,clientX:touch.clientX,clientY:touch.clientY};this._onDragStart(tapEvt,true);evt.preventDefault()}_on(this.el,"dragstart",this._onDragStart);_on(this.el,"dragend",this._onDrop);_on(document,"dragover",_globalDragOver);try{if(document.selection){document.selection.empty()}else{window.getSelection().removeAllRanges()}}catch(err){}}},_emulateDragOver:function(){if(touchEvt){_css(ghostEl,"display","none");var target=document.elementFromPoint(touchEvt.clientX,touchEvt.clientY),parent=target,group=this.options.group,i=touchDragOverListeners.length;if(parent){do{if(parent[expando]===group){while(i--){touchDragOverListeners[i]({clientX:touchEvt.clientX,clientY:touchEvt.clientY,target:target,rootEl:parent})}break}target=parent}while(parent=parent.parentNode)}_css(ghostEl,"display","")}},_onTouchMove:function(evt){if(tapEvt){var touch=evt.touches[0],dx=touch.clientX-tapEvt.clientX,dy=touch.clientY-tapEvt.clientY;touchEvt=touch;_css(ghostEl,"webkitTransform","translate3d("+dx+"px,"+dy+"px,0)")}},_onDragStart:function(evt,isTouch){var target=evt.target,dataTransfer=evt.dataTransfer;rootEl=this.el;dragEl=target;nextEl=target.nextSibling;activeGroup=this.options.group;if(isTouch){var rect=target.getBoundingClientRect(),css=_css(target),ghostRect;ghostEl=target.cloneNode(true);_css(ghostEl,"top",rect.top-parseInt(css.marginTop,10));_css(ghostEl,"left",rect.left-parseInt(css.marginLeft,10));_css(ghostEl,"width",rect.width);_css(ghostEl,"height",rect.height);_css(ghostEl,"opacity","0.8");_css(ghostEl,"position","fixed");_css(ghostEl,"zIndex","100000");rootEl.appendChild(ghostEl);ghostRect=ghostEl.getBoundingClientRect();_css(ghostEl,"width",rect.width*2-ghostRect.width);_css(ghostEl,"height",rect.height*2-ghostRect.height);_on(document,"touchmove",this._onTou
|