PreexistingAnnotation.js 5.3 KB

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