diff --git a/Sortable.js b/Sortable.js index 792450a..69f9ae2 100644 --- a/Sortable.js +++ b/Sortable.js @@ -74,6 +74,7 @@ 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); @@ -142,11 +143,10 @@ tapEvt = evt; target.draggable = true; - // Disable "draggable" - _find(target, 'a', _disableDraggable); - _find(target, 'img', _disableDraggable); - + Array.prototype.forEach.call(options.ignore.split(','), function (criteria) { + _find(target, criteria.trim(), _disableDraggable); + }); if( touch ){ // Touch device support @@ -410,7 +410,6 @@ } }; - function _bind(ctx, fn){ var args = slice.call(arguments, 2); return fn.bind ? fn.bind.apply(fn, [ctx].concat(args)) : function (){ diff --git a/Sortable.min.js b/Sortable.min.js index 721a4d5..2a8e2ca 100644 --- a/Sortable.min.js +++ b/Sortable.min.js @@ -1,2 +1 @@ -/*! Sortable 0.1.9 - MIT | git://github.com/rubaxa/Sortable.git */ -!function(a){"use strict";"function"==typeof define&&define.amd?define(a):"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=a():window.Sortable=a()}(function(){"use strict";function a(a,c){this.el=a,this.options=c=c||{},c.group=c.group||Math.random(),c.handle=c.handle||null,c.draggable=c.draggable||a.children[0]&&a.children[0].nodeName||(/[uo]l/i.test(a.nodeName)?"li":"*"),c.ghostClass=c.ghostClass||"sortable-ghost",c.onAdd=b(this,c.onAdd||D),c.onUpdate=b(this,c.onUpdate||D),c.onRemove=b(this,c.onRemove||D),a[w]=c.group;for(var d in this)"_"===d.charAt(0)&&(this[d]=b(this,this[d]));e(a,"add",c.onAdd),e(a,"update",c.onUpdate),e(a,"remove",c.onRemove),e(a,"mousedown",this._onTapStart),e(a,"touchstart",this._onTapStart),A&&e(a,"selectstart",this._onTapStart),e(a,"dragover",this._onDragOver),e(a,"dragenter",this._onDragOver),F.push(this._onDragOver)}function b(a,b){var c=E.call(arguments,2);return b.bind?b.bind.apply(b,[a].concat(c)):function(){return b.apply(a,c.concat(E.call(arguments)))}}function c(a,b,c){if("*"===b)return a;if(a){c=c||y,b=b.split(".");var d=b.shift().toUpperCase(),e=new RegExp("\\s("+b.join("|")+")\\s","g");do if(!(""!==d&&a.nodeName!=d||b.length&&((" "+a.className+" ").match(e)||[]).length!=b.length))return a;while(a!==c&&(a=a.parentNode))}return null}function d(a){a.dataTransfer.dropEffect="move",a.preventDefault()}function e(a,b,c){a.addEventListener(b,c,!1)}function f(a,b,c){a.removeEventListener(b,c,!1)}function g(a,b,c){if(a)if(a.classList)a.classList[c?"add":"remove"](b);else{var d=(" "+a.className+" ").replace(/\s+/g," ").replace(" "+b+" ","");a.className=d+(c?" "+b:"")}}function h(a,b,c){if(a&&a.style){if(void 0===c)return y.defaultView&&y.defaultView.getComputedStyle?c=y.defaultView.getComputedStyle(a,""):a.currentStyle&&(c=a.currentStyle),void 0===b?c:c[b];a.style[b]=c+("string"==typeof c?"":"px")}}function i(a,b,c){if(a){var d=a.getElementsByTagName(b),e=0,f=d.length;if(c)for(;f>e;e++)c(d[e],e);return d}return[]}function j(a){return a.draggable=!1}function k(){B=!1}function l(a,b){var c=a.lastElementChild.getBoundingClientRect();return b.clientY-(c.top+c.height)>5}var m,n,o,p,q,r,s,t,u,v,w="Sortable"+(new Date).getTime(),x=window,y=x.document,z=x.parseInt,A=!!y.createElement("div").dragDrop,B=!1,C=function(a,b){var c=y.createEvent("Event");return c.initEvent(a,!0,!0),c.item=b,c},D=function(){},E=[].slice,F=[];return a.prototype={constructor:a,_applyEffects:function(){g(m,this.options.ghostClass,!0)},_onTapStart:function(a){var b=a.touches&&a.touches[0],f=(b||a).target,g=this.options,h=this.el;if(g.handle&&(f=c(f,g.handle,h)),f=c(f,g.draggable,h),f&&"selectstart"==a.type&&"A"!=f.tagName&&"IMG"!=f.tagName&&f.dragDrop(),f&&!m&&f.parentNode===h){u=a,f.draggable=!0,i(f,"a",j),i(f,"img",j),b&&(u={target:f,clientX:b.clientX,clientY:b.clientY},this._onDragStart(u,!0),a.preventDefault()),e(this.el,"dragstart",this._onDragStart),e(this.el,"dragend",this._onDrop),e(y,"dragover",d);try{y.selection?y.selection.empty():window.getSelection().removeAllRanges()}catch(k){}}},_emulateDragOver:function(){if(v){h(n,"display","none");var a=y.elementFromPoint(v.clientX,v.clientY),b=a,c=this.options.group,d=F.length;if(b)do{if(b[w]===c){for(;d--;)F[d]({clientX:v.clientX,clientY:v.clientY,target:a,rootEl:b});break}a=b}while(b=b.parentNode);h(n,"display","")}},_onTouchMove:function(a){if(u){var b=a.touches[0],c=b.clientX-u.clientX,d=b.clientY-u.clientY;v=b,h(n,"webkitTransform","translate3d("+c+"px,"+d+"px,0)")}},_onDragStart:function(a,b){var c=a.target,d=a.dataTransfer;if(o=this.el,m=c,p=c.nextSibling,t=this.options.group,b){var f,g=c.getBoundingClientRect(),i=h(c);n=c.cloneNode(!0),h(n,"top",g.top-z(i.marginTop,10)),h(n,"left",g.left-z(i.marginLeft,10)),h(n,"width",g.width),h(n,"height",g.height),h(n,"opacity","0.8"),h(n,"position","fixed"),h(n,"zIndex","100000"),o.appendChild(n),f=n.getBoundingClientRect(),h(n,"width",2*g.width-f.width),h(n,"height",2*g.height-f.height),e(y,"touchmove",this._onTouchMove),e(y,"touchend",this._onDrop),this._loopId=setInterval(this._emulateDragOver,150)}else d.effectAllowed="move",d.setData("Text",c.textContent),e(y,"drop",this._onDrop);setTimeout(this._applyEffects)},_onDragOver:function(a){if(!B&&t===this.options.group&&(void 0===a.rootEl||a.rootEl===this.el)){var b=this.el,d=c(a.target,this.options.draggable,b);if(0===b.children.length||b.children[0]===n||b===a.target&&l(b,a))b.appendChild(m);else if(d&&d!==m&&void 0!==d.parentNode[w]){q!==d&&(q=d,r=h(d),s=d.getBoundingClientRect());var e,f=s,g=f.right-f.left,i=f.bottom-f.top,j=/left|right|inline/.test(r.cssFloat+r.display),o=(j?(a.clientX-f.left)/g:(a.clientY-f.top)/i)>.5,p=d.offsetWidth>m.offsetWidth,u=d.offsetHeight>m.offsetHeight,v=d.nextSibling;B=!0,setTimeout(k,30),e=j?d.previousElementSibling===m&&!p||o>.5&&p:d.nextElementSibling!==m&&!u||o>.5&&u,e&&!v?b.appendChild(m):d.parentNode.insertBefore(m,e?v:d)}}},_onDrop:function(a){clearInterval(this._loopId),f(y,"drop",this._onDrop),f(y,"dragover",d),f(this.el,"dragend",this._onDrop),f(this.el,"dragstart",this._onDragStart),f(this.el,"selectstart",this._onTapStart),f(y,"touchmove",this._onTouchMove),f(y,"touchend",this._onDrop),a&&(a.preventDefault(),a.stopPropagation(),n&&n.parentNode.removeChild(n),m&&(j(m),g(m,this.options.ghostClass,!1),o.contains(m)?m.nextSibling!==p&&m.dispatchEvent(C("update",m)):(o.dispatchEvent(C("remove",m)),m.dispatchEvent(C("add",m)))),o=m=n=p=u=v=q=r=t=null)},destroy:function(){var a=this.el,b=this.options;f(a,"add",b.onAdd),f(a,"update",b.onUpdate),f(a,"remove",b.onRemove),f(a,"mousedown",this._onTapStart),f(a,"touchstart",this._onTapStart),f(a,"selectstart",this._onTapStart),f(a,"dragover",this._onDragOver),f(a,"dragenter",this._onDragOver),Array.prototype.forEach.call(a.querySelectorAll("[draggable]"),function(a){a.removeAttribute("draggable")}),F.splice(F.indexOf(this._onDragOver),1),this._onDrop(),this.el=null}},a.utils={on:e,off:f,css:h,find:i,bind:b,closest:c,toggleClass:g},a.version="0.1.9",a}); \ No newline at end of file +(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._onTouchMove);_on(document,"touchend",this._onDrop);this._loopId=setInterval(this._emulateDragOver,150)}else{dataTransfer.effectAllowed="move";dataTransfer.setData("Text",target.textContent);_on(document,"drop",this._onDrop)}setTimeout(this._applyEffects)},_onDragOver:function(evt){if(!_silent&&activeGroup===this.options.group&&(evt.rootEl===void 0||evt.rootEl===this.el)){var el=this.el,target=_closest(evt.target,this.options.draggable,el);if(el.children.length===0||el.children[0]===ghostEl||el===evt.target&&_ghostInBottom(el,evt)){el.appendChild(dragEl)}else if(target&&target!==dragEl&&target.parentNode[expando]!==void 0){if(lastEl!==target){lastEl=target;lastCSS=_css(target);lastRect=target.getBoundingClientRect()}var rect=lastRect,width=rect.right-rect.left,height=rect.bottom-rect.top,floating=/left|right|inline/.test(lastCSS.cssFloat+lastCSS.display),skew=(floating?(evt.clientX-rect.left)/width:(evt.clientY-rect.top)/height)>.5,isWide=target.offsetWidth>dragEl.offsetWidth,isLong=target.offsetHeight>dragEl.offsetHeight,nextSibling=target.nextSibling,after;_silent=true;setTimeout(_unsilent,30);if(floating){after=target.previousElementSibling===dragEl&&!isWide||skew>.5&&isWide}else{after=target.nextElementSibling!==dragEl&&!isLong||skew>.5&&isLong}if(after&&!nextSibling){el.appendChild(dragEl)}else{target.parentNode.insertBefore(dragEl,after?nextSibling:target)}}}},_onDrop:function(evt){clearInterval(this._loopId);_off(document,"drop",this._onDrop);_off(document,"dragover",_globalDragOver);_off(this.el,"dragend",this._onDrop);_off(this.el,"dragstart",this._onDragStart);_off(this.el,"selectstart",this._onTapStart);_off(document,"touchmove",this._onTouchMove);_off(document,"touchend",this._onDrop);if(evt){evt.preventDefault();evt.stopPropagation();if(ghostEl){ghostEl.parentNode.removeChild(ghostEl)}if(dragEl){_disableDraggable(dragEl);_toggleClass(dragEl,this.options.ghostClass,false);if(!rootEl.contains(dragEl)){rootEl.dispatchEvent(_createEvent("remove",dragEl));dragEl.dispatchEvent(_createEvent("add",dragEl))}else if(dragEl.nextSibling!==nextEl){dragEl.dispatchEvent(_createEvent("update",dragEl))}}rootEl=dragEl=ghostEl=nextEl=tapEvt=touchEvt=lastEl=lastCSS=activeGroup=null}},destroy:function(){var el=this.el,options=this.options;_off(el,"add",options.onAdd);_off(el,"update",options.onUpdate);_off(el,"remove",options.onRemove);_off(el,"mousedown",this._onTapStart);_off(el,"touchstart",this._onTapStart);_off(el,"selectstart",this._onTapStart);_off(el,"dragover",this._onDragOver);_off(el,"dragenter",this._onDragOver);Array.prototype.forEach.call(el.querySelectorAll("[draggable]"),function(el){el.removeAttribute("draggable")});touchDragOverListeners.splice(touchDragOverListeners.indexOf(this._onDragOver),1);this._onDrop();this.el=null}};function _bind(ctx,fn){var args=slice.call(arguments,2);return fn.bind?fn.bind.apply(fn,[ctx].concat(args)):function(){return fn.apply(ctx,args.concat(slice.call(arguments)))}}function _closest(el,selector,ctx){if(selector==="*"){return el}else if(el){ctx=ctx||document;selector=selector.split(".");var tag=selector.shift().toUpperCase(),re=new RegExp("\\s("+selector.join("|")+")\\s","g");do{if((tag===""||el.nodeName==tag)&&(!selector.length||((" "+el.className+" ").match(re)||[]).length==selector.length)){return el}}while(el!==ctx&&(el=el.parentNode))}return null}function _globalDragOver(evt){evt.dataTransfer.dropEffect="move";evt.preventDefault()}function _on(el,event,fn){el.addEventListener(event,fn,false)}function _off(el,event,fn){el.removeEventListener(event,fn,false)}function _toggleClass(el,name,state){if(el){if(el.classList){el.classList[state?"add":"remove"](name)}else{var className=(" "+el.className+" ").replace(/\s+/g," ").replace(" "+name+" ","");el.className=className+(state?" "+name:"")}}}function _css(el,prop,val){if(el&&el.style){if(val===void 0){if(document.defaultView&&document.defaultView.getComputedStyle){val=document.defaultView.getComputedStyle(el,"")}else if(el.currentStyle){val=el.currentStyle}return prop===void 0?val:val[prop]}else{el.style[prop]=val+(typeof val==="string"?"":"px")}}}function _find(ctx,tagName,iterator){if(ctx){var list=ctx.getElementsByTagName(tagName),i=0,n=list.length;if(iterator){for(;i5}Sortable.utils={on:_on,off:_off,css:_css,find:_find,bind:_bind,closest:_closest,toggleClass:_toggleClass};Sortable.version="0.1.9";return Sortable}); \ No newline at end of file