SingleArrowAnnotation.js 7.4 KB

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