UNPKG

5.26 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import Action from '../base';
3import { getCurrentElement, getElementValue, getElementsByField } from '../util';
4import { deepMix, each, isFunction } from '@antv/util';
5/**
6 * Link Elements by color
7 *
8 * public 方法是对外可用的反馈交互。使用方式,如:element-link-by-color:link, element-link-by-color:unlink, element-link-by-color:clear
9 */
10var LinkByColor = /** @class */ (function (_super) {
11 __extends(LinkByColor, _super);
12 function LinkByColor() {
13 var _this = _super !== null && _super.apply(this, arguments) || this;
14 _this.cache = {};
15 return _this;
16 }
17 // 获取颜色对应的 scale
18 LinkByColor.prototype.getColorScale = function (view, element) {
19 var colorAttr = element.geometry.getAttribute('color');
20 if (!colorAttr) {
21 return null;
22 }
23 var scale = view.getScaleByField(colorAttr.getFields()[0]);
24 return scale;
25 };
26 // 获取连接的 path
27 LinkByColor.prototype.getLinkPath = function (element, nextElement) {
28 var view = this.context.view;
29 var isTransposed = view.getCoordinate().isTransposed;
30 var bbox = element.shape.getCanvasBBox();
31 var nextBBox = nextElement.shape.getCanvasBBox();
32 var path = isTransposed
33 ? [
34 ['M', bbox.minX, bbox.minY],
35 ['L', nextBBox.minX, nextBBox.maxY],
36 ['L', nextBBox.maxX, nextBBox.maxY],
37 ['L', bbox.maxX, bbox.minY],
38 ['Z'],
39 ]
40 : [
41 ['M', bbox.maxX, bbox.minY],
42 ['L', nextBBox.minX, nextBBox.minY],
43 ['L', nextBBox.minX, nextBBox.maxY],
44 ['L', bbox.maxX, bbox.maxY],
45 ['Z'],
46 ];
47 return path;
48 };
49 // 添加连接的图形
50 LinkByColor.prototype.addLinkShape = function (group, element, nextElement, activeStyle) {
51 var style = {
52 opacity: 0.4,
53 fill: element.shape.attr('fill'),
54 };
55 group.addShape({
56 type: 'path',
57 attrs: __assign(__assign({}, deepMix({}, style, isFunction(activeStyle) ? activeStyle(style, element) : activeStyle)), { path: this.getLinkPath(element, nextElement) }),
58 });
59 };
60 // 使用图形连接
61 LinkByColor.prototype.linkByElement = function (element, activeStyle) {
62 var _this = this;
63 var view = this.context.view;
64 var scale = this.getColorScale(view, element);
65 if (!scale) {
66 return;
67 }
68 var value = getElementValue(element, scale.field);
69 if (!this.cache[value]) {
70 var elements_1 = getElementsByField(view, scale.field, value);
71 var linkGroup = this.linkGroup;
72 var group_1 = linkGroup.addGroup();
73 this.cache[value] = group_1; // 缓存
74 var count_1 = elements_1.length;
75 each(elements_1, function (el, index) {
76 if (index < count_1 - 1) {
77 var nextEl = elements_1[index + 1];
78 _this.addLinkShape(group_1, el, nextEl, activeStyle);
79 }
80 });
81 }
82 };
83 // 移除连接
84 LinkByColor.prototype.removeLink = function (element) {
85 var scale = this.getColorScale(this.context.view, element);
86 if (!scale) {
87 return;
88 }
89 var value = getElementValue(element, scale.field);
90 if (this.cache[value]) {
91 this.cache[value].remove();
92 this.cache[value] = null;
93 }
94 };
95 /**
96 * 连接 elements
97 *
98 * @usage
99 * registerInteraction('xxx', {
100 * start: [
101 * {
102 * trigger: 'interval:mouseenter',
103 * action: 'element-link-by-color:link',
104 * arg: {
105 * // style: { fill: 'red' }
106 * style: (style, element) => ({ fill: 'red' })
107 * },
108 * },
109 * ],
110 * });
111 */
112 LinkByColor.prototype.link = function (args) {
113 var context = this.context;
114 if (!this.linkGroup) {
115 // 不允许被拾取
116 this.linkGroup = context.view.foregroundGroup.addGroup({
117 id: 'link-by-color-group',
118 capture: false,
119 });
120 }
121 var element = getCurrentElement(context);
122 if (element) {
123 this.linkByElement(element, args === null || args === void 0 ? void 0 : args.style);
124 }
125 };
126 /**
127 * 取消连接 elements
128 */
129 LinkByColor.prototype.unlink = function () {
130 var element = getCurrentElement(this.context);
131 if (element) {
132 this.removeLink(element);
133 }
134 };
135 /**
136 * 清除所有连接
137 */
138 LinkByColor.prototype.clear = function () {
139 if (this.linkGroup) {
140 this.linkGroup.clear();
141 }
142 this.cache = {};
143 };
144 /**
145 * 销毁
146 */
147 LinkByColor.prototype.destroy = function () {
148 _super.prototype.destroy.call(this);
149 if (this.linkGroup) {
150 this.linkGroup.remove();
151 }
152 };
153 return LinkByColor;
154}(Action));
155export default LinkByColor;
156//# sourceMappingURL=link-by-color.js.map
\No newline at end of file