root / trunk / web / dojo / dojox / sketch / SingleArrowAnnotation.js @ 12
History | View | Annotate | Download (5.9 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.sketch.SingleArrowAnnotation"]){ |
9 |
dojo._hasResource["dojox.sketch.SingleArrowAnnotation"]=true; |
10 |
dojo.provide("dojox.sketch.SingleArrowAnnotation");
|
11 |
dojo.require("dojox.sketch.Annotation");
|
12 |
dojo.require("dojox.sketch.Anchor");
|
13 |
(function(){
|
14 |
var ta=dojox.sketch;
|
15 |
ta.SingleArrowAnnotation=function(_1,id){ |
16 |
ta.Annotation.call(this,_1,id);
|
17 |
this.transform={dx:0,dy:0}; |
18 |
this.start={x:0,y:0}; |
19 |
this.control={x:100,y:-50}; |
20 |
this.end={x:200,y:0}; |
21 |
this.textPosition={x:0,y:0}; |
22 |
this.textOffset=4; |
23 |
this.textYOffset=10; |
24 |
this.rotation=0; |
25 |
this.pathShape=null; |
26 |
this.arrowhead=null; |
27 |
this.arrowheadGroup=null; |
28 |
this.labelShape=null; |
29 |
this.anchors.start=new ta.Anchor(this,"start"); |
30 |
this.anchors.control=new ta.Anchor(this,"control"); |
31 |
this.anchors.end=new ta.Anchor(this,"end"); |
32 |
}; |
33 |
ta.SingleArrowAnnotation.prototype=new ta.Annotation;
|
34 |
var p=ta.SingleArrowAnnotation.prototype;
|
35 |
p.constructor=ta.SingleArrowAnnotation; |
36 |
p.type=function(){ |
37 |
return "SingleArrow"; |
38 |
}; |
39 |
p.getType=function(){ |
40 |
return ta.SingleArrowAnnotation;
|
41 |
}; |
42 |
p._rot=function(){ |
43 |
var _2=this.control.y-this.start.y; |
44 |
var _3=this.control.x-this.start.x; |
45 |
this.rotation=Math.atan2(_2,_3);
|
46 |
}; |
47 |
p._pos=function(){ |
48 |
var _4=this.textOffset,x=0,y=0; |
49 |
var _5=this.calculate.slope(this.control,this.end); |
50 |
this.textAlign="middle"; |
51 |
if(Math.abs(_5)>=1){ |
52 |
x=this.end.x+this.calculate.dx(this.control,this.end,_4); |
53 |
if(this.control.y>this.end.y){ |
54 |
y=this.end.y-_4;
|
55 |
}else{
|
56 |
y=this.end.y+_4+this.textYOffset; |
57 |
} |
58 |
}else{
|
59 |
if(_5==0){ |
60 |
x=this.end.x+_4;
|
61 |
y=this.end.y+this.textYOffset; |
62 |
}else{
|
63 |
if(this.start.x>this.end.x){ |
64 |
x=this.end.x-_4;
|
65 |
this.textAlign="end"; |
66 |
}else{
|
67 |
x=this.end.x+_4;
|
68 |
this.textAlign="start"; |
69 |
} |
70 |
if(this.start.y<this.end.y){ |
71 |
y=this.end.y+this.calculate.dy(this.control,this.end,_4)+this.textYOffset; |
72 |
}else{
|
73 |
y=this.end.y+this.calculate.dy(this.control,this.end,-_4); |
74 |
} |
75 |
} |
76 |
} |
77 |
this.textPosition={x:x,y:y}; |
78 |
}; |
79 |
p.apply=function(_6){ |
80 |
if(!_6){
|
81 |
return;
|
82 |
} |
83 |
if(_6.documentElement){
|
84 |
_6=_6.documentElement; |
85 |
} |
86 |
this.readCommonAttrs(_6);
|
87 |
for(var i=0;i<_6.childNodes.length;i++){ |
88 |
var c=_6.childNodes[i];
|
89 |
if(c.localName=="text"){ |
90 |
this.property("label",c.childNodes.length?c.childNodes[0].nodeValue:""); |
91 |
}else{
|
92 |
if(c.localName=="path"){ |
93 |
var d=c.getAttribute("d").split(" "); |
94 |
var s=d[0].split(","); |
95 |
this.start.x=parseFloat(s[0].substr(1),10); |
96 |
this.start.y=parseFloat(s[1],10); |
97 |
s=d[1].split(","); |
98 |
this.control.x=parseFloat(s[0].substr(1),10); |
99 |
this.control.y=parseFloat(s[1],10); |
100 |
s=d[2].split(","); |
101 |
this.end.x=parseFloat(s[0],10); |
102 |
this.end.y=parseFloat(s[1],10); |
103 |
var _7=this.property("stroke"); |
104 |
var _8=c.getAttribute("style"); |
105 |
var m=_8.match(/stroke:([^;]+);/); |
106 |
if(m){
|
107 |
_7.color=m[1];
|
108 |
this.property("fill",m[1]); |
109 |
} |
110 |
m=_8.match(/stroke-width:([^;]+);/);
|
111 |
if(m){
|
112 |
_7.width=m[1];
|
113 |
} |
114 |
this.property("stroke",_7); |
115 |
} |
116 |
} |
117 |
} |
118 |
}; |
119 |
p.initialize=function(_9){ |
120 |
var _a=(ta.Annotation.labelFont)?ta.Annotation.labelFont:{family:"Times",size:"16px"}; |
121 |
this.apply(_9);
|
122 |
this._rot();
|
123 |
this._pos();
|
124 |
var _b=this.rotation; |
125 |
var _c=dojox.gfx.matrix.rotate(_b);
|
126 |
this.shape=this.figure.group.createGroup(); |
127 |
this.shape.getEventSource().setAttribute("id",this.id); |
128 |
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"); |
129 |
this.arrowheadGroup=this.shape.createGroup(); |
130 |
this.arrowhead=this.arrowheadGroup.createPath(); |
131 |
this.labelShape=this.shape.createText({x:this.textPosition.x,y:this.textPosition.y,text:this.property("label"),align:this.textAlign}); |
132 |
this.labelShape.getEventSource().setAttribute("id",this.id+"-labelShape"); |
133 |
this.draw();
|
134 |
}; |
135 |
p.destroy=function(){ |
136 |
if(!this.shape){ |
137 |
return;
|
138 |
} |
139 |
this.arrowheadGroup.remove(this.arrowhead); |
140 |
this.shape.remove(this.arrowheadGroup); |
141 |
this.shape.remove(this.pathShape); |
142 |
this.shape.remove(this.labelShape); |
143 |
this.figure.group.remove(this.shape); |
144 |
this.shape=this.pathShape=this.labelShape=this.arrowheadGroup=this.arrowhead=null; |
145 |
}; |
146 |
p.draw=function(_d){ |
147 |
this.apply(_d);
|
148 |
this._rot();
|
149 |
this._pos();
|
150 |
var _e=this.rotation; |
151 |
var _f=dojox.gfx.matrix.rotate(_e);
|
152 |
this.shape.setTransform(this.transform); |
153 |
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"); |
154 |
this.arrowheadGroup.setTransform({dx:this.start.x,dy:this.start.y}).applyTransform(_f); |
155 |
this.arrowhead.setFill(this.property("fill")); |
156 |
this.labelShape.setShape({x:this.textPosition.x,y:this.textPosition.y,text:this.property("label"),align:this.textAlign}).setFill(this.property("fill")); |
157 |
this.zoom();
|
158 |
}; |
159 |
p.zoom=function(pct){ |
160 |
if(this.arrowhead){ |
161 |
pct=pct||this.figure.zoomFactor;
|
162 |
ta.Annotation.prototype.zoom.call(this,pct);
|
163 |
if(this._curPct!==pct){ |
164 |
this._curPct=pct;
|
165 |
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); |
166 |
this.arrowhead.setShape("M0,0 l"+l+",-"+w+" -"+h+","+w+" "+h+","+w+" Z"); |
167 |
} |
168 |
} |
169 |
}; |
170 |
p.getBBox=function(){ |
171 |
var x=Math.min(this.start.x,this.control.x,this.end.x); |
172 |
var y=Math.min(this.start.y,this.control.y,this.end.y); |
173 |
var w=Math.max(this.start.x,this.control.x,this.end.x)-x; |
174 |
var h=Math.max(this.start.y,this.control.y,this.end.y)-y; |
175 |
return {x:x,y:y,width:w,height:h}; |
176 |
}; |
177 |
p.serialize=function(){ |
178 |
var s=this.property("stroke"); |
179 |
var r=this.rotation*(180/Math.PI); |
180 |
r=Math.round(r*Math.pow(10,4))/Math.pow(10,4); |
181 |
return "<g "+this.writeCommonAttrs()+">"+"<path style=\"stroke:"+s.color+";stroke-width:"+s.width+";fill:none;\" d=\""+"M"+this.start.x+","+this.start.y+" "+"Q"+this.control.x+","+this.control.y+" "+this.end.x+","+this.end.y+"\" />"+"<g transform=\"translate("+this.start.x+","+this.start.y+") "+"rotate("+r+")\">"+"<path style=\"fill:"+s.color+";\" d=\"M0,0 l20,-5, -3,5, 3,5 Z\" />"+"</g>"+"<text style=\"fill:"+s.color+";text-anchor:"+this.textAlign+"\" font-weight=\"bold\" "+"x=\""+this.textPosition.x+"\" "+"y=\""+this.textPosition.y+"\">"+this.property("label")+"</text>"+"</g>"; |
182 |
}; |
183 |
ta.Annotation.register("SingleArrow");
|
184 |
})(); |
185 |
} |