layout.js 4.0 KB

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