root / trunk / web / dojo / dojox / sketch / Annotation.js @ 12
History | View | Annotate | Download (5.92 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.Annotation"]){ |
||
9 | dojo._hasResource["dojox.sketch.Annotation"]=true; |
||
10 | dojo.provide("dojox.sketch.Annotation");
|
||
11 | dojo.require("dojox.sketch.Anchor");
|
||
12 | dojo.require("dojox.sketch._Plugin");
|
||
13 | (function(){
|
||
14 | var ta=dojox.sketch;
|
||
15 | dojo.declare("dojox.sketch.AnnotationTool",ta._Plugin,{onMouseDown:function(e){ |
||
16 | this._omd=true; |
||
17 | },onMouseMove:function(e,_1){ |
||
18 | if(!this._omd){ |
||
19 | return;
|
||
20 | } |
||
21 | if(this._cshape){ |
||
22 | this._cshape.setShape(_1);
|
||
23 | }else{
|
||
24 | this._cshape=this.figure.surface.createRect(_1).setStroke({color:"#999",width:1,style:"ShortDot"}).setFill([255,255,255,0.7]); |
||
25 | this._cshape.getEventSource().setAttribute("shape-rendering","crispEdges"); |
||
26 | } |
||
27 | },onMouseUp:function(e){ |
||
28 | if(!this._omd){ |
||
29 | return;
|
||
30 | } |
||
31 | this._omd=false; |
||
32 | var f=this.figure; |
||
33 | if(this._cshape){ |
||
34 | f.surface.remove(this._cshape);
|
||
35 | delete this._cshape; |
||
36 | } |
||
37 | if(!(f._startPoint.x==e.pageX&&f._startPoint.y==e.pageY)){
|
||
38 | var _2=10; |
||
39 | if(Math.max(_2,Math.abs(f._absEnd.x-f._start.x),Math.abs(f._absEnd.y-f._start.y))>_2){
|
||
40 | this._create(f._start,f._end);
|
||
41 | } |
||
42 | } |
||
43 | },_create:function(_3,_4){ |
||
44 | var f=this.figure; |
||
45 | var _5=f.nextKey();
|
||
46 | var a=new (this.annotation)(f,_5); |
||
47 | a.transform={dx:f._calCol(_3.x/f.zoomFactor),dy:f._calCol(_3.y/f.zoomFactor)}; |
||
48 | a.end={x:f._calCol(_4.x/f.zoomFactor),y:f._calCol(_4.y/f.zoomFactor)}; |
||
49 | if(a.control){
|
||
50 | a.control={x:f._calCol((_4.x/2)/f.zoomFactor),y:f._calCol((_4.y/2)/f.zoomFactor)}; |
||
51 | } |
||
52 | f.onBeforeCreateShape(a); |
||
53 | a.initialize(); |
||
54 | f.select(a); |
||
55 | f.onCreateShape(a); |
||
56 | f.history.add(ta.CommandTypes.Create,a); |
||
57 | }}); |
||
58 | ta.Annotation=function(_6,id){ |
||
59 | this.id=this._key=id; |
||
60 | this.figure=_6;
|
||
61 | this.mode=ta.Annotation.Modes.View;
|
||
62 | this.shape=null; |
||
63 | this.boundingBox=null; |
||
64 | this.hasAnchors=true; |
||
65 | this.anchors={};
|
||
66 | this._properties={"stroke":{color:"blue",width:2},"font":{family:"Arial",size:16,weight:"bold"},"fill":"blue","label":""}; |
||
67 | if(this.figure){ |
||
68 | this.figure.add(this); |
||
69 | } |
||
70 | }; |
||
71 | var p=ta.Annotation.prototype;
|
||
72 | p.constructor=ta.Annotation; |
||
73 | p.type=function(){ |
||
74 | return ""; |
||
75 | }; |
||
76 | p.getType=function(){ |
||
77 | return ta.Annotation;
|
||
78 | }; |
||
79 | p.onRemove=function(_7){ |
||
80 | this.figure.history.add(ta.CommandTypes.Delete,this,this.serialize()); |
||
81 | }; |
||
82 | p.property=function(_8,_9){ |
||
83 | var r;
|
||
84 | _8=_8.toLowerCase(); |
||
85 | if(this._properties[_8]!==undefined){ |
||
86 | r=this._properties[_8];
|
||
87 | } |
||
88 | if(arguments.length>1){ |
||
89 | this._properties[_8]=_9;
|
||
90 | if(r!=_9){
|
||
91 | this.onPropertyChange(_8,r);
|
||
92 | } |
||
93 | } |
||
94 | return r;
|
||
95 | }; |
||
96 | p.onPropertyChange=function(_a,_b){ |
||
97 | }; |
||
98 | p.onCreate=function(){ |
||
99 | this.figure.history.add(ta.CommandTypes.Create,this); |
||
100 | }; |
||
101 | p.onDblClick=function(e){ |
||
102 | var l=prompt("Set new text:",this.property("label")); |
||
103 | if(l!==false){ |
||
104 | this.beginEdit(ta.CommandTypes.Modify);
|
||
105 | this.property("label",l); |
||
106 | this.draw();
|
||
107 | this.endEdit();
|
||
108 | } |
||
109 | }; |
||
110 | p.initialize=function(){ |
||
111 | }; |
||
112 | p.destroy=function(){ |
||
113 | }; |
||
114 | p.draw=function(){ |
||
115 | }; |
||
116 | p.apply=function(_c){ |
||
117 | }; |
||
118 | p.serialize=function(){ |
||
119 | }; |
||
120 | p.getBBox=function(){ |
||
121 | }; |
||
122 | p.beginEdit=function(_d){ |
||
123 | if(!this._type){ |
||
124 | this._type=_d||ta.CommandTypes.Move;
|
||
125 | this._prevState=this.serialize(); |
||
126 | } |
||
127 | }; |
||
128 | p.endEdit=function(){ |
||
129 | if(this._prevState!=this.serialize()){ |
||
130 | this.figure.history.add(this._type,this,this._prevState); |
||
131 | } |
||
132 | this._type=this._prevState=""; |
||
133 | }; |
||
134 | p.calculate={slope:function(p1,p2){ |
||
135 | if(!(p1.x-p2.x)){
|
||
136 | return 0; |
||
137 | } |
||
138 | return ((p1.y-p2.y)/(p1.x-p2.x));
|
||
139 | },dx:function(p1,p2,dy){ |
||
140 | var s=this.slope(p1,p2); |
||
141 | if(s==0){ |
||
142 | return s;
|
||
143 | } |
||
144 | return dy/s;
|
||
145 | },dy:function(p1,p2,dx){ |
||
146 | return this.slope(p1,p2)*dx; |
||
147 | }}; |
||
148 | p.drawBBox=function(){ |
||
149 | var r=this.getBBox(); |
||
150 | if(!this.boundingBox){ |
||
151 | this.boundingBox=this.shape.createRect(r).moveToBack().setStroke({color:"#999",width:1,style:"Dash"}).setFill([238,238,238,0.3]); |
||
152 | this.boundingBox.getEventSource().setAttribute("id",this.id+"-boundingBox"); |
||
153 | this.boundingBox.getEventSource().setAttribute("shape-rendering","crispEdges"); |
||
154 | this.figure._add(this); |
||
155 | }else{
|
||
156 | this.boundingBox.setShape(r);
|
||
157 | } |
||
158 | }; |
||
159 | p.setBinding=function(pt){ |
||
160 | this.transform.dx+=pt.dx;
|
||
161 | this.transform.dy+=pt.dy;
|
||
162 | this.draw();
|
||
163 | }; |
||
164 | p.getTextBox=function(_e){ |
||
165 | var fp=this.property("font"); |
||
166 | var f={fontFamily:fp.family,fontSize:fp.size,fontWeight:fp.weight}; |
||
167 | if(_e){
|
||
168 | f.fontSize=Math.floor(f.fontSize/_e); |
||
169 | } |
||
170 | return dojox.gfx._base._getTextBox(this.property("label"),f); |
||
171 | }; |
||
172 | p.setMode=function(m){ |
||
173 | if(this.mode==m){ |
||
174 | return;
|
||
175 | } |
||
176 | this.mode=m;
|
||
177 | var _f="disable"; |
||
178 | if(m==ta.Annotation.Modes.Edit){
|
||
179 | _f="enable";
|
||
180 | } |
||
181 | if(_f=="enable"){ |
||
182 | this.drawBBox();
|
||
183 | this.figure._add(this); |
||
184 | }else{
|
||
185 | if(this.boundingBox){ |
||
186 | if(this.shape){ |
||
187 | this.shape.remove(this.boundingBox); |
||
188 | } |
||
189 | this.boundingBox=null; |
||
190 | } |
||
191 | } |
||
192 | for(var p in this.anchors){ |
||
193 | this.anchors[p][_f]();
|
||
194 | } |
||
195 | }; |
||
196 | p.zoom=function(pct){ |
||
197 | pct=pct||this.figure.zoomFactor;
|
||
198 | if(this.labelShape){ |
||
199 | var f=dojo.clone(this.property("font")); |
||
200 | f.size=Math.ceil(f.size/pct)+"px";
|
||
201 | this.labelShape.setFont(f);
|
||
202 | } |
||
203 | for(var n in this.anchors){ |
||
204 | this.anchors[n].zoom(pct);
|
||
205 | } |
||
206 | if(dojox.gfx.renderer=="vml"){ |
||
207 | pct=1;
|
||
208 | } |
||
209 | if(this.pathShape){ |
||
210 | var s=dojo.clone(this.property("stroke")); |
||
211 | s.width=pct>1?s.width:Math.ceil(s.width/pct)+"px"; |
||
212 | this.pathShape.setStroke(s);
|
||
213 | } |
||
214 | }; |
||
215 | p.writeCommonAttrs=function(){ |
||
216 | return "id=\""+this.id+"\" dojoxsketch:type=\""+this.type()+"\""+" transform=\"translate("+this.transform.dx+","+this.transform.dy+")\""+(this.data?(" ><![CDATA[data:"+dojo.toJson(this.data)+"]]"):""); |
||
217 | }; |
||
218 | p.readCommonAttrs=function(obj){ |
||
219 | var i=0,cs=obj.childNodes,c; |
||
220 | while((c=cs[i++])){
|
||
221 | if(c.nodeType==4){ |
||
222 | if(c.nodeValue.substr(0,11)=="properties:"){ |
||
223 | this._properties=dojo.fromJson(c.nodeValue.substr(11)); |
||
224 | }else{
|
||
225 | if(c.nodeValue.substr(0,5)=="data:"){ |
||
226 | this.data=dojo.fromJson(c.nodeValue.substr(5)); |
||
227 | }else{
|
||
228 | console.error("unknown CDATA node in node ",obj);
|
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | if(obj.getAttribute("transform")){ |
||
234 | var t=obj.getAttribute("transform").replace("translate(",""); |
||
235 | var pt=t.split(","); |
||
236 | this.transform.dx=parseFloat(pt[0],10); |
||
237 | this.transform.dy=parseFloat(pt[1],10); |
||
238 | } |
||
239 | }; |
||
240 | ta.Annotation.Modes={View:0,Edit:1}; |
||
241 | ta.Annotation.register=function(_10,_11){ |
||
242 | var cls=ta[_10+"Annotation"]; |
||
243 | ta.registerTool(_10,function(p){
|
||
244 | dojo.mixin(p,{shape:_10,annotation:cls}); |
||
245 | return new (_11||ta.AnnotationTool)(p); |
||
246 | }); |
||
247 | }; |
||
248 | })(); |
||
249 | } |