forked from angular-dragdrop/angular-dragdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draganddrop.min.js
executable file
·1 lines (1 loc) · 5.78 KB
/
draganddrop.min.js
1
!function(e){"use strict";function a(){return"ondrag"in document.createElement("a")}function n(e){e.originalEvent&&(e.dataTransfer=e.originalEvent.dataTransfer),"undefined"!=typeof e.dataTransfer&&"none"===e.dataTransfer.dropEffect&&("copy"===e.dataTransfer.effectAllowed||"move"===e.dataTransfer.effectAllowed?e.dataTransfer.dropEffect=e.dataTransfer.effectAllowed:"copyMove"!==e.dataTransfer.effectAllowed&&"copymove"!==e.dataTransfer.effectAllowed||(e.dataTransfer.dropEffect=e.ctrlKey?"copy":"move"))}if(!a())return void e.module("ang-drag-drop",[]);var r=e.module("ang-drag-drop",[]);r.directive("uiDraggable",["$parse","$rootScope","$dragImage",function(a,r,t){return function(o,d,f){function i(e){e.originalEvent&&(e.dataTransfer=e.originalEvent.dataTransfer),setTimeout(function(){d.unbind("$destroy",i)},0);var t=f.dragChannel||"defaultchannel";if(r.$broadcast("ANGULAR_DRAG_END",e,t),n(e),e.dataTransfer&&"none"!==e.dataTransfer.dropEffect){if(f.onDropSuccess){var s=a(f.onDropSuccess);o.$evalAsync(function(){s(o,{$event:e})})}}else if(e.dataTransfer&&"none"===e.dataTransfer.dropEffect&&f.onDropFailure){var l=a(f.onDropFailure);o.$evalAsync(function(){l(o,{$event:e})})}d.removeClass(c)}function s(n,r){var t;n.originalEvent&&(n.dataTransfer=n.originalEvent.dataTransfer),t=a(r),o.$apply(function(){var a,r=t(o,{$event:n});r&&e.isString(r)&&(a=document.getElementById(r),a&&n.dataTransfer.setDragImage(a,0,0))})}function l(n){n.originalEvent&&(n.dataTransfer=n.originalEvent.dataTransfer);var l=!v||u.classList.contains(g);if(l){var p=f.dragChannel||"defaultchannel",$="";f.drag&&($=o.$eval(f.drag));var m=f.dragImage||null;d.addClass(c),d.bind("$destroy",i);var T=!(document.uniqueID||window.opera);if(m&&T){var h=a(f.dragImage);o.$apply(function(){var a=h(o,{$event:n});if(a&&(e.isString(a)&&(a=t.generate(a)),a.image)){var r=a.xOffset||0,d=a.yOffset||0;n.dataTransfer.setDragImage(a.image,r,d)}})}else f.dragImageElementId&&s(n,f.dragImageElementId);var b={x:n.offsetX,y:n.offsetY},D={data:$,channel:p,offset:b},y=e.toJson(D);n.dataTransfer.setData("text",y),n.dataTransfer.effectAllowed="copyMove",r.$broadcast("ANGULAR_DRAG_START",n,p,D)}else n.preventDefault()}var g,u,v=!1,c=f.draggingClass||"on-dragging";d.attr("draggable",!1),o.$watch(f.uiDraggable,function(e){e?(d.attr("draggable",e),d.bind("dragend",i),d.bind("dragstart",l)):(d.removeAttr("draggable"),d.unbind("dragend",i),d.unbind("dragstart",l))}),e.isString(f.dragHandleClass)&&(v=!0,g=f.dragHandleClass.trim()||"drag-handle",d.bind("mousedown",function(e){u=e.target}))}}]),r.directive("uiOnDrop",["$parse","$rootScope",function(a,r){return function(t,o,d){function f(e){for(var a={x:e.offsetX,y:e.offsetY},n=e.target;n!==o[0];)if(a.x=a.x+n.offsetLeft,a.y=a.y+n.offsetTop,n=n.offsetParent,!n)return null;return a}function i(e){e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation();var n=a(d.uiOnDragOver);return t.$evalAsync(function(){n(t,{$event:e,$channel:p})}),!1}function s(e){e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),c--,0===c&&(t.$evalAsync(function(){b(t,{$event:e,$channel:p})}),o.addClass(m),o.removeClass(T));var n=a(d.uiOnDragLeave);t.$evalAsync(function(){n(t,{$event:e,$channel:p})})}function l(e){e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),0===c&&(t.$evalAsync(function(){h(t,{$event:e,$channel:p})}),o.removeClass(m),o.addClass(T)),c++;var n=a(d.uiOnDragEnter);t.$evalAsync(function(){n(t,{$event:e,$channel:p})}),r.$broadcast("ANGULAR_HOVER",$)}function g(r){r.originalEvent&&(r.dataTransfer=r.originalEvent.dataTransfer),r.preventDefault&&r.preventDefault(),r.stopPropagation&&r.stopPropagation();var i=r.dataTransfer.getData("text");i=e.fromJson(i);var s=f(r),l=s?{x:s.x-i.offset.x,y:s.y-i.offset.y}:null;n(r);var g=a(d.uiOnDrop);t.$evalAsync(function(){g(t,{$data:i.data,$event:r,$channel:i.channel,$position:l})}),o.removeClass(m),c=0}function u(e,a){if("*"===a)return!0;var n=new RegExp("(\\s|[,])+("+e+")(\\s|[,])+","i");return n.test(","+a+",")}function v(e){return e.originalEvent&&(e.dataTransfer=e.originalEvent.dataTransfer),e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),e.dataTransfer.dropEffect="none",!1}var c=0,p=d.dropChannel||"defaultchannel",$="",m=d.dragEnterClass||"on-drag-enter",T=d.dragHoverClass||"on-drag-hover",h=a(d.onDragEnter),b=a(d.onDragLeave),D=r.$on("ANGULAR_DRAG_START",function(e,n,r,f){$=r;var c=!0;if(u(r,p)||(c=!1),c&&d.dropValidate){var T=a(d.dropValidate);c=T(t,{$drop:{scope:t,element:o},$event:n,$data:f.data,$channel:f.channel})}c?(o.bind("dragover",i),o.bind("dragenter",l),o.bind("dragleave",s),o.bind("drop",g),o.addClass(m)):(o.bind("dragover",v),o.bind("dragenter",v),o.bind("dragleave",v),o.bind("drop",v),o.removeClass(m))}),y=r.$on("ANGULAR_DRAG_END",function(){o.unbind("dragover",i),o.unbind("dragenter",l),o.unbind("dragleave",s),o.unbind("drop",g),o.removeClass(T),o.removeClass(m),o.unbind("dragover",v),o.unbind("dragenter",v),o.unbind("dragleave",v),o.unbind("drop",v)});t.$on("$destroy",function(){D(),y()}),d.$observe("dropChannel",function(e){e&&(p=e)})}}]),r.constant("$dragImageConfig",{height:20,width:200,padding:10,font:"bold 11px Arial",fontColor:"#eee8d5",backgroundColor:"#93a1a1",xOffset:0,yOffset:0}),r.service("$dragImage",["$dragImageConfig",function(a){function n(e,a,n){var t=e.measureText(a).width;if(t<n.width)return a;for(;t+n.padding>n.width;)a=a.substring(0,a.length-1),t=e.measureText(a+r).width;return a+r}var r="…";this.generate=function(r,t){var o=e.extend({},a,t||{}),d=document.createElement("canvas");d.height=o.height,d.width=o.width;var f=d.getContext("2d");f.fillStyle=o.backgroundColor,f.fillRect(0,0,o.width,o.height),f.font=o.font,f.fillStyle=o.fontColor;var i=n(f,r,o);f.fillText(i,4,o.padding+4);var s=new Image;return s.src=d.toDataURL(),{image:s,xOffset:o.xOffset,yOffset:o.yOffset}}}])}(angular);