| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang", "./Annotation", "./Anchor"], function(dojo){ |
| 2 |
dojo.getObject("sketch", true, dojox); |
| 3 |
|
| 4 |
var ta=dojox.sketch; |
| 5 |
ta.SingleArrowAnnotation=function(figure, id){ |
| 6 |
ta.Annotation.call(this, figure, id); |
| 7 |
this.transform={ dx:0, dy:0 }; |
| 8 |
this.start={x:0, y:0}; |
| 9 |
this.control={x:100, y:-50}; |
| 10 |
this.end={x:200, y:0}; |
| 11 |
this.textPosition={ x:0, y:0 }; |
| 12 |
this.textOffset=4; |
| 13 |
|
| 14 |
this.textYOffset=10; |
| 15 |
this.rotation=0; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
this.pathShape=null; |
| 20 |
this.arrowhead=null; |
| 21 |
this.arrowheadGroup=null; |
| 22 |
this.labelShape=null; |
| 23 |
|
| 24 |
this.anchors.start=new ta.Anchor(this, "start"); |
| 25 |
this.anchors.control=new ta.Anchor(this, "control"); |
| 26 |
this.anchors.end=new ta.Anchor(this, "end"); |
| 27 |
}; |
| 28 |
ta.SingleArrowAnnotation.prototype=new ta.Annotation; |
| 29 |
var p=ta.SingleArrowAnnotation.prototype; |
| 30 |
p.constructor=ta.SingleArrowAnnotation; |
| 31 |
|
| 32 |
p.type=function(){ return 'SingleArrow'; }; |
| 33 |
p.getType=function(){ return ta.SingleArrowAnnotation; }; |
| 34 |
|
| 35 |
|
| 36 |
p._rot=function(){ |
| 37 |
|
| 38 |
var opp=this.control.y-this.start.y; |
| 39 |
var adj=this.control.x-this.start.x; |
| 40 |
|
| 41 |
this.rotation=Math.atan2(opp,adj); |
| 42 |
}; |
| 43 |
p._pos=function(){ |
| 44 |
|
| 45 |
var offset=this.textOffset, x=0, y=0; |
| 46 |
var slope=this.calculate.slope(this.control, this.end); |
| 47 |
this.textAlign="middle"; |
| 48 |
if(Math.abs(slope)>=1){ |
| 49 |
x=this.end.x+this.calculate.dx(this.control, this.end, offset); |
| 50 |
if(this.control.y>this.end.y){ y=this.end.y-offset; } |
| 51 |
else{ y=this.end.y+offset+this.textYOffset; } |
| 52 |
} else if(slope==0){ |
| 53 |
x=this.end.x+offset; |
| 54 |
y=this.end.y+this.textYOffset; |
| 55 |
} else { |
| 56 |
if(this.start.x>this.end.x){ |
| 57 |
x=this.end.x-offset; |
| 58 |
this.textAlign="end"; |
| 59 |
} else { |
| 60 |
x=this.end.x+offset; |
| 61 |
this.textAlign="start"; |
| 62 |
} |
| 63 |
if(this.start.y<this.end.y){ |
| 64 |
y=this.end.y+this.calculate.dy(this.control, this.end, offset)+this.textYOffset; |
| 65 |
} else { |
| 66 |
y=this.end.y+this.calculate.dy(this.control, this.end, -offset); |
| 67 |
} |
| 68 |
} |
| 69 |
this.textPosition={ x:x, y:y }; |
| 70 |
}; |
| 71 |
|
| 72 |
p.apply=function(obj){ |
| 73 |
if(!obj){ return; } |
| 74 |
if(obj.documentElement){ obj=obj.documentElement; } |
| 75 |
this.readCommonAttrs(obj); |
| 76 |
|
| 77 |
for(var i=0; i<obj.childNodes.length; i++){ |
| 78 |
var c=obj.childNodes[i]; |
| 79 |
if(c.localName=="text"){ |
| 80 |
this.property('label',c.childNodes.length?c.childNodes[0].nodeValue:''); |
| 81 |
} |
| 82 |
else if(c.localName=="path"){ |
| 83 |
|
| 84 |
var d=c.getAttribute('d').split(" "); |
| 85 |
var s=d[0].split(","); |
| 86 |
this.start.x=parseFloat(s[0].substr(1),10); |
| 87 |
this.start.y=parseFloat(s[1],10); |
| 88 |
s=d[1].split(","); |
| 89 |
this.control.x=parseFloat(s[0].substr(1),10); |
| 90 |
this.control.y=parseFloat(s[1],10); |
| 91 |
s=d[2].split(","); |
| 92 |
this.end.x=parseFloat(s[0],10); |
| 93 |
this.end.y=parseFloat(s[1],10); |
| 94 |
var stroke=this.property('stroke'); |
| 95 |
var style=c.getAttribute('style'); |
| 96 |
var m=style.match(/stroke:([^;]+);/); |
| 97 |
if(m){ |
| 98 |
stroke.color=m[1]; |
| 99 |
this.property('fill',m[1]); |
| 100 |
} |
| 101 |
m=style.match(/stroke-width:([^;]+);/); |
| 102 |
if(m){ |
| 103 |
stroke.width=m[1]; |
| 104 |
} |
| 105 |
this.property('stroke',stroke); |
| 106 |
} |
| 107 |
} |
| 108 |
}; |
| 109 |
p.initialize=function(obj){ |
| 110 |
|
| 111 |
var font=(ta.Annotation.labelFont)?ta.Annotation.labelFont:{family:"Times", size:"16px"}; |
| 112 |
this.apply(obj); |
| 113 |
|
| 114 |
|
| 115 |
this._rot(); |
| 116 |
this._pos(); |
| 117 |
|
| 118 |
|
| 119 |
var rot=this.rotation; |
| 120 |
var tRot=dojox.gfx.matrix.rotate(rot); |
| 121 |
|
| 122 |
|
| 123 |
this.shape=this.figure.group.createGroup(); |
| 124 |
this.shape.getEventSource().setAttribute("id", this.id); |
| 125 |
|
| 126 |
|
| 127 |
this.pathShape=this.shape.createPath("M"+this.start.x+","+this.start.y+" Q"+this.control.x+","+this.control.y+" "+this.end.x+","+this.end.y+" l0,0") |
| 128 |
|
| 129 |
|
| 130 |
this.arrowheadGroup=this.shape.createGroup(); |
| 131 |
this.arrowhead=this.arrowheadGroup.createPath(); |
| 132 |
|
| 133 |
this.labelShape=this.shape.createText({ |
| 134 |
x:this.textPosition.x, |
| 135 |
y:this.textPosition.y, |
| 136 |
text:this.property('label'), |
| 137 |
align:this.textAlign |
| 138 |
}) |
| 139 |
|
| 140 |
|
| 141 |
this.labelShape.getEventSource().setAttribute('id',this.id+"-labelShape"); |
| 142 |
this.draw(); |
| 143 |
}; |
| 144 |
|
| 145 |
p.destroy=function(){ |
| 146 |
if(!this.shape){ return; } |
| 147 |
this.arrowheadGroup.remove(this.arrowhead); |
| 148 |
this.shape.remove(this.arrowheadGroup); |
| 149 |
this.shape.remove(this.pathShape); |
| 150 |
this.shape.remove(this.labelShape); |
| 151 |
this.figure.group.remove(this.shape); |
| 152 |
this.shape=this.pathShape=this.labelShape=this.arrowheadGroup=this.arrowhead=null; |
| 153 |
}; |
| 154 |
|
| 155 |
p.draw=function(obj){ |
| 156 |
this.apply(obj); |
| 157 |
this._rot(); |
| 158 |
this._pos(); |
| 159 |
|
| 160 |
|
| 161 |
var rot=this.rotation; |
| 162 |
var tRot=dojox.gfx.matrix.rotate(rot); |
| 163 |
|
| 164 |
this.shape.setTransform(this.transform); |
| 165 |
this.pathShape.setShape("M"+this.start.x+","+this.start.y+" Q"+this.control.x+","+this.control.y+" "+this.end.x+","+this.end.y+" l0,0") |
| 166 |
|
| 167 |
|
| 168 |
this.arrowheadGroup.setTransform({dx:this.start.x,dy:this.start.y}).applyTransform(tRot); |
| 169 |
this.arrowhead.setFill(this.property('fill')); |
| 170 |
|
| 171 |
this.labelShape.setShape({ |
| 172 |
x:this.textPosition.x, |
| 173 |
y:this.textPosition.y, |
| 174 |
text:this.property('label'), |
| 175 |
align:this.textAlign |
| 176 |
}) |
| 177 |
.setFill(this.property('fill')); |
| 178 |
this.zoom(); |
| 179 |
}; |
| 180 |
|
| 181 |
p.zoom=function(pct){ |
| 182 |
if(this.arrowhead){ |
| 183 |
pct = pct || this.figure.zoomFactor; |
| 184 |
ta.Annotation.prototype.zoom.call(this,pct); |
| 185 |
|
| 186 |
if(this._curPct!==pct){ |
| 187 |
this._curPct=pct; |
| 188 |
var l=pct>1?20:Math.floor(20/pct), w=pct>1?5:Math.floor(5/pct),h=pct>1?3:Math.floor(3/pct); |
| 189 |
this.arrowhead.setShape("M0,0 l"+l+",-"+w+" -"+h+","+w+" "+h+","+w+" Z"); |
| 190 |
} |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
p.getBBox=function(){ |
| 195 |
var x=Math.min(this.start.x, this.control.x, this.end.x); |
| 196 |
var y=Math.min(this.start.y, this.control.y, this.end.y); |
| 197 |
var w=Math.max(this.start.x, this.control.x, this.end.x)-x; |
| 198 |
var h=Math.max(this.start.y, this.control.y, this.end.y)-y; |
| 199 |
return { x:x, y:y, width:w, height:h }; |
| 200 |
}; |
| 201 |
|
| 202 |
p.serialize=function(){ |
| 203 |
var s=this.property('stroke'); |
| 204 |
var r=this.rotation*(180/Math.PI); |
| 205 |
r=Math.round(r*Math.pow(10,4))/Math.pow(10,4); |
| 206 |
return '<g '+this.writeCommonAttrs()+'>' |
| 207 |
+ '<path style="stroke:'+s.color+';stroke-width:'+s.width+';fill:none;" d="' |
| 208 |
+ "M"+this.start.x+","+this.start.y+" " |
| 209 |
+ "Q"+this.control.x+","+this.control.y+" " |
| 210 |
+ this.end.x+","+this.end.y |
| 211 |
+ '" />' |
| 212 |
+ '<g transform="translate(' + this.start.x + "," + this.start.y + ") " |
| 213 |
+ 'rotate(' + r + ')">' |
| 214 |
+ '<path style="fill:'+s.color+';" d="M0,0 l20,-5, -3,5, 3,5 Z" />' |
| 215 |
+ '</g>' |
| 216 |
+ '<text style="fill:'+s.color+';text-anchor:'+this.textAlign+'" font-weight="bold" ' |
| 217 |
+ 'x="' + this.textPosition.x + '" ' |
| 218 |
+ 'y="' + this.textPosition.y + '">' |
| 219 |
+ this.property('label') |
| 220 |
+ '</text>' |
| 221 |
+ '</g>'; |
| 222 |
}; |
| 223 |
|
| 224 |
ta.Annotation.register("SingleArrow"); |
| 225 |
return dojox.sketch.SingleArrowAnnotation; |
| 226 |
}); |