LeadAnnotation.js 5.3 KB

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