Label.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // wrapped by build app
  2. define("dojox/drawing/annotations/Label", ["dijit","dojo","dojox","dojo/require!dojox/drawing/stencil/Text"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.annotations.Label");
  4. dojo.require("dojox.drawing.stencil.Text");
  5. dojox.drawing.annotations.Label = dojox.drawing.util.oo.declare(
  6. // summary:
  7. // An annotation called internally to label an Stencil.
  8. // description:
  9. // Annotation is positioned with dojox.drawing.util.positioning.label
  10. // That method should be overwritten for custom placement. Or,
  11. // add a 'setLabelCustom' method to the Stencil and it will be used.
  12. //
  13. dojox.drawing.stencil.Text,
  14. function(/*Object*/options){
  15. // arguments:
  16. // options: Object
  17. // One key value: the stencil that called this.
  18. //
  19. this.master = options.stencil;
  20. this.labelPosition = options.labelPosition || "BR"; // TL, TR, BR, BL, or function
  21. if(dojo.isFunction(this.labelPosition)){
  22. this.setLabel = this.setLabelCustom;
  23. }
  24. this.setLabel(options.text || "");
  25. this.connect(this.master, "onTransform", this, "setLabel");
  26. this.connect(this.master, "destroy", this, "destroy");
  27. if(this.style.labelSameColor){
  28. this.connect(this.master, "attr", this, "beforeAttr");
  29. }
  30. },{
  31. _align:"start",
  32. drawingType:"label",
  33. setLabelCustom: function(/* ? String */text){
  34. // summary:
  35. // Attaches to custom positioning within a Stencil
  36. //
  37. var d = dojo.hitch(this.master, this.labelPosition)();
  38. this.setData({
  39. x:d.x,
  40. y:d.y,
  41. width:d.w || this.style.text.minWidth,
  42. height:d.h || this._lineHeight
  43. });
  44. // is an event, not text, so keep the old label:
  45. if(text && !text.split){ text = this.getText(); }
  46. this.render(this.typesetter(text));
  47. },
  48. setLabel: function(/* String */text){
  49. // summary:
  50. // Sets the text of the label. Not called directly. Should
  51. // be called within Stencil. See stencil._Base
  52. //
  53. // onTransform will pass an object here
  54. var x, y, box = this.master.getBounds();
  55. if(/B/.test(this.labelPosition)){
  56. y = box.y2 - this._lineHeight;
  57. }else{
  58. y = box.y1;
  59. }
  60. if(/R/.test(this.labelPosition)){
  61. x = box.x2;
  62. }else{
  63. y = box.y1;
  64. this._align = "end";
  65. }
  66. if(!this.labelWidth || (text && text.split && text != this.getText())){
  67. this.setData({
  68. x:x,
  69. y:y,
  70. height:this._lineHeight,
  71. width:this.style.text.minWidth
  72. });
  73. this.labelWidth = this.style.text.minWidth;
  74. this.render(this.typesetter(text));
  75. }else{
  76. this.setData({
  77. x:x,
  78. y:y,
  79. height:this.data.height,
  80. width:this.data.width
  81. });
  82. this.render();
  83. }
  84. },
  85. beforeAttr: function(key, value){
  86. if(value!==undefined){
  87. // make it an object
  88. var k = key; key = {}; key[k] = value;
  89. }
  90. delete key.x;
  91. delete key.y;
  92. delete key.width;
  93. delete key.height;
  94. this.attr(key);
  95. // FIXME: this.created should already be set, shouldn't it?
  96. !this.created && this.render();
  97. }
  98. }
  99. );
  100. });