SingleArrowAnnotation.js 7.0 KB

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