LeadAnnotation.js 5.0 KB

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