index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Breadcrumb = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("@antv/util");
  6. var core_1 = require("../../core");
  7. var shapes_1 = require("../../shapes");
  8. var util_2 = require("../../util");
  9. var tag_1 = require("../tag");
  10. var Breadcrumb = /** @class */ (function (_super) {
  11. tslib_1.__extends(Breadcrumb, _super);
  12. /**
  13. *
  14. * @param options
  15. */
  16. function Breadcrumb(options) {
  17. return _super.call(this, (0, util_1.deepMix)({}, Breadcrumb.defaultOptions, options)) || this;
  18. }
  19. Breadcrumb.prototype.render = function (attributes, container) {
  20. var x = attributes.x, y = attributes.y, items = attributes.items, textStyle = attributes.textStyle, _a = attributes.padding, padding = _a === void 0 ? 0 : _a, width = attributes.width, separator = attributes.separator;
  21. var _b = tslib_1.__read((0, util_2.parseSeriesAttr)(padding), 3), top = _b[0], right = _b[1], left = _b[2];
  22. var tagStyle = (0, util_2.subStyleProps)(attributes, 'tag');
  23. var selection = (0, util_2.maybeAppend)(container, '.container', 'g').styles({
  24. className: 'container',
  25. x: x + left,
  26. y: y + top,
  27. });
  28. var cursorX = 0;
  29. var cursorY = 0;
  30. selection.node().removeChildren();
  31. var _loop_1 = function (i) {
  32. var datum = items[i];
  33. var shape = new tag_1.Tag({
  34. className: 'breadcrumb-item',
  35. style: tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({ x: cursorX, y: cursorY }, tagStyle), { text: (0, util_1.isNil)(datum.text) ? datum.id : datum.text }), (0, util_1.pick)(datum, ['marker'])), {
  36. // 强制不需要背景
  37. padding: 0 }),
  38. });
  39. selection.append(function () { return shape; });
  40. var bounds = shape.getLocalBounds();
  41. var shapeW = bounds.halfExtents[0] * 2;
  42. var shapeH = bounds.halfExtents[1] * 2;
  43. cursorX += shapeW;
  44. // todo 换行策略还需要考虑 分隔符
  45. if (!(0, util_1.isNil)(width)) {
  46. var avaliableWidth = width - right;
  47. if (cursorX > avaliableWidth) {
  48. shape.attr({ x: 0, y: cursorY + shapeH });
  49. // 更新光标
  50. cursorX = shapeW;
  51. cursorY += shapeH;
  52. }
  53. }
  54. // 绑定事件
  55. this_1.bindInnerEvents(shape, datum);
  56. var _c = separator || {}, _d = _c.spacing, spacing = _d === void 0 ? 0 : _d, _e = _c.text, text = _e === void 0 ? '/' : _e, style = _c.style;
  57. // 最后一个分隔符,不需要渲染
  58. if (i !== items.length - 1) {
  59. var shape_1 = new shapes_1.Text({
  60. className: "".concat(Breadcrumb.tag, "-separator"),
  61. id: "".concat(Breadcrumb.tag, "-separator-").concat(i),
  62. style: tslib_1.__assign(tslib_1.__assign({ x: cursorX + spacing, y: cursorY + shapeH / 2 }, style), { text: text, textAlign: 'end', textBaseline: 'middle' }),
  63. });
  64. selection.append(function () { return shape_1; });
  65. var bounds_1 = shape_1.getLocalBounds();
  66. cursorX += bounds_1.halfExtents[0] * 2 + spacing;
  67. }
  68. };
  69. var this_1 = this;
  70. for (var i = 0; i < items.length; i++) {
  71. _loop_1(i);
  72. }
  73. };
  74. /**
  75. * 组件更新
  76. * @param cfg
  77. */
  78. Breadcrumb.prototype.update = function (cfg) {
  79. this.attr((0, util_1.deepMix)({}, this.attributes, cfg));
  80. this.render(this.attributes, this);
  81. };
  82. /**
  83. * 面包屑绑定事件
  84. * @param shape
  85. * @param item
  86. */
  87. Breadcrumb.prototype.bindInnerEvents = function (shape, item) {
  88. var _a = this.attributes, items = _a.items, onClick = _a.onClick;
  89. if (onClick) {
  90. shape.addEventListener('click', function () {
  91. onClick.call(shape, item.id, item, items);
  92. });
  93. }
  94. };
  95. /**
  96. * 标签类型
  97. */
  98. Breadcrumb.tag = 'breadcrumb';
  99. /**
  100. * 默认参数
  101. */
  102. Breadcrumb.defaultOptions = {
  103. style: {
  104. separator: {
  105. text: '/',
  106. style: {
  107. fontSize: 14,
  108. fill: 'rgba(0, 0, 0, 0.45)',
  109. },
  110. spacing: 8,
  111. },
  112. textStyle: {
  113. default: {
  114. fontSize: 14,
  115. fill: 'rgba(0, 0, 0, 0.45)',
  116. },
  117. active: {
  118. fill: '#5468ff',
  119. cursor: 'pointer',
  120. },
  121. },
  122. padding: [8, 8, 8, 8],
  123. },
  124. };
  125. return Breadcrumb;
  126. }(core_1.GUI));
  127. exports.Breadcrumb = Breadcrumb;
  128. //# sourceMappingURL=index.js.map