index.js 4.7 KB

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