1 | import TableSelection from '../table/selection';
|
2 |
|
3 |
|
4 | export default class Selection extends TableSelection {
|
5 | _buildData(data) {
|
6 | return new Set(this._getDescendants(data));
|
7 | }
|
8 |
|
9 | _buildSelected(data, selected) {
|
10 | const _selected = new Set(selected);
|
11 |
|
12 | [...data].forEach(item => {
|
13 | if (_selected.has(item)) {
|
14 | this._selectDescendants(item, _selected);
|
15 | }
|
16 | });
|
17 |
|
18 | return _selected;
|
19 | }
|
20 |
|
21 | _getDescendants(items) {
|
22 | let result = [];
|
23 |
|
24 | items.forEach(item => {
|
25 | result.push(item);
|
26 | result = [...result, ...this._getDescendants(this._getChildren(item))];
|
27 | });
|
28 |
|
29 | return result;
|
30 | }
|
31 |
|
32 | _getAncestors(item) {
|
33 | let result = [];
|
34 |
|
35 | const parent = [...this._data].find(it => this._getChildren(it).includes(item));
|
36 | if (parent) {
|
37 | result = [parent, ...this._getAncestors(parent)];
|
38 | }
|
39 |
|
40 | return result;
|
41 | }
|
42 |
|
43 | _selectDescendants(item, selected) {
|
44 | this._getDescendants(this._getChildren(item)).
|
45 | forEach(it => selected.add(it));
|
46 | }
|
47 |
|
48 | _deselectDescendants(item, selected) {
|
49 | this._getDescendants(this._getChildren(item)).
|
50 | forEach(it => selected.delete(it));
|
51 | }
|
52 |
|
53 | _selectAncestors(item, selected) {
|
54 | this._getAncestors(item).forEach(ancestor => {
|
55 | const groupIsSelected = this._getChildren(ancestor).
|
56 | filter(it => this._isItemSelectable(it)).
|
57 | every(it => selected.has(it));
|
58 |
|
59 | if (groupIsSelected) {
|
60 | selected.add(ancestor);
|
61 | }
|
62 | });
|
63 | }
|
64 |
|
65 | _deselectAncestors(item, selected) {
|
66 | this._getAncestors(item).forEach(it => selected.delete(it));
|
67 | }
|
68 |
|
69 | select(value = this._focused) {
|
70 | if (!value || !this._isItemSelectable(value)) {
|
71 | return this;
|
72 | }
|
73 |
|
74 | const selected = new Set(this._selected);
|
75 | selected.add(value);
|
76 |
|
77 | this._selectDescendants(value, selected);
|
78 | this._selectAncestors(value, selected);
|
79 |
|
80 | return this.cloneWith({selected});
|
81 | }
|
82 |
|
83 | deselect(value = this._focused) {
|
84 | if (!value || !this._isItemSelectable(value)) {
|
85 | return this;
|
86 | }
|
87 |
|
88 | const selected = new Set(this._selected);
|
89 | selected.delete(value);
|
90 |
|
91 | this._deselectDescendants(value, selected);
|
92 | this._deselectAncestors(value, selected);
|
93 |
|
94 | return this.cloneWith({selected});
|
95 | }
|
96 | }
|