Label.js 3.2 KB

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