Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / sketch / DoubleArrowAnnotation.js @ 12

History | View | Annotate | Download (6.68 KB)

1 9 andrej.cim
/*
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.DoubleArrowAnnotation"]){
9
dojo._hasResource["dojox.sketch.DoubleArrowAnnotation"]=true;
10
dojo.provide("dojox.sketch.DoubleArrowAnnotation");
11
dojo.require("dojox.sketch.Annotation");
12
dojo.require("dojox.sketch.Anchor");
13
(function(){
14
var ta=dojox.sketch;
15
ta.DoubleArrowAnnotation=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=6;
23
this.textYOffset=10;
24
this.textAlign="middle";
25
this.startRotation=0;
26
this.endRotation=0;
27
this.labelShape=null;
28
this.pathShape=null;
29
this.startArrow=null;
30
this.startArrowGroup=null;
31
this.endArrow=null;
32
this.endArrowGroup=null;
33
this.anchors.start=new ta.Anchor(this,"start");
34
this.anchors.control=new ta.Anchor(this,"control");
35
this.anchors.end=new ta.Anchor(this,"end");
36
};
37
ta.DoubleArrowAnnotation.prototype=new ta.Annotation;
38
var p=ta.DoubleArrowAnnotation.prototype;
39
p.constructor=ta.DoubleArrowAnnotation;
40
p.type=function(){
41
return "DoubleArrow";
42
};
43
p.getType=function(){
44
return ta.DoubleArrowAnnotation;
45
};
46
p._rot=function(){
47
var _2=this.control.y-this.start.y;
48
var _3=this.control.x-this.start.x;
49
this.startRotation=Math.atan2(_2,_3);
50
_2=this.end.y-this.control.y;
51
_3=this.end.x-this.control.x;
52
this.endRotation=Math.atan2(_2,_3);
53
};
54
p._pos=function(){
55
var _4=this.textOffset;
56
if(this.control.y<this.end.y){
57
_4*=-1;
58
}else{
59
_4+=this.textYOffset;
60
}
61
var ab={x:((this.control.x-this.start.x)*0.5)+this.start.x,y:((this.control.y-this.start.y)*0.5)+this.start.y};
62
var bc={x:((this.end.x-this.control.x)*0.5)+this.control.x,y:((this.end.y-this.control.y)*0.5)+this.control.y};
63
this.textPosition={x:((bc.x-ab.x)*0.5)+ab.x,y:(((bc.y-ab.y)*0.5)+ab.y)+_4};
64
};
65
p.apply=function(_5){
66
if(!_5){
67
return;
68
}
69
if(_5.documentElement){
70
_5=_5.documentElement;
71
}
72
this.readCommonAttrs(_5);
73
for(var i=0;i<_5.childNodes.length;i++){
74
var c=_5.childNodes[i];
75
if(c.localName=="text"){
76
this.property("label",c.childNodes.length?c.childNodes[0].nodeValue:"");
77
}else{
78
if(c.localName=="path"){
79
var d=c.getAttribute("d").split(" ");
80
var s=d[0].split(",");
81
this.start.x=parseFloat(s[0].substr(1),10);
82
this.start.y=parseFloat(s[1],10);
83
s=d[1].split(",");
84
this.control.x=parseFloat(s[0].substr(1),10);
85
this.control.y=parseFloat(s[1],10);
86
s=d[2].split(",");
87
this.end.x=parseFloat(s[0],10);
88
this.end.y=parseFloat(s[1],10);
89
var _6=this.property("stroke");
90
var _7=c.getAttribute("style");
91
var m=_7.match(/stroke:([^;]+);/);
92
if(m){
93
_6.color=m[1];
94
this.property("fill",m[1]);
95
}
96
m=_7.match(/stroke-width:([^;]+);/);
97
if(m){
98
_6.width=m[1];
99
}
100
this.property("stroke",_6);
101
}
102
}
103
}
104
};
105
p.initialize=function(_8){
106
var _9=(ta.Annotation.labelFont)?ta.Annotation.labelFont:{family:"Times",size:"16px"};
107
this.apply(_8);
108
this._rot();
109
this._pos();
110
var _a=this.startRotation;
111
var _b=dojox.gfx.matrix.rotate(_a);
112
_a=this.endRotation;
113
var _c=dojox.gfx.matrix.rotateAt(_a,this.end.x,this.end.y);
114
this.shape=this.figure.group.createGroup();
115
this.shape.getEventSource().setAttribute("id",this.id);
116
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");
117
this.startArrowGroup=this.shape.createGroup().setTransform({dx:this.start.x,dy:this.start.y});
118
this.startArrowGroup.applyTransform(_b);
119
this.startArrow=this.startArrowGroup.createPath();
120
this.endArrowGroup=this.shape.createGroup().setTransform(_c);
121
this.endArrow=this.endArrowGroup.createPath();
122
this.labelShape=this.shape.createText({x:this.textPosition.x,y:this.textPosition.y,text:this.property("label"),align:this.textAlign}).setFill(this.property("fill"));
123
this.labelShape.getEventSource().setAttribute("id",this.id+"-labelShape");
124
this.draw();
125
};
126
p.destroy=function(){
127
if(!this.shape){
128
return;
129
}
130
this.startArrowGroup.remove(this.startArrow);
131
this.endArrowGroup.remove(this.endArrow);
132
this.shape.remove(this.startArrowGroup);
133
this.shape.remove(this.endArrowGroup);
134
this.shape.remove(this.pathShape);
135
this.shape.remove(this.labelShape);
136
this.figure.group.remove(this.shape);
137
this.shape=this.pathShape=this.labelShape=this.startArrowGroup=this.startArrow=this.endArrowGroup=this.endArrow=null;
138
};
139
p.draw=function(_d){
140
this.apply(_d);
141
this._rot();
142
this._pos();
143
var _e=this.startRotation;
144
var _f=dojox.gfx.matrix.rotate(_e);
145
_e=this.endRotation;
146
var _10=dojox.gfx.matrix.rotateAt(_e,this.end.x,this.end.y);
147
this.shape.setTransform(this.transform);
148
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");
149
this.startArrowGroup.setTransform({dx:this.start.x,dy:this.start.y}).applyTransform(_f);
150
this.startArrow.setFill(this.property("fill"));
151
this.endArrowGroup.setTransform(_10);
152
this.endArrow.setFill(this.property("fill"));
153
this.labelShape.setShape({x:this.textPosition.x,y:this.textPosition.y,text:this.property("label")}).setFill(this.property("fill"));
154
this.zoom();
155
};
156
p.zoom=function(pct){
157
if(this.startArrow){
158
pct=pct||this.figure.zoomFactor;
159
ta.Annotation.prototype.zoom.call(this,pct);
160
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);
161
this.startArrow.setShape("M0,0 l"+l+",-"+w+" -"+h+","+w+" "+h+","+w+" Z");
162
this.endArrow.setShape("M"+this.end.x+","+this.end.y+" l-"+l+",-"+w+" "+h+","+w+" -"+h+","+w+" Z");
163
}
164
};
165
p.getBBox=function(){
166
var x=Math.min(this.start.x,this.control.x,this.end.x);
167
var y=Math.min(this.start.y,this.control.y,this.end.y);
168
var w=Math.max(this.start.x,this.control.x,this.end.x)-x;
169
var h=Math.max(this.start.y,this.control.y,this.end.y)-y;
170
return {x:x,y:y,width:w,height:h};
171
};
172
p.serialize=function(){
173
var s=this.property("stroke");
174
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("+(Math.round((this.startRotation*(180/Math.PI))*Math.pow(10,4))/Math.pow(10,4))+")\">"+"<path style=\"fill:"+s.color+";\" d=\"M0,0 l20,-5, -3,5, 3,5 Z\" />"+"</g>"+"<g transform=\"rotate("+(Math.round((this.endRotation*(180/Math.PI))*Math.pow(10,4))/Math.pow(10,4))+", "+this.end.x+", "+this.end.y+")\">"+"<path style=\"fill:"+s.color+";\" d=\"M"+this.end.x+","+this.end.y+" l-20,-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>";
175
};
176
ta.Annotation.register("DoubleArrow");
177
})();
178
}