-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable-gadget.js
102 lines (99 loc) · 3.02 KB
/
table-gadget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const {buildGadget} = require('goblin-gadgets');
/**
* Retrieve the list of available commands.
*
* @returns {Object} The list and definitions of commands.
*/
exports.xcraftCommands = function () {
return buildGadget({
name: 'table',
events: {
syncSelect: (state) => {
const rowIds = state.get('selectedIds', []).valueSeq().toArray();
return {
selectedIds: rowIds,
//? rows: state.get('data.rows').filter((r) => rowIds.includes(r.get('id'))),
};
},
select: (state) => {
const rowIds = state.get('selectedIds', []).valueSeq().toArray();
return {
selectedIds: rowIds,
//? rows: state.get('data.rows').filter((r) => rowIds.includes(r.get('id'))),
};
},
doubleClick: (state, action) => {
return {
rowId: action.get('rowId'),
};
},
},
actions: {
setData: (state, action) => {
return state.set('data', action.get('data'));
},
syncSelect: (state, action) => {
const selectedIds = action.get('selectedIds');
return state.set('selectedIds', selectedIds);
//? const selectedIds = state.get('selectedIds', []);
//? if (
//? action.get('selected') &&
//? !selectedIds.includes(action.get('rowId'))
//? ) {
//? return state.push('selectedIds', action.get('rowId'));
//? }
//? if (
//? !action.get('selected') &&
//? selectedIds.includes(action.get('rowId'))
//? ) {
//? return state.unpush('selectedIds', action.get('rowId'));
//? }
//? return state;
},
select: (state, action) => {
const mode = action.get('mode');
const id = action.get('rowId');
const selectedIds = state.get('selectedIds', []);
switch (mode) {
case 'multi':
if (selectedIds.includes(id)) {
return state.unpush('selectedIds', id);
} else {
return state.push('selectedIds', id);
}
case 'single':
default:
if (selectedIds.includes(id)) {
return state.set('selectedIds', []); // deselect
} else {
return state.set('selectedIds', [id]); // select
}
}
},
deselect: (state, action) => {
const id = action.rowId;
const selectedIds = state.get('selectedIds', []);
if (selectedIds.includes(id)) {
state = state.unpush('selectedIds', id);
}
return state;
},
deselectAll: (state) => {
return state.set('selectedIds', []);
},
selectAll: (state) => {
const rowIds = state
.get('sortedRows')
.map((row) => row.get('row').get('id'))
.valueSeq()
.toArray();
return state.set('selectedIds', rowIds);
},
doubleClick: (state) => {
// NOP, see doubleClick in events ---^
return state;
},
},
});
};