group.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var g_base_1 = require("@antv/g-base");
  5. var util_1 = require("@antv/util");
  6. var Shape = require("./shape");
  7. var draw_1 = require("./util/draw");
  8. var svg_1 = require("./util/svg");
  9. var constant_1 = require("./constant");
  10. var dom_1 = require("./util/dom");
  11. var Group = /** @class */ (function (_super) {
  12. tslib_1.__extends(Group, _super);
  13. function Group() {
  14. return _super !== null && _super.apply(this, arguments) || this;
  15. }
  16. // SVG 中分组对应实体标签 <g>
  17. Group.prototype.isEntityGroup = function () {
  18. return true;
  19. };
  20. Group.prototype.createDom = function () {
  21. var element = dom_1.createSVGElement('g');
  22. this.set('el', element);
  23. var parent = this.getParent();
  24. if (parent) {
  25. var parentNode = parent.get('el');
  26. if (parentNode) {
  27. parentNode.appendChild(element);
  28. }
  29. else {
  30. // parentNode maybe null for group
  31. parentNode = parent.createDom();
  32. parent.set('el', parentNode);
  33. parentNode.appendChild(element);
  34. }
  35. }
  36. return element;
  37. };
  38. // 覆盖基类的 afterAttrsChange 方法
  39. Group.prototype.afterAttrsChange = function (targetAttrs) {
  40. _super.prototype.afterAttrsChange.call(this, targetAttrs);
  41. var canvas = this.get('canvas');
  42. // 只有挂载到画布下,才对元素进行实际渲染
  43. if (canvas && canvas.get('autoDraw')) {
  44. var context = canvas.get('context');
  45. this.createPath(context, targetAttrs);
  46. }
  47. };
  48. /**
  49. * 一些方法调用会引起画布变化
  50. * @param {ChangeType} changeType 改变的类型
  51. */
  52. Group.prototype.onCanvasChange = function (changeType) {
  53. draw_1.refreshElement(this, changeType);
  54. };
  55. Group.prototype.getShapeBase = function () {
  56. return Shape;
  57. };
  58. Group.prototype.getGroupBase = function () {
  59. return Group;
  60. };
  61. Group.prototype.draw = function (context) {
  62. var children = this.getChildren();
  63. var el = this.get('el');
  64. if (this.get('destroyed')) {
  65. if (el) {
  66. el.parentNode.removeChild(el);
  67. }
  68. }
  69. else {
  70. if (!el) {
  71. this.createDom();
  72. }
  73. svg_1.setClip(this, context);
  74. this.createPath(context);
  75. if (children.length) {
  76. draw_1.drawChildren(context, children);
  77. }
  78. }
  79. };
  80. /**
  81. * 绘制分组的路径
  82. * @param {Defs} context 上下文
  83. * @param {ShapeAttrs} targetAttrs 渲染的目标属性
  84. */
  85. Group.prototype.createPath = function (context, targetAttrs) {
  86. var attrs = this.attr();
  87. var el = this.get('el');
  88. util_1.each(targetAttrs || attrs, function (value, attr) {
  89. if (constant_1.SVG_ATTR_MAP[attr]) {
  90. el.setAttribute(constant_1.SVG_ATTR_MAP[attr], value);
  91. }
  92. });
  93. svg_1.setTransform(this);
  94. };
  95. return Group;
  96. }(g_base_1.AbstractGroup));
  97. exports.default = Group;
  98. //# sourceMappingURL=group.js.map