text-box.js 3.8 KB

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