source: Dev/branches/rest-dojo-ui/client/dojox/sketch/DoubleArrowAnnotation.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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