reset-button.js 4.7 KB

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