forked from marceljuenemann/angular-drag-and-drop-lists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-drag-and-drop-lists.min.js
1 lines (1 loc) · 3.82 KB
/
angular-drag-and-drop-lists.min.js
1
angular.module("dndLists",[]).directive("dndDraggable",["$parse","$timeout","dndDropEffectWorkaround","dndDragTypeWorkaround",function(e,t,n,r){return function(i,s,o){s.attr("draggable","true");if(o.dndDisableIf){i.$watch(o.dndDisableIf,function(e){s.attr("draggable",!e)})}s.on("dragstart",function(u){u=u.originalEvent||u;u.dataTransfer.setData("Text",angular.toJson(i.$eval(o.dndDraggable)));u.dataTransfer.effectAllowed=o.dndEffectAllowed||"move";s.addClass("dndDragging");t(function(){s.addClass("dndDraggingSource")},0);n.dropEffect="none";r.isDragging=true;r.dragType=o.dndType?i.$eval(o.dndType):undefined;e(o.dndDragstart)(i,{event:u});u.stopPropagation()});s.on("dragend",function(t){t=t.originalEvent||t;var u=n.dropEffect;i.$apply(function(){switch(u){case"move":e(o.dndMoved)(i,{event:t});break;case"copy":e(o.dndCopied)(i,{event:t});break}});s.removeClass("dndDragging");s.removeClass("dndDraggingSource");r.isDragging=false;t.stopPropagation()});s.on("click",function(t){t=t.originalEvent||t;i.$apply(function(){e(o.dndSelected)(i,{event:t})});t.stopPropagation()});s.on("selectstart",function(){if(this.dragDrop)this.dragDrop();return false})}}]).directive("dndList",["$parse","$timeout","dndDropEffectWorkaround","dndDragTypeWorkaround",function(e,t,n,r){return function(i,s,o){function h(e,t,n){var r=l?e.offsetX||e.layerX:e.offsetY||e.layerY;var i=l?t.offsetWidth:t.offsetHeight;var s=l?t.offsetLeft:t.offsetTop;s=n?s:0;return r<s+i/2}function p(){return Array.prototype.indexOf.call(f.children,a)}function d(e){if(!r.isDragging&&!c)return false;if(!g(e.dataTransfer.types))return false;if(o.dndAllowedTypes&&r.isDragging){var t=i.$eval(o.dndAllowedTypes);if(angular.isArray(t)&&t.indexOf(r.dragType)===-1){return false}}if(o.dndDisableIf&&i.$eval(o.dndDisableIf))return false;return true}function v(){u.remove();s.removeClass("dndDragover");return true}function m(t,n,s){return e(t)(i,{event:n,index:p(),item:s||undefined,external:!r.isDragging,type:r.isDragging?r.dragType:undefined})}function g(e){if(!e)return true;for(var t=0;t<e.length;t++){if(e[t]==="Text"||e[t]==="text/plain")return true}return false}var u=angular.element("<li class='dndPlaceholder'></li>");var a=u[0];var f=s[0];var l=o.dndHorizontalList&&i.$eval(o.dndHorizontalList);var c=o.dndExternalSources&&i.$eval(o.dndExternalSources);s.on("dragover",function(e){e=e.originalEvent||e;if(!d(e))return true;if(a.parentNode!=f){s.append(u)}if(e.target!==f){var t=e.target;while(t.parentNode!==f&&t.parentNode){t=t.parentNode}if(t.parentNode===f&&t!==a){if(h(e,t)){f.insertBefore(a,t)}else{f.insertBefore(a,t.nextSibling)}}}else{if(h(e,a,true)){while(a.previousElementSibling&&(h(e,a.previousElementSibling,true)||a.previousElementSibling.offsetHeight===0)){f.insertBefore(a,a.previousElementSibling)}}else{while(a.nextElementSibling&&!h(e,a.nextElementSibling,true)){f.insertBefore(a,a.nextElementSibling.nextElementSibling)}}}if(o.dndDragover&&!m(o.dndDragover,e)){return v()}s.addClass("dndDragover");e.preventDefault();e.stopPropagation();return false});s.on("drop",function(e){e=e.originalEvent||e;if(!d(e))return true;e.preventDefault();var t=e.dataTransfer.getData("Text")||e.dataTransfer.getData("text/plain");var r;try{r=JSON.parse(t)}catch(s){return v()}if(o.dndDrop){r=m(o.dndDrop,e,r);if(!r){return v()}}var u=i.$eval(o.dndList);i.$apply(function(){u.splice(p(),0,r)});if(e.dataTransfer.dropEffect==="none"){if(e.dataTransfer.effectAllowed==="copy"||e.dataTransfer.effectAllowed==="move"){n.dropEffect=e.dataTransfer.effectAllowed}else{n.dropEffect=e.ctrlKey?"copy":"move"}}else{n.dropEffect=e.dataTransfer.dropEffect}v();e.stopPropagation();return false});s.on("dragleave",function(e){e=e.originalEvent||e;s.removeClass("dndDragover");t(function(){if(!s.hasClass("dndDragover")){u.remove()}},100)})}}]).factory("dndDragTypeWorkaround",function(){return{}}).factory("dndDropEffectWorkaround",function(){return{}})