UNPKG

4.88 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { isFunction } from '@antv/util';
3import { Component, isEqual as equal } from '@antv/f-engine';
4function isEqual(origin1, origin2, fields) {
5 if (origin1 === origin2) {
6 return true;
7 }
8 for (var i = 0, len = fields.length; i < len; i++) {
9 var field = fields[i];
10 if (origin1[field] !== origin2[field]) {
11 return false;
12 }
13 }
14 return true;
15}
16var Selection = /** @class */function (_super) {
17 __extends(Selection, _super);
18 function Selection(props, context) {
19 var _this = _super.call(this, props, context) || this;
20 var selection = props.selection;
21 if (!selection) return _this;
22 var defaultSelected = selection.defaultSelected;
23 _this.state.selected = defaultSelected;
24 return _this;
25 }
26 Selection.prototype.didMount = function () {
27 var _this = this;
28 var _a = this,
29 props = _a.props,
30 state = _a.state;
31 var selection = props.selection,
32 chart = props.chart;
33 if (!selection) return;
34 // 默认为 click
35 var _b = selection.triggerOn,
36 triggerOn = _b === void 0 ? 'click' : _b;
37 chart.on(triggerOn, function (ev) {
38 var points = ev.points,
39 x = ev.canvasX,
40 y = ev.canvasY;
41 var point = triggerOn === 'click' ? {
42 x: x,
43 y: y
44 } : points[0];
45 var records = _this.getSnapRecords(point);
46 var _a = selection.type,
47 type = _a === void 0 ? 'single' : _a,
48 _b = selection.cancelable,
49 cancelable = _b === void 0 ? true : _b;
50 if (!records || !records.length) {
51 if (cancelable) {
52 _this.setState({
53 selected: null
54 });
55 }
56 return;
57 }
58 var selected = state.selected;
59 var origins = records.map(function (record) {
60 return record.origin;
61 });
62 if (!selected || !selected.length) {
63 _this.setState({
64 selected: origins
65 });
66 }
67 if (type === 'single') {
68 if (!cancelable) {
69 _this.setState({
70 selected: origins
71 });
72 return;
73 }
74 var newSelected_1 = [];
75 records.forEach(function (record) {
76 if (!_this.isSelected(record)) {
77 newSelected_1.push(record.origin);
78 }
79 });
80 _this.setState({
81 selected: newSelected_1
82 });
83 return;
84 }
85 // 多选
86 var scales = chart.getScales();
87 var fields = Object.keys(scales);
88 var selectedMap = {};
89 selected.forEach(function (item) {
90 var key = fields.map(function (field) {
91 return item[field];
92 }).join('-');
93 selectedMap[key] = item;
94 });
95 records.forEach(function (record) {
96 var origin = record.origin;
97 var key = fields.map(function (field) {
98 return origin[field];
99 }).join('-');
100 selectedMap[key] = selectedMap[key] ? null : origin;
101 });
102 var newSelected = Object.keys(selectedMap).map(function (key) {
103 return selectedMap[key];
104 }).filter(Boolean);
105 _this.setState({
106 selected: newSelected
107 });
108 });
109 };
110 Selection.prototype.willReceiveProps = function (nextProps) {
111 var nextSelection = nextProps.selection;
112 var lastSelection = this.props.selection;
113 if (!nextSelection || !lastSelection) {
114 return;
115 }
116 var nextDefaultSelected = nextSelection.defaultSelected;
117 var lastDefaultSelected = lastSelection.defaultSelected;
118 if (!equal(nextDefaultSelected, lastDefaultSelected)) {
119 this.state.selected = nextDefaultSelected;
120 }
121 };
122 Selection.prototype.getSnapRecords = function (_point) {
123 return null;
124 };
125 Selection.prototype.isSelected = function (record) {
126 var _a = this,
127 state = _a.state,
128 props = _a.props;
129 var selected = state.selected;
130 if (!selected || !selected.length) {
131 return false;
132 }
133 var chart = props.chart;
134 var scales = chart.getScales();
135 var fields = Object.keys(scales);
136 for (var i = 0, len = selected.length; i < len; i++) {
137 var item = selected[i];
138 if (isEqual(record.origin, item, fields)) {
139 return true;
140 }
141 }
142 return false;
143 };
144 Selection.prototype.getSelectionStyle = function (record) {
145 var _a = this,
146 state = _a.state,
147 props = _a.props;
148 var selected = state.selected;
149 if (!selected || !selected.length) {
150 return null;
151 }
152 var selection = props.selection;
153 var selectedStyle = selection.selectedStyle,
154 unSelectedStyle = selection.unSelectedStyle;
155 var isSelected = this.isSelected(record);
156 if (isSelected) {
157 return isFunction(selectedStyle) ? selectedStyle(record) : selectedStyle;
158 }
159 return isFunction(unSelectedStyle) ? unSelectedStyle(record) : unSelectedStyle;
160 };
161 return Selection;
162}(Component);
163export default Selection;
\No newline at end of file