123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- "use strict";
- var Util = require('../util/common');
- var _require = require('../graphic/index'),
- Group = _require.Group;
- var TextBox = /*#__PURE__*/function () {
- var _proto = TextBox.prototype;
- _proto.getDefaultCfg = function getDefaultCfg() {
- return {
- x: 0,
- y: 0,
- content: '',
- textStyle: {
- fontSize: 12,
- fill: '#fff',
- textAlign: 'center',
- textBaseline: 'middle'
- },
- background: {
- radius: 1,
- fill: 'rgba(0, 0, 0, 0.65)',
- padding: [3, 5]
- },
- width: 0,
- height: 0,
- className: ''
- };
- };
- function TextBox(cfg) {
- Util.deepMix(this, this.getDefaultCfg(), cfg);
- this._init();
- var content = this.content,
- x = this.x,
- y = this.y;
- if (!Util.isNil(content)) {
- this.updateContent(content);
- }
- this.updatePosition(x, y);
- }
- _proto._init = function _init() {
- var content = this.content,
- textStyle = this.textStyle,
- background = this.background,
- className = this.className,
- visible = this.visible;
- var container = new Group({
- className: className,
- zIndex: 0,
- visible: visible
- });
- var text = container.addShape('Text', {
- className: className + '-text',
- zIndex: 1,
- attrs: Util.mix({
- text: content,
- x: 0,
- y: 0
- }, textStyle)
- });
- var backgroundShape = container.addShape('Rect', {
- className: className + '-bg',
- zIndex: -1,
- attrs: Util.mix({
- x: 0,
- y: 0,
- width: 0,
- height: 0
- }, background)
- });
- container.sort();
- this.container = container;
- this.textShape = text;
- this.backgroundShape = backgroundShape;
- };
- _proto._getBBox = function _getBBox() {
- var textShape = this.textShape;
- var background = this.background;
- var textBBox = textShape.getBBox();
- var padding = Util.parsePadding(background.padding);
- var width = textBBox.width + padding[1] + padding[3];
- var height = textBBox.height + padding[0] + padding[2];
- var x = textBBox.minX - padding[3];
- var y = textBBox.minY - padding[0];
- return {
- x: x,
- y: y,
- width: width,
- height: height
- };
- };
- _proto.updateContent = function updateContent(text) {
- var textShape = this.textShape,
- backgroundShape = this.backgroundShape;
- if (!Util.isNil(text)) {
- if (!Util.isObject(text)) {
- text = {
- text: text
- };
- }
- textShape.attr(text); // update box shape
- var _this$_getBBox = this._getBBox(),
- x = _this$_getBBox.x,
- y = _this$_getBBox.y,
- tipWidth = _this$_getBBox.width,
- tipHeight = _this$_getBBox.height;
- var width = this.width || tipWidth;
- var height = this.height || tipHeight;
- backgroundShape.attr({
- x: x,
- y: y,
- width: width,
- height: height
- });
- this._width = width;
- this._height = height;
- this.content = text.text;
- }
- };
- _proto.updatePosition = function updatePosition(x, y) {
- var container = this.container;
- var _this$_getBBox2 = this._getBBox(),
- xMin = _this$_getBBox2.x,
- yMin = _this$_getBBox2.y;
- container.moveTo(x - xMin, y - yMin);
- this.x = x - xMin;
- this.y = y - yMin;
- };
- _proto.getWidth = function getWidth() {
- return this._width;
- };
- _proto.getHeight = function getHeight() {
- return this._height;
- };
- _proto.show = function show() {
- this.container.show();
- };
- _proto.hide = function hide() {
- this.container.hide();
- };
- _proto.clear = function clear() {
- var container = this.container;
- container.clear();
- container.remove(true);
- this.container = null;
- this.textShape = null;
- this.backgroundShape = null;
- };
- return TextBox;
- }();
- module.exports = TextBox;
|