PreexistingAnnotation.js 5.6 KB

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