//>>built
define("dojox/drawing/manager/Anchors",["dojo","../util/oo","../defaults"],function(e,k,l){var m=k.declare(function(a){this.defaults=l.copy();this.mouse=a.mouse;this.point=a.point;this.pointIdx=a.pointIdx;this.util=a.util;this.id=a.id||this.util.uid("anchor");this.org=e.mixin({},this.point);this.stencil=a.stencil;this.stencil.anchorPositionCheck&&(this.anchorPositionCheck=e.hitch(this.stencil,this.stencil.anchorPositionCheck));this.stencil.anchorConstrain&&(this.anchorConstrain=e.hitch(this.stencil,
this.stencil.anchorConstrain));this._zCon=e.connect(this.mouse,"setZoom",this,"render");this.render();this.connectMouse()},{y_anchor:null,x_anchor:null,render:function(){this.shape&&this.shape.removeShape();var a=this.defaults.anchors,b=this.mouse.zoom,c=a.size*b,d=c/2;b={width:a.width*b,style:a.style,color:a.color,cap:a.cap};this.shape=this.stencil.container.createRect({x:this.point.x-d,y:this.point.y-d,width:c,height:c}).setStroke(b).setFill(a.fill);this.shape.setTransform({dx:0,dy:0});this.util.attr(this,
"drawingType","anchor");this.util.attr(this,"id",this.id)},onRenderStencil:function(a){},onTransformPoint:function(a){},onAnchorDown:function(a){this.selected=a.id==this.id},onAnchorUp:function(a){this.selected=!1;this.stencil.onTransformEnd(this)},onAnchorDrag:function(a){if(this.selected){this.shape.getTransform();var b=this.shape.getParent().getParent().getTransform(),c=this.defaults.anchors.marginZero,d=b.dx+this.org.x,f=b.dy+this.org.y;b=a.x-d;a=a.y-f;var g=this.defaults.anchors.minSize;var h=
this.anchorPositionCheck(b,a,this);if(0>h.x)for(;0>this.anchorPositionCheck(b,a,this).x;)this.shape.getParent().getParent().applyTransform({dx:2,dy:0});if(0>h.y)for(;0>this.anchorPositionCheck(b,a,this).y;)this.shape.getParent().getParent().applyTransform({dx:0,dy:2});this.y_anchor?this.org.y>this.y_anchor.org.y?(f=this.y_anchor.point.y+g-this.org.y,h=Infinity,a<f&&(a=f)):(f=-f+c,h=this.y_anchor.point.y-g-this.org.y,a<f?a=f:a>h&&(a=h)):(f=-f+c,a<f&&(a=f));this.x_anchor?this.org.x>this.x_anchor.org.x?
(c=this.x_anchor.point.x+g-this.org.x,d=Infinity,b<c&&(b=c)):(c=-d+c,d=this.x_anchor.point.x-g-this.org.x,b<c?b=c:b>d&&(b=d)):(c=-d+c,b<c&&(b=c));c=this.anchorConstrain(b,a);null!=c&&(b=c.x,a=c.y);this.shape.setTransform({dx:b,dy:a});this.linkedAnchor&&this.linkedAnchor.shape.setTransform({dx:b,dy:a});this.onTransformPoint(this)}},anchorConstrain:function(a,b){return null},anchorPositionCheck:function(a,b,c){return{x:1,y:1}},setPoint:function(a){this.shape.applyTransform(a)},connectMouse:function(){this._mouseHandle=
this.mouse.register(this)},disconnectMouse:function(){this.mouse.unregister(this._mouseHandle)},reset:function(a){},destroy:function(){e.disconnect(this._zCon);this.disconnectMouse();this.shape.removeShape()}});return k.declare(function(a){this.mouse=a.mouse;this.undo=a.undo;this.util=a.util;this.drawing=a.drawing;this.items={}},{onAddAnchor:function(a){},onReset:function(a){var b=this.util.byId("drawing").stencils;b.onDeselect(a);b.onSelect(a)},onRenderStencil:function(){for(var a in this.items)e.forEach(this.items[a].anchors,
function(b){b.shape.moveToFront()})},onTransformPoint:function(a){var b=this.items[a.stencil.id].item,c=[];e.forEach(this.items[a.stencil.id].anchors,function(d,f){a.id!=d.id&&"group"==a.stencil.anchorType&&(a.org.y==d.org.y?d.setPoint({dx:0,dy:a.shape.getTransform().dy-d.shape.getTransform().dy}):a.org.x==d.org.x&&d.setPoint({dx:a.shape.getTransform().dx-d.shape.getTransform().dx,dy:0}),d.shape.moveToFront());var g=d.shape.getTransform();c.push({x:g.dx+d.org.x,y:g.dy+d.org.y});d.point.t&&(c[c.length-
1].t=d.point.t)},this);b.setPoints(c);b.onTransform(a);this.onRenderStencil()},onAnchorUp:function(a){},onAnchorDown:function(a){},onAnchorDrag:function(a){},onChangeStyle:function(a){for(var b in this.items)e.forEach(this.items[b].anchors,function(c){c.shape.moveToFront()})},add:function(a){this.items[a.id]={item:a,anchors:[]};if("none"!=a.anchorType){var b=a.points;e.forEach(b,function(f,g){if(!f.noAnchor){var h=new m({stencil:a,point:f,pointIdx:g,mouse:this.mouse,util:this.util});this.items[a.id]._cons=
[e.connect(h,"onRenderStencil",this,"onRenderStencil"),e.connect(h,"reset",this,"onReset"),e.connect(h,"onAnchorUp",this,"onAnchorUp"),e.connect(h,"onAnchorDown",this,"onAnchorDown"),e.connect(h,"onAnchorDrag",this,"onAnchorDrag"),e.connect(h,"onTransformPoint",this,"onTransformPoint"),e.connect(a,"onChangeStyle",this,"onChangeStyle")];this.items[a.id].anchors.push(h);this.onAddAnchor(h)}},this);if("path"==a.shortType){var c=b[0];b=b[b.length-1];var d=this.items[a.id].anchors;c.x==b.x&&c.y==b.y&&
(d[0].linkedAnchor=d[d.length-1],d[d.length-1].linkedAnchor=d[0])}"group"==a.anchorType&&e.forEach(this.items[a.id].anchors,function(f){e.forEach(this.items[a.id].anchors,function(g){f.id!=g.id&&(f.org.y==g.org.y?f.x_anchor=g:f.org.x==g.org.x&&(f.y_anchor=g))},this)},this)}},remove:function(a){this.items[a.id]&&(e.forEach(this.items[a.id].anchors,function(b){b.destroy()}),e.forEach(this.items[a.id]._cons,e.disconnect,e),this.items[a.id].anchors=null,delete this.items[a.id])}})});