reset-button.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { __assign, __extends } from "tslib";
  2. import { Action, Util } from '@antv/g2';
  3. import { get } from '@antv/util';
  4. import { deepAssign, normalPadding } from '../../utils';
  5. var PADDING_RIGHT = 10;
  6. var PADDING_TOP = 5;
  7. /**
  8. * Action 中的 Button 按钮配置
  9. *
  10. * 可能的使用场景:brush filter
  11. */
  12. export var BUTTON_ACTION_CONFIG = {
  13. padding: [8, 10],
  14. text: 'reset',
  15. textStyle: {
  16. default: {
  17. x: 0,
  18. y: 0,
  19. fontSize: 12,
  20. fill: '#333333',
  21. cursor: 'pointer',
  22. },
  23. },
  24. buttonStyle: {
  25. default: {
  26. fill: '#f7f7f7',
  27. stroke: '#cccccc',
  28. cursor: 'pointer',
  29. },
  30. active: {
  31. fill: '#e6e6e6',
  32. },
  33. },
  34. };
  35. /**
  36. * @override 复写 G2 Button Action, 后续直接使用 GUI
  37. */
  38. var ButtonAction = /** @class */ (function (_super) {
  39. __extends(ButtonAction, _super);
  40. function ButtonAction() {
  41. var _this = _super !== null && _super.apply(this, arguments) || this;
  42. _this.buttonGroup = null;
  43. _this.buttonCfg = __assign({ name: 'button' }, BUTTON_ACTION_CONFIG);
  44. return _this;
  45. }
  46. /**
  47. * 获取 mix 默认的配置和用户配置
  48. */
  49. ButtonAction.prototype.getButtonCfg = function () {
  50. var view = this.context.view;
  51. var buttonCfg = get(view, ['interactions', 'filter-action', 'cfg', 'buttonConfig']);
  52. return deepAssign(this.buttonCfg, buttonCfg, this.cfg);
  53. };
  54. /**
  55. * 绘制 Button 和 文本
  56. */
  57. ButtonAction.prototype.drawButton = function () {
  58. var config = this.getButtonCfg();
  59. var group = this.context.view.foregroundGroup.addGroup({
  60. name: config.name,
  61. });
  62. var textShape = this.drawText(group);
  63. this.drawBackground(group, textShape.getBBox());
  64. this.buttonGroup = group;
  65. };
  66. /**
  67. * 绘制文本
  68. */
  69. ButtonAction.prototype.drawText = function (group) {
  70. var _a;
  71. var config = this.getButtonCfg();
  72. // 添加文本
  73. return group.addShape({
  74. type: 'text',
  75. name: 'button-text',
  76. attrs: __assign({ text: config.text }, (_a = config.textStyle) === null || _a === void 0 ? void 0 : _a.default),
  77. });
  78. };
  79. ButtonAction.prototype.drawBackground = function (group, bbox) {
  80. var _a;
  81. var config = this.getButtonCfg();
  82. var padding = normalPadding(config.padding);
  83. // 添加背景按钮
  84. var buttonShape = group.addShape({
  85. type: 'rect',
  86. name: 'button-rect',
  87. attrs: __assign({ x: bbox.x - padding[3], y: bbox.y - padding[0], width: bbox.width + padding[1] + padding[3], height: bbox.height + padding[0] + padding[2] }, (_a = config.buttonStyle) === null || _a === void 0 ? void 0 : _a.default),
  88. });
  89. buttonShape.toBack(); // 在后面
  90. // active 效果内置
  91. group.on('mouseenter', function () {
  92. var _a;
  93. buttonShape.attr((_a = config.buttonStyle) === null || _a === void 0 ? void 0 : _a.active);
  94. });
  95. group.on('mouseleave', function () {
  96. var _a;
  97. buttonShape.attr((_a = config.buttonStyle) === null || _a === void 0 ? void 0 : _a.default);
  98. });
  99. return buttonShape;
  100. };
  101. // 重置位置
  102. ButtonAction.prototype.resetPosition = function () {
  103. var view = this.context.view;
  104. var coord = view.getCoordinate();
  105. var point = coord.convert({ x: 1, y: 1 }); // 后面直接改成左上角
  106. var buttonGroup = this.buttonGroup;
  107. var bbox = buttonGroup.getBBox();
  108. var matrix = Util.transform(null, [
  109. ['t', point.x - bbox.width - PADDING_RIGHT, point.y + bbox.height + PADDING_TOP],
  110. ]);
  111. buttonGroup.setMatrix(matrix);
  112. };
  113. /**
  114. * 显示
  115. */
  116. ButtonAction.prototype.show = function () {
  117. if (!this.buttonGroup) {
  118. this.drawButton();
  119. }
  120. this.resetPosition();
  121. this.buttonGroup.show();
  122. };
  123. /**
  124. * 隐藏
  125. */
  126. ButtonAction.prototype.hide = function () {
  127. if (this.buttonGroup) {
  128. this.buttonGroup.hide();
  129. }
  130. };
  131. /**
  132. * 销毁
  133. */
  134. ButtonAction.prototype.destroy = function () {
  135. var buttonGroup = this.buttonGroup;
  136. if (buttonGroup) {
  137. buttonGroup.remove();
  138. }
  139. _super.prototype.destroy.call(this);
  140. };
  141. return ButtonAction;
  142. }(Action));
  143. export { ButtonAction };
  144. //# sourceMappingURL=reset-button.js.map