layout.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { __extends, __read } from "tslib";
  2. import { ElementEvent } from '@antv/g';
  3. import { Group } from '../../shapes';
  4. import { BBox } from '../../util';
  5. import { calcLayout } from '../../util/layout';
  6. import { parseSeriesAttr } from '../../util/series';
  7. var Layout = /** @class */ (function (_super) {
  8. __extends(Layout, _super);
  9. function Layout(options) {
  10. var _this = _super.call(this, options) || this;
  11. _this.layoutEvents = [ElementEvent.BOUNDS_CHANGED, ElementEvent.INSERTED, ElementEvent.REMOVED];
  12. _this.$margin = parseSeriesAttr(0);
  13. _this.$padding = parseSeriesAttr(0);
  14. var _a = options.style || {}, _b = _a.margin, margin = _b === void 0 ? 0 : _b, _c = _a.padding, padding = _c === void 0 ? 0 : _c;
  15. _this.margin = margin;
  16. _this.padding = padding;
  17. _this.isMutationObserved = true;
  18. _this.bindEvents();
  19. return _this;
  20. }
  21. Object.defineProperty(Layout.prototype, "margin", {
  22. get: function () {
  23. return this.$margin;
  24. },
  25. set: function (value) {
  26. this.$margin = parseSeriesAttr(value);
  27. },
  28. enumerable: false,
  29. configurable: true
  30. });
  31. Object.defineProperty(Layout.prototype, "padding", {
  32. get: function () {
  33. return this.$padding;
  34. },
  35. set: function (value) {
  36. this.$padding = parseSeriesAttr(value);
  37. },
  38. enumerable: false,
  39. configurable: true
  40. });
  41. Layout.prototype.getBBox = function () {
  42. var _a = this.attributes, _b = _a.x, x = _b === void 0 ? 0 : _b, _c = _a.y, y = _c === void 0 ? 0 : _c, width = _a.width, height = _a.height;
  43. var _d = __read(this.$margin, 4), marginTop = _d[0], marginRight = _d[1], marginBottom = _d[2], marginLeft = _d[3];
  44. return new BBox(x - marginLeft, y - marginTop, width + marginLeft + marginRight, height + marginTop + marginBottom);
  45. };
  46. Layout.prototype.appendChild = function (child, index) {
  47. child.isMutationObserved = true;
  48. _super.prototype.appendChild.call(this, child, index);
  49. return child;
  50. };
  51. Layout.prototype.getAvailableSpace = function () {
  52. var _a = this.attributes, width = _a.width, height = _a.height;
  53. var _b = __read(this.$padding, 4), paddingTop = _b[0], paddingRight = _b[1], paddingBottom = _b[2], paddingLeft = _b[3];
  54. var _c = __read(this.$margin, 4), marginTop = _c[0], marginLeft = _c[3];
  55. return new BBox(paddingLeft + marginLeft, paddingTop + marginTop, width - paddingLeft - paddingRight, height - paddingTop - paddingBottom);
  56. };
  57. Layout.prototype.layout = function () {
  58. if (!this.attributes.display || !this.isConnected)
  59. return;
  60. if (this.children.some(function (child) { return !child.isConnected; }))
  61. return;
  62. try {
  63. var bboxes_1 = calcLayout(this.getAvailableSpace(), this.children.map(function (child) { return child.getBBox(); }), this.attributes);
  64. this.children.forEach(function (child, index) {
  65. var _a = bboxes_1[index], x = _a.x, y = _a.y;
  66. child.attr({ x: x, y: y });
  67. });
  68. }
  69. catch (e) {
  70. // do nothing
  71. }
  72. };
  73. Layout.prototype.bindEvents = function () {
  74. var _this = this;
  75. this.layoutEvents.forEach(function (event) {
  76. _this.addEventListener(event, function (e) {
  77. e.target.isMutationObserved = true;
  78. _this.layout();
  79. });
  80. });
  81. };
  82. Layout.prototype.attributeChangedCallback = function (name, oldValue, newValue) {
  83. if (name === 'margin')
  84. this.margin = newValue;
  85. else if (name === 'padding')
  86. this.padding = newValue;
  87. this.layout();
  88. };
  89. return Layout;
  90. }(Group));
  91. export { Layout };
  92. //# sourceMappingURL=layout.js.map