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.77 KB
/
draganddrop.min.js
1
!function(a){"use strict";function e(){return"ondrag"in document.createElement("a")}function n(a){a.originalEvent&&(a.dataTransfer=a.originalEvent.dataTransfer),"undefined"!=typeof a.dataTransfer&&"none"===a.dataTransfer.dropEffect&&("copy"===a.dataTransfer.effectAllowed||"move"===a.dataTransfer.effectAllowed?a.dataTransfer.dropEffect=a.dataTransfer.effectAllowed:"copyMove"!==a.dataTransfer.effectAllowed&&"copymove"!==a.dataTransfer.effectAllowed||(a.dataTransfer.dropEffect=a.ctrlKey?"copy":"move"))}if(!e())return void a.module("ang-drag-drop",[]);var r=a.module("ang-drag-drop",[]);r.directive("uiDraggable",["$parse","$rootScope","$dragImage","$timeout",function(e,r,t,o){return function(d,f,i){function s(a){a.originalEvent&&(a.dataTransfer=a.originalEvent.dataTransfer),o(function(){f.unbind("$destroy",s)},0);var t=i.dragChannel||"defaultchannel";if(r.$broadcast("ANGULAR_DRAG_END",a,t),n(a),a.dataTransfer&&"none"!==a.dataTransfer.dropEffect){if(i.onDropSuccess){var l=e(i.onDropSuccess);d.$apply(function(){l(d,{$event:a})})}}else if(a.dataTransfer&&"none"===a.dataTransfer.dropEffect&&i.onDropFailure){var g=e(i.onDropFailure);d.$apply(function(){g(d,{$event:a})})}f.removeClass(p)}function l(n,r){var t;n.originalEvent&&(n.dataTransfer=n.originalEvent.dataTransfer),t=e(r),d.$apply(function(){var e,r=t(d,{$event:n});r&&a.isString(r)&&(e=document.getElementById(r),e&&n.dataTransfer.setDragImage(e,0,0))})}function g(n){n.originalEvent&&(n.dataTransfer=n.originalEvent.dataTransfer);var o=!c||v.classList.contains(u);if(o){var g=i.dragChannel||"defaultchannel",$="";i.drag&&($=d.$eval(i.drag));var m=i.dragImage||null;f.addClass(p),f.bind("$destroy",s);var T=!(document.uniqueID||window.opera);if(m&&T){var h=e(i.dragImage);d.$apply(function(){var e=h(d,{$event:n});if(e&&(a.isString(e)&&(e=t.generate(e)),e.image)){var r=e.xOffset||0,o=e.yOffset||0;n.dataTransfer.setDragImage(e.image,r,o)}})}else i.dragImageElementId&&l(n,i.dragImageElementId);var b={x:n.offsetX,y:n.offsetY},D={data:$,channel:g,offset:b},y=a.toJson(D);n.dataTransfer.setData("text",y),n.dataTransfer.effectAllowed="copyMove",r.$broadcast("ANGULAR_DRAG_START",n,g,D)}else n.preventDefault()}var u,v,c=!1,p=i.draggingClass||"on-dragging";f.attr("draggable",!1),d.$watch(i.uiDraggable,function(a){a?(f.attr("draggable",a),f.bind("dragend",s),f.bind("dragstart",g)):(f.removeAttr("draggable"),f.unbind("dragstart",g),o(function(){f.unbind("dragend",s)},100))}),a.isString(i.dragHandleClass)&&(c=!0,u=i.dragHandleClass.trim()||"drag-handle",f.bind("mousedown",function(a){v=a.target}))}}]),r.directive("uiOnDrop",["$parse","$rootScope",function(e,r){return function(t,o,d){function f(a){for(var e={x:a.offsetX,y:a.offsetY},n=a.target;n!==o[0];)if(e.x=e.x+n.offsetLeft,e.y=e.y+n.offsetTop,n=n.offsetParent,!n)return null;return e}function i(a){a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation();var n=e(d.uiOnDragOver);return t.$apply(function(){n(t,{$event:a,$channel:p})}),!1}function s(a){a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),c--,0===c&&(t.$apply(function(){b(t,{$event:a,$channel:p})}),o.addClass(m),o.removeClass(T));var n=e(d.uiOnDragLeave);t.$apply(function(){n(t,{$event:a,$channel:p})})}function l(a){a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),0===c&&(t.$apply(function(){h(t,{$event:a,$channel:p})}),o.removeClass(m),o.addClass(T)),c++;var n=e(d.uiOnDragEnter);t.$apply(function(){n(t,{$event:a,$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=a.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=e(d.uiOnDrop);t.$apply(function(){g(t,{$data:i.data,$event:r,$channel:i.channel,$position:l})}),o.removeClass(m),c=0}function u(a,e){if("*"===e)return!0;var n=new RegExp("(\\s|[,])+("+a+")(\\s|[,])+","i");return n.test(","+e+",")}function v(a){return a.originalEvent&&(a.dataTransfer=a.originalEvent.dataTransfer),a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.dataTransfer.dropEffect="none",!1}var c=0,p=d.dropChannel||"defaultchannel",$="",m=d.dragEnterClass||"on-drag-enter",T=d.dragHoverClass||"on-drag-hover",h=e(d.onDragEnter),b=e(d.onDragLeave),D=r.$on("ANGULAR_DRAG_START",function(a,n,r,f){$=r;var c=!0;if(u(r,p)||(c=!1),c&&d.dropValidate){var T=e(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(a){a&&(p=a)})}}]),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(e){function n(a,e,n){var t=a.measureText(e).width;if(t<n.width)return e;for(;t+n.padding>n.width;)e=e.substring(0,e.length-1),t=a.measureText(e+r).width;return e+r}var r="…";this.generate=function(r,t){var o=a.extend({},e,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);