-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree-gadget.js
70 lines (70 loc) · 1.89 KB
/
tree-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
'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: 'tree',
events: {
select: (state) => {
return {
selectedIds: state.get('selectedIds', []).valueSeq().toArray(),
};
},
selectAll: (state) => {
return {
selectedIds: state.get('selectedIds', []).valueSeq().toArray(),
};
},
deselectAll: () => {
return {
selectedIds: [],
};
},
},
actions: {
setData: (state, action) => {
return state.set('data', action.get('data'));
},
selectAll: (state) => {
let rows = state
.get('data.rows')
.map((row) => row.get('id'))
.valueSeq()
.toArray();
rows = [...new Set(rows)];
return state.set('selectedIds', rows);
},
deselectAll: (state) => {
return state.set('selectedIds', []);
},
select: (state, action) => {
const mode = action.get('mode');
const id = action.get('rowId');
const selectedIds = state.get('selectedIds', []);
if (mode === 'single') {
if (selectedIds.includes(id)) {
return state.set('selectedIds', []); // deselect
} else {
return state.set('selectedIds', [id]); // select
}
} else if (mode === 'multi') {
if (selectedIds.includes(id)) {
return state.unpush('selectedIds', id);
} else {
return state.push('selectedIds', id);
}
} else {
throw new Error(`Unknow mode ${mode}`);
}
},
doubleClick: (state, action) => {
const id = action.get('rowId');
// TODO...
},
},
});
};