/*
	Copyright (c) 2004-2016, The JS Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/

//>>built
define("dojo/dnd/Manager","../_base/array ../_base/declare ../_base/lang ../_base/window ../dom-class ../Evented ../has ../keys ../on ../topic ../touch ./common ./autoscroll ./Avatar".split(" "),function(u,v,g,c,h,w,k,l,f,d,n,e,p,x){var m=v("dojo.dnd.Manager",[w],{constructor:function(){this.source=this.avatar=null;this.nodes=[];this.copy=!0;this.target=null;this.canDropFlag=!1;this.events=[]},OFFSET_X:k("touch")?4:16,OFFSET_Y:k("touch")?4:16,overSource:function(a){this.avatar&&(this.target=a&&"Disabled"!=
a.targetState?a:null,this.canDropFlag=!!this.target,this.avatar.update());d.publish("/dnd/source/over",a)},outSource:function(a){this.avatar?this.target==a&&(this.target=null,this.canDropFlag=!1,this.avatar.update(),d.publish("/dnd/source/over",null)):d.publish("/dnd/source/over",null)},startDrag:function(a,b,q){function r(t){t.preventDefault();t.stopPropagation()}p.autoScrollStart(c.doc);this.source=a;this.nodes=b;this.copy=!!q;this.avatar=this.makeAvatar();c.body().appendChild(this.avatar.node);
d.publish("/dnd/start",a,b,this.copy);this.events=[f(c.doc,n.move,g.hitch(this,"onMouseMove")),f(c.doc,n.release,g.hitch(this,"onMouseUp")),f(c.doc,"keydown",g.hitch(this,"onKeyDown")),f(c.doc,"keyup",g.hitch(this,"onKeyUp")),f(c.doc,"dragstart",r),f(c.body(),"selectstart",r)];a="dojoDnd"+(q?"Copy":"Move");h.add(c.body(),a)},canDrop:function(a){a=!(!this.target||!a);this.canDropFlag!=a&&(this.canDropFlag=a,this.avatar.update())},stopDrag:function(){h.remove(c.body(),["dojoDndCopy","dojoDndMove"]);
u.forEach(this.events,function(a){a.remove()});this.events=[];this.avatar.destroy();this.source=this.target=this.avatar=null;this.nodes=[]},makeAvatar:function(){return new x(this)},updateAvatar:function(){this.avatar.update()},onMouseMove:function(a){var b=this.avatar;b&&(p.autoScrollNodes(a),b=b.node.style,b.left=a.pageX+this.OFFSET_X+"px",b.top=a.pageY+this.OFFSET_Y+"px",b=!!this.source.copyState(e.getCopyKeyState(a)),this.copy!=b&&this._setCopyStatus(b));k("touch")&&a.preventDefault()},onMouseUp:function(a){if(this.avatar){if(this.target&&
this.canDropFlag){var b=!!this.source.copyState(e.getCopyKeyState(a));d.publish("/dnd/drop/before",this.source,this.nodes,b,this.target,a);d.publish("/dnd/drop",this.source,this.nodes,b,this.target,a)}else d.publish("/dnd/cancel");this.stopDrag()}},onKeyDown:function(a){if(this.avatar)switch(a.keyCode){case l.CTRL:a=!!this.source.copyState(!0);this.copy!=a&&this._setCopyStatus(a);break;case l.ESCAPE:d.publish("/dnd/cancel"),this.stopDrag()}},onKeyUp:function(a){this.avatar&&a.keyCode==l.CTRL&&(a=
!!this.source.copyState(!1),this.copy!=a&&this._setCopyStatus(a))},_setCopyStatus:function(a){this.copy=a;this.source._markDndStatus(this.copy);this.updateAvatar();h.replace(c.body(),"dojoDnd"+(this.copy?"Copy":"Move"),"dojoDnd"+(this.copy?"Move":"Copy"))}});e._manager=null;m.manager=e.manager=function(){e._manager||(e._manager=new m);return e._manager};return m});