index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { __assign, __extends, __read } from "tslib";
  2. import { deepMix, isUndefined } from '@antv/util';
  3. import { GUI } from '../../core';
  4. import { maybeAppend, parseSeriesAttr, select, subStyleProps } from '../../util';
  5. import { Marker } from '../marker';
  6. import { DISABLED_STYLE, SIZE_STYLE, TYPE_STYLE } from './constant';
  7. var Button = /** @class */ (function (_super) {
  8. __extends(Button, _super);
  9. function Button(options) {
  10. var _this = _super.call(this, options, {
  11. cursor: 'pointer',
  12. padding: 10,
  13. size: 'middle',
  14. type: 'default',
  15. text: '',
  16. state: 'default',
  17. markerAlign: 'left',
  18. markerSpacing: 5,
  19. default: {
  20. buttonLineWidth: 1,
  21. buttonRadius: 5,
  22. },
  23. active: {},
  24. }) || this;
  25. _this.state = 'default';
  26. _this.clickEvents = function () {
  27. var _a = _this.attributes, onClick = _a.onClick, state = _a.state;
  28. // 点击事件
  29. if (state !== 'disabled')
  30. onClick === null || onClick === void 0 ? void 0 : onClick.call(_this, _this);
  31. };
  32. _this.mouseenterEvent = function () {
  33. var state = _this.attributes.state;
  34. if (state !== 'disabled') {
  35. _this.state = 'active';
  36. _this.render(_this.attributes, _this);
  37. }
  38. };
  39. _this.mouseleaveEvent = function () {
  40. var state = _this.attributes.state;
  41. _this.state = state;
  42. _this.render(_this.attributes, _this);
  43. };
  44. return _this;
  45. }
  46. Object.defineProperty(Button.prototype, "markerSize", {
  47. get: function () {
  48. var markerSymbol = this.attributes.markerSymbol;
  49. var markerStyle = this.getStyle('marker');
  50. var markerSize = !markerSymbol ? 0 : (markerStyle === null || markerStyle === void 0 ? void 0 : markerStyle.size) || 2;
  51. return markerSize;
  52. },
  53. enumerable: false,
  54. configurable: true
  55. });
  56. Object.defineProperty(Button.prototype, "textAvailableWidth", {
  57. /* 获得文本可用宽度 */
  58. get: function () {
  59. var _a = this.attributes, markerSymbol = _a.markerSymbol, padding = _a.padding, ellipsis = _a.ellipsis, bWidth = _a.width, spacing = _a.markerSpacing;
  60. if (!ellipsis)
  61. return Infinity;
  62. /* 按钮总宽度 */
  63. var width = (isUndefined(bWidth) ? this.getStyle('button').width : bWidth);
  64. if (markerSymbol)
  65. return width - padding * 2 - spacing - this.markerSize;
  66. return width - padding * 2;
  67. },
  68. enumerable: false,
  69. configurable: true
  70. });
  71. Object.defineProperty(Button.prototype, "buttonHeight", {
  72. get: function () {
  73. var height = this.attributes.height;
  74. if (height)
  75. return +height;
  76. return +this.getStyle('button').height;
  77. },
  78. enumerable: false,
  79. configurable: true
  80. });
  81. /**
  82. * 根据size、type属性生成实际渲染的属性
  83. */
  84. Button.prototype.getStyle = function (name) {
  85. var _a = this.attributes, size = _a.size, type = _a.type;
  86. var state = this.state;
  87. var mixedStyle = deepMix({}, SIZE_STYLE[size], TYPE_STYLE[type][state], this.attributes.default, this.attributes[state]);
  88. if (state === 'disabled') {
  89. // 从DISABLED_STYLE中pick中pick mixedStyle里已有的style
  90. Object.keys(mixedStyle).forEach(function (key) {
  91. if (key in DISABLED_STYLE) {
  92. // @ts-ignore
  93. mixedStyle[key] = DISABLED_STYLE[key];
  94. }
  95. });
  96. Object.keys(DISABLED_STYLE.strict).forEach(function (key) {
  97. // @ts-ignore
  98. mixedStyle[key] = DISABLED_STYLE.strict[key];
  99. });
  100. deepMix(mixedStyle, this.attributes.disabled || {});
  101. }
  102. return subStyleProps(mixedStyle, name);
  103. };
  104. // @todo 处理 markerAlign='right' 的场景. 方案: left marker & right marker 处理为两个 shape, 互相不干扰
  105. Button.prototype.render = function (attributes, container) {
  106. var _a = attributes.text, text = _a === void 0 ? '' : _a, _b = attributes.padding, padding = _b === void 0 ? 0 : _b, markerSymbol = attributes.markerSymbol, _c = attributes.markerSpacing, markerSpacing = _c === void 0 ? 0 : _c;
  107. container.attr('cursor', this.state === 'disabled' ? 'not-allowed' : 'pointer');
  108. var _d = __read(parseSeriesAttr(padding), 4), pt = _d[0], pr = _d[1], pb = _d[2], pl = _d[3];
  109. var height = this.buttonHeight;
  110. var markerStyle = this.getStyle('marker');
  111. var markerSize = this.markerSize;
  112. var style = __assign(__assign({}, markerStyle), { symbol: markerSymbol, x: pl + markerSize / 2, y: height / 2, size: markerSize });
  113. var markerShape = maybeAppend(container, '.marker', function () { return new Marker({ className: 'marker', style: style }); })
  114. .update({ style: style })
  115. .node();
  116. var bounds = markerShape.getLocalBounds();
  117. var textStyle = this.getStyle('text');
  118. this.textShape = maybeAppend(container, '.text', 'text')
  119. .attr('className', 'text')
  120. .styles(__assign(__assign({ x: markerSize ? bounds.max[0] + markerSpacing : pl, y: height / 2 }, textStyle), { text: text, textAlign: 'left', textBaseline: 'middle', wordWrap: true, wordWrapWidth: this.textAvailableWidth, maxLines: 1, textOverflow: '...' }))
  121. .node();
  122. var textBounds = this.textShape.getLocalBounds();
  123. var buttonStyle = this.getStyle('button');
  124. select(container)
  125. .maybeAppendByClassName('.background', 'rect')
  126. .styles(__assign(__assign({ zIndex: -1 }, buttonStyle), { height: height, width: pl + (markerSize ? markerSize + markerSpacing : 0) + textBounds.halfExtents[0] * 2 + pr }));
  127. };
  128. /**
  129. * 组件的更新
  130. */
  131. Button.prototype.update = function (attr) {
  132. if (attr === void 0) { attr = {}; }
  133. this.attr(deepMix({}, this.attributes, attr));
  134. var state = this.attributes.state;
  135. // 更新状态
  136. this.state = state;
  137. this.render(this.attributes, this);
  138. };
  139. /** 更新状态 (不需要走 update) */
  140. Button.prototype.setState = function (state) {
  141. this.update({ state: state });
  142. };
  143. Button.prototype.hide = function () {
  144. // @ts-ignore
  145. this.style.visibility = 'hidden';
  146. };
  147. Button.prototype.show = function () {
  148. // @ts-ignore
  149. this.style.visibility = 'visible';
  150. };
  151. Button.prototype.bindEvents = function () {
  152. this.addEventListener('click', this.clickEvents);
  153. this.addEventListener('mouseenter', this.mouseenterEvent);
  154. this.addEventListener('mouseleave', this.mouseleaveEvent);
  155. };
  156. /**
  157. * 组件类型
  158. */
  159. Button.tag = 'button';
  160. return Button;
  161. }(GUI));
  162. export { Button };
  163. //# sourceMappingURL=index.js.map