source: Dev/branches/rest-dojo-ui/client/dojox/sketch/LeadAnnotation.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: 4.9 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "./Annotation", "./Anchor"], function(dojo){
2        dojo.getObject("sketch", true, dojox);
3
4        var ta=dojox.sketch;
5        ta.LeadAnnotation=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                //this.textAlign="middle";
14                this.textYOffset=10;
15
16//              this.property('label',this.id);
17                this.pathShape=null;
18                this.labelShape=null;
19
20                this.anchors.start=new ta.Anchor(this, "start");
21                this.anchors.control=new ta.Anchor(this, "control");
22                this.anchors.end=new ta.Anchor(this, "end");
23        };
24        ta.LeadAnnotation.prototype=new ta.Annotation;
25        var p=ta.LeadAnnotation.prototype;
26        p.constructor=ta.LeadAnnotation;
27
28        p.type=function(){ return 'Lead'; }
29        p.getType=function(){ return ta.LeadAnnotation; };
30
31        p._pos=function(){
32                var offset=this.textOffset, x=0, y=0;
33                var slope=this.calculate.slope(this.control, this.end);
34                this.textAlign="middle";
35                if(Math.abs(slope)>=1){
36                        x=this.end.x+this.calculate.dx(this.control, this.end, offset);
37                        if(this.control.y>this.end.y){
38                                y=this.end.y-offset;
39                        } else {
40                                y=this.end.y+offset+this.textYOffset;
41                        }
42                } else if(slope==0){
43                        x=this.end.x+offset;
44                        y=this.end.y+this.textYOffset;
45                } else {
46                        if(this.start.x>this.end.x){
47                                x=this.end.x-offset;
48                                this.textAlign="end";
49                        } else {
50                                x=this.end.x+offset;
51                                this.textAlign="start";
52                        }
53                        if(this.start.y<this.end.y){
54                                y=this.end.y+this.calculate.dy(this.control, this.end, offset)+this.textYOffset;
55                        } else {
56                                y=this.end.y+this.calculate.dy(this.control, this.end, -offset);
57                        }
58                }
59                this.textPosition={ x:x, y:y };
60        };
61        p.apply=function(obj){
62                if(!obj){ return; }
63                if(obj.documentElement){ obj=obj.documentElement; }
64                this.readCommonAttrs(obj);
65               
66                for(var i=0; i<obj.childNodes.length; i++){
67                        var c=obj.childNodes[i];
68                        if(c.localName=="text"){
69                                this.property('label',c.childNodes.length?c.childNodes[0].nodeValue:'');
70                        }
71                        else if(c.localName=="path"){
72                                //      the line
73                                var d=c.getAttribute('d').split(" ");
74                                var s=d[0].split(",");
75                                this.start.x=parseFloat(s[0].substr(1),10);
76                                this.start.y=parseFloat(s[1],10);
77                                s=d[1].split(",");
78                                this.control.x=parseFloat(s[0].substr(1),10);
79                                this.control.y=parseFloat(s[1],10);
80                                s=d[2].split(",");
81                                this.end.x=parseFloat(s[0],10);
82                                this.end.y=parseFloat(s[1],10);
83                                var stroke=this.property('stroke');
84                                var style=c.getAttribute('style');
85                                var m=style.match(/stroke:([^;]+);/);
86                                if(m){
87                                        stroke.color=m[1];
88                                        this.property('fill',m[1]);
89                                }
90                                m=style.match(/stroke-width:([^;]+);/);
91                                if(m){
92                                        stroke.width=m[1];
93                                }
94                                this.property('stroke',stroke);
95                        }
96                }
97        };
98
99        p.initialize=function(obj){
100                this.apply(obj);
101                this._pos();
102
103                //      create either from scratch or based on the passed node
104                this.shape=this.figure.group.createGroup();
105                this.shape.getEventSource().setAttribute("id", this.id);
106                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");
107                this.labelShape=this.shape.createText({
108                                x:this.textPosition.x,
109                                y:this.textPosition.y,
110                                text:this.property('label'),
111                                align:this.textAlign
112                        });
113                this.labelShape.getEventSource().setAttribute('id',this.id+"-labelShape");
114                this.draw();
115        };
116        p.destroy=function(){
117                if(!this.shape){ return; }
118                this.shape.remove(this.pathShape);
119                this.shape.remove(this.labelShape);
120                this.figure.group.remove(this.shape);
121                this.shape=this.pathShape=this.labelShape=null;
122        };
123        p.getBBox=function(){
124                var x=Math.min(this.start.x, this.control.x, this.end.x);
125                var y=Math.min(this.start.y, this.control.y, this.end.y);
126                var w=Math.max(this.start.x, this.control.x, this.end.x)-x;
127                var h=Math.max(this.start.y, this.control.y, this.end.y)-y;
128                return { x:x, y:y, width:w, height:h };
129        };
130        p.draw=function(obj){
131                this.apply(obj);
132                this._pos();
133                this.shape.setTransform(this.transform);
134                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");
135                this.labelShape.setShape({
136                                x:this.textPosition.x,
137                                y:this.textPosition.y,
138                                text:this.property('label')
139                        })
140                        .setFill(this.property('fill'));
141                this.zoom();
142        };
143        p.serialize=function(){
144                var stroke=this.property('stroke');
145                return '<g '+this.writeCommonAttrs()+'>'
146                        + '<path style="stroke:'+stroke.color+';stroke-width:'+stroke.width+';fill:none;" d="'
147                        + "M"+this.start.x+","+this.start.y+" "
148                        + "Q"+this.control.x+","+this.control.y+" "
149                        + this.end.x+","+this.end.y
150                        + '" />'
151                        + '<text style="fill:'+stroke.color+';text-anchor:'+this.textAlign+'" font-weight="bold" '
152                        + 'x="' + this.textPosition.x + '" '
153                        + 'y="' + this.textPosition.y + '">'
154                        + this.property('label')
155                        + '</text>'
156                        + '</g>';
157        };
158
159        ta.Annotation.register("Lead");
160        return dojox.sketch.LeadAnnotation;
161});
Note: See TracBrowser for help on using the repository browser.