1 | import { __assign, __extends } from "tslib";
|
2 | import Action from '../base';
|
3 | import { getCurrentElement, getElementValue, getElementsByField } from '../util';
|
4 | import { deepMix, each, isFunction } from '@antv/util';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var LinkByColor = (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 |
|
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 |
|
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 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
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 |
|
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));
|
155 | export default LinkByColor;
|
156 |
|
\ | No newline at end of file |