text-box.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../util/common");
  5. var _index = require("../graphic/index");
  6. var TextBox = /*#__PURE__*/function () {
  7. var _proto = TextBox.prototype;
  8. _proto.getDefaultCfg = function getDefaultCfg() {
  9. return {
  10. x: 0,
  11. y: 0,
  12. content: '',
  13. textStyle: {
  14. fontSize: 12,
  15. fill: '#fff',
  16. textAlign: 'center',
  17. textBaseline: 'middle',
  18. fontFamily: 'Arial'
  19. },
  20. background: {
  21. radius: 1,
  22. fill: 'rgba(0, 0, 0, 0.65)',
  23. padding: [3, 5]
  24. },
  25. width: 0,
  26. height: 0,
  27. className: ''
  28. };
  29. };
  30. function TextBox(cfg) {
  31. (0, _common.deepMix)(this, this.getDefaultCfg(), cfg);
  32. this._init();
  33. var content = this.content,
  34. x = this.x,
  35. y = this.y;
  36. if (!(0, _common.isNil)(content)) {
  37. this.updateContent(content);
  38. }
  39. this.updatePosition(x, y);
  40. }
  41. _proto._init = function _init() {
  42. var content = this.content,
  43. textStyle = this.textStyle,
  44. background = this.background,
  45. className = this.className,
  46. visible = this.visible,
  47. context = this.context;
  48. var container = new _index.Group({
  49. context: context,
  50. className: className,
  51. zIndex: 0,
  52. visible: visible
  53. });
  54. var text = container.addShape('Text', {
  55. className: className + '-text',
  56. zIndex: 1,
  57. attrs: (0, _common.mix)({
  58. text: content,
  59. x: 0,
  60. y: 0
  61. }, textStyle)
  62. });
  63. var backgroundShape = container.addShape('Rect', {
  64. className: className + '-bg',
  65. zIndex: -1,
  66. attrs: (0, _common.mix)({
  67. x: 0,
  68. y: 0,
  69. width: 0,
  70. height: 0
  71. }, background)
  72. });
  73. container.sort();
  74. this.container = container;
  75. this.textShape = text;
  76. this.backgroundShape = backgroundShape;
  77. };
  78. _proto._getBBox = function _getBBox() {
  79. var textShape = this.textShape;
  80. var background = this.background;
  81. var textBBox = textShape.getBBox();
  82. var padding = (0, _common.parsePadding)(background.padding);
  83. var width = textBBox.width + padding[1] + padding[3];
  84. var height = textBBox.height + padding[0] + padding[2];
  85. var x = textBBox.minX - padding[3];
  86. var y = textBBox.minY - padding[0];
  87. return {
  88. x: x,
  89. y: y,
  90. width: width,
  91. height: height
  92. };
  93. };
  94. _proto.updateContent = function updateContent(text) {
  95. var textShape = this.textShape,
  96. backgroundShape = this.backgroundShape;
  97. if (!(0, _common.isNil)(text)) {
  98. if (!(0, _common.isObject)(text)) {
  99. text = {
  100. text: text
  101. };
  102. }
  103. textShape.attr(text); // update box shape
  104. var _this$_getBBox = this._getBBox(),
  105. x = _this$_getBBox.x,
  106. y = _this$_getBBox.y,
  107. tipWidth = _this$_getBBox.width,
  108. tipHeight = _this$_getBBox.height;
  109. var width = this.width || tipWidth;
  110. var height = this.height || tipHeight;
  111. backgroundShape.attr({
  112. x: x,
  113. y: y,
  114. width: width,
  115. height: height
  116. });
  117. this._width = width;
  118. this._height = height;
  119. this.content = text.text;
  120. }
  121. };
  122. _proto.updatePosition = function updatePosition(x, y) {
  123. var container = this.container;
  124. var _this$_getBBox2 = this._getBBox(),
  125. xMin = _this$_getBBox2.x,
  126. yMin = _this$_getBBox2.y;
  127. container.moveTo(x - xMin, y - yMin);
  128. this.x = x - xMin;
  129. this.y = y - yMin;
  130. };
  131. _proto.getWidth = function getWidth() {
  132. return this._width;
  133. };
  134. _proto.getHeight = function getHeight() {
  135. return this._height;
  136. };
  137. _proto.show = function show() {
  138. this.container.show();
  139. };
  140. _proto.hide = function hide() {
  141. this.container.hide();
  142. };
  143. _proto.clear = function clear() {
  144. var container = this.container;
  145. container.clear();
  146. container.remove(true);
  147. this.container = null;
  148. this.textShape = null;
  149. this.backgroundShape = null;
  150. };
  151. return TextBox;
  152. }();
  153. var _default = TextBox;
  154. exports["default"] = _default;