index.js 7.3 KB

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